提交 16b3d2b6 编写于 作者: A Andrew Vit 提交者: Pratik Naik

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: NPratik Naik <pratiknaik@gmail.com>
上级 8607740a
......@@ -85,6 +85,7 @@ def plugin(name, options)
# 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 @@ def gem(name, options = {})
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 @@ def gsub_file(relative_destination, regexp, *args, &block)
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
......
......@@ -82,6 +82,17 @@ def test_gem_with_options_should_include_options_in_gem_dependency_in_environmen
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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册