1
- require 'pp'
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
2
4
require 'open3'
5
+ require 'Rake'
3
6
4
7
module Bazel
5
8
def self . execute ( kind , args , target , &block )
@@ -11,27 +14,25 @@ def self.execute(kind, args, target, &block)
11
14
end
12
15
13
16
cmd = %w[ bazel ] + [ kind , target ] + ( args || [ ] )
17
+ cmd_out = ''
18
+ cmd_exit_code = 0
14
19
15
20
if SeleniumRake ::Checks . windows?
16
- cmd = cmd + [ " 2>&1" ]
21
+ cmd += [ ' 2>&1' ]
17
22
cmd_line = cmd . join ( ' ' )
18
23
cmd_out = `#{ cmd_line } ` . encode ( 'UTF-8' , 'binary' , invalid : :replace , undef : :replace , replace : '' )
19
- cmd_exit_code = $?. success?
24
+ puts cmd_out if verbose
25
+ cmd_exit_code = $CHILD_STATUS
20
26
else
21
27
Open3 . popen2e ( *cmd ) do |stdin , stdouts , wait |
22
28
is_running = true
23
29
stdin . close
24
- cmd_out = ''
25
30
while is_running
26
31
begin
27
- pipes = IO . select ( [ stdouts ] )
28
- if pipes . empty?
29
- is_running = false
30
- else
31
- line = stdouts . readpartial ( 512 )
32
- cmd_out << line
33
- STDOUT . print line if verbose
34
- end
32
+ stdouts . wait_readable
33
+ line = stdouts . readpartial ( 512 )
34
+ cmd_out += line
35
+ $stdout. print line if verbose
35
36
rescue EOFError
36
37
is_running = false
37
38
end
@@ -40,13 +41,12 @@ def self.execute(kind, args, target, &block)
40
41
end
41
42
end
42
43
43
- puts cmd_out if verbose
44
-
45
- raise "#{ cmd . join ( ' ' ) } failed with exit code: #{ cmd_exit_code } " unless cmd_exit_code
44
+ raise "#{ cmd . join ( ' ' ) } failed with exit code: #{ cmd_exit_code } " if cmd_exit_code != 0
46
45
47
46
block &.call ( cmd_out )
48
- out_artifact = Regexp . last_match ( 1 ) if cmd_out =~ %r{\s +(bazel-bin/\S +)}
47
+ return unless cmd_out =~ %r{\s +(bazel-bin/\S +)}
49
48
49
+ out_artifact = Regexp . last_match ( 1 )
50
50
puts "#{ target } -> #{ out_artifact } " if out_artifact
51
51
out_artifact
52
52
end
0 commit comments