Class: WebpackNative::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/webpack_native/runner.rb

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ 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

#cmdObject (readonly)

Returns the value of attribute cmd.



4
5
6
# File 'lib/webpack_native/runner.rb', line 4

def cmd
  @cmd
end

#exit_statusObject (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

#stderrObject (readonly)

Returns the value of attribute stderr.



4
5
6
# File 'lib/webpack_native/runner.rb', line 4

def stderr
  @stderr
end

#stdoutObject (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

.run!(*cmd) ⇒ Object

Run a command, raise Runner::Error if it fails

Raises:



15
16
17
# File 'lib/webpack_native/runner.rb', line 15

def self.run!(*cmd)
  Runner.new(*cmd).run!
end

.run?(*cmd) ⇒ Boolean

Run a command, return true if it succeeds, false if not



22
23
24
# File 'lib/webpack_native/runner.rb', line 22

def self.run?(*cmd)
  Runner.new(*cmd).run?
end

Instance Method Details

#runRunner

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

Raises:



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



37
38
39
# File 'lib/webpack_native/runner.rb', line 37

def success?
  exit_status.zero?
end