Permalink
Browse files

Fixed that script/generate would not look for plugin generators in pl…

…ugin_paths (closes #11000) [glv]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9017 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Mar 13, 2008
1 parent e915379 commit dd665ff9679514e354336bb183a9b40ad17145fb
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that script/generate would not look for plugin generators in plugin_paths #11000 [glv]
+
* Fix database rake tasks to work with charset/collation and show proper error messages on failure. Closes #11301 [matt]
* add a -e/--export to script/plugin install, uses svn export. #10847 [jon@blankpad.net)]
@@ -12,6 +12,15 @@
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
module Rails
+ # The Configuration instance used to configure the Rails environment
+ def self.configuration
+ @@configuration
+ end
+
+ def self.configuration=(configuration)
+ @@configuration = configuration
+ end
+
# The Initializer is responsible for processing the Rails configuration, such
# as setting the $LOAD_PATH, requiring the right frameworks, initializing
# logging, and more. It can be run either as a single command that'll just
@@ -60,6 +69,8 @@ def initialize(configuration)
# Sequentially step through all of the available initialization routines,
# in order (view execution order in source).
def process
+ Rails.configuration = configuration
+
check_ruby_version
set_load_path
@@ -1,3 +1,5 @@
+require 'pathname'
+
require File.dirname(__FILE__) + '/spec'
class Object
@@ -104,8 +106,10 @@ def use_component_sources!
if defined? ::RAILS_ROOT
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")
+ Rails.configuration.plugin_paths.each do |path|
+ relative_path = Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(::RAILS_ROOT))
+ sources << PathSource.new(:"plugins (#{relative_path})", "#{path}/**/{,rails_}generators")
+ end
end
sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators")
if Object.const_defined?(:Gem)
@@ -43,12 +43,15 @@ def banner
def usage_message
usage = "\nInstalled Generators\n"
- Rails::Generator::Base.sources.inject({}) do |mem, source|
+ Rails::Generator::Base.sources.inject([]) do |mem, source|
+ # Using an association list instead of a hash to preserve order,
+ # for esthetic reasons more than anything else.
label = source.label.to_s.capitalize
- mem[label] ||= []
- mem[label] |= source.names
+ pair = mem.assoc(label)
+ mem << (pair = [label, []]) if pair.nil?
+ pair[1] |= source.names
mem
- end.each_pair do |label, names|
+ end.each do |label, names|
usage << " #{label}: #{names.join(', ')}\n" unless names.empty?
end
@@ -0,0 +1,4 @@
+class AGenerator < Rails::Generator::Base
+ def manifest
+ end
+end
@@ -0,0 +1,4 @@
+class StubbyGenerator < Rails::Generator::Base
+ def manifest
+ end
+end
@@ -0,0 +1,40 @@
+require 'plugin_test_helper'
+
+class GeneratorLookupTest < Test::Unit::TestCase
+ def setup
+ @fixture_dirs = %w{alternate default}
+ @configuration = Rails.configuration = Rails::Configuration.new
+ # We need to add our testing plugin directory to the plugin paths so
+ # the locator knows where to look for our plugins
+ @configuration.plugin_paths += @fixture_dirs.map{|fd| plugin_fixture_path(fd)}
+ @initializer = Rails::Initializer.new(@configuration)
+ @initializer.add_plugin_load_paths
+ @initializer.load_plugins
+ load 'rails_generator.rb'
+ require 'rails_generator/scripts'
+ end
+
+ def test_should_load_from_all_plugin_paths
+ assert Rails::Generator::Base.lookup('a_generator')
+ assert Rails::Generator::Base.lookup('stubby_generator')
+ end
+
+ def test_should_create_generator_source_for_each_directory_in_plugin_paths
+ sources = Rails::Generator::Base.sources
+ @fixture_dirs.each do |gen_dir|
+ expected_label = "plugins (fixtures/plugins/#{gen_dir})".to_sym
+ assert sources.any? {|source| source.label == expected_label }
+ end
+ end
+
+ def test_should_preserve_order_in_usage_message
+ msg = Rails::Generator::Scripts::Base.new.send(:usage_message)
+ positions = @fixture_dirs.map do |gen_dir|
+ pos = msg.index("Plugins (fixtures/plugins/#{gen_dir})")
+ assert_not_nil pos
+ pos
+ end
+ assert_equal positions.sort, positions
+ end
+
+end

0 comments on commit dd665ff

Please sign in to comment.