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 /railties/lib/rails_generator/generators/applications | |
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>
Diffstat (limited to 'railties/lib/rails_generator/generators/applications')
-rw-r--r-- | railties/lib/rails_generator/generators/applications/app/template_runner.rb | 28 |
1 files changed, 24 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 |