1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Add :default_environment variable, which is applied to every command

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@6917 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2007-06-01 02:56:42 +00:00
parent 68b274e06b
commit e581f83310
3 changed files with 48 additions and 0 deletions

View file

@ -1,3 +1,8 @@
*SVN*
* Add :default_environment variable, which is applied to every command
*1.99.1 (2.0 Preview 2)* May 10, 2007
* Fix some documentation typos [eventualbuddha]

View file

@ -7,6 +7,9 @@ module Capistrano
def self.included(base) #:nodoc:
base.extend(ClassMethods)
base.send :alias_method, :initialize_without_invocation, :initialize
base.send :alias_method, :initialize, :initialize_with_invocation
base.default_io_proc = Proc.new do |ch, stream, out|
level = stream == :err ? :important : :info
ch[:options][:logger].send(level, out, "#{stream} :: #{ch[:server]}")
@ -17,6 +20,11 @@ module Capistrano
attr_accessor :default_io_proc
end
def initialize_with_invocation(*args) #:nodoc:
initialize_without_invocation(*args)
set :default_environment, {}
end
# Invokes the given command. If a +via+ key is given, it will be used
# to determine what method to use to invoke the command. It defaults
# to :run, but may be :sudo, or any other method that conforms to the
@ -37,6 +45,8 @@ module Capistrano
block ||= self.class.default_io_proc
logger.debug "executing #{cmd.strip.inspect}"
options = add_default_environment(options)
execute_on_servers(options) do |servers|
targets = servers.map { |s| sessions[s] }
Command.process(cmd, targets, options.merge(:logger => logger), &block)
@ -88,6 +98,20 @@ module Capistrano
end
end
end
# Merges the default environment into options as the :env key. If the
# :env key already exists, the :env key is merged into default_environment
# and then added back into options.
def add_default_environment(options)
return options if self[:default_environment].empty?
options = options.dup
env = self[:default_environment]
env = env.merge(options[:env]) if options[:env]
options[:env] = env
options
end
end
end
end

View file

@ -13,6 +13,10 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase
@options[*args]
end
def set(name, value)
@options[name] = value
end
def fetch(*args)
@options.fetch(*args)
end
@ -51,6 +55,21 @@ class ConfigurationActionsInvocationTest < Test::Unit::TestCase
@config.run("ls", &inspectable_proc)
end
def test_add_default_environment_should_simply_return_options_if_default_environment_is_blank
assert_equal({:foo => "bar"}, @config.add_default_environment(:foo => "bar"))
end
def test_add_default_environment_should_merge_default_environment_as_env
@config[:default_environment][:bang] = "baz"
assert_equal({:foo => "bar", :env => { :bang => "baz" }}, @config.add_default_environment(:foo => "bar"))
end
def test_add_default_environment_should_merge_env_with_default_environment
@config[:default_environment][:bang] = "baz"
@config[:default_environment][:bacon] = "crunchy"
assert_equal({:foo => "bar", :env => { :bang => "baz", :bacon => "chunky", :flip => "flop" }}, @config.add_default_environment(:foo => "bar", :env => {:bacon => "chunky", :flip => "flop"}))
end
def test_default_io_proc_should_log_stdout_arguments_as_info
ch = { :host => "capistrano",
:server => server("capistrano"),