Class: Kamaze::Project::Tools::Vagrant::Shell

Inherits:
Object
  • Object
show all
Includes:
Concern::Cli::WithExitOnFailure, Concern::Sh
Defined in:
lib/kamaze/project/tools/vagrant/shell.rb,
lib/kamaze/project/tools/vagrant/shell.rb

Overview

Execute commands using executable

Options are passed to Rake::FileUtilsExt.sh(). Executable can be defined through options.

Direct Known Subclasses

Remote

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Shell

Initialize a shell with given options

Parameters:

  • options (Hash) (defaults to: {})


34
35
36
37
38
39
40
41
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 34

def initialize(options = {})
  @options = options
  # Executable used by command
  @executable = options.delete(:executable) || :vagrant

  # default ``sh`` options
  @options[:verbose] = false unless options.key?(:verbose)
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


29
30
31
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 29

def options
  @options
end

Instance Method Details

#create_shell_runner(cmd) ⇒ Proc (protected) Originally defined in module Concern::Sh

Get shell block

Parameters:

  • cmd (Array)

Returns:

  • (Proc)

#debug_cmd(cmd) ⇒ String (protected) Originally defined in module Concern::Sh

Print a debug message

Parameters:

  • cmd (Array)

Returns:

  • (String)

#executableString|nil

Get (absolute) path to executable

Return nil when executable CAN NOT be detected.

Returns:

  • (String|nil)


53
54
55
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 53

def executable
  Cliver.detect(@executable)&.freeze
end

#executable?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 44

def executable?
  executable
end

#execute(*params, &block) ⇒ Object

Run given arguments as system command using executable.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 68

def execute(*params, &block)
  env = preserved_env

  Bundler.with_clean_env do
    with_exit_on_failure do
      [env].concat(to_a.concat(params)).push(options).yield_self do |cmd|
        sh(*cmd, &block)

        self.retcode = self.shell_runner_last_status.exitstatus
      end
    end
  end
end

#failure?Boolean Also known as: failed? Originally defined in module Concern::Cli

Denote execution is a failure.

Returns:

  • (Boolean)

#preserved_env(from = ENV) ⇒ Hash (protected)

TODO:

refactor

Get preserved env (from given env)

Parameters:

  • from (ENV|Hash) (defaults to: ENV)

Returns:

  • (Hash)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 90

def preserved_env(from = ENV)
  env = {}
  from = from.to_h

  ['SILENCE_DUPLICATE_DIRECTORY_ERRORS'].each do |key|
    next unless from.key?(key)

    env[key] = from.fetch(key)
  end

  env
end

#retcodeFixnum Originally defined in module Concern::Cli

Status code usable to eventually initiates the termination.

Returns:

  • (Fixnum)

#sh(*cmd, &block) ⇒ Object (protected) Originally defined in module Concern::Sh

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).

#shell_runner_debug?Boolean Originally defined in module Concern::Sh

Denote shell runner is in debug mode.

Returns:

  • (Boolean)

#success?Boolean Also known as: successful? Originally defined in module Concern::Cli

Denote execution is a success.

Returns:

  • (Boolean)

#to_aArray

Returns:

  • (Array)


58
59
60
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 58

def to_a
  [executable]
end

#to_sString

Returns:

  • (String)


63
64
65
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 63

def to_s
  executable.to_s
end

#with_exit_on_failure {|Object| ... } ⇒ Object (protected) Originally defined in module Concern::Cli::WithExitOnFailure

Initiates termination by raising SystemExit exception depending on success of given block.

Yields:

Yield Parameters:

  • (self)

Returns:

Raises:

  • (SystemExit)