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
@@ -113,8 +113,8 @@ module Toys
113
113
  @terminal.spinner(leading_text: "Getting info on gem #{name.inspect}... ",
114
114
  final_text: "Done.\n") do
115
115
  req = ::Gem::Requirement.new(*requirements)
116
- result = @exec.exec(["gem", "query", "-q", "-r", "-a", "-e", name], out_to: :capture)
117
- if result.captured_out =~ /\(([\w\.,\s]+)\)/
116
+ result = @exec.capture(["gem", "query", "-q", "-r", "-a", "-e", name])
117
+ if result =~ /\(([\w\.,\s]+)\)/
118
118
  $1.split(", ")
119
119
  .map { |v| ::Gem::Version.new(v) }
120
120
  .find { |v| !v.prerelease? && req.satisfied_by?(v) }
@@ -128,7 +128,7 @@ module Toys
128
128
  @terminal.spinner(leading_text: "Installing gem #{name} #{version}... ",
129
129
  final_text: "Done.\n") do
130
130
  result = @exec.exec(["gem", "install", name, "--version", version.to_s],
131
- out_to: :capture, err_to: :capture)
131
+ out: :capture, err: :capture)
132
132
  if result.error?
133
133
  @terminal.puts(result.captured_out + result.captured_err)
134
134
  raise InstallFailedError, "Failed to install gem #{name} #{version}"
@@ -27,8 +27,6 @@
27
27
  # POSSIBILITY OF SUCH DAMAGE.
28
28
  ;
29
29
 
30
- require "toys/utils/terminal"
31
-
32
30
  module Toys
33
31
  module Utils
34
32
  ##
@@ -34,11 +34,7 @@ module Toys
34
34
  # used to obtain named helpers, middleware, and templates from the
35
35
  # respective modules.
36
36
  #
37
- # You generally do not need to use these module methods directly. Instead
38
- # use the convenience methods {Toys::Helpers.lookup},
39
- # {Toys::Middleware.lookup}, or {Toys::Templates.lookup}.
40
- #
41
- module ModuleLookup
37
+ class ModuleLookup
42
38
  class << self
43
39
  ##
44
40
  # Convert the given string to a path element. Specifically, converts
@@ -66,47 +62,71 @@ module Toys
66
62
  end
67
63
 
68
64
  ##
69
- # Obtain a named module from the given collection. Raises an exception
70
- # on failure.
71
- #
72
- # @param [String,Symbol] collection The collection to search. Typical
73
- # values are `:helpers`, `:middleware`, and `:templates`.
74
- # @param [String,Symbol] name The name of the module to return.
65
+ # Given a require path, return the module expected to be defined.
75
66
  #
76
- # @return [Module] The specified module
77
- # @raise [LoadError] No Ruby file containing the given module could
78
- # be found.
79
- # @raise [NameError] The given module was not defined.
67
+ # @param [String] path File path, delimited by forward slash
68
+ # @return [Module] The module loaded from that path
80
69
  #
81
- def lookup!(collection, name)
82
- require "toys/#{to_path_name(collection)}/#{to_path_name(name)}"
83
- collection_sym = to_module_name(collection)
84
- unless ::Toys.constants.include?(collection_sym)
85
- raise ::NameError, "Module does not exist: Toys::#{collection_sym}"
70
+ def path_to_module(path)
71
+ path.split("/").reduce(::Object) do |running_mod, seg|
72
+ mod_name = to_module_name(seg)
73
+ unless running_mod.constants.include?(mod_name)
74
+ raise ::NameError, "Module #{running_mod.name}::#{mod_name} not found"
75
+ end
76
+ running_mod.const_get(mod_name)
86
77
  end
87
- collection_mod = ::Toys.const_get(collection_sym)
88
- name_sym = to_module_name(name)
89
- unless collection_mod.constants.include?(name_sym)
90
- raise ::NameError, "Module does not exist: Toys::#{collection_sym}::#{name_sym}"
91
- end
92
- collection_mod.const_get(name_sym)
93
78
  end
79
+ end
94
80
 
95
- ##
96
- # Obtain a named module from the given collection. Returns `nil` on
97
- # failure.
98
- #
99
- # @param [String,Symbol] collection The collection to search. Typical
100
- # values are `:helpers`, `:middleware`, and `:templates`.
101
- # @param [String,Symbol] name The name of the module to return.
102
- #
103
- # @return [Module,nil] The specified module, or `nil` if not found.
104
- #
105
- def lookup(collection, name)
106
- lookup!(collection, name)
107
- rescue ::NameError, ::LoadError
108
- nil
81
+ ##
82
+ # Create an empty ModuleLookup
83
+ #
84
+ def initialize
85
+ @paths = []
86
+ end
87
+
88
+ ##
89
+ # Add a lookup path for modules.
90
+ #
91
+ # @param [String] path_base The base require path
92
+ # @param [Module] module_base The base module, or `nil` (the default) to
93
+ # infer a default from the path base.
94
+ # @param [Boolean] high_priority If true, add to the head of the lookup
95
+ # path, otherwise add to the end.
96
+ #
97
+ def add_path(path_base, module_base: nil, high_priority: false)
98
+ module_base ||= ModuleLookup.path_to_module(path_base)
99
+ if high_priority
100
+ @paths.unshift([path_base, module_base])
101
+ else
102
+ @paths << [path_base, module_base]
103
+ end
104
+ self
105
+ end
106
+
107
+ ##
108
+ # Obtain a named module. Returns `nil` if the name is not present.
109
+ #
110
+ # @param [String,Symbol] name The name of the module to return.
111
+ #
112
+ # @return [Module] The specified module
113
+ #
114
+ def lookup(name)
115
+ @paths.each do |path_base, module_base|
116
+ path = "#{path_base}/#{ModuleLookup.to_path_name(name)}"
117
+ begin
118
+ require path
119
+ rescue ::LoadError
120
+ next
121
+ end
122
+ mod_name = ModuleLookup.to_module_name(name)
123
+ unless module_base.constants.include?(mod_name)
124
+ raise ::NameError,
125
+ "File #{path.inspect} did not define #{module_base.name}::#{mod_name}"
126
+ end
127
+ return module_base.const_get(mod_name)
109
128
  end
129
+ nil
110
130
  end
111
131
  end
112
132
  end
@@ -27,8 +27,6 @@
27
27
  # POSSIBILITY OF SUCH DAMAGE.
28
28
  ;
29
29
 
30
- require "toys/utils/terminal"
31
-
32
30
  module Toys
33
31
  module Utils
34
32
  ##
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7.1
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-30 00:00:00.000000000 Z
11
+ date: 2018-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -105,28 +105,20 @@ files:
105
105
  - lib/toys/dsl/flag.rb
106
106
  - lib/toys/dsl/tool.rb
107
107
  - lib/toys/errors.rb
108
- - lib/toys/helpers.rb
109
- - lib/toys/helpers/exec.rb
110
- - lib/toys/helpers/fileutils.rb
111
- - lib/toys/helpers/highline.rb
112
- - lib/toys/helpers/terminal.rb
113
108
  - lib/toys/input_file.rb
114
109
  - lib/toys/loader.rb
115
110
  - lib/toys/middleware.rb
116
- - lib/toys/middleware/add_verbosity_flags.rb
117
- - lib/toys/middleware/base.rb
118
- - lib/toys/middleware/handle_usage_errors.rb
119
- - lib/toys/middleware/set_default_descriptions.rb
120
- - lib/toys/middleware/show_help.rb
121
- - lib/toys/middleware/show_root_version.rb
122
111
  - lib/toys/runner.rb
112
+ - lib/toys/standard_middleware/add_verbosity_flags.rb
113
+ - lib/toys/standard_middleware/handle_usage_errors.rb
114
+ - lib/toys/standard_middleware/set_default_descriptions.rb
115
+ - lib/toys/standard_middleware/show_help.rb
116
+ - lib/toys/standard_middleware/show_root_version.rb
117
+ - lib/toys/standard_mixins/exec.rb
118
+ - lib/toys/standard_mixins/fileutils.rb
119
+ - lib/toys/standard_mixins/highline.rb
120
+ - lib/toys/standard_mixins/terminal.rb
123
121
  - lib/toys/template.rb
124
- - lib/toys/templates.rb
125
- - lib/toys/templates/clean.rb
126
- - lib/toys/templates/gem_build.rb
127
- - lib/toys/templates/minitest.rb
128
- - lib/toys/templates/rubocop.rb
129
- - lib/toys/templates/yardoc.rb
130
122
  - lib/toys/tool.rb
131
123
  - lib/toys/utils/exec.rb
132
124
  - lib/toys/utils/gems.rb
data/lib/toys/helpers.rb DELETED
@@ -1,54 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- require "toys/utils/module_lookup"
31
-
32
- module Toys
33
- ##
34
- # Namespace for common helper modules
35
- #
36
- module Helpers
37
- ##
38
- # Return a helper module by name.
39
- #
40
- # Currently recognized module names are:
41
- #
42
- # * `:exec` : Methods to help execute subcommands.
43
- # * `:fileutils` : The FileUtils standard library methods.
44
- # * `:highline` : Methods from the highline gem.
45
- # * `:spinner` : Displays a spinner on the terminal.
46
- #
47
- # @param [String,Symbol] name Name of the helper module to return
48
- # @return [Module,nil] The module.
49
- #
50
- def self.lookup!(name)
51
- Utils::ModuleLookup.lookup!(:helpers, name)
52
- end
53
- end
54
- end
@@ -1,99 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- module Toys
31
- module Middleware
32
- ##
33
- # This is a base middleware with a no-op implementation.
34
- #
35
- # A middleware is an object that has the opportunity to alter the
36
- # configuration and runtime behavior of each tool in a Toys CLI. A CLI
37
- # contains an ordered list of middleware, known as the *middleware stack*,
38
- # that together define the CLI's default behavior.
39
- #
40
- # Specifically, a middleware can perform two functions.
41
- #
42
- # First, it can modify the configuration of a tool. After tools are defined
43
- # from configuration, the middleware stack can make modifications to each
44
- # tool. A middleware can add flags and arguments to the tool, modify the
45
- # description, or make any other changes to how the tool is set up.
46
- #
47
- # Second, a middleware can intercept and change tool execution. Like a Rack
48
- # middleware, a Toys middleware can wrap execution with its own code,
49
- # replace it outright, or leave it unmodified.
50
- #
51
- class Base
52
- ##
53
- # This method is called after a tool has been defined, and gives this
54
- # middleware the opportunity to modify the tool definition. It is passed
55
- # the tool definition object and the loader, and can make any changes to
56
- # the tool definition. In most cases, this method should also call
57
- # `yield`, which passes control to the next middleware in the stack. A
58
- # middleware can disable modifications done by subsequent middleware by
59
- # omitting the `yield` call, but this is uncommon.
60
- #
61
- # The base middleware implementation does nothing and simply yields to
62
- # the next middleware. Subclasses should override this if they want to
63
- # alter the tool definition.
64
- #
65
- # @param [Toys::Definition::Tool] _tool_definition The tool definition
66
- # to modify.
67
- # @param [Toys::Loader] _loader The loader that loaded this tool.
68
- #
69
- def config(_tool_definition, _loader)
70
- yield
71
- end
72
-
73
- ##
74
- # This method is called when the tool is run. It gives the middleware an
75
- # opportunity to modify the runtime behavior of the tool. It is passed
76
- # the tool instance (i.e. the object that hosts a tool's `run` method),
77
- # and you can use this object to access the tool's options and other
78
- # context data. In most cases, this method should also call `yield`,
79
- # which passes control to the next middleware in the stack. A middleware
80
- # can "wrap" normal execution by calling `yield` somewhere in its
81
- # implementation of this method, or it can completely replace the
82
- # execution behavior by not calling `yield` at all.
83
- #
84
- # Like a tool's `run` method, this method's return value is unused. If
85
- # you want to output from a tool, write to stdout or stderr. If you want
86
- # to set the exit status code, call {Toys::Tool#exit} on the tool object.
87
- #
88
- # The base middleware implementation does nothing and simply yields to
89
- # the next middleware. Subclasses should override this if they want to
90
- # alter the tool execution.
91
- #
92
- # @param [Toys::Tool] _tool The tool execution instance.
93
- #
94
- def run(_tool)
95
- yield
96
- end
97
- end
98
- end
99
- end
@@ -1,55 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- require "toys/utils/module_lookup"
31
-
32
- module Toys
33
- ##
34
- # Namespace for common templates
35
- #
36
- module Templates
37
- ##
38
- # Return a template class by name.
39
- #
40
- # Currently recognized template names are:
41
- #
42
- # * `:clean` : Creates a tool that cleans build artifacts.
43
- # * `:gem_build` : Creates a tool that builds and/or releases gems.
44
- # * `:minitest` : Creates a tool that runs unit tests.
45
- # * `:rubocop` : Creates a tool that runs rubocop.
46
- # * `:yardoc` : Creates a tool that generates YARD documentation.
47
- #
48
- # @param [String,Symbol] name Name of the template class to return
49
- # @return [Class,nil] The class, or `nil` if not found
50
- #
51
- def self.lookup!(name)
52
- Utils::ModuleLookup.lookup!(:templates, name)
53
- end
54
- end
55
- end
@@ -1,85 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- module Toys
31
- module Templates
32
- ##
33
- # A template for tools that clean build artifacts
34
- #
35
- class Clean
36
- include Template
37
-
38
- ##
39
- # Default tool name
40
- # @return [String]
41
- #
42
- DEFAULT_TOOL_NAME = "clean".freeze
43
-
44
- ##
45
- # Create the template settings for the Clean template.
46
- #
47
- # @param [String] name Name of the tool to create. Defaults to
48
- # {DEFAULT_TOOL_NAME}.
49
- # @param [Array<String>] paths An array of glob patterns indicating what
50
- # to clean.
51
- #
52
- def initialize(name: DEFAULT_TOOL_NAME, paths: [])
53
- @name = name
54
- @paths = paths
55
- end
56
-
57
- attr_accessor :name
58
- attr_accessor :paths
59
-
60
- to_expand do |template|
61
- tool(template.name) do
62
- desc "Clean built files and directories."
63
-
64
- include :fileutils
65
-
66
- run do
67
- files = []
68
- patterns = Array(template.paths)
69
- patterns.each do |pattern|
70
- files.concat(::Dir.glob(pattern))
71
- end
72
- files.uniq!
73
-
74
- files.each do |file|
75
- if ::File.exist?(file)
76
- rm_rf file
77
- puts "Cleaned: #{file}"
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end