Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (67 sloc) 2.08 KB
require './lib/debug.rb'
def inject_environment(process, environment_variables)
environment_variables.each do |key, value|
process.environment[key] = value
end
end
# just execute a shell command
def execute(command, verbose: false,
working_dir: __dir__,
environment_variables: {},
show_stdout: true)
print_debug_header("executing command '#{command.join(' ')}' in directory '#{working_dir}'", verbose)
process = ChildProcess.build(*command)
process.cwd = working_dir
inject_environment(process, environment_variables)
process.io.inherit! if show_stdout
process.start
process.wait
print_debug_footer verbose
process.exit_code
end
def create_process(command, working_dir: nil, environment: {})
process = ChildProcess.build(*command)
process.cwd = working_dir if working_dir
environment.each do |key, value|
process.environment[key] = value
end
process
end
def capture_output(process)
reader, w = IO.pipe
process.io.stdout = w
process.io.stderr = w
process.start
w.close
reader
end
def wait_for_process(process)
process.poll_for_exit(10)
rescue ChildProcess::TimeoutError
process.stop
end
def get_output(reader)
output = ''
begin
loop { output = output + reader.readpartial(8192) }
rescue EOFError
nil
end
output ? output.strip : output
end
# execute a shell command, capture and return all output (stderr & stdout)
def execute_and_capture(command, working_dir: __dir__, environment: {}, verbose: false)
print_debug_header("executing command '#{command.join(' ')}' in directory '#{working_dir}'", verbose)
process = create_process(command, working_dir: working_dir, environment: environment)
reader = capture_output(process)
output = get_output reader
print_debug_body(output, verbose)
wait_for_process(process)
print_debug_footer(verbose)
[process.exit_code, output]
end
# execute a command and fail directly if the command itself fails
def execute_or_fail(command, verbose)
exit_code, = execute(command, verbose: verbose)
raise "command '#{command.join(' ')}' failed" if exit_code > 0
end