-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
executable.rb
247 lines (225 loc) · 6.88 KB
/
executable.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
module Pod
# Module which provides support for running executables.
#
# In a class it can be used as:
#
# extend Executable
# executable :git
#
# This will create two methods `git` and `git!` both accept a command but
# the later will raise on non successful executions. The methods return the
# output of the command.
#
module Executable
# Creates the methods for the executable with the given name.
#
# @param [Symbol] name
# the name of the executable.
#
# @return [void]
#
def executable(name)
define_method(name) do |*command|
Executable.execute_command(name, Array(command).flatten, false)
end
define_method(name.to_s + '!') do |*command|
Executable.execute_command(name, Array(command).flatten, true)
end
end
# Executes the given command displaying it if in verbose mode.
#
# @param [String] executable
# The binary to use.
#
# @param [Array<#to_s>] command
# The command to send to the binary.
#
# @param [Boolean] raise_on_failure
# Whether it should raise if the command fails.
#
# @raise If the executable could not be located.
#
# @raise If the command fails and the `raise_on_failure` is set to true.
#
# @return [String] the output of the command (STDOUT and STDERR).
#
def self.execute_command(executable, command, raise_on_failure = true)
bin = which!(executable)
command = command.map(&:to_s)
if File.basename(bin) == 'tar.exe'
# Tar on Windows needs --force-local
command.push('--force-local')
end
full_command = "#{bin} #{command.join(' ')}"
if Config.instance.verbose?
UI.message("$ #{full_command}")
stdout = Indenter.new(STDOUT)
stderr = Indenter.new(STDERR)
else
stdout = Indenter.new
stderr = Indenter.new
end
status = popen3(bin, command, stdout, stderr)
stdout = stdout.join
stderr = stderr.join
output = stdout + stderr
unless status.success?
if raise_on_failure
raise Informative, "#{full_command}\n\n#{output}"
else
UI.message("[!] Failed: #{full_command}".red)
end
end
output
end
# Returns the absolute path to the binary with the given name on the current
# `PATH`, or `nil` if none is found.
#
# @param [String] program
# The name of the program being searched for.
#
# @return [String,Nil] The absolute path to the given program, or `nil` if
# it wasn't found in the current `PATH`.
#
def self.which(program)
program = program.to_s
paths = ENV.fetch('PATH') { '' }.split(File::PATH_SEPARATOR)
paths.unshift('./')
paths.uniq!
paths.each do |path|
bin = File.expand_path(program, path)
if Gem.win_platform?
bin += '.exe'
end
if File.file?(bin) && File.executable?(bin)
return bin
end
end
nil
end
# Returns the absolute path to the binary with the given name on the current
# `PATH`, or raises if none is found.
#
# @param [String] program
# The name of the program being searched for.
#
# @return [String] The absolute path to the given program.
#
def self.which!(program)
which(program).tap do |bin|
raise Informative, "Unable to locate the executable `#{program}`" unless bin
end
end
# Runs the given command, capturing the desired output.
#
# @param [String] executable
# The binary to use.
#
# @param [Array<#to_s>] command
# The command to send to the binary.
#
# @param [Symbol] capture
# Whether it should raise if the command fails.
#
# @param [Hash] env
# Environment variables to be set for the command.
#
# @raise If the executable could not be located.
#
# @return [(String, Process::Status)]
# The desired captured output from the command, and the status from
# running the command.
#
def self.capture_command(executable, command, capture: :merge, env: {}, **kwargs)
bin = which!(executable)
require 'open3'
command = command.map(&:to_s)
case capture
when :merge then Open3.capture2e(env, [bin, bin], *command, **kwargs)
when :both then Open3.capture3(env, [bin, bin], *command, **kwargs)
when :out then Open3.capture3(env, [bin, bin], *command, **kwargs).values_at(0, -1)
when :err then Open3.capture3(env, [bin, bin], *command, **kwargs).drop(1)
when :none then Open3.capture3(env, [bin, bin], *command, **kwargs).last
end
end
# (see Executable.capture_command)
#
# @raise If running the command fails
#
def self.capture_command!(executable, command, **kwargs)
capture_command(executable, command, **kwargs).tap do |result|
result = Array(result)
status = result.last
unless status.success?
output = result[0..-2].join
raise Informative, "#{executable} #{command.join(' ')}\n\n#{output}".strip
end
end
end
private
def self.popen3(bin, command, stdout, stderr)
require 'open3'
Open3.popen3(bin, *command) do |i, o, e, t|
reader(o, stdout)
reader(e, stderr)
i.close
status = t.value
o.flush
e.flush
sleep(0.01)
status
end
end
def self.reader(input, output)
Thread.new do
buf = ''
begin
loop do
buf << input.readpartial(4096)
loop do
string, separator, buf = buf.partition(/[\r\n]/)
if separator.empty?
buf = string
break
end
output << (string << separator)
end
end
rescue EOFError, IOError
output << (buf << $/) unless buf.empty?
end
end
end
#-------------------------------------------------------------------------#
# Helper class that allows to write to an {IO} instance taking into account
# the UI indentation level.
#
class Indenter < ::Array
# @return [Fixnum] The indentation level of the UI.
#
attr_reader :indent
# @return [IO] the {IO} to which the output should be printed.
#
attr_reader :io
# Init a new Indenter
#
# @param [IO] io @see io
#
def initialize(io = nil)
@io = io
@indent = ' ' * UI.indentation_level
end
# Stores a portion of the output and prints it to the {IO} instance.
#
# @param [String] value
# the output to print.
#
# @return [void]
#
def <<(value)
super
io << "#{indent}#{value}" if io
end
end
end
end