Class: RubyWasm::BuildExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_wasm/build.rb

Overview

Build executor to run the actual build commands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false, process_count: nil) ⇒ BuildExecutor

Returns a new instance of BuildExecutor.



10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_wasm/build.rb', line 10

def initialize(verbose: false, process_count: nil)
  @verbose = verbose
  @github_actions_markup = ENV["ENABLE_GITHUB_ACTIONS_MARKUP"] != nil
  __skip__ = begin
    require "etc"
    @process_count = process_count || Etc.nprocessors
  rescue LoadError
    @process_count = process_count || 1
  end
end

Instance Attribute Details

#process_countObject (readonly)

Returns the value of attribute process_count.



8
9
10
# File 'lib/ruby_wasm/build.rb', line 8

def process_count
  @process_count
end

Instance Method Details

#begin_section(klass, name, note) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_wasm/build.rb', line 92

def begin_section(klass, name, note)
  message = "\e[1;36m==>\e[0m \e[1m#{klass}(#{name}) -- #{note}\e[0m"
  if @github_actions_markup
    puts "::group::#{message}"
  else
    puts message
  end

  # Record the start time
  @start_times ||= Hash.new
  @start_times[[klass, name]] = Time.now

  $stdout.flush
end

#cp_r(src, dest) ⇒ Object



121
122
123
# File 'lib/ruby_wasm/build.rb', line 121

def cp_r(src, dest)
  FileUtils.cp_r(src, dest)
end

#end_section(klass, name) ⇒ Object



107
108
109
110
111
# File 'lib/ruby_wasm/build.rb', line 107

def end_section(klass, name)
  took = Time.now - @start_times[[klass, name]]
  puts "::endgroup::" if @github_actions_markup
  puts "\e[1;36m==>\e[0m \e[1m#{klass}(#{name}) -- done in #{took.round(2)}s\e[0m"
end

#mkdir_p(list) ⇒ Object



129
130
131
# File 'lib/ruby_wasm/build.rb', line 129

def mkdir_p(list)
  FileUtils.mkdir_p(list)
end

#mv(src, dest) ⇒ Object



125
126
127
# File 'lib/ruby_wasm/build.rb', line 125

def mv(src, dest)
  FileUtils.mv(src, dest)
end

#rm_f(list) ⇒ Object



117
118
119
# File 'lib/ruby_wasm/build.rb', line 117

def rm_f(list)
  FileUtils.rm_f(list)
end

#rm_rf(list) ⇒ Object



113
114
115
# File 'lib/ruby_wasm/build.rb', line 113

def rm_rf(list)
  FileUtils.rm_rf(list)
end

#system(*args, chdir: nil, env: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby_wasm/build.rb', line 21

def system(*args, chdir: nil, env: nil)
  require "open3"

  _print_command(args, env)

  # @type var kwargs: Hash[Symbol, untyped]
  kwargs = {}
  kwargs[:chdir] = chdir if chdir

  args = args.to_a.map(&:to_s)
  # TODO: Remove __skip__ once we have open3 RBS definitions.
  __skip__ =
    if @verbose || !$stdout.tty?
      kwargs[:exception] = true
      if env
        Kernel.system(env, *args, **kwargs)
      else
        Kernel.system(*args, **kwargs)
      end
    else
      printer = StatusPrinter.new
      block =
        proc do |stdin, stdout, stderr, wait_thr|
          mux = Mutex.new
          out = String.new
          err = String.new
          readers =
            [
              [stdout, :stdout, out],
              [stderr, :stderr, err]
            ].map do |io, name, str|
              reader =
                Thread.new do
                  while (line = io.gets)
                    mux.synchronize do
                      printer.send(name, line)
                      str << line
                    end
                  end
                end
              reader.report_on_exception = false
              reader
            end

          readers.each(&:join)

          [out, err, wait_thr.value]
        end
      begin
        stdout, stderr, status =
          if env
            Open3.popen3(env, *args, **kwargs, &block)
          else
            Open3.popen3(*args, **kwargs, &block)
          end
        unless status.success?
          $stderr.puts stdout
          $stderr.puts stderr
          raise "Command failed with status (#{status.exitstatus}): #{args.join(" ")}"
        end
      ensure
        printer.done
      end
    end
  return
rescue => e
  $stdout.flush
  $stderr.puts "Try running with `rake --verbose` for more complete output."
  raise e
end

#write(path, data) ⇒ Object



133
134
135
# File 'lib/ruby_wasm/build.rb', line 133

def write(path, data)
  File.write(path, data)
end