2010-10-19 17:26:48 -04:00
|
|
|
require 'digest/md5'
|
2011-05-23 14:45:49 -04:00
|
|
|
require 'securerandom'
|
2011-03-27 19:42:30 -04:00
|
|
|
require 'active_support/core_ext/string/strip'
|
2010-10-19 17:26:48 -04:00
|
|
|
require 'rails/version' unless defined?(Rails::VERSION)
|
|
|
|
require 'rbconfig'
|
|
|
|
require 'open-uri'
|
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
2012-10-07 16:36:39 -04:00
|
|
|
class AppBase < Base # :nodoc:
|
2011-09-07 16:27:32 -04:00
|
|
|
DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver )
|
2011-06-20 19:52:28 -04:00
|
|
|
JDBC_DATABASES = %w( jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc )
|
2011-04-23 14:47:19 -04:00
|
|
|
DATABASES.concat(JDBC_DATABASES)
|
2010-10-26 18:16:57 -04:00
|
|
|
|
2010-10-23 14:42:01 -04:00
|
|
|
attr_accessor :rails_template
|
|
|
|
add_shebang_option!
|
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
argument :app_path, type: :string
|
2010-10-23 14:42:01 -04:00
|
|
|
|
|
|
|
def self.add_shared_options_for(name)
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :template, type: :string, aliases: '-m',
|
2013-02-06 11:31:50 -05:00
|
|
|
desc: "Path to some #{name} template (can be a filesystem path or URL)"
|
2010-10-23 14:42:01 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_gemfile, type: :boolean, default: false,
|
|
|
|
desc: "Don't create a Gemfile"
|
2010-10-23 14:42:01 -04:00
|
|
|
|
2013-01-01 12:36:26 -05:00
|
|
|
class_option :skip_bundle, type: :boolean, aliases: '-B', default: false,
|
2012-09-12 21:15:38 -04:00
|
|
|
desc: "Don't run bundle install"
|
2011-05-13 20:36:29 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_git, type: :boolean, aliases: '-G', default: false,
|
|
|
|
desc: 'Skip .gitignore file'
|
2010-10-23 14:42:01 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_keeps, type: :boolean, default: false,
|
|
|
|
desc: 'Skip source control .keep files'
|
2010-10-26 18:16:57 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_active_record, type: :boolean, aliases: '-O', default: false,
|
|
|
|
desc: 'Skip Active Record files'
|
2011-06-29 08:39:41 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false,
|
|
|
|
desc: 'Skip Sprockets files'
|
2010-10-26 18:16:57 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :database, type: :string, aliases: '-d', default: 'sqlite3',
|
|
|
|
desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})"
|
2011-05-01 18:59:57 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :javascript, type: :string, aliases: '-j', default: 'jquery',
|
|
|
|
desc: 'Preconfigure for selected JavaScript library'
|
2010-10-26 16:22:15 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_javascript, type: :boolean, aliases: '-J', default: false,
|
|
|
|
desc: 'Skip JavaScript files'
|
2011-08-23 06:10:37 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :dev, type: :boolean, default: false,
|
|
|
|
desc: "Setup the #{name} with Gemfile pointing to your Rails checkout"
|
2010-10-23 14:42:01 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :edge, type: :boolean, default: false,
|
|
|
|
desc: "Setup the #{name} with Gemfile pointing to Rails repository"
|
2010-11-02 09:52:08 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :skip_test_unit, type: :boolean, aliases: '-T', default: false,
|
|
|
|
desc: 'Skip Test::Unit files'
|
|
|
|
|
2013-02-04 16:25:44 -05:00
|
|
|
class_option :rc, type: :string, default: false,
|
|
|
|
desc: "Path to file containing extra configuration options for rails command"
|
|
|
|
|
2013-02-02 09:51:17 -05:00
|
|
|
class_option :no_rc, type: :boolean, default: false,
|
|
|
|
desc: 'Skip loading of extra configuration options from .railsrc file'
|
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
class_option :help, type: :boolean, aliases: '-h', group: :rails,
|
|
|
|
desc: 'Show this help message and quit'
|
2010-10-23 14:42:01 -04:00
|
|
|
end
|
|
|
|
|
2010-10-19 17:26:48 -04:00
|
|
|
def initialize(*args)
|
|
|
|
@original_wd = Dir.pwd
|
|
|
|
super
|
2011-06-15 13:59:11 -04:00
|
|
|
convert_database_option_for_jruby
|
2010-10-19 17:26:48 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 10:19:46 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def builder
|
|
|
|
@builder ||= begin
|
|
|
|
builder_class = get_builder_class
|
|
|
|
builder_class.send(:include, ActionMethods)
|
|
|
|
builder_class.new(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(meth, *args)
|
|
|
|
builder.send(meth, *args) if builder.respond_to?(meth)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_root
|
|
|
|
self.destination_root = File.expand_path(app_path, destination_root)
|
|
|
|
valid_const?
|
|
|
|
|
|
|
|
empty_directory '.'
|
|
|
|
set_default_accessors!
|
|
|
|
FileUtils.cd(destination_root) unless options[:pretend]
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_rails_template
|
|
|
|
apply rails_template if rails_template
|
|
|
|
rescue Thor::Error, LoadError, Errno::ENOENT => e
|
|
|
|
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default_accessors!
|
|
|
|
self.rails_template = case options[:template]
|
|
|
|
when /^https?:\/\//
|
|
|
|
options[:template]
|
|
|
|
when String
|
|
|
|
File.expand_path(options[:template], Dir.pwd)
|
|
|
|
else
|
|
|
|
options[:template]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def database_gemfile_entry
|
2012-04-14 10:51:33 -04:00
|
|
|
options[:skip_active_record] ? "" : "gem '#{gem_for_database}'"
|
2011-04-13 10:19:46 -04:00
|
|
|
end
|
|
|
|
|
2011-05-06 11:29:15 -04:00
|
|
|
def include_all_railties?
|
2011-06-29 08:39:41 -04:00
|
|
|
!options[:skip_active_record] && !options[:skip_test_unit] && !options[:skip_sprockets]
|
2011-05-06 11:29:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def comment_if(value)
|
2011-05-25 19:18:29 -04:00
|
|
|
options[value] ? '# ' : ''
|
2011-05-06 11:29:15 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 10:19:46 -04:00
|
|
|
def rails_gemfile_entry
|
|
|
|
if options.dev?
|
|
|
|
<<-GEMFILE.strip_heredoc
|
2012-05-02 04:21:02 -04:00
|
|
|
gem 'rails', path: '#{Rails::Generators::RAILS_DEV_PATH}'
|
|
|
|
gem 'arel', github: 'rails/arel'
|
2012-08-17 15:20:20 -04:00
|
|
|
gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders'
|
2011-04-13 10:19:46 -04:00
|
|
|
GEMFILE
|
|
|
|
elsif options.edge?
|
|
|
|
<<-GEMFILE.strip_heredoc
|
2012-05-02 04:21:02 -04:00
|
|
|
gem 'rails', github: 'rails/rails'
|
|
|
|
gem 'arel', github: 'rails/arel'
|
2012-08-17 15:20:20 -04:00
|
|
|
gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders'
|
2011-04-13 10:19:46 -04:00
|
|
|
GEMFILE
|
|
|
|
else
|
|
|
|
<<-GEMFILE.strip_heredoc
|
2012-10-08 11:57:28 -04:00
|
|
|
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
2011-04-13 10:19:46 -04:00
|
|
|
gem 'rails', '#{Rails::VERSION::STRING}'
|
|
|
|
GEMFILE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def gem_for_database
|
2011-09-07 16:27:32 -04:00
|
|
|
# %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql )
|
2011-04-13 10:19:46 -04:00
|
|
|
case options[:database]
|
2011-10-13 13:16:50 -04:00
|
|
|
when "oracle" then "ruby-oci8"
|
|
|
|
when "postgresql" then "pg"
|
|
|
|
when "frontbase" then "ruby-frontbase"
|
|
|
|
when "mysql" then "mysql2"
|
|
|
|
when "sqlserver" then "activerecord-sqlserver-adapter"
|
2011-06-20 19:52:28 -04:00
|
|
|
when "jdbcmysql" then "activerecord-jdbcmysql-adapter"
|
|
|
|
when "jdbcsqlite3" then "activerecord-jdbcsqlite3-adapter"
|
|
|
|
when "jdbcpostgresql" then "activerecord-jdbcpostgresql-adapter"
|
|
|
|
when "jdbc" then "activerecord-jdbc-adapter"
|
2011-04-13 10:19:46 -04:00
|
|
|
else options[:database]
|
|
|
|
end
|
|
|
|
end
|
2011-05-02 02:23:31 -04:00
|
|
|
|
2011-06-15 13:59:11 -04:00
|
|
|
def convert_database_option_for_jruby
|
|
|
|
if defined?(JRUBY_VERSION)
|
|
|
|
case options[:database]
|
|
|
|
when "oracle" then options[:database].replace "jdbc"
|
|
|
|
when "postgresql" then options[:database].replace "jdbcpostgresql"
|
|
|
|
when "mysql" then options[:database].replace "jdbcmysql"
|
|
|
|
when "sqlite3" then options[:database].replace "jdbcsqlite3"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-01 14:21:08 -04:00
|
|
|
def assets_gemfile_entry
|
2011-12-09 21:52:10 -05:00
|
|
|
return if options[:skip_sprockets]
|
2011-12-17 06:51:38 -05:00
|
|
|
|
|
|
|
gemfile = if options.dev? || options.edge?
|
|
|
|
<<-GEMFILE
|
|
|
|
# Gems used only for assets and not required
|
|
|
|
# in production environments by default.
|
|
|
|
group :assets do
|
2012-10-17 22:51:37 -04:00
|
|
|
gem 'sprockets-rails', github: 'rails/sprockets-rails'
|
2012-05-02 04:21:02 -04:00
|
|
|
gem 'sass-rails', github: 'rails/sass-rails'
|
|
|
|
gem 'coffee-rails', github: 'rails/coffee-rails'
|
2012-01-12 21:58:25 -05:00
|
|
|
|
|
|
|
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
|
|
|
#{javascript_runtime_gemfile_entry}
|
2011-12-17 06:51:38 -05:00
|
|
|
gem 'uglifier', '>= 1.0.3'
|
|
|
|
end
|
|
|
|
GEMFILE
|
|
|
|
else
|
|
|
|
<<-GEMFILE
|
|
|
|
# Gems used only for assets and not required
|
|
|
|
# in production environments by default.
|
|
|
|
group :assets do
|
2013-02-25 16:39:25 -05:00
|
|
|
gem 'sass-rails', '~> 4.0.0.beta1'
|
|
|
|
gem 'coffee-rails', '~> 4.0.0.beta1'
|
2012-01-12 21:58:25 -05:00
|
|
|
|
|
|
|
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
|
|
|
|
#{javascript_runtime_gemfile_entry}
|
2011-12-17 06:51:38 -05:00
|
|
|
gem 'uglifier', '>= 1.0.3'
|
|
|
|
end
|
|
|
|
GEMFILE
|
|
|
|
end
|
|
|
|
|
|
|
|
gemfile.strip_heredoc.gsub(/^[ \t]*$/, '')
|
2011-07-01 14:21:08 -04:00
|
|
|
end
|
|
|
|
|
2011-06-21 10:18:50 -04:00
|
|
|
def javascript_gemfile_entry
|
2012-10-04 14:22:18 -04:00
|
|
|
unless options[:skip_javascript]
|
2012-10-04 16:46:17 -04:00
|
|
|
<<-GEMFILE.strip_heredoc
|
2012-10-04 14:22:18 -04:00
|
|
|
gem '#{options[:javascript]}-rails'
|
2012-10-04 16:46:17 -04:00
|
|
|
|
2012-10-04 14:22:18 -04:00
|
|
|
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
|
|
|
|
gem 'turbolinks'
|
|
|
|
GEMFILE
|
|
|
|
end
|
2011-05-01 18:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-01-12 21:58:25 -05:00
|
|
|
def javascript_runtime_gemfile_entry
|
|
|
|
if defined?(JRUBY_VERSION)
|
|
|
|
"gem 'therubyrhino'\n"
|
|
|
|
else
|
2012-05-30 21:25:00 -04:00
|
|
|
"# gem 'therubyracer', platforms: :ruby\n"
|
2012-01-12 21:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-12 18:43:43 -04:00
|
|
|
def bundle_command(command)
|
2011-05-12 18:57:16 -04:00
|
|
|
say_status :run, "bundle #{command}"
|
2011-06-10 17:43:24 -04:00
|
|
|
|
2011-06-10 19:18:08 -04:00
|
|
|
# We are going to shell out rather than invoking Bundler::CLI.new(command)
|
2011-06-10 17:43:24 -04:00
|
|
|
# because `rails new` loads the Thor gem and on the other hand bundler uses
|
|
|
|
# its own vendored Thor, which could be a different version. Running both
|
|
|
|
# things in the same process is a recipe for a night with paracetamol.
|
|
|
|
#
|
|
|
|
# We use backticks and #print here instead of vanilla #system because it
|
|
|
|
# is easier to silence stdout in the existing test suite this way. The
|
|
|
|
# end-user gets the bundler commands called anyway, so no big deal.
|
|
|
|
#
|
2012-06-25 00:57:11 -04:00
|
|
|
# We unset temporary bundler variables to load proper bundler and Gemfile.
|
|
|
|
#
|
2011-06-10 17:43:24 -04:00
|
|
|
# Thanks to James Tucker for the Gem tricks involved in this call.
|
2012-06-25 00:57:11 -04:00
|
|
|
_bundle_command = Gem.bin_path('bundler', 'bundle')
|
|
|
|
|
2012-09-01 14:08:08 -04:00
|
|
|
require 'bundler'
|
2012-05-16 06:35:12 -04:00
|
|
|
Bundler.with_clean_env do
|
|
|
|
print `"#{Gem.ruby}" "#{_bundle_command}" #{command}`
|
|
|
|
end
|
2011-05-12 18:43:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_bundle
|
2012-12-28 16:23:01 -05:00
|
|
|
bundle_command('install') unless options[:skip_gemfile] || options[:skip_bundle] || options[:pretend]
|
2011-04-13 10:19:46 -04:00
|
|
|
end
|
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
def empty_directory_with_keep_file(destination, config = {})
|
2011-04-13 10:19:46 -04:00
|
|
|
empty_directory(destination, config)
|
2012-09-12 21:15:38 -04:00
|
|
|
keep_file(destination)
|
2011-04-13 10:19:46 -04:00
|
|
|
end
|
2011-05-02 02:23:31 -04:00
|
|
|
|
2012-09-12 21:15:38 -04:00
|
|
|
def keep_file(destination)
|
|
|
|
create_file("#{destination}/.keep") unless options[:skip_keeps]
|
2011-04-13 10:19:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-02 02:23:31 -04:00
|
|
|
end
|