module Shell
Provides shell command execution methods with proper logging and error handling.
Definitions
def system(*arguments, **options)
Execute a system command with logging and error handling.
Signature
-
parameter
arguments
Array
The command and its arguments to execute.
-
parameter
options
Hash
Additional options to pass to Process.spawn.
-
returns
Boolean
True if the command executed successfully.
-
raises
CommandExecutionError
If the command fails.
Implementation
def system(*arguments, **options)
Console::Event::Spawn.for(*arguments, **options).emit(self)
begin
pid = Process.spawn(*arguments, **options)
return yield if block_given?
ensure
pid, status = Process.wait2(pid) if pid
unless status.success?
raise Bake::Gem::CommandExecutionError.new("Failed to execute #{arguments}: #{status}!", status)
end
return true
end
end
def execute(*arguments, **options)
Execute a command and yield its output to a block.
Signature
-
parameter
arguments
Array
The command and its arguments to execute.
-
parameter
options
Hash
Additional options to pass to Process.spawn.
-
yields
{|input| ...}
The input stream from the executed command.
-
returns
Object
The return value of the block.
-
raises
CommandExecutionError
If the command fails.
Implementation
def execute(*arguments, **options)
Console::Event::Spawn.for(*arguments, **options).emit(self)
IO.pipe do |input, output|
pid = Process.spawn(*arguments, out: output, **options)
output.close
begin
return yield(input)
ensure
pid, status = Process.wait2(pid)
unless status.success?
raise Bake::Gem::CommandExecutionError.new("Failed to execute #{arguments}: #{status}!", status)
end
end
end
end
def readlines(*arguments, **options)
Execute a command and return its output as an array of lines.
Signature
-
parameter
arguments
Array
The command and its arguments to execute.
-
parameter
options
Hash
Additional options to pass to Process.spawn.
-
returns
Array(String)
The output lines from the executed command.
-
raises
CommandExecutionError
If the command fails.
Implementation
def readlines(*arguments, **options)
execute(*arguments, **options) do |output|
return output.readlines
end
end