aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-03 12:10:09 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-03 14:57:46 +0200
commitd1c404ee90ea7470d662517aa7a2d312a61c8c61 (patch)
tree5ba308e204ff3d5d3929324e55f18ff2c057504d /railties/lib
parent3bf45890b323d0fbb5dbfa3e3a2e8f85c627679a (diff)
downloadrails-d1c404ee90ea7470d662517aa7a2d312a61c8c61.tar.gz
rails-d1c404ee90ea7470d662517aa7a2d312a61c8c61.tar.bz2
rails-d1c404ee90ea7470d662517aa7a2d312a61c8c61.zip
Added lookup to generators.
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/generators.rb87
-rw-r--r--railties/lib/generators/base.rb3
-rw-r--r--railties/lib/generators/rails/controller/controller_generator.rb2
-rw-r--r--railties/lib/generators/rails/plugin/plugin_generator.rb47
-rw-r--r--railties/lib/generators/rails/resource/resource_generator.rb2
-rw-r--r--railties/lib/generators/test_unit/plugin/plugin_generator.rb2
6 files changed, 97 insertions, 46 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb
index c97801dea8..91a0f5edca 100644
--- a/railties/lib/generators.rb
+++ b/railties/lib/generators.rb
@@ -12,10 +12,62 @@ require 'rails/version' unless defined?(Rails::VERSION)
require 'generators/base'
require 'generators/named_base'
+require 'generators/active_record' # We will need ActionORM from ActiveRecord, but just it.
module Rails
module Generators
- # Remove builtin generators.
+ mattr_accessor :load_path
+
+ # Generators load paths. First search on generators in the RAILS_ROOT, then
+ # look for them in rails generators.
+ #
+ def self.load_path
+ @@load_path ||= begin
+ paths = []
+ paths << File.expand_path(File.join(File.dirname(__FILE__), "generators"))
+ paths << File.join(RAILS_ROOT, "lib", "generators") if defined?(RAILS_ROOT)
+ paths
+ end
+ end
+ load_path # Cache load paths
+
+ # Receives paths in an array and tries to find generators for it in the load
+ # path.
+ #
+ def self.lookup(attempts)
+ generators_path = []
+
+ # Traverse attempts into directory lookups. For example:
+ #
+ # rails:generators:model
+ #
+ # Becomes:
+ #
+ # generators/rails/model/model_generator.rb
+ # generators/rails/model_generator.rb
+ # generators/model_generator.rb
+ #
+ attempts.each do |attempt|
+ paths = attempt.gsub(':generators:', ':').split(':')
+ paths << "#{paths.last}_generator.rb"
+
+ until paths.empty?
+ generators_path << File.join(*paths)
+ paths.delete_at(-1) unless paths.delete_at(-2)
+ end
+ end
+
+ generators_path.uniq!
+ generators_path.each do |generator_path|
+ self.load_path.each do |path|
+ Dir[File.join(path, generator_path)].each do |file|
+ require file
+ end
+ end
+ end
+ end
+
+ # Keep builtin generators in an Array[Array[group, name]].
#
def self.builtin
Dir[File.dirname(__FILE__) + '/generators/*/*'].collect do |file|
@@ -53,17 +105,18 @@ module Rails
def self.find_by_namespace(name, base=nil, context=nil)
name, attempts = name.to_s, []
- attempts << "#{base}:generators:#{name}" if base && name.count(':') == 0
- attempts << "#{name}:generators:#{context}" if context && name.count(':') == 0
- attempts << name.sub(':', ':generators:') if name.count(':') == 1
+ if name.count(':') == 0
+ attempts << "#{base}:generators:#{name}" if base
+ attempts << "#{name}:generators:#{context}" if context
+ end
+ attempts << name.sub(':', ':generators:') if name.count(':') == 1
attempts << name
- attempts.each do |namespace|
- klass = Thor::Util.find_by_namespace(namespace)
- return klass if klass
+ unless klass = find_many_by_namespace(attempts)
+ lookup(attempts)
+ klass = find_many_by_namespace(attempts)
end
-
- nil
+ klass
end
# Show help message with available generators.
@@ -87,11 +140,6 @@ module Rails
# 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
@@ -99,6 +147,17 @@ module Rails
puts "Could not find generator #{namespace}."
end
end
+
+ protected
+
+ def self.find_many_by_namespace(attempts)
+ attempts.each do |namespace|
+ klass = Thor::Util.find_by_namespace(namespace)
+ return klass if klass
+ end
+ nil
+ end
+
end
end
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb
index 4b47ebc49b..f0d2b4980e 100644
--- a/railties/lib/generators/base.rb
+++ b/railties/lib/generators/base.rb
@@ -3,7 +3,6 @@ require 'generators/actions'
module Rails
module Generators
DEFAULTS = {
- :actions => [],
:fixture => true,
:force_plural => false,
:helper => true,
@@ -147,7 +146,7 @@ module Rails
# end
#
class_eval <<-METHOD, __FILE__, __LINE__
- def invoke_for_#{name}
+ def hook_for_#{name}
return unless options[#{name.inspect}]
klass_name = options[#{name.inspect}]
diff --git a/railties/lib/generators/rails/controller/controller_generator.rb b/railties/lib/generators/rails/controller/controller_generator.rb
index 53e839d34d..3fed058c00 100644
--- a/railties/lib/generators/rails/controller/controller_generator.rb
+++ b/railties/lib/generators/rails/controller/controller_generator.rb
@@ -1,7 +1,7 @@
module Rails
module Generators
class ControllerGenerator < NamedBase
- argument :actions, :type => :array, :default => DEFAULTS[:actions], :banner => "action action"
+ argument :actions, :type => :array, :default => [], :banner => "action action"
check_class_collision :suffix => "Controller"
def create_controller_files
diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb
index ec563be805..9eaf902021 100644
--- a/railties/lib/generators/rails/plugin/plugin_generator.rb
+++ b/railties/lib/generators/rails/plugin/plugin_generator.rb
@@ -1,51 +1,44 @@
module Rails
module Generators
class PluginGenerator < NamedBase
- class_option :with_tasks, :type => :boolean, :aliases => "-r", :default => false,
- :desc => "When supplied creates tasks base files."
+ class_option :tasks, :type => :boolean, :aliases => "-t", :default => false,
+ :desc => "When supplied creates tasks base files."
- class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false,
- :desc => "When supplied creates generator base files."
+ class_option :generator, :type => :boolean, :aliases => "-g", :default => false,
+ :desc => "When supplied creates generator base files."
check_class_collision
- def create_root
- self.root = File.expand_path("vendor/plugins/#{file_name}", root)
- empty_directory '.' if behavior == :invoke
- FileUtils.cd(root)
- end
-
def create_root_files
- %w(README MIT-LICENSE Rakefile init.rb install.rb uninstall.rb).each do |file|
- template file
- end
+ directory '.', plugin_dir, false # non-recursive
end
def create_lib_files
- directory 'lib'
+ directory 'lib', plugin_dir('lib'), false # non-recursive
end
hook_for :test_framework
def create_tasks_files
- return unless options[:with_tasks]
- directory 'tasks'
+ return unless options[:tasks]
+ directory 'tasks', plugin_dir('tasks')
end
def create_generator_files
- return unless options[:with_generator]
- directory 'generators'
+ return unless options[:generator]
+ directory 'generators', plugin_dir('generators')
end
- # Work around for generator to work on revoke. If we remove the root
- # folder at the beginning, it will raise an error since FileUtils.cd
- # will move to a non-existent folder.
- #
- def remove_on_revoke
- return unless behavior == :revoke
- FileUtils.cd("../../..")
- empty_directory "vendor/plugins/#{file_name}"
- end
+ protected
+
+ def plugin_dir(join=nil)
+ if join
+ File.join(plugin_dir, join)
+ else
+ "vendor/plugins/#{file_name}"
+ end
+ end
+
end
end
end
diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb
index 8ff6d820f2..70babc0550 100644
--- a/railties/lib/generators/rails/resource/resource_generator.rb
+++ b/railties/lib/generators/rails/resource/resource_generator.rb
@@ -7,7 +7,7 @@ module Rails
base.invoke controller, [ base.name.pluralize, base.options[:actions] ]
end
- class_option :actions, :type => :array, :banner => "ACTION ACTION",
+ class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [],
:desc => "Actions for the resource controller"
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
diff --git a/railties/lib/generators/test_unit/plugin/plugin_generator.rb b/railties/lib/generators/test_unit/plugin/plugin_generator.rb
index 293342c5d8..fd8c4cb413 100644
--- a/railties/lib/generators/test_unit/plugin/plugin_generator.rb
+++ b/railties/lib/generators/test_unit/plugin/plugin_generator.rb
@@ -6,7 +6,7 @@ module TestUnit
check_class_collision :suffix => "Test"
def create_test_files
- directory 'test'
+ directory 'test', "vendor/plugins/#{file_name}/test"
end
end
end