toys 0.15.6 → 0.16.0

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -1
  3. data/README.md +2 -2
  4. data/builtins/system/tools.rb +1 -1
  5. data/lib/toys/templates/clean.rb +1 -1
  6. data/lib/toys/templates/gem_build.rb +1 -1
  7. data/lib/toys/templates/rubocop.rb +4 -1
  8. data/lib/toys/version.rb +1 -1
  9. metadata +8 -63
  10. data/core-docs/toys/acceptor.rb +0 -474
  11. data/core-docs/toys/arg_parser.rb +0 -397
  12. data/core-docs/toys/cli.rb +0 -466
  13. data/core-docs/toys/compat.rb +0 -2
  14. data/core-docs/toys/completion.rb +0 -340
  15. data/core-docs/toys/context.rb +0 -386
  16. data/core-docs/toys/core.rb +0 -14
  17. data/core-docs/toys/dsl/base.rb +0 -56
  18. data/core-docs/toys/dsl/flag.rb +0 -284
  19. data/core-docs/toys/dsl/flag_group.rb +0 -267
  20. data/core-docs/toys/dsl/internal.rb +0 -4
  21. data/core-docs/toys/dsl/positional_arg.rb +0 -155
  22. data/core-docs/toys/dsl/tool.rb +0 -1639
  23. data/core-docs/toys/errors.rb +0 -126
  24. data/core-docs/toys/flag.rb +0 -560
  25. data/core-docs/toys/flag_group.rb +0 -186
  26. data/core-docs/toys/input_file.rb +0 -17
  27. data/core-docs/toys/loader.rb +0 -363
  28. data/core-docs/toys/middleware.rb +0 -336
  29. data/core-docs/toys/mixin.rb +0 -142
  30. data/core-docs/toys/module_lookup.rb +0 -75
  31. data/core-docs/toys/positional_arg.rb +0 -145
  32. data/core-docs/toys/settings.rb +0 -524
  33. data/core-docs/toys/source_info.rb +0 -271
  34. data/core-docs/toys/standard_middleware/add_verbosity_flags.rb +0 -49
  35. data/core-docs/toys/standard_middleware/apply_config.rb +0 -24
  36. data/core-docs/toys/standard_middleware/handle_usage_errors.rb +0 -33
  37. data/core-docs/toys/standard_middleware/set_default_descriptions.rb +0 -222
  38. data/core-docs/toys/standard_middleware/show_help.rb +0 -190
  39. data/core-docs/toys/standard_middleware/show_root_version.rb +0 -45
  40. data/core-docs/toys/standard_mixins/bundler.rb +0 -98
  41. data/core-docs/toys/standard_mixins/exec.rb +0 -711
  42. data/core-docs/toys/standard_mixins/fileutils.rb +0 -18
  43. data/core-docs/toys/standard_mixins/gems.rb +0 -62
  44. data/core-docs/toys/standard_mixins/git_cache.rb +0 -41
  45. data/core-docs/toys/standard_mixins/highline.rb +0 -133
  46. data/core-docs/toys/standard_mixins/pager.rb +0 -50
  47. data/core-docs/toys/standard_mixins/terminal.rb +0 -135
  48. data/core-docs/toys/standard_mixins/xdg.rb +0 -49
  49. data/core-docs/toys/template.rb +0 -112
  50. data/core-docs/toys/tool_definition.rb +0 -1079
  51. data/core-docs/toys/utils/completion_engine.rb +0 -49
  52. data/core-docs/toys/utils/exec.rb +0 -776
  53. data/core-docs/toys/utils/gems.rb +0 -185
  54. data/core-docs/toys/utils/git_cache.rb +0 -353
  55. data/core-docs/toys/utils/help_text.rb +0 -134
  56. data/core-docs/toys/utils/pager.rb +0 -118
  57. data/core-docs/toys/utils/standard_ui.rb +0 -184
  58. data/core-docs/toys/utils/terminal.rb +0 -310
  59. data/core-docs/toys/utils/xdg.rb +0 -253
  60. data/core-docs/toys/wrappable_string.rb +0 -132
  61. data/core-docs/toys-core.rb +0 -111
@@ -1,711 +0,0 @@
1
- module Toys
2
- module StandardMixins
3
- ##
4
- # **_Defined in the toys-core gem_**
5
- #
6
- # The `:exec` mixin provides set of helper methods for executing processes
7
- # and subcommands. It provides shortcuts for common cases such as invoking
8
- # a Ruby script in a subprocess or capturing output in a string. It also
9
- # provides an interface for controlling a spawned process's streams.
10
- #
11
- # You can make these methods available to your tool by including the
12
- # following directive in your tool configuration:
13
- #
14
- # include :exec
15
- #
16
- # This is a frontend for {Toys::Utils::Exec}. More information is
17
- # available in that class's documentation.
18
- #
19
- # ### Mixin overview
20
- #
21
- # The mixin provides a number of methods for spawning processes. The most
22
- # basic are {#exec} and {#exec_proc}. The {#exec} method spawns an
23
- # operating system process specified by an executable and a set of
24
- # arguments. The {#exec_proc} method takes a `Proc` and forks a Ruby
25
- # process. Both of these can be heavily configured with stream handling,
26
- # result handling, and numerous other options described below. The mixin
27
- # also provides convenience methods for common cases such as spawning a
28
- # Ruby process, spawning a shell script, or capturing output.
29
- #
30
- # The mixin also stores default configuration that it applies to processes
31
- # it spawns. You can change these defaults by calling {#configure_exec}.
32
- #
33
- # Underlying the mixin is a service object of type {Toys::Utils::Exec}.
34
- # Normally you would use the mixin methods to access this functionality,
35
- # but you can also retrieve the service object itself by calling
36
- # {Toys::Context#get} with the key {Toys::StandardMixins::Exec::KEY}.
37
- #
38
- # ### Controlling processes
39
- #
40
- # A process can be started in the *foreground* or the *background*. If you
41
- # start a foreground process, it will "take over" your standard input and
42
- # output streams by default, and it will keep control until it completes.
43
- # If you start a background process, its streams will be redirected to null
44
- # by default, and control will be returned to you immediately.
45
- #
46
- # While a process is running, you can control it using a
47
- # {Toys::Utils::Exec::Controller} object. Use a controller to interact with
48
- # the process's input and output streams, send it signals, or wait for it
49
- # to complete.
50
- #
51
- # When running a process in the foreground, the controller will be yielded
52
- # to an optional block. For example, the following code starts a process in
53
- # the foreground and passes its output stream to a controller.
54
- #
55
- # exec(["git", "init"], out: :controller) do |controller|
56
- # loop do
57
- # line = controller.out.gets
58
- # break if line.nil?
59
- # puts "Got line: #{line}"
60
- # end
61
- # end
62
- #
63
- # When running a process in the background, the controller is returned from
64
- # the method that starts the process:
65
- #
66
- # controller = exec(["git", "init"], background: true)
67
- #
68
- # ### Stream handling
69
- #
70
- # By default, subprocess streams are connected to the corresponding streams
71
- # in the parent process. You can change this behavior, redirecting streams
72
- # or providing ways to control them, using the `:in`, `:out`, and `:err`
73
- # options.
74
- #
75
- # Three general strategies are available for custom stream handling. First,
76
- # you can redirect to other streams such as files, IO objects, or Ruby
77
- # strings. Some of these options map directly to options provided by the
78
- # `Process#spawn` method. Second, you can use a controller to manipulate
79
- # the streams programmatically. Third, you can capture output stream data
80
- # and make it available in the result.
81
- #
82
- # Following is a full list of the stream handling options, along with how
83
- # to specify them using the `:in`, `:out`, and `:err` options.
84
- #
85
- # * **Inherit parent stream:** You can inherit the corresponding stream
86
- # in the parent process by passing `:inherit` as the option value. This
87
- # is the default if the subprocess is *not* run in the background.
88
- #
89
- # * **Redirect to null:** You can redirect to a null stream by passing
90
- # `:null` as the option value. This connects to a stream that is not
91
- # closed but contains no data, i.e. `/dev/null` on unix systems. This
92
- # is the default if the subprocess is run in the background.
93
- #
94
- # * **Close the stream:** You can close the stream by passing `:close` as
95
- # the option value. This is the same as passing `:close` to
96
- # `Process#spawn`.
97
- #
98
- # * **Redirect to a file:** You can redirect to a file. This reads from
99
- # an existing file when connected to `:in`, and creates or appends to a
100
- # file when connected to `:out` or `:err`. To specify a file, use the
101
- # setting `[:file, "/path/to/file"]`. You can also, when writing a
102
- # file, append an optional mode and permission code to the array. For
103
- # example, `[:file, "/path/to/file", "a", 0644]`.
104
- #
105
- # * **Redirect to an IO object:** You can redirect to an IO object in the
106
- # parent process, by passing the IO object as the option value. You can
107
- # use any IO object. For example, you could connect the child's output
108
- # to the parent's error using `out: $stderr`, or you could connect to
109
- # an existing File stream. Unlike `Process#spawn`, this works for IO
110
- # objects that do not have a corresponding file descriptor (such as
111
- # StringIO objects). In such a case, a thread will be spawned to pipe
112
- # the IO data through to the child process.
113
- #
114
- # * **Redirect to a pipe:** You can redirect to a pipe created using
115
- # `IO.pipe` (i.e. a two-element array of read and write IO objects) by
116
- # passing the array as the option value. This will connect the
117
- # appropriate IO (either read or write), and close it in the parent.
118
- # Thus, you can connect only one process to each end. If you want more
119
- # direct control over IO closing behavior, pass the IO object (i.e. the
120
- # element of the pipe array) directly.
121
- #
122
- # * **Combine with another child stream:** You can redirect one child
123
- # output stream to another, to combine them. To merge the child's error
124
- # stream into its output stream, use `err: [:child, :out]`.
125
- #
126
- # * **Read from a string:** You can pass a string to the input stream by
127
- # setting `[:string, "the string"]`. This works only for `:in`.
128
- #
129
- # * **Capture output stream:** You can capture a stream and make it
130
- # available on the {Toys::Utils::Exec::Result} object, using the
131
- # setting `:capture`. This works only for the `:out` and `:err`
132
- # streams.
133
- #
134
- # * **Use the controller:** You can hook a stream to the controller using
135
- # the setting `:controller`. You can then manipulate the stream via the
136
- # controller. If you pass a block to {Toys::StandardMixins::Exec#exec},
137
- # it yields the {Toys::Utils::Exec::Controller}, giving you access to
138
- # streams.
139
- #
140
- # * **Make copies of an output stream:** You can "tee," or duplicate the
141
- # `:out` or `:err` stream and redirect those copies to various
142
- # destinations. To specify a tee, use the setting `[:tee, ...]` where
143
- # the additional array elements include two or more of the following.
144
- # See the corresponding documentation above for more detail.
145
- # * `:inherit` to direct to the parent process's stream.
146
- # * `:capture` to capture the stream and store it in the result.
147
- # * `:controller` to direct the stream to the controller.
148
- # * `[:file, "/path/to/file"]` to write to a file.
149
- # * An `IO` or `StringIO` object.
150
- # * An array of two `IO` objects representing a pipe
151
- #
152
- # Additionally, the last element of the array can be a hash of options.
153
- # Supported options include:
154
- # * `:buffer_size` The size of the memory buffer for each element of
155
- # the tee. Larger buffers may allow higher throughput. The default
156
- # is 65536.
157
- #
158
- # ### Result handling
159
- #
160
- # A subprocess result is represented by a {Toys::Utils::Exec::Result}
161
- # object, which includes the exit code, the content of any captured output
162
- # streams, and any exeption raised when attempting to run the process.
163
- # When you run a process in the foreground, the method will return a result
164
- # object. When you run a process in the background, you can obtain the
165
- # result from the controller once the process completes.
166
- #
167
- # The following example demonstrates running a process in the foreground
168
- # and getting the exit code:
169
- #
170
- # result = exec(["git", "init"])
171
- # puts "exit code: #{result.exit_code}"
172
- #
173
- # The following example demonstrates starting a process in the background,
174
- # waiting for it to complete, and getting its exit code:
175
- #
176
- # controller = exec(["git", "init"], background: true)
177
- # result = controller.result(timeout: 1.0)
178
- # if result
179
- # puts "exit code: #{result.exit_code}"
180
- # else
181
- # puts "timed out"
182
- # end
183
- #
184
- # You can also provide a callback that is executed once a process
185
- # completes. This callback can be specified as a method name or a `Proc`
186
- # object, and will be passed the result object. For example:
187
- #
188
- # def run
189
- # exec(["git", "init"], result_callback: :handle_result)
190
- # end
191
- # def handle_result(result)
192
- # puts "exit code: #{result.exit_code}"
193
- # end
194
- #
195
- # Finally, you can force your tool to exit if a subprocess fails, similar
196
- # to setting the `set -e` option in bash, by setting the
197
- # `:exit_on_nonzero_status` option. This is often set as a default
198
- # configuration for all subprocesses run in a tool, by passing it as an
199
- # argument to the `include` directive:
200
- #
201
- # include :exec, exit_on_nonzero_status: true
202
- #
203
- # ### Configuration Options
204
- #
205
- # A variety of options can be used to control subprocesses. These can be
206
- # provided to any method that starts a subprocess. You can also set
207
- # defaults by passing them as keyword arguments when you `include` the
208
- # mixin.
209
- #
210
- # Options that affect the behavior of subprocesses:
211
- #
212
- # * `:env` (Hash) Environment variables to pass to the subprocess.
213
- # Keys represent variable names and should be strings. Values should be
214
- # either strings or `nil`, which unsets the variable.
215
- #
216
- # * `:background` (Boolean) Runs the process in the background if `true`.
217
- #
218
- # Options related to handling results
219
- #
220
- # * `:result_callback` (Proc,Symbol) A procedure that is called, and
221
- # passed the result object, when the subprocess exits. You can provide
222
- # a `Proc` object, or the name of a method as a `Symbol`.
223
- #
224
- # * `:exit_on_nonzero_status` (Boolean) If set to true, a nonzero exit
225
- # code will cause the tool to exit immediately with that same code.
226
- #
227
- # * `:e` (Boolean) A short name for `:exit_on_nonzero_status`.
228
- #
229
- # Options for connecting input and output streams. See the section above on
230
- # stream handling for info on the values that can be passed.
231
- #
232
- # * `:in` Connects the input stream of the subprocess. See the section on
233
- # stream handling.
234
- #
235
- # * `:out` Connects the standard output stream of the subprocess. See the
236
- # section on stream handling.
237
- #
238
- # * `:err` Connects the standard error stream of the subprocess. See the
239
- # section on stream handling.
240
- #
241
- # Options related to logging and reporting:
242
- #
243
- # * `:logger` (Logger) Logger to use for logging the actual command. If
244
- # not present, the command is not logged.
245
- #
246
- # * `:log_level` (Integer,false) Level for logging the actual command.
247
- # Defaults to Logger::INFO if not present. You can also pass `false` to
248
- # disable logging of the command.
249
- #
250
- # * `:log_cmd` (String) The string logged for the actual command.
251
- # Defaults to the `inspect` representation of the command.
252
- #
253
- # * `:name` (Object) An optional object that can be used to identify this
254
- # subprocess. It is available in the controller and result objects.
255
- #
256
- # In addition, the following options recognized by
257
- # [`Process#spawn`](https://ruby-doc.org/core/Process.html#method-c-spawn)
258
- # are supported.
259
- #
260
- # * `:chdir` (String) Set the working directory for the command.
261
- #
262
- # * `:close_others` (Boolean) Whether to close non-redirected
263
- # non-standard file descriptors.
264
- #
265
- # * `:new_pgroup` (Boolean) Create new process group (Windows only).
266
- #
267
- # * `:pgroup` (Integer,true,nil) The process group setting.
268
- #
269
- # * `:umask` (Integer) Umask setting for the new process.
270
- #
271
- # * `:unsetenv_others` (Boolean) Clear environment variables except those
272
- # explicitly set.
273
- #
274
- # Any other option key will result in an `ArgumentError`.
275
- #
276
- module Exec
277
- include Mixin
278
-
279
- ##
280
- # Context key for the executor object.
281
- # @return [Object]
282
- #
283
- KEY = ::Object.new.freeze
284
-
285
- ##
286
- # Set default configuration options.
287
- #
288
- # See the {Toys::StandardMixins::Exec} module documentation for a
289
- # description of the options.
290
- #
291
- # @param opts [keywords] The default options.
292
- # @return [self]
293
- #
294
- def configure_exec(**opts)
295
- # Source available in the toys-core gem
296
- end
297
-
298
- ##
299
- # Execute a command. The command can be given as a single string to pass
300
- # to a shell, or an array of strings indicating a posix command.
301
- #
302
- # If the process is not set to run in the background, and a block is
303
- # provided, a {Toys::Utils::Exec::Controller} will be yielded to it.
304
- #
305
- # ### Examples
306
- #
307
- # Run a command without a shell, and print the exit code (0 for success):
308
- #
309
- # result = exec(["git", "init"])
310
- # puts "exit code: #{result.exit_code}"
311
- #
312
- # Run a shell command:
313
- #
314
- # result = exec("cd mydir && git init")
315
- # puts "exit code: #{result.exit_code}"
316
- #
317
- # @param cmd [String,Array<String>] The command to execute.
318
- # @param opts [keywords] The command options. See the section on
319
- # Configuration Options in the {Toys::StandardMixins::Exec} module
320
- # documentation.
321
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
322
- # the subprocess. See the section on Controlling Processes in the
323
- # {Toys::StandardMixins::Exec} module documentation.
324
- #
325
- # @return [Toys::Utils::Exec::Controller] The subprocess controller, if
326
- # the process is running in the background.
327
- # @return [Toys::Utils::Exec::Result] The result, if the process ran in
328
- # the foreground.
329
- #
330
- def exec(cmd, **opts, &block)
331
- # Source available in the toys-core gem
332
- end
333
-
334
- ##
335
- # Spawn a ruby process and pass the given arguments to it.
336
- #
337
- # If the process is not set to run in the background, and a block is
338
- # provided, a {Toys::Utils::Exec::Controller} will be yielded to it.
339
- #
340
- # ### Example
341
- #
342
- # Execute a small script with warnings
343
- #
344
- # exec_ruby(["-w", "-e", "(1..10).each { |i| puts i }"])
345
- #
346
- # @param args [String,Array<String>] The arguments to ruby.
347
- # @param opts [keywords] The command options. See the section on
348
- # Configuration Options in the {Toys::StandardMixins::Exec} module
349
- # documentation.
350
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
351
- # the subprocess. See the section on Controlling Processes in the
352
- # {Toys::StandardMixins::Exec} module documentation.
353
- #
354
- # @return [Toys::Utils::Exec::Controller] The subprocess controller, if
355
- # the process is running in the background.
356
- # @return [Toys::Utils::Exec::Result] The result, if the process ran in
357
- # the foreground.
358
- #
359
- def exec_ruby(args, **opts, &block)
360
- # Source available in the toys-core gem
361
- end
362
- alias ruby exec_ruby
363
-
364
- ##
365
- # Execute a proc in a forked subprocess.
366
- #
367
- # If the process is not set to run in the background, and a block is
368
- # provided, a {Toys::Utils::Exec::Controller} will be yielded to it.
369
- #
370
- # Beware that some Ruby environments (e.g. JRuby, and Ruby on Windows)
371
- # do not support this method because they do not support fork.
372
- #
373
- # ### Example
374
- #
375
- # Run a proc in a forked process.
376
- #
377
- # code = proc do
378
- # puts "Spawned process ID is #{Process.pid}"
379
- # end
380
- # puts "Main process ID is #{Process.pid}"
381
- # exec_proc(code)
382
- #
383
- # @param func [Proc] The proc to call.
384
- # @param opts [keywords] The command options. See the section on
385
- # Configuration Options in the {Toys::StandardMixins::Exec} module
386
- # documentation.
387
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
388
- # the subprocess. See the section on Controlling Processes in the
389
- # {Toys::StandardMixins::Exec} module documentation.
390
- #
391
- # @return [Toys::Utils::Exec::Controller] The subprocess controller, if
392
- # the process is running in the background.
393
- # @return [Toys::Utils::Exec::Result] The result, if the process ran in
394
- # the foreground.
395
- #
396
- def exec_proc(func, **opts, &block)
397
- # Source available in the toys-core gem
398
- end
399
-
400
- ##
401
- # Execute a tool in the current CLI in a forked process.
402
- #
403
- # The command can be given as a single string or an array of strings,
404
- # representing the tool to run and the arguments to pass.
405
- #
406
- # If the process is not set to run in the background, and a block is
407
- # provided, a {Toys::Utils::Exec::Controller} will be yielded to it.
408
- #
409
- # Beware that some Ruby environments (e.g. JRuby, and Ruby on Windows)
410
- # do not support this method because they do not support fork.
411
- #
412
- # ### Example
413
- #
414
- # Run the "system update" tool and pass it an argument.
415
- #
416
- # exec_tool(["system", "update", "--verbose"])
417
- #
418
- # @param cmd [String,Array<String>] The tool to execute.
419
- # @param opts [keywords] The command options. See the section on
420
- # Configuration Options in the {Toys::StandardMixins::Exec} module
421
- # documentation.
422
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
423
- # the subprocess. See the section on Controlling Processes in the
424
- # {Toys::StandardMixins::Exec} module documentation.
425
- #
426
- # @return [Toys::Utils::Exec::Controller] The subprocess controller, if
427
- # the process is running in the background.
428
- # @return [Toys::Utils::Exec::Result] The result, if the process ran in
429
- # the foreground.
430
- #
431
- def exec_tool(cmd, **opts, &block)
432
- # Source available in the toys-core gem
433
- end
434
-
435
- ##
436
- # Execute a tool in a separately spawned process.
437
- #
438
- # The command can be given as a single string or an array of strings,
439
- # representing the tool to run and the arguments to pass.
440
- #
441
- # If the process is not set to run in the background, and a block is
442
- # provided, a {Toys::Utils::Exec::Controller} will be yielded to it.
443
- #
444
- # An entirely separate spawned process is run for this tool, using the
445
- # setting of {Toys.executable_path}. Thus, this method can be run only if
446
- # that setting is present. The normal Toys gem does set it, but if you
447
- # are writing your own executable using Toys-Core, you will need to set
448
- # it explicitly for this method to work. Furthermore, Bundler, if
449
- # present, is reset to its "unbundled" environment. Thus, the tool found,
450
- # the behavior of the CLI, and the gem environment, might not be the same
451
- # as those of the calling tool.
452
- #
453
- # This method is often used if you are already in a bundle and need to
454
- # run a tool that uses a different bundle. It may also be necessary on
455
- # environments without "fork" (such as JRuby or Ruby on Windows).
456
- #
457
- # ### Example
458
- #
459
- # Run the "system update" tool and pass it an argument.
460
- #
461
- # exec_separate_tool(["system", "update", "--verbose"])
462
- #
463
- # @param cmd [String,Array<String>] The tool to execute.
464
- # @param opts [keywords] The command options. See the section on
465
- # Configuration Options in the {Toys::StandardMixins::Exec} module
466
- # documentation.
467
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
468
- # the subprocess. See the section on Controlling Processes in the
469
- # {Toys::StandardMixins::Exec} module documentation.
470
- #
471
- # @return [Toys::Utils::Exec::Controller] The subprocess controller, if
472
- # the process is running in the background.
473
- # @return [Toys::Utils::Exec::Result] The result, if the process ran in
474
- # the foreground.
475
- #
476
- def exec_separate_tool(cmd, **opts, &block)
477
- # Source available in the toys-core gem
478
- end
479
-
480
- ##
481
- # Execute a command. The command can be given as a single string to pass
482
- # to a shell, or an array of strings indicating a posix command.
483
- #
484
- # Captures standard out and returns it as a string.
485
- # Cannot be run in the background.
486
- #
487
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
488
- # yielded to it.
489
- #
490
- # ### Example
491
- #
492
- # Capture the output of an echo command
493
- #
494
- # str = capture(["echo", "hello"])
495
- # assert_equal("hello\n", str)
496
- #
497
- # @param cmd [String,Array<String>] The command to execute.
498
- # @param opts [keywords] The command options. See the section on
499
- # Configuration Options in the {Toys::StandardMixins::Exec} module
500
- # documentation.
501
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
502
- # the subprocess. See the section on Controlling Processes in the
503
- # {Toys::StandardMixins::Exec} module documentation.
504
- #
505
- # @return [String] What was written to standard out.
506
- #
507
- def capture(cmd, **opts, &block)
508
- # Source available in the toys-core gem
509
- end
510
-
511
- ##
512
- # Spawn a ruby process and pass the given arguments to it.
513
- #
514
- # Captures standard out and returns it as a string.
515
- # Cannot be run in the background.
516
- #
517
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
518
- # yielded to it.
519
- #
520
- # ### Example
521
- #
522
- # Capture the output of a ruby script.
523
- #
524
- # str = capture_ruby("-e", "(1..3).each { |i| puts i }")
525
- # assert_equal "1\n2\n3\n", str
526
- #
527
- # @param args [String,Array<String>] The arguments to ruby.
528
- # @param opts [keywords] The command options. See the section on
529
- # Configuration Options in the {Toys::StandardMixins::Exec} module
530
- # documentation.
531
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
532
- # the subprocess. See the section on Controlling Processes in the
533
- # {Toys::StandardMixins::Exec} module documentation.
534
- #
535
- # @return [String] What was written to standard out.
536
- #
537
- def capture_ruby(args, **opts, &block)
538
- # Source available in the toys-core gem
539
- end
540
-
541
- ##
542
- # Execute a proc in a forked subprocess.
543
- #
544
- # Captures standard out and returns it as a string.
545
- # Cannot be run in the background.
546
- #
547
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
548
- # yielded to it.
549
- #
550
- # Beware that some Ruby environments (e.g. JRuby, and Ruby on Windows)
551
- # do not support this method because they do not support fork.
552
- #
553
- # ### Example
554
- #
555
- # Run a proc in a forked process and capture its output:
556
- #
557
- # code = proc do
558
- # puts Process.pid
559
- # end
560
- # forked_pid = capture_proc(code).chomp
561
- # puts "I forked PID #{forked_pid}"
562
- #
563
- # @param func [Proc] The proc to call.
564
- # @param opts [keywords] The command options. See the section on
565
- # Configuration Options in the {Toys::StandardMixins::Exec} module
566
- # documentation.
567
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
568
- # the subprocess. See the section on Controlling Processes in the
569
- # {Toys::StandardMixins::Exec} module documentation.
570
- #
571
- # @return [String] What was written to standard out.
572
- #
573
- def capture_proc(func, **opts, &block)
574
- # Source available in the toys-core gem
575
- end
576
-
577
- ##
578
- # Execute a tool in the current CLI in a forked process.
579
- #
580
- # Captures standard out and returns it as a string.
581
- # Cannot be run in the background.
582
- #
583
- # The command can be given as a single string or an array of strings,
584
- # representing the tool to run and the arguments to pass.
585
- #
586
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
587
- # yielded to it.
588
- #
589
- # Beware that some Ruby environments (e.g. JRuby, and Ruby on Windows)
590
- # do not support this method because they do not support fork.
591
- #
592
- # ### Example
593
- #
594
- # Run the "system version" tool and capture its output.
595
- #
596
- # str = capture_tool(["system", "version"]).chomp
597
- # puts "Version was #{str}"
598
- #
599
- # @param cmd [String,Array<String>] The tool to execute.
600
- # @param opts [keywords] The command options. See the section on
601
- # Configuration Options in the {Toys::StandardMixins::Exec} module
602
- # documentation.
603
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
604
- # the subprocess. See the section on Controlling Processes in the
605
- # {Toys::StandardMixins::Exec} module documentation.
606
- #
607
- # @return [String] What was written to standard out.
608
- #
609
- def capture_tool(cmd, **opts, &block)
610
- # Source available in the toys-core gem
611
- end
612
-
613
- ##
614
- # Execute a tool in a separately spawned process.
615
- #
616
- # Captures standard out and returns it as a string.
617
- # Cannot be run in the background.
618
- #
619
- # The command can be given as a single string or an array of strings,
620
- # representing the tool to run and the arguments to pass.
621
- #
622
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
623
- # yielded to it.
624
- #
625
- # An entirely separate spawned process is run for this tool, using the
626
- # setting of {Toys.executable_path}. Thus, this method can be run only if
627
- # that setting is present. The normal Toys gem does set it, but if you
628
- # are writing your own executable using Toys-Core, you will need to set
629
- # it explicitly for this method to work. Furthermore, Bundler, if
630
- # present, is reset to its "unbundled" environment. Thus, the tool found,
631
- # the behavior of the CLI, and the gem environment, might not be the same
632
- # as those of the calling tool.
633
- #
634
- # This method is often used if you are already in a bundle and need to
635
- # run a tool that uses a different bundle. It may also be necessary on
636
- # environments without "fork" (such as JRuby or Ruby on Windows).
637
- #
638
- # ### Example
639
- #
640
- # Run the "system version" tool and capture its output.
641
- #
642
- # str = capture_separate_tool(["system", "version"]).chomp
643
- # puts "Version was #{str}"
644
- #
645
- # @param cmd [String,Array<String>] The tool to execute.
646
- # @param opts [keywords] The command options. See the section on
647
- # Configuration Options in the {Toys::StandardMixins::Exec} module
648
- # documentation.
649
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
650
- # the subprocess. See the section on Controlling Processes in the
651
- # {Toys::StandardMixins::Exec} module documentation.
652
- #
653
- # @return [String] What was written to standard out.
654
- #
655
- def capture_separate_tool(cmd, **opts, &block)
656
- # Source available in the toys-core gem
657
- end
658
-
659
- ##
660
- # Execute the given string in a shell. Returns the exit code.
661
- # Cannot be run in the background.
662
- #
663
- # If a block is provided, a {Toys::Utils::Exec::Controller} will be
664
- # yielded to it.
665
- #
666
- # ### Example
667
- #
668
- # Run a shell script
669
- #
670
- # exit_code = sh("cd mydir && git init")
671
- # puts exit_code == 0 ? "Success!" : "Failed!"
672
- #
673
- # @param cmd [String] The shell command to execute.
674
- # @param opts [keywords] The command options. See the section on
675
- # Configuration Options in the {Toys::StandardMixins::Exec} module
676
- # documentation.
677
- # @yieldparam controller [Toys::Utils::Exec::Controller] A controller for
678
- # the subprocess. See the section on Controlling Processes in the
679
- # {Toys::StandardMixins::Exec} module documentation.
680
- #
681
- # @return [Integer] The exit code
682
- #
683
- def sh(cmd, **opts, &block)
684
- # Source available in the toys-core gem
685
- end
686
-
687
- ##
688
- # Exit if the given status code is nonzero. Otherwise, returns 0.
689
- #
690
- # @param status [Integer,Process::Status,Toys::Utils::Exec::Result]
691
- # @return [Integer]
692
- #
693
- def exit_on_nonzero_status(status)
694
- # Source available in the toys-core gem
695
- end
696
-
697
- ##
698
- # Returns an array of standard verbosity flags needed to replicate the
699
- # current verbosity level. This is useful when you want to spawn tools
700
- # with the same verbosity level as the current tool.
701
- #
702
- # @param short [Boolean] Whether to emit short rather than long flags.
703
- # Default is false.
704
- # @return [Array<String>]
705
- #
706
- def verbosity_flags(short: false)
707
- # Source available in the toys-core gem
708
- end
709
- end
710
- end
711
- end