diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-27 21:26:53 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-27 21:30:00 +0200 |
commit | c03585aa8da175888ad0893ef29d887f87fb749b (patch) | |
tree | 0c9c0e1a0667b90bda642c2e854c01713b5d5b18 /railties/lib/generators/base.rb | |
parent | fdeee65c9624fa1490aaee829468af3b015fe956 (diff) | |
download | rails-c03585aa8da175888ad0893ef29d887f87fb749b.tar.gz rails-c03585aa8da175888ad0893ef29d887f87fb749b.tar.bz2 rails-c03585aa8da175888ad0893ef29d887f87fb749b.zip |
Show invoked generators options on meta generators.
Diffstat (limited to 'railties/lib/generators/base.rb')
-rw-r--r-- | railties/lib/generators/base.rb | 70 |
1 files changed, 42 insertions, 28 deletions
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 |