From 55575e21655023259d0dae22bc1b148b34168d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 7 Aug 2009 15:34:10 +0200 Subject: Allow rails templates relative to the current path to be given. --- railties/lib/generators/rails/app/app_generator.rb | 21 ++++++++++++++++++--- railties/test/fixtures/lib/template.rb | 1 + railties/test/generators/app_generator_test.rb | 5 +++++ railties/test/generators/generators_test_helper.rb | 2 +- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 railties/test/fixtures/lib/template.rb diff --git a/railties/lib/generators/rails/app/app_generator.rb b/railties/lib/generators/rails/app/app_generator.rb index c8044d13b1..c80a344e0d 100644 --- a/railties/lib/generators/rails/app/app_generator.rb +++ b/railties/lib/generators/rails/app/app_generator.rb @@ -49,7 +49,7 @@ module Rails::Generators self.destination_root = File.expand_path(app_path, destination_root) empty_directory '.' - app_name # Sets the app name + set_default_accessors! FileUtils.cd(destination_root) end @@ -164,9 +164,9 @@ module Rails::Generators end def apply_rails_template - apply options[:template] if options[:template] + apply rails_template if rails_template rescue Thor::Error, LoadError, Errno::ENOENT => e - raise Error, "The template [#{options[:template]}] could not be loaded. Error: #{e}" + raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}" end def freeze? @@ -175,6 +175,21 @@ module Rails::Generators protected + attr_accessor :rails_template + + def set_default_accessors! + app_name # Cache app name + + self.rails_template = case options[:template] + when /^http:\/\// + options[:template] + when String + File.expand_path(options[:template], Dir.pwd) + else + options[:template] + end + end + # Define file as an alias to create_file for backwards compatibility. # def file(*args, &block) diff --git a/railties/test/fixtures/lib/template.rb b/railties/test/fixtures/lib/template.rb new file mode 100644 index 0000000000..c14a1a8784 --- /dev/null +++ b/railties/test/fixtures/lib/template.rb @@ -0,0 +1 @@ +say "It works from file!" diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index c794a2ade6..19e41c15c8 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -131,6 +131,11 @@ class AppGeneratorTest < GeneratorsTestCase assert_file 'config/environment.rb', /# RAILS_GEM_VERSION/ end + def test_template_from_dir_pwd + FileUtils.cd(RAILS_ROOT) + assert_match /It works from file!/, run_generator(["-m", "lib/template.rb"]) + end + def test_template_raises_an_error_with_invalid_path content = capture(:stderr){ run_generator(["-m", "non/existant/path"]) } assert_match /The template \[.*\] could not be loaded/, content diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 011bd518f8..9444a9ed4b 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -20,7 +20,7 @@ class GeneratorsTestCase < Test::Unit::TestCase def destination_root @destination_root ||= File.expand_path(File.join(File.dirname(__FILE__), - '..', '..', 'fixtures', 'tmp')) + '..', 'fixtures', 'tmp')) end def setup -- cgit v1.2.3