From ec55e59e993d6c39c6ba8d57d4ef6d270ee8c14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 2 Jul 2009 11:08:07 +0200 Subject: Updated commands. --- railties/bin/gen | 28 ++------------------------- railties/lib/commands/destroy.rb | 12 ++++++++---- railties/lib/commands/generate.rb | 12 ++++++++---- railties/lib/commands/update.rb | 12 +++++++++--- railties/lib/generators.rb | 40 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 64 insertions(+), 40 deletions(-) diff --git a/railties/bin/gen b/railties/bin/gen index 9f105d2e19..809e75acb5 100755 --- a/railties/bin/gen +++ b/railties/bin/gen @@ -10,33 +10,9 @@ end require File.dirname(__FILE__) + '/../lib/generators' if ARGV.size == 0 - rails = Rails::Generators.builtin.map do |group, name| - name if group == "rails" - end - rails.compact! - rails.sort! - - others = Rails::Generators.builtin.map do |group, name| - "#{group}:#{name}" unless rails.include?(name) - end.compact - others.sort! - - puts "Please select a generator." - puts "Builtin: #{rails.join(', ')}." - puts "Others: #{others.join(', ')}." unless others.empty? - + Rails::Generators.help exit end -Rails::Generators.builtin.each do |group, name| - require "generators/#{group}/#{name}/#{name}_generator" -end - name = ARGV.shift - -if klass = Rails::Generators.find_by_namespace(name, "rails") - ARGV << "--help" if klass.arguments.any? { |a| a.required? } && ARGV.empty? - klass.start -else - puts "Could not find generator #{name}." -end +Rails::Generators.invoke name, ARGV, :invoke diff --git a/railties/lib/commands/destroy.rb b/railties/lib/commands/destroy.rb index f4b81d6511..204477b738 100644 --- a/railties/lib/commands/destroy.rb +++ b/railties/lib/commands/destroy.rb @@ -1,6 +1,10 @@ require "#{RAILS_ROOT}/config/environment" -require 'rails_generator' -require 'rails_generator/scripts/destroy' +require 'generators' -ARGV.shift if ['--help', '-h'].include?(ARGV[0]) -Rails::Generator::Scripts::Destroy.new.run(ARGV) +if ARGV.size == 0 + Rails::Generators.help + exit +end + +name = ARGV.shift +Rails::Generators.invoke name, ARGV, :revoke diff --git a/railties/lib/commands/generate.rb b/railties/lib/commands/generate.rb index 3d3db3d856..7d133a179e 100755 --- a/railties/lib/commands/generate.rb +++ b/railties/lib/commands/generate.rb @@ -1,6 +1,10 @@ require "#{RAILS_ROOT}/config/environment" -require 'rails_generator' -require 'rails_generator/scripts/generate' +require 'generators' -ARGV.shift if ['--help', '-h'].include?(ARGV[0]) -Rails::Generator::Scripts::Generate.new.run(ARGV) +if ARGV.size == 0 + Rails::Generators.help + exit +end + +name = ARGV.shift +Rails::Generators.invoke name, ARGV, :invoke diff --git a/railties/lib/commands/update.rb b/railties/lib/commands/update.rb index 83ef833300..0c14355f24 100644 --- a/railties/lib/commands/update.rb +++ b/railties/lib/commands/update.rb @@ -1,4 +1,10 @@ require "#{RAILS_ROOT}/config/environment" -require 'rails_generator' -require 'rails_generator/scripts/update' -Rails::Generator::Scripts::Update.new.run(ARGV) +require 'generators' + +if ARGV.size == 0 + Rails::Generators.help + exit +end + +name = ARGV.shift +Rails::Generators.invoke name, ARGV, :skip diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb index fd7d3c9580..8814f6e800 100644 --- a/railties/lib/generators.rb +++ b/railties/lib/generators.rb @@ -25,7 +25,7 @@ module Rails # # ==== Examples # - # lookup_by_namespace :webrat, :rails, :integration + # find_by_namespace :webrat, :rails, :integration # # Will search for the following generators: # @@ -36,7 +36,7 @@ module Rails # # Finally, it deals with one kind of shortcut: # - # lookup_by_namespace "test_unit:model" + # find_by_namespace "test_unit:model" # # It will search for generators at: # @@ -51,12 +51,46 @@ module Rails attempts << name attempts.each do |namespace| - klass, task = Thor::Util.find_by_namespace(namespace) + klass = Thor::Util.find_by_namespace(namespace) return klass if klass end nil end + + # Show help message with available generators. + # + def self.help + rails = Rails::Generators.builtin.map do |group, name| + name if group == "rails" + end + rails.compact! + rails.sort! + + puts "Please select a generator." + puts "Builtin: #{rails.join(', ')}." + + # TODO Show others after lookup is implemented + # puts "Others: #{others.join(', ')}." + end + + # Receives a namespace, arguments and the behavior to invoke the generator. + # It's used as the default entry point for generate, destroy and update + # commands. + # + def self.invoke(namespace, args=ARGV, behavior=:invoke) + # Load everything right now ... + builtin.each do |group, name| + require "generators/#{group}/#{name}/#{name}_generator" + end + + if klass = find_by_namespace(namespace, "rails") + args << "--help" if klass.arguments.any? { |a| a.required? } && args.empty? + klass.start args, :behavior => behavior + else + puts "Could not find generator #{namespace}." + end + end end end -- cgit v1.2.3