toys 0.9.4 → 0.10.0

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
  ##
@@ -33,7 +12,7 @@ module Toys
33
12
  # Default version requirements for the rspec gem.
34
13
  # @return [Array<String>]
35
14
  #
36
- DEFAULT_GEM_VERSION_REQUIREMENTS = "~> 3.1"
15
+ DEFAULT_GEM_VERSION_REQUIREMENTS = ["~> 3.1"].freeze
37
16
 
38
17
  ##
39
18
  # Default tool name
@@ -53,6 +32,12 @@ module Toys
53
32
  #
54
33
  DEFAULT_ORDER = "defined"
55
34
 
35
+ ##
36
+ # Default format code
37
+ # @return [String]
38
+ #
39
+ DEFAULT_FORMAT = "p"
40
+
56
41
  ##
57
42
  # Default spec file glob
58
43
  # @return [String]
@@ -68,16 +53,21 @@ module Toys
68
53
  # the rspec gem. Defaults to {DEFAULT_GEM_VERSION_REQUIREMENTS}.
69
54
  # @param libs [Array<String>] An array of library paths to add to the
70
55
  # ruby require path. Defaults to {DEFAULT_LIBS}.
71
- # @param options [String] The path to a custom options file.
56
+ # @param options [String] The path to a custom options file, if any.
72
57
  # @param order [String] The order in which to run examples. Default is
73
58
  # {DEFAULT_ORDER}.
74
- # @param format [String] Choose a formatter code. Default is `p`.
59
+ # @param format [String] The formatter code. Default is {DEFAULT_FORMAT}.
75
60
  # @param out [String] Write output to a file instead of stdout.
76
61
  # @param backtrace [Boolean] Enable full backtrace (default is false).
77
62
  # @param pattern [String] A glob indicating the spec files to load.
78
63
  # Defaults to {DEFAULT_PATTERN}.
79
64
  # @param warnings [Boolean] If true, runs specs with Ruby warnings.
80
65
  # Defaults to true.
66
+ # @param bundler [Boolean,Hash] If `false` (the default), bundler is not
67
+ # enabled for this tool. If `true` or a Hash of options, bundler is
68
+ # enabled. See the documentation for the
69
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
70
+ # for information on available options.
81
71
  #
82
72
  def initialize(name: nil,
83
73
  gem_version: nil,
@@ -88,29 +78,185 @@ module Toys
88
78
  out: nil,
89
79
  backtrace: false,
90
80
  pattern: nil,
91
- warnings: true)
92
- @name = name || DEFAULT_TOOL_NAME
93
- @gem_version = gem_version || DEFAULT_GEM_VERSION_REQUIREMENTS
94
- @libs = libs || DEFAULT_LIBS
81
+ warnings: true,
82
+ bundler: false)
83
+ @name = name
84
+ @gem_version = gem_version
85
+ @libs = libs
95
86
  @options = options
96
- @order = order || DEFAULT_ORDER
97
- @format = format || "p"
87
+ @order = order
88
+ @format = format
98
89
  @out = out
99
90
  @backtrace = backtrace
100
- @pattern = pattern || DEFAULT_PATTERN
91
+ @pattern = pattern
101
92
  @warnings = warnings
93
+ @bundler = bundler
102
94
  end
103
95
 
104
- attr_accessor :name
105
- attr_accessor :gem_version
106
- attr_accessor :libs
107
- attr_accessor :options
108
- attr_accessor :order
109
- attr_accessor :format
110
- attr_accessor :out
111
- attr_accessor :backtrace
112
- attr_accessor :pattern
113
- attr_accessor :warnings
96
+ ##
97
+ # Name of the tool to create.
98
+ #
99
+ # @param value [String]
100
+ # @return [String]
101
+ #
102
+ attr_writer :name
103
+
104
+ ##
105
+ # Version requirements for the rspec gem.
106
+ # If set to `nil`, uses the bundled version if bundler is enabled, or
107
+ # defaults to {DEFAULT_GEM_VERSION_REQUIREMENTS} if bundler is not
108
+ # enabled.
109
+ #
110
+ # @param value [String,Array<String>,nil]
111
+ # @return [String,Array<String>,nil]
112
+ #
113
+ attr_writer :gem_version
114
+
115
+ ##
116
+ # An array of directories to add to the Ruby require path.
117
+ # If set to `nil`, defaults to {DEFAULT_LIBS}.
118
+ #
119
+ # @param value [Array<String>,nil]
120
+ # @return [Array<String>,nil]
121
+ #
122
+ attr_writer :libs
123
+
124
+ ##
125
+ # Path to the custom options file, or `nil` for none.
126
+ #
127
+ # @param value [String,nil]
128
+ # @return [String,nil]
129
+ #
130
+ attr_writer :options
131
+
132
+ ##
133
+ # The order in which to run examples.
134
+ # If set to `nil`, defaults to {DEFAULT_ORDER}.
135
+ #
136
+ # @param value [String,nil]
137
+ # @return [String,nil]
138
+ #
139
+ attr_writer :order
140
+
141
+ ##
142
+ # The formatter code.
143
+ # If set to `nil`, defaults to {DEFAULT_FORMAT}.
144
+ #
145
+ # @param value [String,nil]
146
+ # @return [String,nil]
147
+ #
148
+ attr_writer :format
149
+
150
+ ##
151
+ # Path to a file to write output to.
152
+ # If set to `nil`, writes output to standard out.
153
+ #
154
+ # @param value [String,nil]
155
+ # @return [String,nil]
156
+ #
157
+ attr_writer :out
158
+
159
+ ##
160
+ # Whether to enable full backtraces.
161
+ #
162
+ # @param value [Boolean]
163
+ # @return [Boolean]
164
+ #
165
+ attr_writer :backtrace
166
+
167
+ ##
168
+ # A glob indicating the spec files to load.
169
+ # If set to `nil`, defaults to {DEFAULT_PATTERN}.
170
+ #
171
+ # @param value [String,nil]
172
+ # @return [String,nil]
173
+ #
174
+ attr_writer :pattern
175
+
176
+ ##
177
+ # Whether to run with Ruby warnings.
178
+ #
179
+ # @param value [Boolean]
180
+ # @return [Boolean]
181
+ #
182
+ attr_writer :warnings
183
+
184
+ ##
185
+ # Set the bundler state and options for this tool.
186
+ #
187
+ # Pass `false` to disable bundler. Pass `true` or a hash of options to
188
+ # enable bundler. See the documentation for the
189
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
190
+ # for information on the options that can be passed.
191
+ #
192
+ # @param value [Boolean,Hash]
193
+ # @return [Boolean,Hash]
194
+ #
195
+ attr_writer :bundler
196
+
197
+ ##
198
+ # Activate bundler for this tool.
199
+ #
200
+ # See the documentation for the
201
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
202
+ # for information on the options that can be passed.
203
+ #
204
+ # @param opts [keywords] Options for bundler
205
+ # @return [self]
206
+ #
207
+ def use_bundler(**opts)
208
+ @bundler = opts
209
+ self
210
+ end
211
+
212
+ ## @private
213
+ attr_reader :options
214
+ ## @private
215
+ attr_reader :out
216
+ ## @private
217
+ attr_reader :backtrace
218
+ ## @private
219
+ attr_reader :warnings
220
+
221
+ # @private
222
+ def name
223
+ @name || DEFAULT_TOOL_NAME
224
+ end
225
+
226
+ # @private
227
+ def gem_version
228
+ return Array(@gem_version) if @gem_version
229
+ @bundler ? [] : DEFAULT_GEM_VERSION_REQUIREMENTS
230
+ end
231
+
232
+ # @private
233
+ def libs
234
+ @libs ? Array(@libs) : DEFAULT_LIBS
235
+ end
236
+
237
+ # @private
238
+ def order
239
+ @order || DEFAULT_ORDER
240
+ end
241
+
242
+ # @private
243
+ def format
244
+ @format || DEFAULT_FORMAT
245
+ end
246
+
247
+ # @private
248
+ def pattern
249
+ @pattern || DEFAULT_PATTERN
250
+ end
251
+
252
+ # @private
253
+ def bundler_settings
254
+ if @bundler && !@bundler.is_a?(::Hash)
255
+ {}
256
+ else
257
+ @bundler
258
+ end
259
+ end
114
260
 
115
261
  on_expand do |template|
116
262
  tool(template.name) do
@@ -119,6 +265,9 @@ module Toys
119
265
  include :exec
120
266
  include :gems
121
267
 
268
+ bundler_settings = template.bundler_settings
269
+ include :bundler, **bundler_settings if bundler_settings
270
+
122
271
  flag :order, "--order TYPE",
123
272
  default: template.order,
124
273
  desc: "Run examples by the specified order type (default: #{template.order})"
@@ -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
  ##
@@ -52,37 +31,133 @@ module Toys
52
31
  # Rubocop fails. Defaults to true.
53
32
  # @param options [Array<String>] Additional options passed to the Rubocop
54
33
  # CLI.
34
+ # @param bundler [Boolean,Hash] If `false` (the default), bundler is not
35
+ # enabled for this tool. If `true` or a Hash of options, bundler is
36
+ # enabled. See the documentation for the
37
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
38
+ # for information on available options.
55
39
  #
56
40
  def initialize(name: DEFAULT_TOOL_NAME,
57
41
  gem_version: nil,
58
42
  fail_on_error: true,
59
- options: [])
43
+ options: [],
44
+ bundler: false)
60
45
  @name = name
61
- @gem_version = gem_version || DEFAULT_GEM_VERSION_REQUIREMENTS
46
+ @gem_version = gem_version
62
47
  @fail_on_error = fail_on_error
63
48
  @options = options
49
+ @bundler = bundler
64
50
  end
65
51
 
66
- attr_accessor :name
67
- attr_accessor :gem_version
68
- attr_accessor :fail_on_error
69
- attr_accessor :options
52
+ ##
53
+ # Name of the tool to create.
54
+ #
55
+ # @param value [String]
56
+ # @return [String]
57
+ #
58
+ attr_writer :name
59
+
60
+ ##
61
+ # Version requirements for the rdoc gem.
62
+ # If set to `nil`, uses the bundled version if bundler is enabled, or
63
+ # defaults to {DEFAULT_GEM_VERSION_REQUIREMENTS} if bundler is not
64
+ # enabled.
65
+ #
66
+ # @param value [String,Array<String>,nil]
67
+ # @return [String,Array<String>,nil]
68
+ #
69
+ attr_writer :gem_version
70
+
71
+ ##
72
+ # Whether to exit with a nonzero code if Rubocop fails.
73
+ #
74
+ # @param value [Boolean]
75
+ # @return [Boolean]
76
+ #
77
+ attr_writer :fail_on_error
78
+
79
+ ##
80
+ # Additional options to pass to Rubocop
81
+ #
82
+ # @param value [Array<String>]
83
+ # @return [Array<String>]
84
+ #
85
+ attr_writer :options
86
+
87
+ ##
88
+ # Set the bundler state and options for this tool.
89
+ #
90
+ # Pass `false` to disable bundler. Pass `true` or a hash of options to
91
+ # enable bundler. See the documentation for the
92
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
93
+ # for information on the options that can be passed.
94
+ #
95
+ # @param value [Boolean,Hash]
96
+ # @return [Boolean,Hash]
97
+ #
98
+ attr_writer :bundler
99
+
100
+ ##
101
+ # Activate bundler for this tool.
102
+ #
103
+ # See the documentation for the
104
+ # [bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
105
+ # for information on the options that can be passed.
106
+ #
107
+ # @param opts [keywords] Options for bundler
108
+ # @return [self]
109
+ #
110
+ def use_bundler(**opts)
111
+ @bundler = opts
112
+ self
113
+ end
114
+
115
+ ## @private
116
+ attr_reader :fail_on_error
117
+ ## @private
118
+ attr_reader :options
119
+
120
+ # @private
121
+ def name
122
+ @name || DEFAULT_TOOL_NAME
123
+ end
124
+
125
+ # @private
126
+ def gem_version
127
+ return Array(@gem_version) if @gem_version
128
+ @bundler ? [] : DEFAULT_GEM_VERSION_REQUIREMENTS
129
+ end
130
+
131
+ # @private
132
+ def bundler_settings
133
+ if @bundler && !@bundler.is_a?(::Hash)
134
+ {}
135
+ else
136
+ @bundler
137
+ end
138
+ end
70
139
 
71
140
  on_expand do |template|
72
141
  tool(template.name) do
73
142
  desc "Run rubocop on the current project."
74
143
 
75
144
  include :gems
145
+ include :exec
146
+
147
+ bundler_settings = template.bundler_settings
148
+ include :bundler, **bundler_settings if bundler_settings
76
149
 
77
150
  to_run do
78
- gem "rubocop", *Array(template.gem_version)
79
- require "rubocop"
151
+ gem "rubocop", *template.gem_version
80
152
 
81
153
  ::Dir.chdir(context_directory || ::Dir.getwd) do
82
- rubocop = ::RuboCop::CLI.new
83
154
  logger.info "Running RuboCop..."
84
- result = rubocop.run(template.options)
85
- if result.nonzero?
155
+ result = exec_ruby([], in: :controller) do |controller|
156
+ controller.in.puts("gem 'rubocop', *#{template.gem_version.inspect}")
157
+ controller.in.puts("require 'rubocop'")
158
+ controller.in.puts("exit(::RuboCop::CLI.new.run(#{template.options.inspect}))")
159
+ end
160
+ if result.error?
86
161
  logger.error "RuboCop failed!"
87
162
  exit(1) if template.fail_on_error
88
163
  end