exec_service 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +11 -0
- data/CHANGELOG.md +9 -0
- data/LICENSE.md +21 -0
- data/README.md +101 -7
- data/lib/exec_service/controller.rb +359 -0
- data/lib/exec_service/executor.rb +962 -0
- data/lib/exec_service/opts.rb +102 -0
- data/lib/exec_service/result.rb +191 -0
- data/lib/exec_service/version.rb +9 -0
- data/lib/exec_service.rb +511 -6
- metadata +40 -11
data/lib/exec_service.rb
CHANGED
|
@@ -1,8 +1,513 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "exec_service/controller"
|
|
4
|
+
require "exec_service/executor"
|
|
5
|
+
require "exec_service/opts"
|
|
6
|
+
require "exec_service/result"
|
|
7
|
+
|
|
8
|
+
##
|
|
9
|
+
# A service that executes subprocesses.
|
|
1
10
|
#
|
|
2
|
-
# This
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
# If this is a problem, or if the actual gem has not been
|
|
6
|
-
# released in a timely manner, you can contact the owner at
|
|
7
|
-
# dazuma@gmail.com
|
|
11
|
+
# This service provides a convenient interface for controlling spawned
|
|
12
|
+
# processes and their streams. It also provides shortcuts for common cases
|
|
13
|
+
# such as invoking Ruby in a subprocess or capturing output in a string.
|
|
8
14
|
#
|
|
15
|
+
# ### The exec service
|
|
16
|
+
#
|
|
17
|
+
# The main entrypoint class is this one, {ExecService}. It is a "service"
|
|
18
|
+
# object that provides functionality, primarily methods that spawn processes.
|
|
19
|
+
# Create it like any object:
|
|
20
|
+
#
|
|
21
|
+
# require "exec_service"
|
|
22
|
+
# exec_service = ExecService.new
|
|
23
|
+
#
|
|
24
|
+
# There are two "primitive" functions: {#exec} and {#exec_proc}. The {#exec}
|
|
25
|
+
# method spawns an operating system process specified by an executable and
|
|
26
|
+
# a set of arguments. The {#exec_proc} method takes a `Proc` and forks a
|
|
27
|
+
# Ruby process. Both of these can be heavily configured with stream
|
|
28
|
+
# handling, result handling, and numerous other options described below.
|
|
29
|
+
# The class also provides convenience methods for common cases such as
|
|
30
|
+
# spawning a Ruby process, spawning a shell script, or capturing output.
|
|
31
|
+
#
|
|
32
|
+
# The exec service class also stores default configuration that it applies
|
|
33
|
+
# to processes it spawns. You can set these defaults when constructing the
|
|
34
|
+
# service class, or at any time by calling {#configure_defaults}.
|
|
35
|
+
#
|
|
36
|
+
# ### Stream handling
|
|
37
|
+
#
|
|
38
|
+
# By default, subprocess streams are connected to the corresponding streams
|
|
39
|
+
# in the parent process. You can change this behavior, redirecting streams
|
|
40
|
+
# or providing ways to control them, using the `:in`, `:out`, and `:err`
|
|
41
|
+
# options.
|
|
42
|
+
#
|
|
43
|
+
# Three general strategies are available for custom stream handling. First,
|
|
44
|
+
# you can redirect to other streams such as files, IO objects, or Ruby
|
|
45
|
+
# strings. Some of these options map directly to options provided by the
|
|
46
|
+
# `Process#spawn` method. Second, you can use a controller to manipulate
|
|
47
|
+
# the streams programmatically. Third, you can capture output stream data
|
|
48
|
+
# and make it available in the result.
|
|
49
|
+
#
|
|
50
|
+
# Following is a full list of the stream handling options, along with how
|
|
51
|
+
# to specify them using the `:in`, `:out`, and `:err` options.
|
|
52
|
+
#
|
|
53
|
+
# * **Inherit parent stream:** You can inherit the corresponding stream
|
|
54
|
+
# in the parent process by passing `:inherit` as the option value. This
|
|
55
|
+
# is the default if the subprocess is run in the foreground.
|
|
56
|
+
#
|
|
57
|
+
# * **Redirect to null:** You can redirect to a null stream by passing
|
|
58
|
+
# `:null` as the option value. This connects to a stream that is not
|
|
59
|
+
# closed but contains no data, i.e. `/dev/null` on unix systems. This
|
|
60
|
+
# is the default if the subprocess is run in the background.
|
|
61
|
+
#
|
|
62
|
+
# * **Close the stream:** You can close the stream by passing `:close` as
|
|
63
|
+
# the option value. This is the same as passing `:close` to
|
|
64
|
+
# `Process#spawn`.
|
|
65
|
+
#
|
|
66
|
+
# * **Redirect to a file:** You can redirect to a file. This reads from
|
|
67
|
+
# an existing file when connected to `:in`, and creates or appends to a
|
|
68
|
+
# file when connected to `:out` or `:err`. To specify a file, use the
|
|
69
|
+
# setting `[:file, "/path/to/file"]`. You can also, when writing a
|
|
70
|
+
# file, append an optional mode and permission code to the array. For
|
|
71
|
+
# example, `[:file, "/path/to/file", "a", 0644]`.
|
|
72
|
+
#
|
|
73
|
+
# * **Redirect to an IO object:** You can redirect to an IO object in the
|
|
74
|
+
# parent process, by passing the IO object as the option value. You can
|
|
75
|
+
# use any IO object. For example, you could connect the child's output
|
|
76
|
+
# to the parent's error using `out: $stderr`, or you could connect to
|
|
77
|
+
# an existing File stream. Unlike `Process#spawn`, this works for IO
|
|
78
|
+
# objects that do not have a corresponding file descriptor (such as
|
|
79
|
+
# StringIO objects). In such a case, a thread will be spawned to pipe
|
|
80
|
+
# the IO data through to the child process. Note that the IO object
|
|
81
|
+
# will _not_ be closed on completion.
|
|
82
|
+
#
|
|
83
|
+
# * **Redirect to a pipe:** You can redirect to a pipe created using
|
|
84
|
+
# `IO.pipe` (i.e. a two-element array of read and write IO objects) by
|
|
85
|
+
# passing the array as the option value. This will connect the
|
|
86
|
+
# appropriate IO (either read or write), and close it in the parent.
|
|
87
|
+
# Thus, you can connect only one process to each end. If you want more
|
|
88
|
+
# direct control over IO closing behavior, pass the IO object (i.e. the
|
|
89
|
+
# element of the pipe array) directly.
|
|
90
|
+
#
|
|
91
|
+
# * **Combine with another child stream:** You can redirect one child
|
|
92
|
+
# output stream to another, to combine them. To merge the child's error
|
|
93
|
+
# stream into its output stream, use `err: [:child, :out]`.
|
|
94
|
+
#
|
|
95
|
+
# * **Read from a string:** You can pass a string to the input stream by
|
|
96
|
+
# setting `[:string, "the string"]`. This works only for `:in`.
|
|
97
|
+
#
|
|
98
|
+
# * **Capture output stream:** You can capture a stream and make it
|
|
99
|
+
# available on the {ExecService::Result} object, using the
|
|
100
|
+
# setting `:capture`. This works only for the `:out` and `:err`
|
|
101
|
+
# streams.
|
|
102
|
+
#
|
|
103
|
+
# * **Use the controller:** You can hook a stream to the controller using
|
|
104
|
+
# the setting `:controller`. You can then manipulate the stream via the
|
|
105
|
+
# controller. If you pass a block to {ExecService#exec}, it
|
|
106
|
+
# yields the {ExecService::Controller}, giving you access to
|
|
107
|
+
# streams. See the section below on controlling processes.
|
|
108
|
+
#
|
|
109
|
+
# * **Make copies of an output stream:** You can "tee," or duplicate the
|
|
110
|
+
# `:out` or `:err` stream and redirect those copies to various
|
|
111
|
+
# destinations. To specify a tee, use the setting `[:tee, ...]` where
|
|
112
|
+
# the additional array elements include two or more of the following.
|
|
113
|
+
# See the corresponding documentation above for more detail.
|
|
114
|
+
# * `:inherit` to direct to the parent process's stream.
|
|
115
|
+
# * `:capture` to capture the stream and store it in the result.
|
|
116
|
+
# * `:controller` to direct the stream to the controller.
|
|
117
|
+
# * `[:file, "/path/to/file"]` to write to a file.
|
|
118
|
+
# * An `IO` or `StringIO` object.
|
|
119
|
+
# * An array of two `IO` objects representing a pipe
|
|
120
|
+
#
|
|
121
|
+
# Additionally, the last element of the array can be a hash of options.
|
|
122
|
+
# Supported options include:
|
|
123
|
+
# * `:buffer_size` The size of the memory buffer for each element of
|
|
124
|
+
# the tee. Larger buffers may allow higher throughput. The default
|
|
125
|
+
# is 65536.
|
|
126
|
+
#
|
|
127
|
+
# ### Controlling processes
|
|
128
|
+
#
|
|
129
|
+
# A process can be started in the *foreground* or the *background*. If you
|
|
130
|
+
# start a foreground process, it will inherit your standard input and
|
|
131
|
+
# output streams by default, and it will keep control until it completes.
|
|
132
|
+
# If you start a background process, its streams will be redirected to null
|
|
133
|
+
# by default, and control will be returned to you immediately.
|
|
134
|
+
#
|
|
135
|
+
# While a process is running, you can control it using a
|
|
136
|
+
# {ExecService::Controller} object. Use a controller to interact with
|
|
137
|
+
# the process's input and output streams, send it signals, or wait for it
|
|
138
|
+
# to complete.
|
|
139
|
+
#
|
|
140
|
+
# When running a process in the foreground, the controller will be yielded
|
|
141
|
+
# to an optional block. For example, the following code starts a process in
|
|
142
|
+
# the foreground and passes its output stream to a controller.
|
|
143
|
+
#
|
|
144
|
+
# exec_service.exec(["git", "init"], out: :controller) do |controller|
|
|
145
|
+
# loop do
|
|
146
|
+
# line = controller.out.gets
|
|
147
|
+
# break if line.nil?
|
|
148
|
+
# puts "Got line: #{line}"
|
|
149
|
+
# end
|
|
150
|
+
# end
|
|
151
|
+
#
|
|
152
|
+
# At the end of the block, if the controller is handling the process's
|
|
153
|
+
# input stream, that stream will automatically be closed. The following
|
|
154
|
+
# example programmatically sends data to the `wc` unix program, and
|
|
155
|
+
# captures its output. Because the controller is handling the input stream,
|
|
156
|
+
# it automatically closes the stream at the end of the block, which causes
|
|
157
|
+
# `wc` to end.
|
|
158
|
+
#
|
|
159
|
+
# result = exec_service.exec(["wc"],
|
|
160
|
+
# in: :controller,
|
|
161
|
+
# out: :capture) do |controller|
|
|
162
|
+
# controller.in.puts "Hello, world!"
|
|
163
|
+
# end
|
|
164
|
+
# puts "Results: #{result.captured_out}"
|
|
165
|
+
#
|
|
166
|
+
# Otherwise, depending on the process's behavior, it may continue to run
|
|
167
|
+
# after the end of the block. Control will not be returned to the caller
|
|
168
|
+
# until the process actually terminates. Conversely, it is also possible
|
|
169
|
+
# the process could terminate by itself while the block is still executing.
|
|
170
|
+
# You can call controller methods to obtain the process's actual current
|
|
171
|
+
# state.
|
|
172
|
+
#
|
|
173
|
+
# When running a process in the background, the controller is returned
|
|
174
|
+
# immediately from the method that starts the process. In the following
|
|
175
|
+
# example, git init is kicked off in the background and the output is
|
|
176
|
+
# thrown away to /dev/null.
|
|
177
|
+
#
|
|
178
|
+
# controller = exec_service.exec(["git", "init"], background: true)
|
|
179
|
+
#
|
|
180
|
+
# In this mode, use the returned controller to query the process's state
|
|
181
|
+
# and interact with it. Streams directed to the controller are not
|
|
182
|
+
# automatically closed, so you will need to do so yourself. Following is an
|
|
183
|
+
# example of running `wc` in the background:
|
|
184
|
+
#
|
|
185
|
+
# controller = exec_service.exec(["wc"], background: true,
|
|
186
|
+
# in: :controller, out: :controller)
|
|
187
|
+
# controller.in.puts "Hello, world!"
|
|
188
|
+
# controller.in.close # Do this explicitly to cause wc to finish
|
|
189
|
+
# puts "Results: #{controller.out.read}" # Read the entire stream
|
|
190
|
+
#
|
|
191
|
+
# ### Result handling
|
|
192
|
+
#
|
|
193
|
+
# A subprocess result is represented by a {ExecService::Result}
|
|
194
|
+
# object, which includes the exit code, the content of any captured output
|
|
195
|
+
# streams, and any exception raised when attempting to run the process.
|
|
196
|
+
# When you run a process in the foreground, the method will return a result
|
|
197
|
+
# object. When you run a process in the background, you can obtain the
|
|
198
|
+
# result from the controller once the process completes.
|
|
199
|
+
#
|
|
200
|
+
# The following example demonstrates running a process in the foreground
|
|
201
|
+
# and getting the exit code:
|
|
202
|
+
#
|
|
203
|
+
# result = exec_service.exec(["git", "init"])
|
|
204
|
+
# puts "exit code: #{result.exit_code}"
|
|
205
|
+
#
|
|
206
|
+
# The following example demonstrates starting a process in the background,
|
|
207
|
+
# waiting for it to complete, and getting its exit code:
|
|
208
|
+
#
|
|
209
|
+
# controller = exec_service.exec(["git", "init"], background: true)
|
|
210
|
+
# result = controller.result(timeout: 1.0)
|
|
211
|
+
# if result
|
|
212
|
+
# puts "exit code: #{result.exit_code}"
|
|
213
|
+
# else
|
|
214
|
+
# puts "timed out"
|
|
215
|
+
# end
|
|
216
|
+
#
|
|
217
|
+
# You can also provide a callback that is executed once a process
|
|
218
|
+
# completes. For example:
|
|
219
|
+
#
|
|
220
|
+
# my_callback = proc do |result|
|
|
221
|
+
# puts "exit code: #{result.exit_code}"
|
|
222
|
+
# end
|
|
223
|
+
# exec_service.exec(["git", "init"], result_callback: my_callback)
|
|
224
|
+
#
|
|
225
|
+
# In foreground mode, the callback is executed in the calling thread, after
|
|
226
|
+
# the process terminates (and after any controller block has completed) but
|
|
227
|
+
# before control is returned to the caller. In background mode, the
|
|
228
|
+
# callback is executed asynchronously in a separate thread after the
|
|
229
|
+
# process terminates.
|
|
230
|
+
#
|
|
231
|
+
# ### Configuration options
|
|
232
|
+
#
|
|
233
|
+
# A variety of options can be used to control subprocesses. These can be
|
|
234
|
+
# provided to any method that starts a subprocess. You can also set
|
|
235
|
+
# defaults by calling {ExecService#configure_defaults}.
|
|
236
|
+
#
|
|
237
|
+
# Options that affect the behavior of subprocesses:
|
|
238
|
+
#
|
|
239
|
+
# * `:env` (Hash) Environment variables to pass to the subprocess.
|
|
240
|
+
# Keys represent variable names and should be strings. Values should be
|
|
241
|
+
# either strings or `nil`, which unsets the variable.
|
|
242
|
+
#
|
|
243
|
+
# * `:background` (boolean) Runs the process in the background if `true`.
|
|
244
|
+
#
|
|
245
|
+
# * `:result_callback` (Proc) Called and passed the result object when
|
|
246
|
+
# the subprocess exits. If the process was run in the background, this
|
|
247
|
+
# callback is executed in a separate thread. If the process was run in
|
|
248
|
+
# the foreground, this callback is executed in the calling thread.
|
|
249
|
+
#
|
|
250
|
+
# * `:unbundle` (boolean) Disables any existing bundle when running the
|
|
251
|
+
# subprocess. Has no effect if Bundler isn't active at the call point.
|
|
252
|
+
# Cannot be used when executing in a fork, e.g. via {#exec_proc}.
|
|
253
|
+
#
|
|
254
|
+
# Options for connecting input and output streams. See the section above on
|
|
255
|
+
# stream handling for info on the values that can be passed.
|
|
256
|
+
#
|
|
257
|
+
# * `:in` Connects the input stream of the subprocess. See the section on
|
|
258
|
+
# stream handling.
|
|
259
|
+
#
|
|
260
|
+
# * `:out` Connects the standard output stream of the subprocess. See the
|
|
261
|
+
# section on stream handling.
|
|
262
|
+
#
|
|
263
|
+
# * `:err` Connects the standard error stream of the subprocess. See the
|
|
264
|
+
# section on stream handling.
|
|
265
|
+
#
|
|
266
|
+
# Options related to logging and reporting:
|
|
267
|
+
#
|
|
268
|
+
# * `:logger` (Logger) Logger to use for logging the actual command. If
|
|
269
|
+
# not present, the command is not logged.
|
|
270
|
+
#
|
|
271
|
+
# * `:log_level` (Integer,false) Level for logging the actual command.
|
|
272
|
+
# Defaults to Logger::INFO if not present. You can also pass `false` to
|
|
273
|
+
# disable logging of the command.
|
|
274
|
+
#
|
|
275
|
+
# * `:log_cmd` (String) The string logged for the actual command.
|
|
276
|
+
# Defaults to the `inspect` representation of the command.
|
|
277
|
+
#
|
|
278
|
+
# * `:name` (Object) An optional object that can be used to identify this
|
|
279
|
+
# subprocess. It is available in the controller and result objects.
|
|
280
|
+
#
|
|
281
|
+
# In addition, the following options recognized by
|
|
282
|
+
# [`Process#spawn`](https://ruby-doc.org/core/Process.html#method-c-spawn)
|
|
283
|
+
# are supported.
|
|
284
|
+
#
|
|
285
|
+
# * `:chdir` (String) Set the working directory for the command.
|
|
286
|
+
#
|
|
287
|
+
# * `:close_others` (boolean) Whether to close non-redirected
|
|
288
|
+
# non-standard file descriptors.
|
|
289
|
+
#
|
|
290
|
+
# * `:new_pgroup` (boolean) Create new process group (Windows only).
|
|
291
|
+
#
|
|
292
|
+
# * `:pgroup` (Integer,true,nil) The process group setting.
|
|
293
|
+
#
|
|
294
|
+
# * `:umask` (Integer) Umask setting for the new process.
|
|
295
|
+
#
|
|
296
|
+
# * `:unsetenv_others` (boolean) Clear environment variables except those
|
|
297
|
+
# explicitly set.
|
|
298
|
+
#
|
|
299
|
+
# Any other option key will result in an `ArgumentError`.
|
|
300
|
+
#
|
|
301
|
+
class ExecService
|
|
302
|
+
##
|
|
303
|
+
# Create an exec service.
|
|
304
|
+
#
|
|
305
|
+
# @param block [Proc] A block that is called if a key is not found. It is
|
|
306
|
+
# passed the unknown key, and expected to return a default value
|
|
307
|
+
# (which can be nil).
|
|
308
|
+
# @param opts [keywords] Initial default options. See {ExecService}
|
|
309
|
+
# for a description of the options.
|
|
310
|
+
#
|
|
311
|
+
def initialize(**opts, &block)
|
|
312
|
+
require "logger"
|
|
313
|
+
require "rbconfig"
|
|
314
|
+
require "stringio"
|
|
315
|
+
@default_opts = Opts.new(&block).add(opts)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
##
|
|
319
|
+
# Set default options. See {ExecService} for a description of the
|
|
320
|
+
# options.
|
|
321
|
+
#
|
|
322
|
+
# @param opts [keywords] New default options to set
|
|
323
|
+
# @return [self]
|
|
324
|
+
#
|
|
325
|
+
def configure_defaults(**opts)
|
|
326
|
+
@default_opts.add(opts)
|
|
327
|
+
self
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
##
|
|
331
|
+
# Execute a command. The command can be given as a single string to pass
|
|
332
|
+
# to a shell, or an array of strings indicating a posix command.
|
|
333
|
+
#
|
|
334
|
+
# If the process is not set to run in the background, and a block is
|
|
335
|
+
# provided, a {ExecService::Controller} will be yielded to it.
|
|
336
|
+
#
|
|
337
|
+
# @param cmd [String,Array<String>] The command to execute.
|
|
338
|
+
# @param opts [keywords] The command options. See the section on
|
|
339
|
+
# configuration options in the {ExecService} class docs.
|
|
340
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
341
|
+
# for the subprocess streams.
|
|
342
|
+
#
|
|
343
|
+
# @return [ExecService::Controller] The subprocess controller, if
|
|
344
|
+
# the process is running in the background.
|
|
345
|
+
# @return [ExecService::Result] The result, if the process ran in
|
|
346
|
+
# the foreground.
|
|
347
|
+
#
|
|
348
|
+
def exec(cmd, **opts, &block)
|
|
349
|
+
exec_opts = Opts.new(@default_opts).add(opts)
|
|
350
|
+
spawn_cmd =
|
|
351
|
+
if cmd.is_a?(::Array)
|
|
352
|
+
if cmd.size > 1
|
|
353
|
+
binary = canonical_binary_spec(cmd.first, exec_opts)
|
|
354
|
+
[binary] + cmd[1..].map(&:to_s)
|
|
355
|
+
else
|
|
356
|
+
[canonical_binary_spec(Array(cmd.first), exec_opts)]
|
|
357
|
+
end
|
|
358
|
+
else
|
|
359
|
+
[cmd.to_s]
|
|
360
|
+
end
|
|
361
|
+
executor = Executor.new(exec_opts, spawn_cmd, block)
|
|
362
|
+
executor.execute
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
##
|
|
366
|
+
# Spawn a ruby process and pass the given arguments to it.
|
|
367
|
+
#
|
|
368
|
+
# If the process is not set to run in the background, and a block is
|
|
369
|
+
# provided, a {ExecService::Controller} will be yielded to it.
|
|
370
|
+
#
|
|
371
|
+
# @param args [String,Array<String>] The arguments to ruby.
|
|
372
|
+
# @param opts [keywords] The command options. See the section on
|
|
373
|
+
# configuration options in the {ExecService} class docs.
|
|
374
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
375
|
+
# for the subprocess streams.
|
|
376
|
+
#
|
|
377
|
+
# @return [ExecService::Controller] The subprocess controller, if
|
|
378
|
+
# the process is running in the background.
|
|
379
|
+
# @return [ExecService::Result] The result, if the process ran in
|
|
380
|
+
# the foreground.
|
|
381
|
+
#
|
|
382
|
+
def exec_ruby(args, **opts, &block)
|
|
383
|
+
cmd = args.is_a?(::Array) ? [::RbConfig.ruby] + args : "#{::RbConfig.ruby} #{args}"
|
|
384
|
+
log_cmd = "exec ruby: #{args.inspect}"
|
|
385
|
+
opts = {argv0: "ruby", log_cmd: log_cmd}.merge(opts)
|
|
386
|
+
exec(cmd, **opts, &block)
|
|
387
|
+
end
|
|
388
|
+
alias ruby exec_ruby
|
|
389
|
+
|
|
390
|
+
##
|
|
391
|
+
# Execute a proc in a fork.
|
|
392
|
+
#
|
|
393
|
+
# If the process is not set to run in the background, and a block is
|
|
394
|
+
# provided, a {ExecService::Controller} will be yielded to it.
|
|
395
|
+
#
|
|
396
|
+
# @param func [Proc] The proc to call.
|
|
397
|
+
# @param opts [keywords] The command options. See the section on
|
|
398
|
+
# configuration options in the {ExecService} class docs.
|
|
399
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
400
|
+
# for the subprocess streams.
|
|
401
|
+
#
|
|
402
|
+
# @return [ExecService::Controller] The subprocess controller, if
|
|
403
|
+
# the process is running in the background.
|
|
404
|
+
# @return [ExecService::Result] The result, if the process ran in
|
|
405
|
+
# the foreground.
|
|
406
|
+
#
|
|
407
|
+
def exec_proc(func, **opts, &block)
|
|
408
|
+
raise ::ArgumentError, "Given proc is not callable" unless func.respond_to?(:call)
|
|
409
|
+
exec_opts = Opts.new(@default_opts).add(opts)
|
|
410
|
+
raise ::ArgumentError, "Cannot use :unbundle option with exec_proc" if exec_opts.config_opts[:unbundle]
|
|
411
|
+
executor = Executor.new(exec_opts, func, block)
|
|
412
|
+
executor.execute
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
##
|
|
416
|
+
# Execute a command. The command can be given as a single string to pass
|
|
417
|
+
# to a shell, or an array of strings indicating a posix command.
|
|
418
|
+
#
|
|
419
|
+
# Captures standard out and returns it as a string.
|
|
420
|
+
# Cannot be run in the background.
|
|
421
|
+
#
|
|
422
|
+
# If a block is provided, a {ExecService::Controller} will be
|
|
423
|
+
# yielded to it.
|
|
424
|
+
#
|
|
425
|
+
# @param cmd [String,Array<String>] The command to execute.
|
|
426
|
+
# @param opts [keywords] The command options. See the section on
|
|
427
|
+
# configuration options in the {ExecService} class docs.
|
|
428
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
429
|
+
# for the subprocess streams.
|
|
430
|
+
#
|
|
431
|
+
# @return [String] What was written to standard out.
|
|
432
|
+
#
|
|
433
|
+
def capture(cmd, **opts, &block)
|
|
434
|
+
opts = opts.merge(out: :capture, background: false)
|
|
435
|
+
exec(cmd, **opts, &block).captured_out
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
##
|
|
439
|
+
# Spawn a ruby process and pass the given arguments to it.
|
|
440
|
+
#
|
|
441
|
+
# Captures standard out and returns it as a string.
|
|
442
|
+
# Cannot be run in the background.
|
|
443
|
+
#
|
|
444
|
+
# If a block is provided, a {ExecService::Controller} will be
|
|
445
|
+
# yielded to it.
|
|
446
|
+
#
|
|
447
|
+
# @param args [String,Array<String>] The arguments to ruby.
|
|
448
|
+
# @param opts [keywords] The command options. See the section on
|
|
449
|
+
# configuration options in the {ExecService} class docs.
|
|
450
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
451
|
+
# for the subprocess streams.
|
|
452
|
+
#
|
|
453
|
+
# @return [String] What was written to standard out.
|
|
454
|
+
#
|
|
455
|
+
def capture_ruby(args, **opts, &block)
|
|
456
|
+
opts = opts.merge(out: :capture, background: false)
|
|
457
|
+
ruby(args, **opts, &block).captured_out
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
##
|
|
461
|
+
# Execute a proc in a fork.
|
|
462
|
+
#
|
|
463
|
+
# Captures standard out and returns it as a string.
|
|
464
|
+
# Cannot be run in the background.
|
|
465
|
+
#
|
|
466
|
+
# If a block is provided, a {ExecService::Controller} will be
|
|
467
|
+
# yielded to it.
|
|
468
|
+
#
|
|
469
|
+
# @param func [Proc] The proc to call.
|
|
470
|
+
# @param opts [keywords] The command options. See the section on
|
|
471
|
+
# configuration options in the {ExecService} class docs.
|
|
472
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
473
|
+
# for the subprocess streams.
|
|
474
|
+
#
|
|
475
|
+
# @return [String] What was written to standard out.
|
|
476
|
+
#
|
|
477
|
+
def capture_proc(func, **opts, &block)
|
|
478
|
+
opts = opts.merge(out: :capture, background: false)
|
|
479
|
+
exec_proc(func, **opts, &block).captured_out
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
##
|
|
483
|
+
# Execute the given string in a shell. Returns an effective exit code
|
|
484
|
+
# that is always an integer. Cannot be run in the background.
|
|
485
|
+
#
|
|
486
|
+
# If a block is provided, a {ExecService::Controller} will be
|
|
487
|
+
# yielded to it.
|
|
488
|
+
#
|
|
489
|
+
# @param cmd [String] The shell command to execute.
|
|
490
|
+
# @param opts [keywords] The command options. See the section on
|
|
491
|
+
# configuration options in the {ExecService} class docs.
|
|
492
|
+
# @yieldparam controller [ExecService::Controller] A controller
|
|
493
|
+
# for the subprocess streams.
|
|
494
|
+
#
|
|
495
|
+
# @return [Integer] An effective exit code. See
|
|
496
|
+
# {ExecService::Result#effective_code}.
|
|
497
|
+
#
|
|
498
|
+
def sh(cmd, **opts, &block)
|
|
499
|
+
opts = opts.merge(background: false)
|
|
500
|
+
exec(cmd, **opts, &block).effective_code
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
private
|
|
504
|
+
|
|
505
|
+
def canonical_binary_spec(cmd, exec_opts)
|
|
506
|
+
config_argv0 = exec_opts.config_opts[:argv0]
|
|
507
|
+
return cmd.to_s if !config_argv0 && !cmd.is_a?(::Array)
|
|
508
|
+
cmd = Array(cmd)
|
|
509
|
+
actual_cmd = cmd.first
|
|
510
|
+
argv0 = cmd[1] || config_argv0 || actual_cmd
|
|
511
|
+
[actual_cmd.to_s, argv0.to_s]
|
|
512
|
+
end
|
|
513
|
+
end
|
metadata
CHANGED
|
@@ -1,26 +1,55 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: exec_service
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Daniel Azuma
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: logger
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: This service provides a convenient interface for controlling spawned
|
|
27
|
+
processes and their streams. It also provides shortcuts for common cases such as
|
|
28
|
+
invoking Ruby in a subprocess or capturing output in a string.
|
|
29
|
+
email:
|
|
30
|
+
- dazuma@gmail.com
|
|
16
31
|
executables: []
|
|
17
32
|
extensions: []
|
|
18
33
|
extra_rdoc_files: []
|
|
19
34
|
files:
|
|
35
|
+
- ".yardopts"
|
|
36
|
+
- CHANGELOG.md
|
|
37
|
+
- LICENSE.md
|
|
20
38
|
- README.md
|
|
21
39
|
- lib/exec_service.rb
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
- lib/exec_service/controller.rb
|
|
41
|
+
- lib/exec_service/executor.rb
|
|
42
|
+
- lib/exec_service/opts.rb
|
|
43
|
+
- lib/exec_service/result.rb
|
|
44
|
+
- lib/exec_service/version.rb
|
|
45
|
+
homepage: https://github.com/dazuma/exec_service
|
|
46
|
+
licenses:
|
|
47
|
+
- MIT
|
|
48
|
+
metadata:
|
|
49
|
+
bug_tracker_uri: https://github.com/dazuma/exec_service/issues
|
|
50
|
+
changelog_uri: https://rubydoc.info/gems/exec_service/0.1.1/file/CHANGELOG.md
|
|
51
|
+
documentation_uri: https://rubydoc.info/gems/exec_service/0.1.1
|
|
52
|
+
homepage_uri: https://github.com/dazuma/exec_service
|
|
24
53
|
rdoc_options: []
|
|
25
54
|
require_paths:
|
|
26
55
|
- lib
|
|
@@ -28,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
28
57
|
requirements:
|
|
29
58
|
- - ">="
|
|
30
59
|
- !ruby/object:Gem::Version
|
|
31
|
-
version: '
|
|
60
|
+
version: '2.7'
|
|
32
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
62
|
requirements:
|
|
34
63
|
- - ">="
|
|
@@ -37,5 +66,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
37
66
|
requirements: []
|
|
38
67
|
rubygems_version: 4.0.6
|
|
39
68
|
specification_version: 4
|
|
40
|
-
summary:
|
|
69
|
+
summary: A service that executes subprocesses.
|
|
41
70
|
test_files: []
|