Module: EcoRake::Shell::Command

Includes:
Rake::DSL
Included in:
Gpg
Defined in:
lib/eco-rake/shell/command.rb

Instance Method Summary collapse

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

#sh_chain(cmds, continue: false, &block) ⇒ Object



32
33
34
35
36
37
# 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



41
42
43
# File 'lib/eco-rake/shell/command.rb', line 41

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.



48
49
50
51
52
53
54
55
56
57
# File 'lib/eco-rake/shell/command.rb', line 48

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

#string_cmd(base, *options, join: ' ') ⇒ String



26
27
28
# File 'lib/eco-rake/shell/command.rb', line 26

def string_cmd(base, *options, join: ' ')
  array_cmd(base, *options).join(join)
end