Module: Kamaze::Project::Concern::Sh

Included in:
Tools::Gemspec::Packer::Command, Tools::Vagrant::Shell
Defined in:
lib/kamaze/project/concern/sh.rb

Overview

Concern for Sh

This module provides a wrapper around Rake::FileUtilsExt.sh()

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/kamaze/project/concern/sh.rb', line 19

def included(base)
  base.class_eval <<-"ACCESSORS", __FILE__, __LINE__ + 1
    protected

    attr_accessor :shell_runner_last_status

    attr_writer :shell_runner_debug
  ACCESSORS
end

Instance Method Details

#create_shell_runner(cmd) ⇒ Proc (protected)

Get shell block

Parameters:

  • cmd (Array)

Returns:

  • (Proc)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kamaze/project/concern/sh.rb', line 59

def create_shell_runner(cmd)
  proc do |ok, status|
    self.shell_runner_last_status = status

    if !ok and shell_runner_debug?
      # rubocop:disable Layout/IndentHash
      warn("Command failed with status (%<retcode>s):\n# %<command>s" % {
             retcode: status.exitstatus,
             command: debug_cmd(cmd.clone).gsub(/\{\}$/, '')
           })
      # rubocop:enable Layout/IndentHash
    end
  end
end

#debug_cmd(cmd) ⇒ String (protected)

Print a debug message

Parameters:

  • cmd (Array)

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kamaze/project/concern/sh.rb', line 78

def debug_cmd(cmd)
  options = cmd.last.is_a?(Hash) ? cmd.delete_at(-1) : {}

  '%<command>s' % {
    command: [
      (cmd[0].is_a?(Hash) ? cmd.delete_at(0) : {}).to_a.map do |v|
        "#{v[0]}=#{Shellwords.shellescape(v[1])}"
      end.join(' '),
      Shellwords.shelljoin(cmd),
      options
    ].join(' ')
  }
end

#sh(*cmd, &block) ⇒ Object (protected)

Run given (cmd) system command.

If multiple arguments are given the command is run directly (without the shell, same semantics as Kernel::exec and Kernel::system).



46
47
48
49
50
51
52
53
# File 'lib/kamaze/project/concern/sh.rb', line 46

def sh(*cmd, &block)
  require 'rake'
  require 'rake/file_utils'

  block ||= create_shell_runner(cmd)

  ::Rake::FileUtilsExt.sh(*cmd, &block)
end

#shell_runner_debug?Boolean

Denote shell runner is in debug mode.

Returns:

  • (Boolean)


33
34
35
# File 'lib/kamaze/project/concern/sh.rb', line 33

def shell_runner_debug?
  @shell_runner_debug.nil? ? true : @shell_runner_debug
end