Class: WebpackNative::Runner
- Inherits:
-
Object
- Object
- WebpackNative::Runner
- Defined in:
- lib/webpack_native/runner.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.run(*cmd) ⇒ Object
Run a command, return runner instance.
-
.run!(*cmd) ⇒ Object
Run a command, raise Runner::Error if it fails.
-
.run?(*cmd) ⇒ Boolean
Run a command, return true if it succeeds, false if not.
Instance Method Summary collapse
-
#initialize(cmd) ⇒ Runner
constructor
A new instance of Runner.
-
#run ⇒ Runner
Run the command, return self.
-
#run! ⇒ Object
Run the command and return stdout, raise if fails.
-
#run? ⇒ Boolean
Run the command and return true if success, false if failure.
-
#success? ⇒ Boolean
Success or failure?.
Constructor Details
#initialize(cmd) ⇒ Runner
Returns a new instance of Runner.
29 30 31 32 33 34 |
# File 'lib/webpack_native/runner.rb', line 29 def initialize(cmd) @cmd = cmd.is_a?(Array) ? cmd.join(' ') : cmd @stdout = +'' @stderr = +'' @exit_status = nil end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
4 5 6 |
# File 'lib/webpack_native/runner.rb', line 4 def cmd @cmd end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status.
4 5 6 |
# File 'lib/webpack_native/runner.rb', line 4 def exit_status @exit_status end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
4 5 6 |
# File 'lib/webpack_native/runner.rb', line 4 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
4 5 6 |
# File 'lib/webpack_native/runner.rb', line 4 def stdout @stdout end |
Class Method Details
.run(*cmd) ⇒ Object
Run a command, return runner instance
8 9 10 |
# File 'lib/webpack_native/runner.rb', line 8 def self.run(*cmd) Runner.new(*cmd).run end |
Instance Method Details
#run ⇒ Runner
Run the command, return self
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/webpack_native/runner.rb', line 43 def run Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| until [stdout, stderr].all?(&:eof?) readable = IO.select([stdout, stderr]) next unless readable&.first readable.first.each do |stream| data = +'' # rubocop:disable Lint/HandleExceptions begin stream.read_nonblock(1024, data) rescue EOFError # ignore, it's expected for read_nonblock to raise EOFError # when all is read end if stream == stdout @stdout << data $stdout.write(data) else @stderr << data $stderr.write(data) end end end @exit_status = wait_thr.value.exitstatus end self end |
#run! ⇒ Object
Run the command and return stdout, raise if fails
77 78 79 80 81 |
# File 'lib/webpack_native/runner.rb', line 77 def run! return run.stdout if run.success? raise(Error, "command failed, exit: %d - stdout: %s / stderr: %s" % [exit_status, stdout, stderr]) end |
#run? ⇒ Boolean
Run the command and return true if success, false if failure
85 86 87 |
# File 'lib/webpack_native/runner.rb', line 85 def run? run.success? end |
#success? ⇒ Boolean
Returns success or failure?.
37 38 39 |
# File 'lib/webpack_native/runner.rb', line 37 def success? exit_status.zero? end |