toys 0.9.4 → 0.10.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,26 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Daniel Azuma
4
- #
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:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
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.
22
- ;
23
-
24
3
  module Toys
25
4
  module Templates
26
5
  ##
@@ -47,24 +26,114 @@ module Toys
47
26
  # @param use_flags [Boolean] Generated tools use flags instead of
48
27
  # positional arguments to pass arguments to rake tasks. Default is
49
28
  # false.
29
+ # @param bundler [Boolean,Hash] If `false` (the default), bundler is not
30
+ # enabled for Rake tools. If `true` or a Hash of options, bundler is
31
+ # enabled. See the documentation for the
32
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
33
+ # for information on available options.
50
34
  #
51
35
  def initialize(gem_version: nil,
52
36
  rakefile_path: nil,
53
37
  only_described: false,
54
- use_flags: false)
38
+ use_flags: false,
39
+ bundler: false)
55
40
  @gem_version = gem_version
56
- @rakefile_path = rakefile_path || DEFAULT_RAKEFILE_PATH
41
+ @rakefile_path = rakefile_path
57
42
  @only_described = only_described
58
43
  @use_flags = use_flags
44
+ @bundler = bundler
59
45
  end
60
46
 
61
- attr_accessor :gem_version
62
- attr_accessor :rakefile_path
63
- attr_accessor :only_described
64
- attr_accessor :use_flags
47
+ ##
48
+ # Version requirements for the minitest gem.
49
+ # If set to `nil`, has no version requirement (unless one is specified in
50
+ # the bundle.)
51
+ #
52
+ # @param value [String,Array<String>,nil]
53
+ # @return [String,Array<String>,nil]
54
+ #
55
+ attr_writer :gem_version
56
+
57
+ ##
58
+ # Path to the Rakefile.
59
+ # If set to `nil`, defaults to {DEFAULT_RAKEFILE_PATH}.
60
+ #
61
+ # @param value [String,nil]
62
+ # @return [String,nil]
63
+ #
64
+ attr_writer :rakefile_path
65
+
66
+ ##
67
+ # Whether to generate tools only for rake tasks with descriptions.
68
+ #
69
+ # @param value [Boolean]
70
+ # @return [Boolean]
71
+ #
72
+ attr_writer :only_described
73
+
74
+ ##
75
+ # Whether generated tools should use flags instead of positional
76
+ # arguments to pass arguments to rake tasks.
77
+ #
78
+ # @param value [Boolean]
79
+ # @return [Boolean]
80
+ #
81
+ attr_writer :use_flags
82
+
83
+ ##
84
+ # Set the bundler state and options for all Rake tools.
85
+ #
86
+ # Pass `false` to disable bundler. Pass `true` or a hash of options to
87
+ # enable bundler. See the documentation for the
88
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
89
+ # for information on the options that can be passed.
90
+ #
91
+ # @param value [Boolean,Hash]
92
+ # @return [Boolean,Hash]
93
+ #
94
+ attr_writer :bundler
95
+
96
+ ##
97
+ # Use bundler for all Rake tools.
98
+ #
99
+ # See the documentation for the
100
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
101
+ # for information on the options that can be passed.
102
+ #
103
+ # @param opts [keywords] Options for bundler
104
+ # @return [self]
105
+ #
106
+ def use_bundler(**opts)
107
+ @bundler = opts
108
+ self
109
+ end
110
+
111
+ # @private
112
+ attr_reader :only_described
113
+ # @private
114
+ attr_reader :use_flags
115
+
116
+ # @private
117
+ def gem_version
118
+ Array(@gem_version)
119
+ end
120
+
121
+ # @private
122
+ def rakefile_path
123
+ @rakefile_path || DEFAULT_RAKEFILE_PATH
124
+ end
125
+
126
+ # @private
127
+ def bundler_settings
128
+ if @bundler && !@bundler.is_a?(::Hash)
129
+ {}
130
+ else
131
+ @bundler
132
+ end
133
+ end
65
134
 
66
135
  on_expand do |template|
67
- gem "rake", *Array(template.gem_version)
136
+ gem "rake", *template.gem_version
68
137
  require "rake"
69
138
  rakefile_path = Templates::Rake.find_rakefile(template.rakefile_path, context_directory)
70
139
  raise "Cannot find #{template.rakefile_path}" unless rakefile_path
@@ -74,6 +143,8 @@ module Toys
74
143
  comments = task.full_comment.to_s.split("\n")
75
144
  next if comments.empty? && template.only_described
76
145
  tool(task.name.split(":"), if_defined: :ignore) do
146
+ bundler_settings = template.bundler_settings
147
+ include :bundler, **bundler_settings if bundler_settings
77
148
  unless comments.empty?
78
149
  desc(comments.first)
79
150
  comments << "" << "Defined as a Rake task in #{rakefile_path}"
@@ -1,26 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 Daniel Azuma
4
- #
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:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
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.
22
- ;
23
-
24
3
  module Toys
25
4
  module Templates
26
5
  ##
@@ -33,7 +12,7 @@ module Toys
33
12
  # Default version requirements for the rdoc gem.
34
13
  # @return [Array<String>]
35
14
  #
36
- DEFAULT_GEM_VERSION_REQUIREMENTS = ">= 5.0.0"
15
+ DEFAULT_GEM_VERSION_REQUIREMENTS = ["~> 6.1.0"].freeze
37
16
 
38
17
  ##
39
18
  # Default tool name
@@ -47,6 +26,12 @@ module Toys
47
26
  #
48
27
  DEFAULT_OUTPUT_DIR = "html"
49
28
 
29
+ ##
30
+ # Default file globs
31
+ # @return [Array<String>]
32
+ #
33
+ DEFAULT_FILES = ["lib/**/*.rb"].freeze
34
+
50
35
  ##
51
36
  # Create the template settings for the Rdoc template.
52
37
  #
@@ -55,53 +40,210 @@ module Toys
55
40
  # @param gem_version [String,Array<String>] Version requirements for
56
41
  # the rdoc gem. Defaults to {DEFAULT_GEM_VERSION_REQUIREMENTS}.
57
42
  # @param files [Array<String>] An array of globs indicating the files
58
- # to document.
43
+ # to document. Defaults to {DEFAULT_FILES}.
59
44
  # @param output_dir [String] Name of directory to receive html output
60
45
  # files. Defaults to {DEFAULT_OUTPUT_DIR}.
61
- # @param markup [String,nil] Markup format. Allowed values include
62
- # "rdoc", "rd", and "tomdoc". Default is "rdoc".
63
- # @param title [String,nil] Title of RDoc documentation. If `nil`, RDoc
64
- # will use a default title.
65
- # @param main [String,nil] Name of the file to use as the main top level
46
+ # @param markup [String] Markup format. Allowed values include "rdoc",
47
+ # "rd", and "tomdoc". If not specified, RDoc will use its default
48
+ # markup, which is "rdoc".
49
+ # @param title [String] Title of RDoc documentation. If not specified,
50
+ # RDoc will use a default title.
51
+ # @param main [String] Name of the file to use as the main top level
66
52
  # document. Default is none.
67
- # @param template [String,nil] Name of the template to use. If `nil`,
53
+ # @param template [String] Name of the template to use. If not specified,
68
54
  # RDoc will use its default template.
69
- # @param generator [String,nil] Name of the format generator. If `nil`,
70
- # RDoc will use its default generator.
55
+ # @param generator [String] Name of the format generator. If not
56
+ # specified, RDoc will use its default generator.
71
57
  # @param options [Array<String>] Additional options to pass to RDoc.
58
+ # @param bundler [Boolean,Hash] If `false` (the default), bundler is not
59
+ # enabled for this tool. If `true` or a Hash of options, bundler is
60
+ # enabled. See the documentation for the
61
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
62
+ # for information on available options.
72
63
  #
73
64
  def initialize(name: nil,
74
65
  gem_version: nil,
75
- files: [],
66
+ files: nil,
76
67
  output_dir: nil,
77
68
  markup: nil,
78
69
  title: nil,
79
70
  main: nil,
80
71
  template: nil,
81
72
  generator: nil,
82
- options: [])
83
- @name = name || DEFAULT_TOOL_NAME
84
- @gem_version = gem_version || DEFAULT_GEM_VERSION_REQUIREMENTS
73
+ options: [],
74
+ bundler: false)
75
+ @name = name
76
+ @gem_version = gem_version
85
77
  @files = files
86
- @output_dir = output_dir || DEFAULT_OUTPUT_DIR
78
+ @output_dir = output_dir
87
79
  @markup = markup
88
80
  @title = title
89
81
  @main = main
90
82
  @template = template
91
83
  @generator = generator
92
84
  @options = options
85
+ @bundler = bundler
93
86
  end
94
87
 
95
- attr_accessor :name
96
- attr_accessor :gem_version
97
- attr_accessor :files
98
- attr_accessor :output_dir
99
- attr_accessor :markup
100
- attr_accessor :title
101
- attr_accessor :main
102
- attr_accessor :template
103
- attr_accessor :generator
104
- attr_accessor :options
88
+ ##
89
+ # Name of the tool to create.
90
+ #
91
+ # @param value [String]
92
+ # @return [String]
93
+ #
94
+ attr_writer :name
95
+
96
+ ##
97
+ # Version requirements for the rdoc gem.
98
+ # If set to `nil`, uses the bundled version if bundler is enabled, or
99
+ # defaults to {DEFAULT_GEM_VERSION_REQUIREMENTS} if bundler is not
100
+ # enabled.
101
+ #
102
+ # @param value [String,Array<String>,nil]
103
+ # @return [String,Array<String>,nil]
104
+ #
105
+ attr_writer :gem_version
106
+
107
+ ##
108
+ # An array of globs indicating which files to document.
109
+ #
110
+ # @param value [Array<String>]
111
+ # @return [Array<String>]
112
+ #
113
+ attr_writer :files
114
+
115
+ ##
116
+ # Name of directory to receive html output files.
117
+ # If set to `nil`, defaults to {DEFAULT_OUTPUT_DIR}.
118
+ #
119
+ # @param value [String,nil]
120
+ # @return [String,nil]
121
+ #
122
+ attr_writer :output_dir
123
+
124
+ ##
125
+ # Markup format. Allowed values include "rdoc", "rd", and "tomdoc".
126
+ # If set to `nil`, RDoc will use its default markup, which is "rdoc".
127
+ #
128
+ # @param value [String,nil]
129
+ # @return [String,nil]
130
+ #
131
+ attr_writer :markup
132
+
133
+ ##
134
+ # Title of RDoc documentation pages.
135
+ # If set to `nil`, RDoc will use a default title.
136
+ #
137
+ # @param value [String,nil]
138
+ # @return [String,nil]
139
+ #
140
+ attr_writer :title
141
+
142
+ ##
143
+ # Name of the file to use as the main top level document, or `nil` for
144
+ # no top level document.
145
+ #
146
+ # @param value [String,nil]
147
+ # @return [String,nil]
148
+ #
149
+ attr_writer :main
150
+
151
+ ##
152
+ # Name of the template to use.
153
+ # If set to `nil`, RDoc will choose a default template.
154
+ #
155
+ # @param value [String,nil]
156
+ # @return [String,nil]
157
+ #
158
+ attr_writer :template
159
+
160
+ ##
161
+ # Name of the format generator.
162
+ # If set to `nil`, RDoc will use its default generator.
163
+ #
164
+ # @param value [String,nil]
165
+ # @return [String,nil]
166
+ #
167
+ attr_writer :generator
168
+
169
+ ##
170
+ # Additional options to pass to RDoc
171
+ #
172
+ # @param value [Array<String>]
173
+ # @return [Array<String>]
174
+ #
175
+ attr_writer :options
176
+
177
+ ##
178
+ # Set the bundler state and options for this tool.
179
+ #
180
+ # Pass `false` to disable bundler. Pass `true` or a hash of options to
181
+ # enable bundler. See the documentation for the
182
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
183
+ # for information on the options that can be passed.
184
+ #
185
+ # @param value [Boolean,Hash]
186
+ # @return [Boolean,Hash]
187
+ #
188
+ attr_writer :bundler
189
+
190
+ ##
191
+ # Use bundler for this tool.
192
+ #
193
+ # See the documentation for the
194
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
195
+ # for information on the options that can be passed.
196
+ #
197
+ # @param opts [keywords] Options for bundler
198
+ # @return [self]
199
+ #
200
+ def use_bundler(**opts)
201
+ @bundler = opts
202
+ self
203
+ end
204
+
205
+ # @private
206
+ attr_reader :markup
207
+ # @private
208
+ attr_reader :title
209
+ # @private
210
+ attr_reader :main
211
+ # @private
212
+ attr_reader :template
213
+ # @private
214
+ attr_reader :generator
215
+ # @private
216
+ attr_reader :options
217
+
218
+ # @private
219
+ def name
220
+ @name || DEFAULT_TOOL_NAME
221
+ end
222
+
223
+ # @private
224
+ def gem_version
225
+ return Array(@gem_version) if @gem_version
226
+ @bundler ? [] : DEFAULT_GEM_VERSION_REQUIREMENTS
227
+ end
228
+
229
+ # @private
230
+ def files
231
+ @files ? Array(@files) : DEFAULT_FILES
232
+ end
233
+
234
+ # @private
235
+ def output_dir
236
+ @output_dir || DEFAULT_OUTPUT_DIR
237
+ end
238
+
239
+ # @private
240
+ def bundler_settings
241
+ if @bundler && !@bundler.is_a?(::Hash)
242
+ {}
243
+ else
244
+ @bundler
245
+ end
246
+ end
105
247
 
106
248
  on_expand do |template|
107
249
  tool(template.name) do
@@ -110,15 +252,16 @@ module Toys
110
252
  include :exec, exit_on_nonzero_status: true
111
253
  include :gems
112
254
 
255
+ bundler_settings = template.bundler_settings
256
+ include :bundler, **bundler_settings if bundler_settings
257
+
113
258
  to_run do
114
259
  gem_requirements = Array(template.gem_version)
115
260
  gem "rdoc", *gem_requirements
116
261
 
117
262
  ::Dir.chdir(context_directory || ::Dir.getwd) do
118
263
  files = []
119
- patterns = Array(template.files)
120
- patterns = ["lib/**/*.rb"] if patterns.empty?
121
- patterns.each do |pattern|
264
+ template.files.each do |pattern|
122
265
  files.concat(::Dir.glob(pattern))
123
266
  end
124
267
  files.uniq!
@@ -134,7 +277,7 @@ module Toys
134
277
  exec_ruby([], in: :controller) do |controller|
135
278
  controller.in.puts("gem 'rdoc', *#{gem_requirements.inspect}")
136
279
  controller.in.puts("require 'rdoc'")
137
- controller.in.puts("::RDoc::RDoc.new.document(*#{(args + files).inspect})")
280
+ controller.in.puts("::RDoc::RDoc.new.document(#{(args + files).inspect})")
138
281
  end
139
282
  end
140
283
  end