Helpers: Default helpers |
module Mina
module Helpers |
invoke |
|
Invokes another Rake task. Invokes the task given in
Options: reenable (bool) - Execute the task even next time. |
def invoke(task, options = {})
Rake.application.invoke_task task
Rake::Task[task].reenable if options[:reenable]
end |
erb |
|
Evaluates an ERB block in the current scope and returns a string.
Returns the output string of the ERB template. |
def erb(file, b=binding)
require 'erb'
erb = ERB.new(File.read(file))
erb.result b
end |
run! |
|
SSHs into the host and runs the code that has been queued. This is already automatically invoked before Rake exits to run all commands that have been queued up.
Returns nothing. |
def run!
report_time { ssh commands(:default) }
end |
report_time |
|
Report time elapsed in the block. Returns the output of the block. |
def report_time(&blk)
time, output = measure &blk
print_str "Elapsed time: %.2f seconds" % [time]
output
end |
measure |
|
Measures the time (in ms) a block takes. Returns a [time, output] tuple. |
def measure(&blk)
t = Time.now
output = yield
[(Time.now - t).to_i, output]
end |
mina_cleanup |
|
Internal: Invoked when Rake exits. Returns nothing. |
def mina_cleanup!
run! if commands.any?
end |
Errors |
|
die |
|
Exits with a nice looking message. Returns nothing. |
def die(code=1, msg=null)
str = "Failed with status #{code}"
str += " (#{msg})" if msg
err = Failed.new(str)
err.exitstatus = code
raise err
end |
error |
|
Internal: Prints to stdout.
Consider using |
def error(str)
$stderr.write "#{str}\n"
end |
Queueing |
|
queue |
|
Queues code to be ran. This queues code to be ran to the current code bucket (defaults to Returns nothing. |
def queue(code)
commands
commands(@to) << unindent(code)
end |
queue! |
|
Shortcut for |
def queue!(code)
queue echo_cmd(code)
end |
echo_cmd |
|
Converts a bash command to a command that echoes before execution. Used to show commands in verbose mode. This does nothing unless verbose mode is on. Returns a string of the compound bash command, typically in the format of
|
def echo_cmd(str)
if verbose_mode?
require 'shellwords'
"echo #{Shellwords.escape("$ " + str)} &&\n#{str}"
else
str
end
end |
Commands |
|
commands |
|
Returns an array of queued code strings. You may give an optional Returns an array of strings. |
def commands(aspect=:default)
(@commands ||= begin
@to = :default
Hash.new { |h, k| h[k] = Array.new }
end)[aspect]
end |
isolate |
|
Starts a new block where new Returns nothing. |
def isolate(&blk)
old, @commands = @commands, nil
result = yield
new_code, @commands = @commands, old
result
end |
in_directory |
|
Starts a new block where #commands are collected, to be executed inside Returns nothing. in_directory './webapp' do queue "./reload" end commands.should == ['cd ./webapp && (./reload && true)'] |
def in_directory(path, &blk)
isolated_commands = isolate { yield; commands }
isolated_commands.each { |cmd| queue "(cd #{path} && (#{cmd}))" }
end |
Defines instructions on how to do a certain thing.
This makes the commands that are Returns nothing. |
def to(name, &blk)
old, @to = @to, name
yield
ensure
@to = old
end |
Settings helpers |
|
set |
|
Sets settings.
Sets given symbol Returns the value. |
def set(key, value)
settings.send :"#{key}=", value
end |
set_default |
|
Sets default settings.
Sets given symbol Returns the value. |
def set_default(key, value)
settings.send :"#{key}=", value unless settings.send(:"#{key}?")
end |
settings |
|
Accesses the settings hash. |
def settings
@settings ||= Settings.new
end |
method_missing |
|
Hook to get settings. See #settings for an explanation. Returns things. |
def method_missing(meth, *args, &blk)
settings.send meth, *args
end |
Command line mode helpers |
|
verbose_mode? |
|
Checks if Rake was invoked with --verbose. Returns true or false. |
def verbose_mode?
if Rake.respond_to?(:verbose) |
Rake 0.9.x |
Rake.verbose == true
else |
Rake 0.8.x |
RakeFileUtils.verbose_flag != :default
end
end |
simulate_mode? |
|
Checks if Rake was invoked with --simulate. Returns true or false. |
def simulate_mode?
!! ENV['simulate']
end |
Internal helpers |
|
indent |
|
Indents a given code block with |
def indent(count, str)
str.gsub(/^/, " "*count)
end |
unindent |
|
Internal: Normalizes indentation on a given string. Returns the normalized string without extraneous indentation. |
def unindent(code)
if code =~ /^\n([ \t]+)/
code = code.gsub(/^#{$1}/, '')
end
code.strip
end |
reindent |
|
Resets the indentation on a given code block. |
def reindent(n, code)
indent n, unindent(code)
end |
capture |
|
Returns the output of command via SSH. |
def capture(cmd, options={})
ssh cmd, options.merge(:return => true)
end
end
end
|