Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rake_tasks/bazel.rb
3986 views
1
# frozen_string_literal: true
2
3
require 'English'
4
require 'open3'
5
require 'rake'
6
require 'rbconfig'
7
require 'io/wait'
8
9
module Bazel
10
def self.windows?
11
(RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw32/) != nil
12
end
13
14
def self.format_cmd(cmd, verbose: false, max_args: 6)
15
return cmd.join(' ') if verbose || cmd.length <= max_args
16
17
"#{cmd[0...max_args].join(' ')} ... (#{cmd.length - max_args} more args)"
18
end
19
20
def self.execute(kind, args, target, &block)
21
verbose = Rake::FileUtilsExt.verbose_flag
22
23
if target.end_with?(':run')
24
kind = 'run'
25
target = target[0, target.length - 4]
26
end
27
28
cmd = %w[bazel] + [kind, target] + (args || [])
29
cmd_out = ''
30
cmd_exit_code = 0
31
32
puts "Executing: #{format_cmd(cmd, verbose: verbose)}"
33
if windows?
34
cmd += ['2>&1']
35
cmd_line = cmd.join(' ')
36
cmd_out = `#{cmd_line}`.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
37
puts cmd_out if verbose
38
cmd_exit_code = $CHILD_STATUS
39
else
40
Open3.popen2e(*cmd) do |stdin, stdouts, wait|
41
is_running = true
42
stdin.close
43
while is_running
44
begin
45
stdouts.wait_readable
46
line = stdouts.readpartial(512)
47
cmd_out += line
48
$stdout.print line if verbose
49
rescue EOFError
50
is_running = false
51
end
52
end
53
cmd_exit_code = wait.value.exitstatus
54
end
55
end
56
57
raise "#{cmd.join(' ')} failed with exit code: #{cmd_exit_code}\nOutput: #{cmd_out}" if cmd_exit_code != 0
58
59
block&.call(cmd_out)
60
return unless cmd_out =~ %r{\s+(bazel-bin/\S+)}
61
62
out_artifact = Regexp.last_match(1)
63
puts "#{target} -> #{out_artifact}" if out_artifact
64
out_artifact
65
end
66
end
67
68