Module: EcoRake::Shell::Command
Instance Method Summary collapse
-
#array_cmd(base, *opts) ⇒ String+
Helper to build command line.
-
#double_quote(str) ⇒ String
It double quotes a string (escapes the double quotes).
- #middlewared_callback(original) ⇒ Object
- #sh_chain(cmds, continue: false, &block) ⇒ Object
-
#sh_continue(comm, &block) ⇒ Object
It continues even if there was an error or
exit(1)
during the execution. -
#sh_default_block(comm) ⇒ Proc
Returns the default block for
sh
native method. -
#sh_exit_on_fail(comm) ⇒ Object
It exits if there is an error.
- #string_cmd(base, *options, join: ' ') ⇒ String
Instance Method Details
#array_cmd(base, *opts) ⇒ String+
Note:
it excludes nil
values.
Helper to build command line
16 17 18 19 20 21 22 |
# File 'lib/eco-rake/shell/command.rb', line 16 def array_cmd(base, *opts) base = [base] unless base.is_a?(Array) base.tap do |out| opts.each {|opt| out << opt unless opt.nil?} yield(out) if block_given? end end |
#double_quote(str) ⇒ String
It double quotes a string (escapes the double quotes)
9 10 11 |
# File 'lib/eco-rake/shell/command.rb', line 9 def double_quote(str) "\"#{str}\"" if str end |
#middlewared_callback(original) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/eco-rake/shell/command.rb', line 74 def middlewared_callback(original) proc do |*args, **kargs| yield(*args, **kargs) ensure original.call(*args, **kargs) end end |
#sh_chain(cmds, continue: false, &block) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/eco-rake/shell/command.rb', line 32 def sh_chain(cmds, continue: false, &block) cmds.each do |cmd| next sh(cmd, &block) unless continue ch_continue(cmd, &block) end end |
#sh_continue(comm, &block) ⇒ Object
It continues even if there was an error or exit(1)
during the execution
42 43 44 |
# File 'lib/eco-rake/shell/command.rb', line 42 def sh_continue(comm, &block) sh(comm, &sh_default_block(comm, &block)) end |
#sh_default_block(comm) ⇒ Proc
Note:
it wraps block
if given
Returns the default block for sh
native method.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/eco-rake/shell/command.rb', line 61 def sh_default_block(comm) proc do |ok, res| yield(ok, res) if block_given? unless ok msg = "Command failed (status = #{res.exitstatus})" puts "#{msg}\n • #{comm}" end res.exitstatus end end |
#sh_exit_on_fail(comm) ⇒ Object
Note:
if doesn't raise (prevents) a RuntimeError
It exits if there is an error
49 50 51 52 53 54 55 56 |
# File 'lib/eco-rake/shell/command.rb', line 49 def sh_exit_on_fail(comm) callback = middlewared_callback(sh_default_block(comm)) do |ok, res| next if ok exit(1) end sh(comm, &callback) end |
#string_cmd(base, *options, join: ' ') ⇒ String
26 27 28 |
# File 'lib/eco-rake/shell/command.rb', line 26 def string_cmd(base, *, join: ' ') array_cmd(base, *).join(join) end |