toys-core 0.3.7.1 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -0
  3. data/README.md +1 -2
  4. data/lib/toys-core.rb +23 -8
  5. data/lib/toys/cli.rb +62 -23
  6. data/lib/toys/core_version.rb +1 -1
  7. data/lib/toys/definition/arg.rb +0 -2
  8. data/lib/toys/definition/flag.rb +2 -4
  9. data/lib/toys/definition/tool.rb +38 -36
  10. data/lib/toys/dsl/arg.rb +4 -0
  11. data/lib/toys/dsl/flag.rb +9 -5
  12. data/lib/toys/dsl/tool.rb +35 -28
  13. data/lib/toys/input_file.rb +2 -1
  14. data/lib/toys/loader.rb +97 -51
  15. data/lib/toys/middleware.rb +61 -87
  16. data/lib/toys/runner.rb +19 -2
  17. data/lib/toys/{middleware → standard_middleware}/add_verbosity_flags.rb +24 -8
  18. data/lib/toys/{middleware → standard_middleware}/handle_usage_errors.rb +4 -6
  19. data/lib/toys/{middleware → standard_middleware}/set_default_descriptions.rb +4 -4
  20. data/lib/toys/{middleware → standard_middleware}/show_help.rb +32 -16
  21. data/lib/toys/{middleware → standard_middleware}/show_root_version.rb +4 -5
  22. data/lib/toys/{helpers → standard_mixins}/exec.rb +8 -8
  23. data/lib/toys/{helpers → standard_mixins}/fileutils.rb +1 -1
  24. data/lib/toys/{helpers → standard_mixins}/highline.rb +2 -3
  25. data/lib/toys/{helpers → standard_mixins}/terminal.rb +2 -4
  26. data/lib/toys/tool.rb +3 -2
  27. data/lib/toys/utils/exec.rb +255 -82
  28. data/lib/toys/utils/gems.rb +3 -3
  29. data/lib/toys/utils/help_text.rb +0 -2
  30. data/lib/toys/utils/module_lookup.rb +60 -40
  31. data/lib/toys/utils/wrappable_string.rb +0 -2
  32. metadata +11 -19
  33. data/lib/toys/helpers.rb +0 -54
  34. data/lib/toys/middleware/base.rb +0 -99
  35. data/lib/toys/templates.rb +0 -55
  36. data/lib/toys/templates/clean.rb +0 -85
  37. data/lib/toys/templates/gem_build.rb +0 -125
  38. data/lib/toys/templates/minitest.rb +0 -125
  39. data/lib/toys/templates/rubocop.rb +0 -86
  40. data/lib/toys/templates/yardoc.rb +0 -101
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1aa9e869c65cf77b35297969e3ca1982242afecadf90d9cb916dac0e0d5f5167
4
- data.tar.gz: e46555ffe1779fc8f4375f301e4ee34c892785cfb01b454f24854ffac905bab8
3
+ metadata.gz: bbe7cd88d7c1eba9d74c0006bfe81372963949d969671ad3d13597b941525a3f
4
+ data.tar.gz: 5c567a9a61f747fd292339681d24d7bc56fa89859d9f60f6f0f6796665b93148
5
5
  SHA512:
6
- metadata.gz: f9e864b63f7ea61c2e1eed7e5ca0fbdeabc393d8fc2c1c284a7b899a63d3cfa33a58f6790417fa73bb4fd20ca73bfd04c1ccdf4fe093259ce090a01b81ce4d09
7
- data.tar.gz: 5ac0fe98810ac7b71880fb843f3deb1148d81aa5912af67e0385e5e422e02bc059747b77c59306de743a65f724942a5f813f322ba4209a4f03f8ad12f8cb7ac6
6
+ metadata.gz: fe5308be03b7d7348390caf05d6c6ba76503746133b45931b1b7c75c7b5095f03ca4ee88cefde3e0a46583901a66fa56bd5fb628c242c64d3abb854d3fb548c8
7
+ data.tar.gz: 9766792fef7ba0e77aa8e0254f222ea6f64238eaa3bdc1ed758faf8429105662be235014de8c375803a2da38fe2b0c3da1ae110e77a96d127e318f452f29453b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Release History
2
2
 
3
+ ### 0.3.8 / 2018-06-10
4
+
5
+ * CHANGED: Renamed helpers to mixins.
6
+ * CHANGED: ModuleLookup is now a customizable class and can have multiple sources.
7
+ * CHANGED: Moved the existing templates to the toys gem since they are rake replacements.
8
+ * CHANGED: Renamed :in_from, :out_to, and :err_to exec options to :in, :out, :err
9
+ * ADDED: CLI can now customize the standard mixins, templates, and middleware.
10
+ * IMPROVED: Exec raises an error if passed an unknown option.
11
+ * IMPROVED: Exec now accepts nearly all the same stream specifications as Process#spawn.
12
+
3
13
  ### 0.3.7.1 / 2018-05-30
4
14
 
5
15
  * No changes.
data/README.md CHANGED
@@ -19,8 +19,7 @@ highly experimental, and the code is evolving very rapidly. Please contact the
19
19
  author before embarking on a major pull request. More detailed contribution
20
20
  guidelines will be provided when the software stabilizes further.
21
21
 
22
- The source can be found on Github at
23
- [https://github.com/dazuma/toys](https://github.com/dazuma/toys)
22
+ The source can be found on Github at https://github.com/dazuma/toys
24
23
 
25
24
  ## License
26
25
 
data/lib/toys-core.rb CHANGED
@@ -34,20 +34,31 @@
34
34
  # individual directories.
35
35
  #
36
36
  module Toys
37
- ##
38
- # Namespace for common utility classes.
39
- #
40
- module Utils; end
41
-
42
37
  ##
43
38
  # Namespace for object definition classes.
44
39
  #
45
40
  module Definition; end
46
41
 
47
42
  ##
48
- # Namespace for DSL classes.
43
+ # Namespace for DSL classes. These classes provide the directives that can be
44
+ # used in configuration files. Most are defined in {Toys::DSL::Tool}.
49
45
  #
50
46
  module DSL; end
47
+
48
+ ##
49
+ # Namespace for standard mixin classes.
50
+ #
51
+ module StandardMixins; end
52
+
53
+ ##
54
+ # Namespace for standard middleware classes.
55
+ #
56
+ module StandardMiddleware; end
57
+
58
+ ##
59
+ # Namespace for common utility classes.
60
+ #
61
+ module Utils; end
51
62
  end
52
63
 
53
64
  require "toys/cli"
@@ -61,11 +72,15 @@ require "toys/dsl/arg"
61
72
  require "toys/dsl/flag"
62
73
  require "toys/dsl/tool"
63
74
  require "toys/errors"
64
- require "toys/helpers"
65
75
  require "toys/input_file"
66
76
  require "toys/loader"
67
77
  require "toys/middleware"
68
78
  require "toys/runner"
69
79
  require "toys/template"
70
- require "toys/templates"
71
80
  require "toys/tool"
81
+ require "toys/utils/exec"
82
+ require "toys/utils/gems"
83
+ require "toys/utils/help_text"
84
+ require "toys/utils/module_lookup"
85
+ require "toys/utils/terminal"
86
+ require "toys/utils/wrappable_string"
data/lib/toys/cli.rb CHANGED
@@ -29,8 +29,6 @@
29
29
 
30
30
  require "logger"
31
31
 
32
- require "toys/utils/terminal"
33
-
34
32
  module Toys
35
33
  ##
36
34
  # A Toys-based CLI.
@@ -39,7 +37,7 @@ module Toys
39
37
  #
40
38
  class CLI
41
39
  ##
42
- # Create a CLI
40
+ # Create a CLI.
43
41
  #
44
42
  # @param [String,nil] binary_name The binary name displayed in help text.
45
43
  # Optional. Defaults to the ruby program name.
@@ -65,7 +63,17 @@ module Toys
65
63
  # may use such a file to define auxiliary Ruby modules and classes that
66
64
  # used by the tools defined in that directory.
67
65
  # @param [Array] middleware_stack An array of middleware that will be used
68
- # by default for all tools loaded by this CLI.
66
+ # by default for all tools loaded by this CLI. If not provided, uses
67
+ # {Toys::CLI.default_middleware_stack}.
68
+ # @param [Toys::Utils::ModuleLookup] mixin_lookup A lookup for well-known
69
+ # mixin modules. If not provided, uses
70
+ # {Toys::CLI.default_mixin_lookup}.
71
+ # @param [Toys::Utils::ModuleLookup] middleware_lookup A lookup for
72
+ # well-known middleware classes. If not provided, uses
73
+ # {Toys::CLI.default_middleware_lookup}.
74
+ # @param [Toys::Utils::ModuleLookup] template_lookup A lookup for
75
+ # well-known template classes. If not provided, uses
76
+ # {Toys::CLI.default_template_lookup}.
69
77
  # @param [Logger,nil] logger The logger to use. If not provided, a default
70
78
  # logger that writes to `STDERR` is used.
71
79
  # @param [Integer,nil] base_level The logger level that should correspond
@@ -77,15 +85,11 @@ module Toys
77
85
  # Default is a {Toys::CLI::DefaultErrorHandler} writing to the logger.
78
86
  #
79
87
  def initialize(
80
- binary_name: nil,
81
- config_dir_name: nil,
82
- config_file_name: nil,
83
- index_file_name: nil,
84
- preload_file_name: nil,
85
- middleware_stack: nil,
86
- logger: nil,
87
- base_level: nil,
88
- error_handler: nil
88
+ binary_name: nil, middleware_stack: nil,
89
+ config_dir_name: nil, config_file_name: nil,
90
+ index_file_name: nil, preload_file_name: nil,
91
+ mixin_lookup: nil, middleware_lookup: nil, template_lookup: nil,
92
+ logger: nil, base_level: nil, error_handler: nil
89
93
  )
90
94
  @logger = logger || self.class.default_logger
91
95
  @base_level = base_level || @logger.level
@@ -95,10 +99,13 @@ module Toys
95
99
  @config_file_name = config_file_name
96
100
  @index_file_name = index_file_name
97
101
  @preload_file_name = preload_file_name
102
+ @mixin_lookup = mixin_lookup || self.class.default_mixin_lookup
103
+ @middleware_lookup = middleware_lookup || self.class.default_middleware_lookup
104
+ @template_lookup = template_lookup || self.class.default_template_lookup
98
105
  @loader = Loader.new(
99
- index_file_name: index_file_name,
100
- preload_file_name: preload_file_name,
101
- middleware_stack: @middleware_stack
106
+ index_file_name: index_file_name, preload_file_name: preload_file_name,
107
+ mixin_lookup: @mixin_lookup, template_lookup: @template_lookup,
108
+ middleware_lookup: @middleware_lookup, middleware_stack: @middleware_stack
102
109
  )
103
110
  @error_handler = error_handler || DefaultErrorHandler.new
104
111
  end
@@ -236,6 +243,9 @@ module Toys
236
243
  index_file_name: @index_file_name,
237
244
  preload_file_name: @preload_file_name,
238
245
  middleware_stack: @middleware_stack,
246
+ mixin_lookup: @mixin_lookup,
247
+ middleware_lookup: @middleware_lookup,
248
+ template_lookup: @template_lookup,
239
249
  logger: @logger,
240
250
  base_level: @base_level,
241
251
  error_handler: @error_handler)
@@ -301,14 +311,14 @@ module Toys
301
311
  # Returns a default set of middleware that may be used as a starting
302
312
  # point for a typical CLI. This set includes the following in order:
303
313
  #
304
- # * {Toys::Middleware::SetDefaultDescriptions} providing defaults for
305
- # description fields
306
- # * {Toys::Middleware::ShowHelp} adding the `--help` flag
307
- # * {Toys::Middleware::HandleUsageErrors}
308
- # * {Toys::Middleware::ShowHelp} providing default behavior for
314
+ # * {Toys::StandardMiddleware::SetDefaultDescriptions} providing
315
+ # defaults for description fields
316
+ # * {Toys::StandardMiddleware::ShowHelp} adding the `--help` flag
317
+ # * {Toys::StandardMiddleware::HandleUsageErrors}
318
+ # * {Toys::StandardMiddleware::ShowHelp} providing default behavior for
309
319
  # namespaces
310
- # * {Toys::Middleware::AddVerbosityFlags} adding the `--verbose` and
311
- # `--quiet` flags for managing the logger level
320
+ # * {Toys::StandardMiddleware::AddVerbosityFlags} adding the `--verbose`
321
+ # and `--quiet` flags for managing the logger level
312
322
  #
313
323
  # @return [Array]
314
324
  #
@@ -322,6 +332,35 @@ module Toys
322
332
  ]
323
333
  end
324
334
 
335
+ ##
336
+ # Returns a default ModuleLookup for mixins that points at the
337
+ # StandardMixins module.
338
+ #
339
+ # @return [Toys::Utils::ModuleLookup]
340
+ #
341
+ def default_mixin_lookup
342
+ Utils::ModuleLookup.new.add_path("toys/standard_mixins")
343
+ end
344
+
345
+ ##
346
+ # Returns a default ModuleLookup for middleware that points at the
347
+ # StandardMiddleware module.
348
+ #
349
+ # @return [Toys::Utils::ModuleLookup]
350
+ #
351
+ def default_middleware_lookup
352
+ Utils::ModuleLookup.new.add_path("toys/standard_middleware")
353
+ end
354
+
355
+ ##
356
+ # Returns a default empty ModuleLookup for templates.
357
+ #
358
+ # @return [Toys::Utils::ModuleLookup]
359
+ #
360
+ def default_template_lookup
361
+ Utils::ModuleLookup.new
362
+ end
363
+
325
364
  ##
326
365
  # Returns a default logger that logs to `$stderr`.
327
366
  #
@@ -32,5 +32,5 @@ module Toys
32
32
  # Current version of Toys core
33
33
  # @return [String]
34
34
  #
35
- CORE_VERSION = "0.3.7.1".freeze
35
+ CORE_VERSION = "0.3.8".freeze
36
36
  end
@@ -29,8 +29,6 @@
29
29
 
30
30
  require "optparse"
31
31
 
32
- require "toys/utils/wrappable_string"
33
-
34
32
  module Toys
35
33
  module Definition
36
34
  ##
@@ -27,10 +27,6 @@
27
27
  # POSSIBILITY OF SUCH DAMAGE.
28
28
  ;
29
29
 
30
- require "optparse"
31
-
32
- require "toys/utils/wrappable_string"
33
-
34
30
  module Toys
35
31
  module Definition
36
32
  ##
@@ -55,6 +51,8 @@ module Toys
55
51
  setup(str, [$1], $1, "--", nil, nil, nil, nil)
56
52
  when /^(--\w[\?\w-]*)([= ])\[(\w+)\]$/
57
53
  setup(str, [$1], $1, "--", :value, :optional, $2, $3)
54
+ when /^(--\w[\?\w-]*)\[([= ])(\w+)\]$/
55
+ setup(str, [$1], $1, "--", :value, :optional, $2, $3)
58
56
  when /^(--\w[\?\w-]*)([= ])(\w+)$/
59
57
  setup(str, [$1], $1, "--", :value, :required, $2, $3)
60
58
  else
@@ -29,8 +29,6 @@
29
29
 
30
30
  require "optparse"
31
31
 
32
- require "toys/utils/wrappable_string"
33
-
34
32
  module Toys
35
33
  module Definition
36
34
  ##
@@ -71,12 +69,12 @@ module Toys
71
69
  @full_name = full_name.dup.freeze
72
70
  @tool_class = DSL::Tool.new_class(@full_name, priority, loader)
73
71
  @priority = priority
74
- @middleware_stack = Middleware.resolve_stack(middleware_stack)
72
+ @middleware_stack = middleware_stack
75
73
 
76
74
  @source_path = nil
77
75
  @definition_finished = false
78
76
 
79
- @desc = Toys::Utils::WrappableString.new("")
77
+ @desc = Utils::WrappableString.new("")
80
78
  @long_desc = []
81
79
 
82
80
  @default_data = {}
@@ -84,7 +82,7 @@ module Toys
84
82
  OPTPARSER_ACCEPTORS.each { |a| @acceptors[a] = a }
85
83
  @used_flags = []
86
84
 
87
- @helpers = {}
85
+ @mixins = {}
88
86
 
89
87
  @flag_definitions = []
90
88
  @required_arg_definitions = []
@@ -253,7 +251,7 @@ module Toys
253
251
 
254
252
  ##
255
253
  # Returns a list of all custom acceptors used by this tool.
256
- # @return [Array<Toys::Tool::Acceptor>]
254
+ # @return [Array<Toys::Definition::Acceptor>]
257
255
  #
258
256
  def custom_acceptors
259
257
  result = []
@@ -267,21 +265,21 @@ module Toys
267
265
  end
268
266
 
269
267
  ##
270
- # Get the named helper from this tool or its ancestors.
268
+ # Get the named mixin from this tool or its ancestors.
271
269
  #
272
- # @param [String] name The helper name
273
- # @return [Module,nil] The helper module, or `nil` if not found.
270
+ # @param [String] name The mixin name
271
+ # @return [Module,nil] The mixin module, or `nil` if not found.
274
272
  #
275
- def resolve_helper(name)
276
- @helpers.fetch(name.to_s) { |k| @parent ? @parent.resolve_helper(k) : nil }
273
+ def resolve_mixin(name)
274
+ @mixins.fetch(name.to_s) { |k| @parent ? @parent.resolve_mixin(k) : nil }
277
275
  end
278
276
 
279
277
  ##
280
- # Include the given helper in the tool class.
278
+ # Include the given mixin in the tool class.
281
279
  #
282
- # @param [String,Symbol,Module] name The helper name or module
280
+ # @param [String,Symbol,Module] name The mixin name or module
283
281
  #
284
- def include_helper(name)
282
+ def include_mixin(name)
285
283
  tool_class.include(name)
286
284
  self
287
285
  end
@@ -334,7 +332,7 @@ module Toys
334
332
  # Add an acceptor to the tool. This acceptor may be refereneced by name
335
333
  # when adding a flag or an arg.
336
334
  #
337
- # @param [Toys::Tool::Acceptor] acceptor The acceptor to add.
335
+ # @param [Toys::Definition::Acceptor] acceptor The acceptor to add.
338
336
  #
339
337
  def add_acceptor(acceptor)
340
338
  @acceptors[acceptor.name] = acceptor
@@ -342,13 +340,13 @@ module Toys
342
340
  end
343
341
 
344
342
  ##
345
- # Add a named helper module to this tool.
343
+ # Add a named mixin module to this tool.
346
344
  #
347
- # @param [String] name The name of the helper.
348
- # @param [Module] helper_module The helper module.
345
+ # @param [String] name The name of the mixin.
346
+ # @param [Module] mixin_module The mixin module.
349
347
  #
350
- def add_helper(name, helper_module)
351
- @helpers[name.to_s] = helper_module
348
+ def add_mixin(name, mixin_module)
349
+ @mixins[name.to_s] = mixin_module
352
350
  self
353
351
  end
354
352
 
@@ -376,11 +374,12 @@ module Toys
376
374
  # requested that is already in use or marked as disabled. Default is
377
375
  # true.
378
376
  # @param [String,Array<String>,Toys::Utils::WrappableString] desc Short
379
- # description for the flag. See {Toys::Tool#desc=} for a description of
380
- # allowed formats. Defaults to the empty string.
377
+ # description for the flag. See {Toys::Definition::Tool#desc=} for a
378
+ # description of allowed formats. Defaults to the empty string.
381
379
  # @param [Array<String,Array<String>,Toys::Utils::WrappableString>] long_desc
382
- # Long description for the flag. See {Toys::Tool#long_desc=} for a
383
- # description of allowed formats. Defaults to the empty array.
380
+ # Long description for the flag. See
381
+ # {Toys::Definition::Tool#long_desc=} for a description of allowed
382
+ # formats. Defaults to the empty array.
384
383
  #
385
384
  def add_flag(key, flags = [],
386
385
  accept: nil, default: nil, handler: nil,
@@ -428,11 +427,12 @@ module Toys
428
427
  # @param [String] display_name A name to use for display (in help text and
429
428
  # error reports). Defaults to the key in upper case.
430
429
  # @param [String,Array<String>,Toys::Utils::WrappableString] desc Short
431
- # description for the flag. See {Toys::Tool#desc=} for a description of
432
- # allowed formats. Defaults to the empty string.
430
+ # description for the arg. See {Toys::Definition::Tool#desc=} for a
431
+ # description of allowed formats. Defaults to the empty string.
433
432
  # @param [Array<String,Array<String>,Toys::Utils::WrappableString>] long_desc
434
- # Long description for the flag. See {Toys::Tool#long_desc=} for a
435
- # description of allowed formats. Defaults to the empty array.
433
+ # Long description for the arg. See
434
+ # {Toys::Definition::Tool#long_desc=} for a description of allowed
435
+ # formats. Defaults to the empty array.
436
436
  #
437
437
  def add_required_arg(key, accept: nil, display_name: nil, desc: nil, long_desc: nil)
438
438
  check_definition_state
@@ -460,11 +460,12 @@ module Toys
460
460
  # @param [String] display_name A name to use for display (in help text and
461
461
  # error reports). Defaults to the key in upper case.
462
462
  # @param [String,Array<String>,Toys::Utils::WrappableString] desc Short
463
- # description for the flag. See {Toys::Tool#desc=} for a description of
464
- # allowed formats. Defaults to the empty string.
463
+ # description for the arg. See {Toys::Definition::Tool#desc=} for a
464
+ # description of allowed formats. Defaults to the empty string.
465
465
  # @param [Array<String,Array<String>,Toys::Utils::WrappableString>] long_desc
466
- # Long description for the flag. See {Toys::Tool#long_desc=} for a
467
- # description of allowed formats. Defaults to the empty array.
466
+ # Long description for the arg. See
467
+ # {Toys::Definition::Tool#long_desc=} for a description of allowed
468
+ # formats. Defaults to the empty array.
468
469
  #
469
470
  def add_optional_arg(key, default: nil, accept: nil, display_name: nil,
470
471
  desc: nil, long_desc: nil)
@@ -494,11 +495,12 @@ module Toys
494
495
  # @param [String] display_name A name to use for display (in help text and
495
496
  # error reports). Defaults to the key in upper case.
496
497
  # @param [String,Array<String>,Toys::Utils::WrappableString] desc Short
497
- # description for the flag. See {Toys::Tool#desc=} for a description of
498
- # allowed formats. Defaults to the empty string.
498
+ # description for the arg. See {Toys::Definition::Tool#desc=} for a
499
+ # description of allowed formats. Defaults to the empty string.
499
500
  # @param [Array<String,Array<String>,Toys::Utils::WrappableString>] long_desc
500
- # Long description for the flag. See {Toys::Tool#long_desc=} for a
501
- # description of allowed formats. Defaults to the empty array.
501
+ # Long description for the arg. See
502
+ # {Toys::Definition::Tool#long_desc=} for a description of allowed
503
+ # formats. Defaults to the empty array.
502
504
  #
503
505
  def set_remaining_args(key, default: [], accept: nil, display_name: nil,
504
506
  desc: nil, long_desc: nil)
data/lib/toys/dsl/arg.rb CHANGED
@@ -33,6 +33,10 @@ module Toys
33
33
  # DSL for an arg definition block. Lets you set arg attributes in a block
34
34
  # instead of a long series of keyword arguments.
35
35
  #
36
+ # These directives are available inside a block passed to
37
+ # {Toys::DSL::Tool#required_arg}, {Toys::DSL::Tool#optional_arg}, or
38
+ # {Toys::DSL::Tool#remaining_args}.
39
+ #
36
40
  class Arg
37
41
  ## @private
38
42
  def initialize(accept, default, display_name, desc, long_desc)