aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-06-27 09:25:25 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-06-27 09:25:25 +0000
commit868e6b08df4b16fb71cc8a66308174253ff67bdb (patch)
tree70e1a892af080abab1f69e0aa826c2e5c1faebae /railties
parent557e19346ad144deecd7d76a8a4e4cec22a6597e (diff)
downloadrails-868e6b08df4b16fb71cc8a66308174253ff67bdb.tar.gz
rails-868e6b08df4b16fb71cc8a66308174253ff67bdb.tar.bz2
rails-868e6b08df4b16fb71cc8a66308174253ff67bdb.zip
Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also. Closes #8730.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7139 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails_generator/lookup.rb43
-rw-r--r--railties/lib/rails_generator/scripts.rb7
-rw-r--r--railties/lib/rails_generator/spec.rb2
4 files changed, 45 insertions, 9 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 9fe7ad6a48..22ed384237 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also. #8730 [Dr Nic, topfunky]
+
* MySQL, PostgreSQL: database.yml defaults to utf-8. #8701 [matt]
* Added db:version to get the current schema number [via Err The Blog]
diff --git a/railties/lib/rails_generator/lookup.rb b/railties/lib/rails_generator/lookup.rb
index 9446353013..ca46afa418 100644
--- a/railties/lib/rails_generator/lookup.rb
+++ b/railties/lib/rails_generator/lookup.rb
@@ -46,7 +46,7 @@ module Rails
#
# A spec is not a generator: it's a description of where to find
# the generator and how to create it. A source is anything that
- # yields generators from #each. PathSource and GemSource are provided.
+ # yields generators from #each. PathSource and GemGeneratorSource are provided.
module Lookup
def self.included(base)
base.extend(ClassMethods)
@@ -101,9 +101,13 @@ module Rails
sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators")
sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators")
sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/generators")
+ sources << PathSource.new(:plugins, "#{::RAILS_ROOT}/vendor/plugins/*/**/rails_generators")
end
sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators")
- sources << GemSource.new if Object.const_defined?(:Gem)
+ if Object.const_defined?(:Gem)
+ sources << GemGeneratorSource.new
+ sources << GemPathSource.new
+ end
sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components")
end
@@ -185,14 +189,15 @@ module Rails
end
end
-
- # GemSource hits the mines to quarry for generators. The latest versions
- # of gems named *_generator are selected.
- class GemSource < Source
+ class AbstractGemSource < Source
def initialize
super :RubyGems
end
+ end
+ # GemGeneratorSource hits the mines to quarry for generators. The latest versions
+ # of gems named *_generator are selected.
+ class GemGeneratorSource < AbstractGemSource
# Yield latest versions of generator gems.
def each
Gem::cache.search(/_generator$/).inject({}) { |latest, gem|
@@ -205,5 +210,31 @@ module Rails
end
end
+ # GemPathSource looks for generators within any RubyGem's /rails_generators/<generator_name>_generator.rb file.
+ class GemPathSource < AbstractGemSource
+ # Yield each generator within rails_generator subdirectories.
+ def each
+ generator_full_paths.each do |generator|
+ yield Spec.new(File.basename(generator).sub(/_generator.rb$/, ''), File.dirname(generator), label)
+ end
+ end
+
+ private
+ def generator_full_paths
+ @generator_full_paths ||=
+ Gem::cache.inject({}) do |latest, name_gem|
+ name, gem = name_gem
+ hem = latest[gem.name]
+ latest[gem.name] = gem if hem.nil? or gem.version > hem.version
+ latest
+ end.values.inject([]) do |mem, gem|
+ Dir[gem.full_gem_path + '/{rails_,}generators/**/*_generator.rb'].each do |generator|
+ mem << generator
+ end
+ mem
+ end
+ end
+ end
+
end
end
diff --git a/railties/lib/rails_generator/scripts.rb b/railties/lib/rails_generator/scripts.rb
index 14156e9c04..2d6019ade7 100644
--- a/railties/lib/rails_generator/scripts.rb
+++ b/railties/lib/rails_generator/scripts.rb
@@ -43,9 +43,12 @@ module Rails
def usage_message
usage = "\nInstalled Generators\n"
- Rails::Generator::Base.sources.each do |source|
+ Rails::Generator::Base.sources.inject({}) do |mem, source|
label = source.label.to_s.capitalize
- names = source.names
+ mem[label] ||= []
+ mem[label] |= source.names
+ mem
+ end.each_pair do |label, names|
usage << " #{label}: #{names.join(', ')}\n" unless names.empty?
end
diff --git a/railties/lib/rails_generator/spec.rb b/railties/lib/rails_generator/spec.rb
index ad609b8fe3..9d780b7ac5 100644
--- a/railties/lib/rails_generator/spec.rb
+++ b/railties/lib/rails_generator/spec.rb
@@ -2,7 +2,7 @@ module Rails
module Generator
# A spec knows where a generator was found and how to instantiate it.
# Metadata include the generator's name, its base path, and the source
- # which yielded it (PathSource, GemSource, etc.)
+ # which yielded it (PathSource, GemPathSource, etc.)
class Spec
attr_reader :name, :path, :source