Class: Kamaze::Project::Tools::Vagrant::Shell
- 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
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
Instance Method Summary collapse
-
#create_shell_runner(cmd) ⇒ Proc
included
from Concern::Sh
protected
Get shell block.
-
#debug_cmd(cmd) ⇒ String
included
from Concern::Sh
protected
Print a debug message.
-
#executable ⇒ String|nil
Get (absolute) path to executable.
- #executable? ⇒ Boolean
-
#execute(*params, &block) ⇒ Object
Run given arguments as system command using
executable
. -
#failure? ⇒ Boolean
(also: #failed?)
included
from Concern::Cli
Denote execution is a failure.
-
#initialize(options = {}) ⇒ Shell
constructor
Initialize a shell with given options.
-
#preserved_env(from = ENV) ⇒ Hash
protected
Get preserved env (from given env).
-
#retcode ⇒ Fixnum
included
from Concern::Cli
Status code usable to eventually initiates the termination.
-
#sh(*cmd, &block) ⇒ Object
included
from Concern::Sh
protected
Run given (
cmd
) system command. -
#shell_runner_debug? ⇒ Boolean
included
from Concern::Sh
Denote shell runner is in debug mode.
-
#success? ⇒ Boolean
(also: #successful?)
included
from Concern::Cli
Denote execution is a success.
- #to_a ⇒ Array
- #to_s ⇒ String
-
#with_exit_on_failure {|Object| ... } ⇒ Object
included
from Concern::Cli::WithExitOnFailure
protected
Initiates termination by raising
SystemExit
exception depending onsuccess
of given block.
Constructor Details
#initialize(options = {}) ⇒ Shell
Initialize a shell with given options
34 35 36 37 38 39 40 41 |
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 34 def initialize( = {}) @options = # Executable used by command @executable = .delete(:executable) || :vagrant # default ``sh`` options @options[:verbose] = false unless .key?(:verbose) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
29 30 31 |
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 29 def @options end |
Instance Method Details
#create_shell_runner(cmd) ⇒ Proc (protected) Originally defined in module Concern::Sh
Get shell block
#debug_cmd(cmd) ⇒ String (protected) Originally defined in module Concern::Sh
Print a debug message
#executable ⇒ String|nil
Get (absolute) path to executable
Return nil
when executable CAN NOT be detected.
53 54 55 |
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 53 def executable Cliver.detect(@executable)&.freeze end |
#executable? ⇒ 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().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.
#preserved_env(from = ENV) ⇒ Hash (protected)
refactor
Get preserved env (from given env)
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 |
#retcode ⇒ Fixnum Originally defined in module Concern::Cli
Status code usable to eventually initiates the termination.
#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.
#success? ⇒ Boolean Also known as: successful? Originally defined in module Concern::Cli
Denote execution is a success.
#to_a ⇒ Array
58 59 60 |
# File 'lib/kamaze/project/tools/vagrant/shell.rb', line 58 def to_a [executable] end |
#to_s ⇒ 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.