Skip to content

Commit 30dcb1a

Browse files
committed
[build] update ruby bazel module to execute rake tasks with logs
1 parent 41aa89a commit 30dcb1a

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

rake_tasks/bazel.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
require_relative 'selenium_rake/checks'
88

99
module Bazel
10-
def self.execute(kind, args, target, &block)
11-
verbose = Rake::FileUtilsExt.verbose_flag
12-
10+
def self.execute(kind, args, target, verbose: Rake::FileUtilsExt.verbose_flag, log_file: nil, &block)
1311
if target.end_with?(':run')
1412
kind = 'run'
1513
target = target[0, target.length - 4]
@@ -18,28 +16,34 @@ def self.execute(kind, args, target, &block)
1816
cmd = %w[bazel] + [kind, target] + (args || [])
1917
cmd_out = ''
2018
cmd_exit_code = 0
19+
puts "Executing:\n#{cmd.join(' ')}" if verbose
2120

2221
if SeleniumRake::Checks.windows?
23-
cmd += ['2>&1']
2422
cmd_line = cmd.join(' ')
25-
cmd_out = `#{cmd_line}`.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
26-
puts cmd_out if verbose
27-
cmd_exit_code = $CHILD_STATUS
23+
begin
24+
cmd_out = `#{cmd_line} 2>&1`.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
25+
puts cmd_out if verbose
26+
File.write(log_file, cmd_out) if log_file
27+
cmd_exit_code = $CHILD_STATUS.exitstatus
28+
rescue => e
29+
raise "Windows command execution failed: #{e.message}"
30+
end
2831
else
29-
Open3.popen2e(*cmd) do |stdin, stdouts, wait|
30-
is_running = true
31-
stdin.close
32-
while is_running
33-
begin
34-
stdouts.wait_readable
35-
line = stdouts.readpartial(512)
32+
begin
33+
Open3.popen2e(*cmd) do |stdin, stdouts, wait|
34+
stdin.close
35+
log = log_file ? File.open(log_file, 'a') : nil
36+
while (line = stdouts.gets)
3637
cmd_out += line
3738
$stdout.print line if verbose
38-
rescue EOFError
39-
is_running = false
39+
log&.write(line)
40+
log&.flush
4041
end
42+
log&.close
43+
cmd_exit_code = wait.value.exitstatus
4144
end
42-
cmd_exit_code = wait.value.exitstatus
45+
rescue => e
46+
raise "Command execution failed: #{e.message}"
4347
end
4448
end
4549

0 commit comments

Comments
 (0)