aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-04 19:03:52 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-04 20:17:32 +0200
commiteeb6a0786a061b640bcd90a5d90bd0d0215fe809 (patch)
treee5c94ea53641fe5a1cb8923a4bbcba69dae76f40
parentb277cf28e8a6f5636d0485ab8c04ea0660444440 (diff)
downloadrails-eeb6a0786a061b640bcd90a5d90bd0d0215fe809.tar.gz
rails-eeb6a0786a061b640bcd90a5d90bd0d0215fe809.tar.bz2
rails-eeb6a0786a061b640bcd90a5d90bd0d0215fe809.zip
Load generators from user home and show a error message if they can't be loaded.
-rw-r--r--railties/lib/generators.rb37
-rw-r--r--railties/test/fixtures/vendor/gems/wrong/lib/generators/wrong_generator.rb3
-rw-r--r--railties/test/generators_test.rb8
3 files changed, 37 insertions, 11 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb
index e7495e8036..de751f61a1 100644
--- a/railties/lib/generators.rb
+++ b/railties/lib/generators.rb
@@ -1,27 +1,39 @@
activesupport_path = "#{File.dirname(__FILE__)}/../../activesupport/lib"
$:.unshift(activesupport_path) if File.directory?(activesupport_path)
-require 'active_support/all'
+
+begin
+ require 'active_support/all'
+rescue LoadError
+ require 'rubygems'
+ gem 'activesupport'
+ require 'active_support/all'
+end
+
+$:.unshift(File.dirname(__FILE__))
+require 'rails/version' unless defined?(Rails::VERSION)
# TODO Use vendored Thor
require 'rubygems'
gem 'josevalim-thor'
require 'thor'
-$:.unshift(File.dirname(__FILE__))
-require 'rails/version' unless defined?(Rails::VERSION)
-
require 'generators/base'
require 'generators/named_base'
module Rails
module Generators
- # Generators load paths. First search on generators in the RAILS_ROOT, then
- # look for them in rails generators.
+ # Generators load paths used on lookup. The lookup happens as:
+ #
+ # 1) builtin generators
+ # 2) frozen gems generators
+ # 3) rubygems gems generators (not available yet)
+ # 4) plugin generators
+ # 5) lib generators
+ # 6) ~/rails/generators
#
- # TODO Right now, only plugin and frozen gems generators are loaded. Gems
- # loaded by rubygems are not available since Rails dependencies system is
- # being reworked.
+ # TODO Add Rubygems generators (depends on dependencies system rework)
+ # TODO Remove hardcoded paths for all, except (1).
#
def self.load_path
@@load_path ||= begin
@@ -32,6 +44,7 @@ module Rails
paths += Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
paths << File.join(RAILS_ROOT, "lib", "generators")
end
+ paths << File.join(Thor::Util.user_home, ".rails", "generators")
paths
end
end
@@ -174,7 +187,11 @@ module Rails
self.load_path.each do |path|
Dir[File.join(path, generators_path, name)].each do |file|
- require file
+ begin
+ require file
+ rescue Exception => e
+ warn "[WARNING] Could not load generator at #{file.inspect}. Error: #{e.message}"
+ end
end
end
end
diff --git a/railties/test/fixtures/vendor/gems/wrong/lib/generators/wrong_generator.rb b/railties/test/fixtures/vendor/gems/wrong/lib/generators/wrong_generator.rb
new file mode 100644
index 0000000000..6aa7cb052e
--- /dev/null
+++ b/railties/test/fixtures/vendor/gems/wrong/lib/generators/wrong_generator.rb
@@ -0,0 +1,3 @@
+# Old generator version
+class WrongGenerator < Rails::Generator::NamedBase
+end
diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb
index c7195f753d..964def4158 100644
--- a/railties/test/generators_test.rb
+++ b/railties/test/generators_test.rb
@@ -81,7 +81,13 @@ class GeneratorsTest < GeneratorsTestCase
def test_rails_generators_with_others_information
output = capture(:stdout){ Rails::Generators.help }.split("\n").last
- assert_equal "Others: active_record:fixjour, fixjour, mspec, rails:javascripts.", output
+ assert_equal "Others: active_record:fixjour, fixjour, mspec, rails:javascripts, wrong.", output
+ end
+
+ def test_warning_is_raised_if_generator_cant_be_loaded
+ output = capture(:stderr){ Rails::Generators.find_by_namespace(:wrong) }
+ assert_match /\[WARNING\] Could not load generator at/, output
+ assert_match /Error: uninitialized constant Rails::Generator/, output
end
def test_no_color_sets_proper_shell