diff options
Diffstat (limited to 'railties/lib/rails/generators')
3 files changed, 79 insertions, 57 deletions
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 diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 44f9fde0a6..16150c306a 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -1,9 +1,4 @@ -require 'digest/md5' -require 'active_support/secure_random' -require 'rails/version' unless defined?(Rails::VERSION) -require 'rbconfig' -require 'open-uri' -require 'uri' +require 'rails/generators/app_base' module Rails module ActionMethods @@ -158,7 +153,7 @@ module Rails RESERVED_NAMES = %w[application destroy benchmarker profiler plugin runner test] - class AppGenerator < Base + class AppGenerator < AppBase DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db ) JAVASCRIPTS = %w( prototype jquery ) @@ -210,8 +205,6 @@ module Rails def initialize(*args) raise Error, "Options should be given after the application name. For details run: rails --help" if args[0].blank? - @original_wd = Dir.pwd - super if !options[:skip_active_record] && !DATABASES.include?(options[:database]) @@ -224,12 +217,8 @@ module Rails end def create_root - self.destination_root = File.expand_path(app_path, destination_root) - valid_app_const? - - empty_directory '.' set_default_accessors! - FileUtils.cd(destination_root) unless options[:pretend] + super end def create_root_files @@ -339,7 +328,7 @@ module Rails instance_eval(&prok) end - builder_class = defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder + builder_class = get_builder_class builder_class.send(:include, ActionMethods) builder_class.new(self) end @@ -388,7 +377,7 @@ module Rails @app_const ||= "#{app_const_base}::Application" end - def valid_app_const? + def valid_const? if app_const =~ /^\d/ raise Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers." elsif RESERVED_NAMES.include?(app_name) @@ -442,6 +431,10 @@ module Rails empty_directory(destination, config) create_file("#{destination}/.gitkeep") unless options[:skip_git] end + + def get_builder_class + defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder + end end end end diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb index 239115c07d..9ee7edfc2b 100644 --- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +++ b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb @@ -1,3 +1,4 @@ +require 'rails/generators/app_base' require "rails/generators/rails/app/app_generator" module Rails @@ -70,12 +71,13 @@ module Rails end module Generators - class PluginNewGenerator < Base + class PluginNewGenerator < AppBase attr_accessor :rails_template add_shebang_option! argument :plugin_path, :type => :string + alias_method :app_path, :plugin_path class_option :builder, :type => :string, :aliases => "-b", :desc => "Path to a plugin builder (can be a filesystem path or URL)" @@ -89,32 +91,17 @@ module Rails class_option :help, :type => :boolean, :aliases => "-h", :group => :rails, :desc => "Show this help message and quit" - 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) raise Error, "Options should be given after plugin name. For details run: rails plugin --help" if args[0].blank? - @original_wd = Dir.pwd - super end say_step "Creating gem skeleton" def create_root - self.destination_root = File.expand_path(plugin_path, destination_root) - valid_plugin_const? - - empty_directory '.' - FileUtils.cd(destination_root) unless options[:pretend] + super end def create_root_files @@ -166,29 +153,6 @@ module Rails "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]" end - 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 = defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder - 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 name @name ||= File.basename(destination_root) end @@ -197,7 +161,7 @@ module Rails @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize end - def valid_plugin_const? + def valid_const? if camelized =~ /^\d/ raise Error, "Invalid plugin name #{name}. Please give a name which does not start with numbers." elsif RESERVED_NAMES.include?(name) @@ -220,6 +184,10 @@ module Rails end end alias :store_application_definition! :application_definition + + def get_builder_class + defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder + end end end end |