From c03585aa8da175888ad0893ef29d887f87fb749b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 27 Jun 2009 21:26:53 +0200 Subject: Show invoked generators options on meta generators. --- railties/lib/generators/base.rb | 70 +++++++++++++++++++++-------------- railties/lib/generators/named_base.rb | 2 +- 2 files changed, 43 insertions(+), 29 deletions(-) (limited to 'railties/lib/generators') diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index 5dd3b2aa84..2bc6f7ec8a 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -38,7 +38,7 @@ module Rails # def self.desc(description=nil) return super if description - usage = File.join(source_root, "..", "USAGE") + usage = File.expand_path(File.join(source_root, "..", "USAGE")) @desc ||= if File.exist?(usage) File.read(usage) @@ -90,32 +90,10 @@ module Rails # # ruby script/generate controller Account --no-test-framework # - # ==== Another example - # - # Another example of invoke_for hooks is fixture replacement tools. - # Consider this TestUnit model generator: - # - # class ModelGenerator < TestUnit::Generators::Base - # hook_for :fixture_replacement, :aliases => "-r" - # end - # - # When invoked as: - # - # ruby script/generate test_unit:model Account -r fixjour - # - # It will lookup for any of these generators: - # - # "test_unit:generators:fixjour", "fixjour:generators:model", "fixjour" - # - # Fixjour can choose any of these hooks to implement. For example, if - # Fixjour has different output for rspec and test_unit, it will likely - # implement the first hook. However if the output is the same for both, - # it will implement the second or the third depending on the number of - # generators it has. - # def self.hook_for(*names) default_options = names.extract_options! verbose = default_options.key?(:verbose) ? default_options[:verbose] : :blue + invocations.concat(names) names.each do |name| options = default_options.dup @@ -169,6 +147,7 @@ module Rails def self.invoke_if(*names) default_options = names.extract_options! verbose = default_options.key?(:verbose) ? default_options[:verbose] : :blue + invocations.concat(names) names.each do |name| conditional_class_option name, default_options.dup @@ -248,6 +227,12 @@ module Rails end end + # Stores invocations for this class merging with superclass values. + # + def self.invocations #:nodoc: + @invocations ||= from_superclass(:invocations, []) + end + # Creates a conditional class option with type boolean, default value # lookup and default description. # @@ -256,6 +241,34 @@ module Rails class_option name, options.merge!(:type => :boolean, :default => DEFAULTS[name] || false) end + # Overwrite class options help to allow invoked generators options to be + # shown when invoking a generator. Only first level options and options + # that belongs to the default group are shown. + # + def self.class_options_help(shell, ungrouped_name=nil, extra_group=nil) + klass_options = Thor::CoreExt::OrderedHash.new + + invocations.each do |name| + option = class_options[name] + + klass_name = option.type == :boolean ? name : option.default + next unless klass_name + + klass = Rails::Generators.find_by_namespace(klass_name, base_name, generator_name) + next unless klass + + human_name = klass_name.to_s.classify + + klass_options[human_name] ||= [] + klass_options[human_name] += klass.class_options.values.select do |option| + class_options[option.human_name.to_sym].nil? && option.group.nil? + end + end + + klass_options.merge!(extra_group) if extra_group + super(shell, ungrouped_name, klass_options) + end + # Small macro to add ruby as an option to the generator with proper # default value plus an instance helper method called shebang. # @@ -266,11 +279,12 @@ module Rails class_option :ruby, :type => :string, :aliases => "-r", :default => default, :desc => "Path to the Ruby binary of your choice", :banner => "PATH" - no_tasks do - define_method :shebang do - "#!#{options[:ruby] || "/usr/bin/env ruby"}" + class_eval <<-METHOD, __FILE__, __LINE__ + protected + def shebang + "#!\#{options[:ruby] || "/usr/bin/env ruby"}" end - end + METHOD end end diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb index 4a5aba38d8..9538931a6e 100644 --- a/railties/lib/generators/named_base.rb +++ b/railties/lib/generators/named_base.rb @@ -30,7 +30,7 @@ module Rails # superclass. The from_superclass method used below is from Thor. # def class_collisions #:nodoc: - @class_collisions ||= from_superclass(:class_collisions, nil) rescue nil + @class_collisions ||= from_superclass(:class_collisions, nil) end end -- cgit v1.2.3