Class: TTY::Command
- Inherits:
-
Object
- Object
- TTY::Command
- Defined in:
- lib/tty/command.rb,
lib/tty/command/cmd.rb,
lib/tty/command/result.rb,
lib/tty/command/version.rb,
lib/tty/command/truncator.rb,
lib/tty/command/dry_runner.rb,
lib/tty/command/exit_error.rb,
lib/tty/command/child_process.rb,
lib/tty/command/printers/null.rb,
lib/tty/command/printers/quiet.rb,
lib/tty/command/process_runner.rb,
lib/tty/command/printers/pretty.rb,
lib/tty/command/printers/abstract.rb,
lib/tty/command/printers/progress.rb
Defined Under Namespace
Modules: ChildProcess, Printers Classes: Cmd, DryRunner, ExitError, ProcessRunner, Result, Truncator
Constant Summary collapse
- ExecuteError =
Class.new(StandardError)
- TimeoutExceeded =
Class.new(StandardError)
- RUBY =
Path to the current Ruby
ENV["RUBY"] || ::File.join( RbConfig::CONFIG["bindir"], RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"] )
- WIN_PLATFORMS =
/cygwin|mswin|mingw|bccwin|wince|emx/.freeze
- VERSION =
"0.10.1"
Instance Attribute Summary collapse
-
#printer ⇒ Object
readonly
Returns the value of attribute printer.
Class Method Summary collapse
Instance Method Summary collapse
-
#dry_run? ⇒ Boolean
Check if in dry mode.
-
#initialize(**options) ⇒ Command
constructor
Initialize a Command object.
-
#ruby(*args, &block) ⇒ Object
Run Ruby interperter with the given arguments.
-
#run(*args) {|out, err| ... } ⇒ Object
Start external executable in a child process.
-
#run!(*args, &block) ⇒ Object
Start external executable without raising ExitError.
-
#test(*args) ⇒ Object
Execute shell test command.
-
#wait(*args) ⇒ Object
Wait on long running script until output matches a specific pattern.
Constructor Details
#initialize(**options) ⇒ Command
Initialize a Command object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tty/command.rb', line 54 def initialize(**) @output = .fetch(:output) { $stdout } @color = .fetch(:color) { true } @uuid = .fetch(:uuid) { true } @printer_name = .fetch(:printer) { :pretty } @dry_run = .fetch(:dry_run) { false } @printer = use_printer(@printer_name, color: @color, uuid: @uuid) @cmd_options = {} @cmd_options[:verbose] = .fetch(:verbose, true) @cmd_options[:pty] = true if [:pty] @cmd_options[:binmode] = true if [:binmode] @cmd_options[:timeout] = [:timeout] if [:timeout] end |
Instance Attribute Details
#printer ⇒ Object (readonly)
Returns the value of attribute printer.
41 42 43 |
# File 'lib/tty/command.rb', line 41 def printer @printer end |
Class Method Details
.record_separator ⇒ Object
29 30 31 |
# File 'lib/tty/command.rb', line 29 def self.record_separator @record_separator ||= $/ end |
.record_separator=(sep) ⇒ Object
33 34 35 |
# File 'lib/tty/command.rb', line 33 def self.record_separator=(sep) @record_separator = sep end |
.windows? ⇒ Boolean
37 38 39 |
# File 'lib/tty/command.rb', line 37 def self.windows? !!(RbConfig::CONFIG["host_os"] =~ WIN_PLATFORMS) end |
Instance Method Details
#dry_run? ⇒ Boolean
Check if in dry mode
168 169 170 |
# File 'lib/tty/command.rb', line 168 def dry_run? @dry_run end |
#ruby(*args, &block) ⇒ Object
Run Ruby interperter with the given arguments
154 155 156 157 158 159 160 161 |
# File 'lib/tty/command.rb', line 154 def ruby(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} if args.length > 1 run(*([RUBY] + args + []), &block) else run("#{RUBY} #{args.first}", , &block) end end |
#run(*args) {|out, err| ... } ⇒ Object
Start external executable in a child process
102 103 104 105 106 107 108 109 |
# File 'lib/tty/command.rb', line 102 def run(*args, &block) cmd = command(*args) result = execute_command(cmd, &block) if result && result.failure? raise ExitError.new(cmd.to_command, result) end result end |
#run!(*args, &block) ⇒ Object
Start external executable without raising ExitError
117 118 119 120 |
# File 'lib/tty/command.rb', line 117 def run!(*args, &block) cmd = command(*args) execute_command(cmd, &block) end |
#test(*args) ⇒ Object
Execute shell test command
144 145 146 |
# File 'lib/tty/command.rb', line 144 def test(*args) run!(:test, *args).success? end |
#wait(*args) ⇒ Object
Wait on long running script until output matches a specific pattern
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/tty/command.rb', line 128 def wait(*args) pattern = args.pop unless pattern raise ArgumentError, "Please provide condition to wait for" end run(*args) do |out, _| raise if out =~ /#{pattern}/ end rescue ExitError # noop end |