#!/usr/bin/env ruby module InlineForms require File.join(File.dirname(__FILE__), "../lib/inline_forms/version.rb") require 'rvm' if not RVM.current puts "ruby or rvm not found" exit 2 end # what is this? Signal.trap("INT") { puts; exit } require 'thor' class Creator < Thor include Thor::Actions String.class_eval do def strip_heredoc_with_indent(indent=0) new_indent = ( self.empty? ? 0 : ( scan(/^[ \t]*(?=\S)/).min.size - indent ) ) gsub(/^[ \t]{#{new_indent}}/, '') end end def self.source_root File.dirname(__FILE__)+"/.." end desc "create APP", "create an application with inline_forms v#{VERSION}" DATABASE_OPTIONS = %w(sqlite mysql) method_option :database, :aliases => "-d", :default => DATABASE_OPTIONS.first, :banner => DATABASE_OPTIONS.join('|'), :desc => 'specify development database' method_option :dry, :type => :boolean, :desc => 'dry run. Do not do most things. Only useful for development of inline_forms' method_option :example, :type => :boolean, :desc => 'install the example app. incompatible with --dry and uses sqlite as development database' def create(app_name) def self.dry_run? options[:dry] end def self.install_example? options[:example] end database = DATABASE_OPTIONS.include?(options[:database]) ? options[:database] : DATABASE_OPTIONS.first if install_example? && dry_run? say "--example and --dry-run can not be used together", :red exit 1 end if install_example? && (database != 'sqlite') say "--example can only be used with an sqlite development database", :red exit 1 end say "This is a dry run. I hope you know what you are doing...", :red if dry_run? say "Creating #{app_name} with inline_forms v#{VERSION} and development database #{database}...", :green regex = /\A[0-9a-zA-Z][0-9a-zA-Z_-]+[0-9a-zA-Z]\Z/ if ! regex.match(app_name) say "Error: APP must match #{regex.source}", :red exit 1 end if File.exists?(app_name) say "Error: APP exists", :red exit 1 end say "- Generating Rails app '#{app_name}'..." dry_run? ? empty_directory(app_name) : RVM.run("rails new #{app_name}") say "- Creating and trusting .rvmrc..." ruby_version = %x[rvm current] create_file "#{app_name}/.rvmrc", "rvm use #{ruby_version.chop}@#{app_name} --create" say ("- " + %x[cd #{app_name} && rvm rvmrc trust .rvmrc]).chop say "- Changing to '#{app_name}' with RVM..." RVM.chdir(app_name) do say "- Working directory is now #{`pwd`}" RVM.use_from_path! '.' rvm_gemset = %x[rvm current] say "- RVM gemset is now #{rvm_gemset}" say "- Recreating Gemfile..." remove_file "#{app_name}/Gemfile" # the one that 'rails new' created create_file "#{app_name}/Gemfile", <<-END_GEMFILE.strip_heredoc_with_indent # generated by inline_forms v#{VERSION} source 'http://rubygems.org' gem 'test-unit' gem 'rails' gem 'rake' gem 'jquery-rails' gem 'capistrano' gem 'will_paginate', :git => 'git://github.com/acesuares/will_paginate.git' gem 'tabs_on_rails' # , :git => 'git://github.com/acesuares/tabs_on_rails.git', :branch => 'add_remote' gem 'ckeditor', :git => 'git://github.com/acesuares/ckeditor.git', :branch => 'master' gem 'carrierwave' gem 'remotipart', '~> 1.0' gem 'paper_trail' gem 'devise' gem 'cancan' gem 'inline_forms' gem 'mini_magick' gem 'jquery_datepicker' gem 'yaml_db' gem 'rails-i18n' gem 'unicorn' gem 'rvm' gem 'rvm-capistrano' # Include everything needed to run rake, tests, features, etc. group :development do gem 'sqlite3' gem 'rspec-rails' gem 'shoulda', '>= 0' gem 'bundler' gem 'jeweler' # gem 'rcov', '>= 0' end # these are just for production group :production do gem 'mysql2' gem 'therubyracer' gem 'uglifier' end END_GEMFILE say "- Running bundle..." system "bundle install" unless dry_run? say "- Database setup: creating config/database.yml with development database #{database}" remove_file "#{app_name}/config/database.yml" # the one that 'rails new' created if database == 'mysql' create_file "#{app_name}/config/database.yml", <<-END_DATABASEYML.strip_heredoc_with_indent development: adapter: mysql2 database: #{app_name}_dev username: #{app_name} password: #{app_name} END_DATABASEYML else create_file "#{app_name}/config/database.yml", <<-END_DATABASEYML.strip_heredoc_with_indent development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 END_DATABASEYML end append_file "#{app_name}/config/database.yml", <<-END_DATABASEYML.strip_heredoc_with_indent production: adapter: mysql2 database: #{app_name}_p username: #{app_name} password: #{app_name}444 END_DATABASEYML say "- Devise install..." RVM.run "bundle exec rails g devise:install" unless dry_run? say "- Devise User model install with added name field..." RVM.run "bundle exec rails g devise User name:string" unless dry_run? say "- Install ckeditor..." RVM.run "bundle exec rails g ckeditor:install" unless dry_run? say "- Create ckeditor config.js" copy_file "lib/app/assets/javascripts/ckeditor/config.js", "#{app_name}/app/assets/javascripts/ckeditor/config.js" say "- Add remotipart to application.js..." create_file "#{app_name}/app/assets/javascripts/application.js", "//= require_tree .\n" if dry_run? insert_into_file "#{app_name}/app/assets/javascripts/application.js", "//= require jquery.remotipart\n", :before => "//= require_tree .\n" say "- Paper_trail install..." RVM.run "bundle exec rails g paper_trail:install" unless dry_run? say "- Migrating Devise and Versions" RVM.run "bundle exec rake db:migrate" unless dry_run? say "- Creating header in app/views/inline_forms/_header.html.erb..." create_file "#{app_name}/app/views/inline_forms/_header.html.erb", <<-END_HEADER.strip_heredoc_with_indent