toys-core 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +98 -0
- data/LICENSE.md +16 -24
- data/README.md +307 -59
- data/docs/guide.md +44 -4
- data/lib/toys-core.rb +58 -49
- data/lib/toys/acceptor.rb +672 -0
- data/lib/toys/alias.rb +106 -0
- data/lib/toys/arg_parser.rb +624 -0
- data/lib/toys/cli.rb +422 -181
- data/lib/toys/compat.rb +83 -0
- data/lib/toys/completion.rb +442 -0
- data/lib/toys/context.rb +354 -0
- data/lib/toys/core_version.rb +18 -26
- data/lib/toys/dsl/flag.rb +213 -56
- data/lib/toys/dsl/flag_group.rb +237 -51
- data/lib/toys/dsl/positional_arg.rb +210 -0
- data/lib/toys/dsl/tool.rb +968 -317
- data/lib/toys/errors.rb +46 -28
- data/lib/toys/flag.rb +821 -0
- data/lib/toys/flag_group.rb +282 -0
- data/lib/toys/input_file.rb +18 -26
- data/lib/toys/loader.rb +110 -100
- data/lib/toys/middleware.rb +24 -31
- data/lib/toys/mixin.rb +90 -59
- data/lib/toys/module_lookup.rb +125 -0
- data/lib/toys/positional_arg.rb +184 -0
- data/lib/toys/source_info.rb +192 -0
- data/lib/toys/standard_middleware/add_verbosity_flags.rb +38 -43
- data/lib/toys/standard_middleware/handle_usage_errors.rb +39 -40
- data/lib/toys/standard_middleware/set_default_descriptions.rb +111 -89
- data/lib/toys/standard_middleware/show_help.rb +130 -113
- data/lib/toys/standard_middleware/show_root_version.rb +29 -35
- data/lib/toys/standard_mixins/exec.rb +116 -78
- data/lib/toys/standard_mixins/fileutils.rb +16 -24
- data/lib/toys/standard_mixins/gems.rb +29 -30
- data/lib/toys/standard_mixins/highline.rb +34 -41
- data/lib/toys/standard_mixins/terminal.rb +72 -26
- data/lib/toys/template.rb +51 -35
- data/lib/toys/tool.rb +1161 -206
- data/lib/toys/utils/completion_engine.rb +171 -0
- data/lib/toys/utils/exec.rb +279 -182
- data/lib/toys/utils/gems.rb +58 -49
- data/lib/toys/utils/help_text.rb +117 -111
- data/lib/toys/utils/terminal.rb +69 -62
- data/lib/toys/wrappable_string.rb +162 -0
- metadata +24 -22
- data/lib/toys/definition/acceptor.rb +0 -191
- data/lib/toys/definition/alias.rb +0 -112
- data/lib/toys/definition/arg.rb +0 -140
- data/lib/toys/definition/flag.rb +0 -370
- data/lib/toys/definition/flag_group.rb +0 -205
- data/lib/toys/definition/source_info.rb +0 -190
- data/lib/toys/definition/tool.rb +0 -842
- data/lib/toys/dsl/arg.rb +0 -132
- data/lib/toys/runner.rb +0 -188
- data/lib/toys/standard_middleware.rb +0 -47
- data/lib/toys/utils/module_lookup.rb +0 -135
- data/lib/toys/utils/wrappable_string.rb +0 -165
@@ -1,32 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2019 Daniel Azuma
|
4
4
|
#
|
5
|
-
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
6
11
|
#
|
7
|
-
#
|
8
|
-
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
9
14
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# derived from this software without specific prior written permission.
|
18
|
-
#
|
19
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
-
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
23
|
-
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
24
|
-
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
-
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
-
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
-
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
-
# POSSIBILITY OF SUCH DAMAGE.
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
20
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
21
|
+
# IN THE SOFTWARE.
|
30
22
|
;
|
31
23
|
|
32
24
|
module Toys
|
@@ -125,7 +117,7 @@ module Toys
|
|
125
117
|
##
|
126
118
|
# Create a ShowHelp middleware.
|
127
119
|
#
|
128
|
-
# @param [Boolean,Array<String>,Proc]
|
120
|
+
# @param help_flags [Boolean,Array<String>,Proc] Specify flags to
|
129
121
|
# display help. The value may be any of the following:
|
130
122
|
#
|
131
123
|
# * An array of flags.
|
@@ -133,7 +125,7 @@ module Toys
|
|
133
125
|
# * The `false` value for no flags. (Default)
|
134
126
|
# * A proc that takes a tool and returns any of the above.
|
135
127
|
#
|
136
|
-
# @param [Boolean,Array<String>,Proc]
|
128
|
+
# @param usage_flags [Boolean,Array<String>,Proc] Specify flags to
|
137
129
|
# display usage. The value may be any of the following:
|
138
130
|
#
|
139
131
|
# * An array of flags.
|
@@ -141,7 +133,7 @@ module Toys
|
|
141
133
|
# * The `false` value for no flags. (Default)
|
142
134
|
# * A proc that takes a tool and returns any of the above.
|
143
135
|
#
|
144
|
-
# @param [Boolean,Array<String>,Proc]
|
136
|
+
# @param list_flags [Boolean,Array<String>,Proc] Specify flags to
|
145
137
|
# display subtool list. The value may be any of the following:
|
146
138
|
#
|
147
139
|
# * An array of flags.
|
@@ -149,7 +141,7 @@ module Toys
|
|
149
141
|
# * The `false` value for no flags. (Default)
|
150
142
|
# * A proc that takes a tool and returns any of the above.
|
151
143
|
#
|
152
|
-
# @param [Boolean,Array<String>,Proc]
|
144
|
+
# @param recursive_flags [Boolean,Array<String>,Proc] Specify flags
|
153
145
|
# to control recursive subtool search. The value may be any of the
|
154
146
|
# following:
|
155
147
|
#
|
@@ -158,7 +150,7 @@ module Toys
|
|
158
150
|
# * The `false` value for no flags. (Default)
|
159
151
|
# * A proc that takes a tool and returns any of the above.
|
160
152
|
#
|
161
|
-
# @param [Boolean,Array<String>,Proc]
|
153
|
+
# @param search_flags [Boolean,Array<String>,Proc] Specify flags
|
162
154
|
# to search subtools for a search term. The value may be any of
|
163
155
|
# the following:
|
164
156
|
#
|
@@ -167,7 +159,7 @@ module Toys
|
|
167
159
|
# * The `false` value for no flags. (Default)
|
168
160
|
# * A proc that takes a tool and returns any of the above.
|
169
161
|
#
|
170
|
-
# @param [Boolean,Array<String>,Proc]
|
162
|
+
# @param show_all_subtools_flags [Boolean,Array<String>,Proc] Specify
|
171
163
|
# flags to show all subtools, including hidden tools and non-runnable
|
172
164
|
# namespaces. The value may be any of the following:
|
173
165
|
#
|
@@ -176,24 +168,24 @@ module Toys
|
|
176
168
|
# * The `false` value for no flags. (Default)
|
177
169
|
# * A proc that takes a tool and returns any of the above.
|
178
170
|
#
|
179
|
-
# @param [Boolean]
|
171
|
+
# @param default_recursive [Boolean] Whether to search recursively for
|
180
172
|
# subtools by default. Default is `false`.
|
181
|
-
# @param [Boolean]
|
173
|
+
# @param default_show_all_subtools [Boolean] Whether to show all subtools
|
182
174
|
# by default. Default is `false`.
|
183
|
-
# @param [Boolean]
|
175
|
+
# @param fallback_execution [Boolean] Cause the tool to display its own
|
184
176
|
# help text if it is not otherwise runnable. This is mostly useful
|
185
177
|
# for namespaces, which have children are not runnable. Default is
|
186
178
|
# `false`.
|
187
|
-
# @param [Boolean]
|
179
|
+
# @param allow_root_args [Boolean] If the root tool includes flags for
|
188
180
|
# help or usage, and doesn't otherwise use positional arguments,
|
189
181
|
# then a tool name can be passed as arguments to display help for
|
190
182
|
# that tool.
|
191
|
-
# @param [Boolean]
|
183
|
+
# @param show_source_path [Boolean] Show the source path section. Default
|
192
184
|
# is `false`.
|
193
|
-
# @param [Boolean]
|
185
|
+
# @param use_less [Boolean] If the `less` tool is available, and the
|
194
186
|
# output stream is a tty, then use `less` to display help text.
|
195
|
-
# @param [IO]
|
196
|
-
# @param [Boolean,nil]
|
187
|
+
# @param stream [IO] Output stream to write to. Default is stdout.
|
188
|
+
# @param styled_output [Boolean,nil] Cause the tool to display help text
|
197
189
|
# with ansi styles. If `nil`, display styles if the output stream is
|
198
190
|
# a tty. Default is `nil`.
|
199
191
|
#
|
@@ -229,21 +221,21 @@ module Toys
|
|
229
221
|
|
230
222
|
##
|
231
223
|
# Configure flags and default data.
|
224
|
+
# @private
|
232
225
|
#
|
233
|
-
def config(
|
234
|
-
unless
|
235
|
-
StandardMiddleware.append_common_flag_group(
|
236
|
-
has_subtools = loader.has_subtools?(
|
237
|
-
help_flags = add_help_flags(
|
238
|
-
usage_flags = add_usage_flags(
|
239
|
-
list_flags = has_subtools ? add_list_flags(
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
add_root_args(tool_definition)
|
226
|
+
def config(tool, loader)
|
227
|
+
unless tool.argument_parsing_disabled?
|
228
|
+
StandardMiddleware.append_common_flag_group(tool)
|
229
|
+
has_subtools = loader.has_subtools?(tool.full_name)
|
230
|
+
help_flags = add_help_flags(tool)
|
231
|
+
usage_flags = add_usage_flags(tool)
|
232
|
+
list_flags = has_subtools ? add_list_flags(tool) : []
|
233
|
+
can_display_help = !help_flags.empty? || !list_flags.empty? ||
|
234
|
+
!usage_flags.empty? || @fallback_execution
|
235
|
+
if can_display_help && has_subtools
|
236
|
+
add_recursive_flags(tool)
|
237
|
+
add_search_flags(tool)
|
238
|
+
add_show_all_subtools_flags(tool)
|
247
239
|
end
|
248
240
|
end
|
249
241
|
yield
|
@@ -251,39 +243,59 @@ module Toys
|
|
251
243
|
|
252
244
|
##
|
253
245
|
# Display help text if requested.
|
246
|
+
# @private
|
254
247
|
#
|
255
|
-
def run(
|
256
|
-
if
|
257
|
-
|
258
|
-
elsif
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
wrap_width: terminal.width))
|
263
|
-
elsif should_show_help(tool)
|
264
|
-
output_help(get_help_text(tool).help_string(recursive: tool[RECURSIVE_SUBTOOLS_KEY],
|
265
|
-
search: tool[SEARCH_STRING_KEY],
|
266
|
-
include_hidden: tool[SHOW_ALL_SUBTOOLS_KEY],
|
267
|
-
show_source_path: @show_source_path,
|
268
|
-
wrap_width: terminal.width))
|
248
|
+
def run(context)
|
249
|
+
if context[SHOW_USAGE_KEY]
|
250
|
+
show_usage(context)
|
251
|
+
elsif context[SHOW_LIST_KEY]
|
252
|
+
show_list(context)
|
253
|
+
elsif context[SHOW_HELP_KEY]
|
254
|
+
show_help(context, true)
|
269
255
|
else
|
270
|
-
|
256
|
+
begin
|
257
|
+
yield
|
258
|
+
rescue NotRunnableError => e
|
259
|
+
raise e unless @fallback_execution
|
260
|
+
show_help(context, false)
|
261
|
+
end
|
271
262
|
end
|
272
263
|
end
|
273
264
|
|
274
265
|
private
|
275
266
|
|
276
267
|
def terminal
|
268
|
+
require "toys/utils/terminal"
|
277
269
|
@terminal ||= Utils::Terminal.new(output: @stream, styled: @styled_output)
|
278
270
|
end
|
279
271
|
|
280
|
-
def
|
281
|
-
|
282
|
-
|
272
|
+
def show_usage(context)
|
273
|
+
help_text = get_help_text(context, true)
|
274
|
+
str = help_text.usage_string(
|
275
|
+
recursive: context[RECURSIVE_SUBTOOLS_KEY],
|
276
|
+
include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], wrap_width: terminal.width
|
277
|
+
)
|
278
|
+
terminal.puts(str)
|
283
279
|
end
|
284
280
|
|
285
|
-
def
|
281
|
+
def show_list(context)
|
282
|
+
help_text = get_help_text(context, true)
|
283
|
+
str = help_text.list_string(
|
284
|
+
recursive: context[RECURSIVE_SUBTOOLS_KEY], search: context[SEARCH_STRING_KEY],
|
285
|
+
include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], wrap_width: terminal.width
|
286
|
+
)
|
287
|
+
terminal.puts(str)
|
288
|
+
end
|
289
|
+
|
290
|
+
def show_help(context, use_extra_args)
|
291
|
+
help_text = get_help_text(context, use_extra_args)
|
292
|
+
str = help_text.help_string(
|
293
|
+
recursive: context[RECURSIVE_SUBTOOLS_KEY], search: context[SEARCH_STRING_KEY],
|
294
|
+
include_hidden: context[SHOW_ALL_SUBTOOLS_KEY], show_source_path: @show_source_path,
|
295
|
+
wrap_width: terminal.width
|
296
|
+
)
|
286
297
|
if less_path
|
298
|
+
require "toys/utils/exec"
|
287
299
|
Utils::Exec.new.exec([less_path, "-R"], in: [:string, str])
|
288
300
|
else
|
289
301
|
terminal.puts(str)
|
@@ -301,27 +313,41 @@ module Toys
|
|
301
313
|
@less_path
|
302
314
|
end
|
303
315
|
|
304
|
-
def get_help_text(
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
316
|
+
def get_help_text(context, use_extra_args)
|
317
|
+
require "toys/utils/help_text"
|
318
|
+
if use_extra_args && @allow_root_args && context[Context::Key::TOOL].root?
|
319
|
+
tool_name = Array(context[Context::Key::UNMATCHED_POSITIONAL])
|
320
|
+
unless tool_name.empty?
|
321
|
+
cli = context[Context::Key::CLI]
|
322
|
+
loader = cli.loader
|
323
|
+
tool, rest = loader.lookup(tool_name)
|
324
|
+
help_text = Utils::HelpText.new(tool, loader, cli.executable_name)
|
325
|
+
report_usage_error(help_text, loader, tool.full_name, rest.first) unless rest.empty?
|
326
|
+
return help_text
|
327
|
+
end
|
328
|
+
end
|
329
|
+
Utils::HelpText.from_context(context)
|
312
330
|
end
|
313
331
|
|
314
|
-
def report_usage_error(tool_name,
|
315
|
-
|
332
|
+
def report_usage_error(help_text, loader, tool_name, next_word)
|
333
|
+
dict = loader.list_subtools(tool_name).map(&:simple_name)
|
334
|
+
suggestions = Compat.suggestions(next_word, dict)
|
335
|
+
tool_name = (tool_name + [next_word]).join(" ")
|
336
|
+
message = "Tool not found: \"#{tool_name}\"."
|
337
|
+
unless suggestions.empty?
|
338
|
+
suggestions_str = suggestions.join("\n ")
|
339
|
+
message = "#{message}\nDid you mean... #{suggestions_str}"
|
340
|
+
end
|
341
|
+
terminal.puts(message, :bright_red, :bold)
|
316
342
|
terminal.puts
|
317
343
|
terminal.puts help_text.usage_string(wrap_width: terminal.width)
|
318
|
-
|
344
|
+
Context.exit(1)
|
319
345
|
end
|
320
346
|
|
321
|
-
def add_help_flags(
|
322
|
-
flags = resolve_flags_spec(@help_flags,
|
347
|
+
def add_help_flags(tool)
|
348
|
+
flags = resolve_flags_spec(@help_flags, tool, DEFAULT_HELP_FLAGS)
|
323
349
|
unless flags.empty?
|
324
|
-
|
350
|
+
tool.add_flag(
|
325
351
|
SHOW_HELP_KEY, flags,
|
326
352
|
report_collisions: false,
|
327
353
|
desc: "Display help for this tool",
|
@@ -331,10 +357,10 @@ module Toys
|
|
331
357
|
flags
|
332
358
|
end
|
333
359
|
|
334
|
-
def add_usage_flags(
|
335
|
-
flags = resolve_flags_spec(@usage_flags,
|
360
|
+
def add_usage_flags(tool)
|
361
|
+
flags = resolve_flags_spec(@usage_flags, tool, DEFAULT_USAGE_FLAGS)
|
336
362
|
unless flags.empty?
|
337
|
-
|
363
|
+
tool.add_flag(
|
338
364
|
SHOW_USAGE_KEY, flags,
|
339
365
|
report_collisions: false,
|
340
366
|
desc: "Display a brief usage string for this tool",
|
@@ -344,10 +370,10 @@ module Toys
|
|
344
370
|
flags
|
345
371
|
end
|
346
372
|
|
347
|
-
def add_list_flags(
|
348
|
-
flags = resolve_flags_spec(@list_flags,
|
373
|
+
def add_list_flags(tool)
|
374
|
+
flags = resolve_flags_spec(@list_flags, tool, DEFAULT_LIST_FLAGS)
|
349
375
|
unless flags.empty?
|
350
|
-
|
376
|
+
tool.add_flag(
|
351
377
|
SHOW_LIST_KEY, flags,
|
352
378
|
report_collisions: false,
|
353
379
|
desc: "List the subtools under this tool",
|
@@ -357,12 +383,12 @@ module Toys
|
|
357
383
|
flags
|
358
384
|
end
|
359
385
|
|
360
|
-
def add_recursive_flags(
|
361
|
-
flags = resolve_flags_spec(@recursive_flags,
|
386
|
+
def add_recursive_flags(tool)
|
387
|
+
flags = resolve_flags_spec(@recursive_flags, tool, DEFAULT_RECURSIVE_FLAGS)
|
362
388
|
if flags.empty?
|
363
|
-
|
389
|
+
tool.default_data[RECURSIVE_SUBTOOLS_KEY] = @default_recursive
|
364
390
|
else
|
365
|
-
|
391
|
+
tool.add_flag(
|
366
392
|
RECURSIVE_SUBTOOLS_KEY, flags,
|
367
393
|
report_collisions: false, default: @default_recursive,
|
368
394
|
desc: "List all subtools recursively when displaying help" \
|
@@ -373,10 +399,10 @@ module Toys
|
|
373
399
|
flags
|
374
400
|
end
|
375
401
|
|
376
|
-
def add_search_flags(
|
377
|
-
flags = resolve_flags_spec(@search_flags,
|
402
|
+
def add_search_flags(tool)
|
403
|
+
flags = resolve_flags_spec(@search_flags, tool, DEFAULT_SEARCH_FLAGS)
|
378
404
|
unless flags.empty?
|
379
|
-
|
405
|
+
tool.add_flag(
|
380
406
|
SEARCH_STRING_KEY, flags,
|
381
407
|
report_collisions: false,
|
382
408
|
desc: "Search subtools for the given regular expression when displaying help",
|
@@ -386,13 +412,12 @@ module Toys
|
|
386
412
|
flags
|
387
413
|
end
|
388
414
|
|
389
|
-
def add_show_all_subtools_flags(
|
390
|
-
flags = resolve_flags_spec(@show_all_subtools_flags,
|
391
|
-
DEFAULT_SHOW_ALL_SUBTOOLS_FLAGS)
|
415
|
+
def add_show_all_subtools_flags(tool)
|
416
|
+
flags = resolve_flags_spec(@show_all_subtools_flags, tool, DEFAULT_SHOW_ALL_SUBTOOLS_FLAGS)
|
392
417
|
if flags.empty?
|
393
|
-
|
418
|
+
tool.default_data[SHOW_ALL_SUBTOOLS_KEY] = @default_show_all_subtools
|
394
419
|
else
|
395
|
-
|
420
|
+
tool.add_flag(
|
396
421
|
SHOW_ALL_SUBTOOLS_KEY, flags,
|
397
422
|
report_collisions: false, default: @default_show_all_subtools,
|
398
423
|
desc: "List all subtools including hidden subtools and namespaces" \
|
@@ -403,16 +428,8 @@ module Toys
|
|
403
428
|
flags
|
404
429
|
end
|
405
430
|
|
406
|
-
def
|
407
|
-
|
408
|
-
tool_definition.set_remaining_args(TOOL_NAME_KEY,
|
409
|
-
display_name: "TOOL_NAME",
|
410
|
-
desc: "The tool for which to display help")
|
411
|
-
end
|
412
|
-
end
|
413
|
-
|
414
|
-
def resolve_flags_spec(flags, tool_definition, defaults)
|
415
|
-
flags = flags.call(tool_definition) if flags.respond_to?(:call)
|
431
|
+
def resolve_flags_spec(flags, tool, defaults)
|
432
|
+
flags = flags.call(tool) if flags.respond_to?(:call)
|
416
433
|
case flags
|
417
434
|
when true, :default
|
418
435
|
Array(defaults)
|
@@ -1,32 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2019 Daniel Azuma
|
4
4
|
#
|
5
|
-
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
6
11
|
#
|
7
|
-
#
|
8
|
-
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
9
14
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# derived from this software without specific prior written permission.
|
18
|
-
#
|
19
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
-
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
23
|
-
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
24
|
-
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
-
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
-
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
-
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
-
# POSSIBILITY OF SUCH DAMAGE.
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
20
|
+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
21
|
+
# IN THE SOFTWARE.
|
30
22
|
;
|
31
23
|
|
32
24
|
module Toys
|
@@ -59,11 +51,11 @@ module Toys
|
|
59
51
|
##
|
60
52
|
# Create a ShowVersion middleware
|
61
53
|
#
|
62
|
-
# @param [String]
|
63
|
-
# @param [Array<String>]
|
54
|
+
# @param version_string [String] The string that should be displayed.
|
55
|
+
# @param version_flags [Array<String>] A list of flags that should
|
64
56
|
# trigger displaying the version. Default is
|
65
57
|
# {DEFAULT_VERSION_FLAGS}.
|
66
|
-
# @param [IO]
|
58
|
+
# @param stream [IO] Output stream to write to. Default is stdout.
|
67
59
|
#
|
68
60
|
def initialize(version_string: nil,
|
69
61
|
version_flags: DEFAULT_VERSION_FLAGS,
|
@@ -72,26 +64,28 @@ module Toys
|
|
72
64
|
@version_string = version_string
|
73
65
|
@version_flags = version_flags
|
74
66
|
@version_flag_desc = version_flag_desc
|
75
|
-
@
|
67
|
+
@output = stream
|
76
68
|
end
|
77
69
|
|
78
70
|
##
|
79
71
|
# Adds the version flag if requested.
|
72
|
+
# @private
|
80
73
|
#
|
81
|
-
def config(
|
82
|
-
if @version_string &&
|
83
|
-
|
84
|
-
|
74
|
+
def config(tool, _loader)
|
75
|
+
if @version_string && tool.root?
|
76
|
+
tool.add_flag(SHOW_VERSION_KEY, @version_flags,
|
77
|
+
report_collisions: false, desc: @version_flag_desc)
|
85
78
|
end
|
86
79
|
yield
|
87
80
|
end
|
88
81
|
|
89
82
|
##
|
90
83
|
# This middleware displays the version.
|
84
|
+
# @private
|
91
85
|
#
|
92
|
-
def run(
|
93
|
-
if
|
94
|
-
@
|
86
|
+
def run(context)
|
87
|
+
if context[SHOW_VERSION_KEY]
|
88
|
+
@output.puts(@version_string)
|
95
89
|
else
|
96
90
|
yield
|
97
91
|
end
|