From b37938eff72fe370ddc84afb656f6b4775053f6a Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 19 Oct 2010 23:26:48 +0200 Subject: Refactored AppGenerator and PluginNewGenerator to inherit from AppBase. --- railties/lib/rails/generators/app_base.rb | 61 +++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 railties/lib/rails/generators/app_base.rb (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb new file mode 100644 index 0000000000..d66b1345c1 --- /dev/null +++ b/railties/lib/rails/generators/app_base.rb @@ -0,0 +1,61 @@ +require 'digest/md5' +require 'active_support/secure_random' +require 'rails/version' unless defined?(Rails::VERSION) +require 'rbconfig' +require 'open-uri' +require 'uri' + +module Rails + module Generators + class AppBase < Base + def self.say_step(message) + @step = (@step || 0) + 1 + class_eval <<-METHOD, __FILE__, __LINE__ + 1 + def step_#{@step} + #{"puts" if @step > 1} + say_status "STEP #{@step}", #{message.inspect} + end + METHOD + end + + def initialize(*args) + @original_wd = Dir.pwd + + super + end + + protected + + def builder + @builder ||= begin + if path = options[:builder] + if URI(path).is_a?(URI::HTTP) + contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read } + else + contents = open(File.expand_path(path, @original_wd)) {|io| io.read } + end + + prok = eval("proc { #{contents} }", TOPLEVEL_BINDING, path, 1) + instance_eval(&prok) + end + + 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 '.' + FileUtils.cd(destination_root) unless options[:pretend] + end + end + end +end -- cgit v1.2.3 From bcd414fd10a0e401cfb1de95cc9b2940b1df0ff6 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 20 Oct 2010 01:05:34 +0200 Subject: Add support for templates for rails plugin new --- railties/lib/rails/generators/app_base.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index d66b1345c1..ecaf92c683 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -54,8 +54,26 @@ module Rails 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 /^http:\/\// + options[:template] + when String + File.expand_path(options[:template], Dir.pwd) + else + options[:template] + end + end end end end -- cgit v1.2.3 From e51e9e2db0359355feef71e735a1f9cb764ec929 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 20 Oct 2010 07:59:28 +0200 Subject: Add --dev and --edge options to rails plugin new --- railties/lib/rails/generators/app_base.rb | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index ecaf92c683..e0f0242da8 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -74,6 +74,40 @@ module Rails options[:template] end end + + def rails_gemfile_entry + if options.dev? + <<-GEMFILE +gem 'rails', :path => '#{Rails::Generators::RAILS_DEV_PATH}' +gem 'arel', :git => 'git://github.com/rails/arel.git' +gem "rack", :git => "git://github.com/rack/rack.git" + GEMFILE + elsif options.edge? + <<-GEMFILE +gem 'rails', :git => 'git://github.com/rails/rails.git' +gem 'arel', :git => 'git://github.com/rails/arel.git' +gem "rack", :git => "git://github.com/rack/rack.git" + GEMFILE + else + <<-GEMFILE +gem 'rails', '#{Rails::VERSION::STRING}' + +# Bundle edge Rails instead: +# gem 'rails', :git => 'git://github.com/rails/rails.git' +# gem 'arel', :git => 'git://github.com/rails/arel.git' +# gem "rack", :git => "git://github.com/rack/rack.git" + GEMFILE + end + end + + def bundle_if_dev_or_edge + bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle') + run "#{bundle_command} install" if dev_or_edge? + end + + def dev_or_edge? + options.dev? || options.edge? + end end end end -- cgit v1.2.3 From d995953869ca09906af8f5c5058d47cb66bc2467 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 23 Oct 2010 20:42:01 +0200 Subject: DRY up app generator and plugin new generator. Moved shared options to AppBase generator --- railties/lib/rails/generators/app_base.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index e0f0242da8..ad44d9a14e 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -8,6 +8,34 @@ require 'uri' module Rails module Generators class AppBase < Base + attr_accessor :rails_template + add_shebang_option! + + argument :app_path, :type => :string + + def self.add_shared_options_for(name) + class_option :builder, :type => :string, :aliases => "-b", + :desc => "Path to a #{name} builder (can be a filesystem path or URL)" + + class_option :template, :type => :string, :aliases => "-m", + :desc => "Path to an #{name} template (can be a filesystem path or URL)" + + class_option :skip_gemfile, :type => :boolean, :default => false, + :desc => "Don't create a Gemfile" + + class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false, + :desc => "Skip Git ignores and keeps" + + class_option :dev, :type => :boolean, :default => false, + :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout" + + class_option :edge, :type => :boolean, :default => false, + :desc => "Setup the #{name} with Gemfile pointing to Rails repository" + + class_option :help, :type => :boolean, :aliases => "-h", :group => :rails, + :desc => "Show this help message and quit" + end + def self.say_step(message) @step = (@step || 0) + 1 class_eval <<-METHOD, __FILE__, __LINE__ + 1 -- cgit v1.2.3 From fdbd9df21e0063da4b34346c54fbe21ac9583ca6 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sat, 23 Oct 2010 20:56:02 +0200 Subject: No need for say_step in 'plugin new' generator --- railties/lib/rails/generators/app_base.rb | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index ad44d9a14e..a355f3b055 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -36,16 +36,6 @@ module Rails :desc => "Show this help message and quit" end - def self.say_step(message) - @step = (@step || 0) + 1 - class_eval <<-METHOD, __FILE__, __LINE__ + 1 - def step_#{@step} - #{"puts" if @step > 1} - say_status "STEP #{@step}", #{message.inspect} - end - METHOD - end - def initialize(*args) @original_wd = Dir.pwd -- cgit v1.2.3 From 671d1469c6f70abb7cbd373bb37f18268ed7069c Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 26 Oct 2010 22:22:15 +0200 Subject: Add --full option to 'plugin new' generator, which generates rails engine --- railties/lib/rails/generators/app_base.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index a355f3b055..1b3673e96e 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -26,6 +26,11 @@ module Rails class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false, :desc => "Skip Git ignores and keeps" + skip_active_record_desc = "Skip Active Record files" + skip_active_record_desc << " for dummy application" if name == "plugin" + class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false, + :desc => skip_active_record_desc + class_option :dev, :type => :boolean, :default => false, :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout" -- cgit v1.2.3 From 46fdb3197d270551da260cfed3dea7928dd15b0f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Wed, 27 Oct 2010 00:16:57 +0200 Subject: Pass more options to test/dummy in 'plugin new' generator --- railties/lib/rails/generators/app_base.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 1b3673e96e..0bf5ee86b1 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -8,6 +8,9 @@ require 'uri' module Rails module Generators class AppBase < Base + DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db ) + JAVASCRIPTS = %w( prototype jquery ) + attr_accessor :rails_template add_shebang_option! @@ -26,10 +29,17 @@ module Rails class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false, :desc => "Skip Git ignores and keeps" - skip_active_record_desc = "Skip Active Record files" - skip_active_record_desc << " for dummy application" if name == "plugin" class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false, - :desc => skip_active_record_desc + :desc => "Skip Active Record files" + + class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3", + :desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})" + + class_option :javascript, :type => :string, :aliases => "-j", :default => "prototype", + :desc => "Preconfigure for selected javascript library (options: #{JAVASCRIPTS.join('/')})" + + class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false, + :desc => "Skip javascript files" class_option :dev, :type => :boolean, :default => false, :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout" -- cgit v1.2.3 From 2133495b8cdcb40a68a03aa786c4353031abe49e Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 2 Nov 2010 14:23:45 +0100 Subject: Properly handle other databases in 'plugin new' generator --- railties/lib/rails/generators/app_base.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 0bf5ee86b1..b7a4b16f10 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -108,6 +108,15 @@ module Rails end end + def database_gemfile_entry + entry = "" + unless options[:skip_active_record] + entry = "gem '#{gem_for_database}'" + entry << ", :require => '#{require_for_database}'" if require_for_database + end + entry + end + def rails_gemfile_entry if options.dev? <<-GEMFILE @@ -133,6 +142,24 @@ gem 'rails', '#{Rails::VERSION::STRING}' end end + def gem_for_database + # %w( mysql oracle postgresql sqlite3 frontbase ibm_db ) + case options[:database] + when "oracle" then "ruby-oci8" + when "postgresql" then "pg" + when "sqlite3" then "sqlite3-ruby" + when "frontbase" then "ruby-frontbase" + when "mysql" then "mysql2" + else options[:database] + end + end + + def require_for_database + case options[:database] + when "sqlite3" then "sqlite3" + end + end + def bundle_if_dev_or_edge bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle') run "#{bundle_command} install" if dev_or_edge? -- cgit v1.2.3 From fadad11f9056a0166ee490a5eb0a4d9a01120d38 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Tue, 2 Nov 2010 14:52:08 +0100 Subject: Properly skip test unit in 'plugin new' generator --- railties/lib/rails/generators/app_base.rb | 3 +++ 1 file changed, 3 insertions(+) (limited to 'railties/lib/rails/generators/app_base.rb') diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index b7a4b16f10..f5c626553c 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -47,6 +47,9 @@ module Rails class_option :edge, :type => :boolean, :default => false, :desc => "Setup the #{name} with Gemfile pointing to Rails repository" + class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false, + :desc => "Skip Test::Unit files" + class_option :help, :type => :boolean, :aliases => "-h", :group => :rails, :desc => "Show this help message and quit" end -- cgit v1.2.3