diff options
author | Andrew Vit <andrew@avit.ca> | 2009-02-15 19:33:12 -0800 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-02-28 17:59:01 +0000 |
commit | 16b3d2b621b1682f249209097c31b0a10f0f87ef (patch) | |
tree | 50dcce6566e3cd30d3a45fd05ad6d2e9951b51b3 | |
parent | 8607740a29d0f9ceb2ca21c9ef714f74b64d4a88 (diff) | |
download | rails-16b3d2b621b1682f249209097c31b0a10f0f87ef.tar.gz rails-16b3d2b621b1682f249209097c31b0a10f0f87ef.tar.bz2 rails-16b3d2b621b1682f249209097c31b0a10f0f87ef.zip |
Added :env option for gem in template runner [#1983 state:resolved]
For installing gems that are only needed in the test environment, specify the :env option so the dependency is written to config/environments/test.rb:
gem 'rspec', :env => 'test'
gem 'quietbacktrace', :env => %w[development test]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
-rw-r--r-- | railties/lib/rails_generator/generators/applications/app/template_runner.rb | 28 | ||||
-rw-r--r-- | railties/test/generators/rails_template_runner_test.rb | 11 |
2 files changed, 35 insertions, 4 deletions
diff --git a/railties/lib/rails_generator/generators/applications/app/template_runner.rb b/railties/lib/rails_generator/generators/applications/app/template_runner.rb index eeb6b17661..d6e3ceeba9 100644 --- a/railties/lib/rails_generator/generators/applications/app/template_runner.rb +++ b/railties/lib/rails_generator/generators/applications/app/template_runner.rb @@ -85,6 +85,7 @@ module Rails # Adds an entry into config/environment.rb for the supplied gem : def gem(name, options = {}) log 'gem', name + env = options.delete(:env) gems_code = "config.gem '#{name}'" @@ -93,18 +94,26 @@ module Rails gems_code << ", #{opts}" end - environment gems_code + environment gems_code, :env => env end # Adds a line inside the Initializer block for config/environment.rb. Used by #gem - def environment(data = nil, &block) + # If options :env is specified, the line is appended to the corresponding + # file in config/environments/#{env}.rb + def environment(data = nil, options = {}, &block) sentinel = 'Rails::Initializer.run do |config|' data = block.call if !data && block_given? in_root do - gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| - "#{match}\n " << data + if options[:env].nil? + gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| + "#{match}\n " << data + end + else + options[:env].to_a.each do|env| + append_file "config/environments/#{env}.rb", "\n#{data}" + end end end end @@ -356,6 +365,17 @@ module Rails File.open(path, 'wb') { |file| file.write(content) } end + # Append text to a file + # + # ==== Example + # + # append_file 'config/environments/test.rb', 'config.gem "rspec"' + # + def append_file(relative_destination, data) + path = destination_path(relative_destination) + File.open(path, 'ab') { |file| file.write(data) } + end + def destination_path(relative_destination) File.join(root, relative_destination) end diff --git a/railties/test/generators/rails_template_runner_test.rb b/railties/test/generators/rails_template_runner_test.rb index 56afc85eba..4e1937e0c6 100644 --- a/railties/test/generators/rails_template_runner_test.rb +++ b/railties/test/generators/rails_template_runner_test.rb @@ -82,6 +82,17 @@ class RailsTemplateRunnerTest < GeneratorTestCase assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'") end + def test_gem_with_env_string_should_put_gem_dependency_in_specified_environment + run_template_method(:gem, 'rspec', :env => 'test') + assert_generated_file_with_data('config/environments/test.rb', "config.gem 'rspec'", 'test') + end + + def test_gem_with_env_array_should_put_gem_dependency_in_specified_environments + run_template_method(:gem, 'quietbacktrace', :env => %w[ development test ]) + assert_generated_file_with_data('config/environments/development.rb', "config.gem 'quietbacktrace'") + assert_generated_file_with_data('config/environments/test.rb', "config.gem 'quietbacktrace'") + end + def test_environment_should_include_data_in_environment_initializer_block load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]' run_template_method(:environment, load_paths) |