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

Allow the :hosts and :roles keys to accept lambdas, which will be evaluated lazily to allow runtime selection of hosts and roles in tasks

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@7043 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jamis Buck 2007-06-17 14:27:47 +00:00
parent 510576ba27
commit 4d49ecad8b
4 changed files with 23 additions and 2 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Allow the :hosts and :roles keys to accept lambdas, which will be evaluated lazily to allow runtime selection of hosts and roles in tasks [Jamis Buck]
* Use `which' to test whether a command exists in the remote path, instead of `test -p' [Jamis Buck]
* Make sure the connection factory is established synchronously, to avoid multiple gateway instances being spawned [Jamis Buck]

View file

@ -25,6 +25,9 @@ module Capistrano
@logger = Logger.new
end
# make the DSL easier to read when using lazy evaluation via lambdas
alias lazy lambda
# The includes must come at the bottom, since they may redefine methods
# defined in the base class.
include Connections, Execution, Loading, Namespaces, Roles, Servers, Variables

View file

@ -53,17 +53,23 @@ module Capistrano
def server_list_from(hosts)
hosts = hosts.split(/,/) if String === hosts
Array(hosts).map { |s| String === s ? ServerDefinition.new(s.strip) : s }
hosts = build_list(hosts)
hosts.map { |s| String === s ? ServerDefinition.new(s.strip) : s }
end
def role_list_from(roles)
roles = roles.split(/,/) if String === roles
Array(roles).map do |role|
roles = build_list(roles)
roles.map do |role|
role = String === role ? role.strip.to_sym : role
raise ArgumentError, "unknown role `#{role}'" unless self.roles.key?(role)
role
end
end
def build_list(list)
Array(list).map { |item| item.respond_to?(:call) ? item.call : item }.flatten
end
end
end
end

View file

@ -77,4 +77,14 @@ class ConfigurationServersTest < Test::Unit::TestCase
task = new_task(:testing, @config, :roles => :web)
assert_equal %w(app1 app2 app3).sort, @config.find_servers_for_task(task, :roles => :app).map { |s| s.host }.sort
end
def test_find_servers_with_lambda_for_hosts_should_be_evaluated
assert_equal %w(foo), @config.find_servers(:hosts => lambda { "foo" }).map { |s| s.host }.sort
assert_equal %w(bar foo), @config.find_servers(:hosts => lambda { %w(foo bar) }).map { |s| s.host }.sort
end
def test_find_servers_with_lambda_for_roles_should_be_evaluated
assert_equal %w(app1 app2 app3), @config.find_servers(:roles => lambda { :app }).map { |s| s.host }.sort
assert_equal %w(app2 file), @config.find_servers(:roles => lambda { [:report, :file] }).map { |s| s.host }.sort
end
end