/
dsl.rb
177 lines (161 loc) · 5.06 KB
/
dsl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'cape/capistrano'
require 'cape/rake'
module Cape
# Provides methods for integrating Capistrano and Rake.
module DSL
# Enumerates Rake tasks.
#
# @param [String, Symbol] task_expression the full name of a task or
# namespace to filter; optional
# @param [Proc] block a block that processes tasks
#
# @yield [task] a block that processes tasks
# @yieldparam [Hash] task metadata on a task
#
# @return [DSL] the object
#
# @example Enumerating all Rake tasks
# # config/deploy.rb
#
# require 'cape'
#
# Cape do
# each_rake_task do |t|
# # Do something interesting with this hash:
# # * t[:name] -- the full name of the task
# # * t[:parameters] -- the names of task arguments
# # * t[:description] -- documentation on the task, including parameters
# end
# end
#
# @example Enumerating some Rake tasks
# # config/deploy.rb
#
# require 'cape'
#
# Cape do
# each_rake_task :foo do |t|
# # Do something interesting with this hash:
# # * t[:name] -- the full name of the task
# # * t[:parameters] -- the names of task arguments
# # * t[:description] -- documentation on the task, including parameters
# end
# end
def each_rake_task(task_expression=nil, &block)
rake.each_task(task_expression, &block)
self
end
# The command used to run Rake on the local computer.
#
# @return [String] the command used to run Rake on the local computer
#
# @see Rake::DEFAULT_EXECUTABLE
def local_rake_executable
rake.local_executable
end
# Sets the command used to run Rake on the local computer.
#
# @param [String] value the command used to run Rake on the local computer
# @return [String] _value_
#
# @example Changing the local Rake executable
# require 'cape'
#
# Cape do
# self.local_rake_executable = '/path/to/rake'
# $stdout.puts "We changed the local Rake executable to #{self.local_rake_executable.inspect}."
# end
def local_rake_executable=(value)
rake.local_executable = value
end
# Makes the use of a Cape block parameter optional by forwarding non-Cape
# method calls to the containing binding.
#
# @param [Symbol, String] method the method called
# @param [Array] args the arguments passed to _method_
# @param [Proc] block the block passed to _method_
# @return the result of the forwarded method call
def method_missing(method, *args, &block)
@outer_self.send(method, *args, &block)
end
# Defines Rake tasks as Capistrano recipes.
#
# @param [String, Symbol] task_expression the full name of a Rake task or
# namespace to filter; optional
# @return [DSL] the object
#
# @note Any parameters that the Rake tasks have are integrated via environment variables, since Capistrano does not support recipe parameters per se.
#
# @example Mirroring all Rake tasks
# # config/deploy.rb
#
# require 'cape'
#
# Cape do
# mirror_rake_tasks
# end
#
# @example Mirroring specific Rake tasks
# # config/deploy.rb
#
# require 'cape'
#
# Cape do
# mirror_rake_tasks 'log:clear'
# end
#
# @example Mirroring Rake tasks into a Capistrano namespace
# # config/deploy.rb
#
# require 'cape'
#
# namespace :rake_tasks do
# Cape do |cape|
# cape.mirror_rake_tasks
# end
# end
def mirror_rake_tasks(task_expression=nil)
rake.each_task task_expression do |t|
deployment_library.define_rake_wrapper t, :binding => binding
end
self
end
# The command used to run Rake on remote computers.
#
# @return [String] the command used to run Rake on remote computers
#
# @see Rake::DEFAULT_EXECUTABLE
def remote_rake_executable
rake.remote_executable
end
# Sets the command used to run Rake on remote computers.
#
# @param [String] value the command used to run Rake on remote computers
# @return [String] _value_
#
# @example Changing the remote Rake executable
# require 'cape'
#
# Cape do
# self.remote_rake_executable = '/path/to/rake'
# $stdout.puts "We changed the remote Rake executable to #{self.remote_rake_executable.inspect}."
# end
def remote_rake_executable=(value)
rake.remote_executable = value
end
private
def deployment_library
return @deployment_library if @deployment_library
raise_unless_capistrano
@deployment_library = Capistrano.new(:rake => rake)
end
def raise_unless_capistrano
if @outer_self.method(:task).owner.name !~ /^Capistrano::/
raise 'Use this in the context of Capistrano recipes'
end
end
def rake
@rake ||= Rake.new
end
end
end