Module: Tebako::BuildHelpers

Defined in:
lib/tebako/build_helpers.rb

Overview

Ruby build helpers

Class Method Summary collapse

Class Method Details

.run_with_capture(args) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tebako/build_helpers.rb', line 35

def run_with_capture(args)
  args = args.compact
  puts "   ... @ #{args.join(" ")}"
  # Read merged stdout+stderr in the calling thread via IO.popen rather than
  # Open3.capture2e: capture2e spawns a background reader thread that races and
  # raises "stream closed in another thread (IOError)" on large-output builds
  # (e.g. `make ruby`) under Ruby >= 4.0. Reading inline avoids that race.
  out = +""
  IO.popen(args, err: %i[child out]) { |io| io.each_line { |line| out << line } }
  st = $? # rubocop:disable Style/SpecialGlobalVars
  raise Tebako::Error, "Failed to run #{args.join(" ")} (#{st}):\n #{out}" if st.signaled? || !st.exitstatus.zero?

  out
end

.run_with_capture_v(args) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/tebako/build_helpers.rb', line 50

def run_with_capture_v(args)
  if @verbose
    args_v = args.dup
    args_v.push("--verbose")
    puts run_with_capture(args_v)
  else
    run_with_capture(args)
  end
end

.with_env(hash) ⇒ Object

Sets up temporary environment variables and yields to the block. When the block exits, the environment variables are set back to their original values.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tebako/build_helpers.rb', line 63

def with_env(hash)
  old = {}
  hash.each do |k, v|
    old[k] = ENV.fetch(k, nil)
    ENV[k] = v
  end
  begin
    yield
  ensure
    hash.each_key { |k| ENV[k] = old[k] }
  end
end