Module: Philiprehberger::TaskRunner
- Defined in:
- lib/philiprehberger/task_runner.rb,
lib/philiprehberger/task_runner/result.rb,
lib/philiprehberger/task_runner/version.rb
Defined Under Namespace
Classes: CommandError, Error, Result, TimeoutError
Constant Summary collapse
- VERSION =
'0.6.0'
Class Method Summary collapse
-
.run(cmd, *args, timeout: nil, env: nil, chdir: nil, signal: :TERM, kill_after: 5, stdin: nil) {|line, stream| ... } ⇒ Result
Run a shell command with output capture, optional timeout, streaming, signal handling, and stdin piping.
-
.run!(cmd) ⇒ Result
Run a shell command, raising CommandError on non-zero exit.
-
.run?(cmd) ⇒ Boolean
Boolean convenience — run the command and return whether it succeeded (exit code 0).
-
.which(cmd) ⇒ String?
Find the absolute path of an executable on PATH.
Class Method Details
.run(cmd, *args, timeout: nil, env: nil, chdir: nil, signal: :TERM, kill_after: 5, stdin: nil) {|line, stream| ... } ⇒ Result
Run a shell command with output capture, optional timeout, streaming, signal handling, and stdin piping.
When a block is given, each line of stdout/stderr is yielded as it arrives. If the block accepts two arguments, it receives (line, stream) where stream is :stdout or :stderr. If the block accepts one argument, it receives only the line (stdout lines only, for backward compatibility).
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/philiprehberger/task_runner.rb', line 44 def self.run(cmd, *args, timeout: nil, env: nil, chdir: nil, signal: :TERM, kill_after: 5, stdin: nil, &block) full_cmd = args.empty? ? cmd : [cmd, *args] spawn_opts = {} spawn_opts[:chdir] = chdir if chdir env_hash = env || {} start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) if block run_streaming(env_hash, full_cmd, spawn_opts, timeout, start_time, signal, kill_after, stdin, &block) else run_capture(env_hash, full_cmd, spawn_opts, timeout, start_time, signal, kill_after, stdin) end end |
.run!(cmd) ⇒ Result
Run a shell command, raising CommandError on non-zero exit.
Accepts the same arguments as run.
66 67 68 69 70 71 |
# File 'lib/philiprehberger/task_runner.rb', line 66 def self.run!(cmd, ...) result = run(cmd, ...) raise CommandError, result unless result.success? result end |
.run?(cmd) ⇒ Boolean
Boolean convenience — run the command and return whether it succeeded (exit code 0). Swallows ‘TimeoutError` (reported as false). Accepts the same arguments as run.
78 79 80 81 82 |
# File 'lib/philiprehberger/task_runner.rb', line 78 def self.run?(cmd, ...) run(cmd, ...).success? rescue TimeoutError false end |
.which(cmd) ⇒ String?
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/philiprehberger/task_runner.rb', line 94 def self.which(cmd) raise ArgumentError, 'cmd cannot be nil or empty' if cmd.nil? || cmd.to_s.empty? exts = if Gem.win_platform? (ENV['PATHEXT'] || '.COM;.EXE;.BAT;.CMD').split(';') else [''] end ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).each do |dir| next if dir.empty? exts.each do |ext| candidate = File.join(dir, "#{cmd}#{ext}") return File.(candidate) if File.file?(candidate) && File.executable?(candidate) end end nil end |