Module: Bake::Gem::Shell

Included in:
Helper
Defined in:
lib/bake/gem/shell.rb

Overview

Provides shell command execution methods with proper logging and error handling.

Instance Method Summary collapse

Instance Method Details

#execute(*arguments, **options) ⇒ Object

Execute a command and yield its output to a block.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bake/gem/shell.rb', line 61

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

#readlines(*arguments, **options) ⇒ Object

Execute a command and return its output as an array of lines.



85
86
87
88
89
# File 'lib/bake/gem/shell.rb', line 85

def readlines(*arguments, **options)
	execute(*arguments, **options) do |output|
		return output.readlines
	end
end

#system(*arguments, **options) ⇒ Object

Execute a system command with logging and error handling.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bake/gem/shell.rb', line 38

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