api_recipes 3.0.0.beta4 → 3.0.0.beta6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +340 -107
- data/UPGRADING.md +197 -49
- data/lib/api_recipes/cli/calls.rb +27 -0
- data/lib/api_recipes/cli/convert/migration.rb +201 -6
- data/lib/api_recipes/cli/convert.rb +130 -23
- data/lib/api_recipes/cli/description.rb +65 -0
- data/lib/api_recipes/cli/import/server.rb +20 -9
- data/lib/api_recipes/cli/import/writer.rb +16 -4
- data/lib/api_recipes/cli/import.rb +23 -9
- data/lib/api_recipes/cli/recipes/summary.rb +7 -5
- data/lib/api_recipes/cli/recipes.rb +5 -2
- data/lib/api_recipes/cli/routes.rb +62 -7
- data/lib/api_recipes/cli/tags.rb +19 -0
- data/lib/api_recipes/cli/upgrade/fix.rb +206 -0
- data/lib/api_recipes/cli/upgrade/screen.rb +165 -0
- data/lib/api_recipes/cli/upgrade/tree_rules.rb +290 -0
- data/lib/api_recipes/cli/upgrade.rb +385 -63
- data/lib/api_recipes/cli.rb +84 -17
- data/lib/api_recipes/configuration.rb +17 -108
- data/lib/api_recipes/errors.rb +20 -3
- data/lib/api_recipes/internals/compiler.rb +77 -70
- data/lib/api_recipes/internals/context.rb +7 -2
- data/lib/api_recipes/internals/declaration.rb +17 -15
- data/lib/api_recipes/internals/definition.rb +15 -2
- data/lib/api_recipes/internals/level.rb +6 -1
- data/lib/api_recipes/internals/origins.rb +70 -0
- data/lib/api_recipes/internals/path_template.rb +23 -6
- data/lib/api_recipes/internals/recipe.rb +31 -5
- data/lib/api_recipes/internals/recipe_lock.rb +12 -9
- data/lib/api_recipes/internals/registry.rb +21 -10
- data/lib/api_recipes/internals/request.rb +2 -2
- data/lib/api_recipes/internals/template.rb +32 -2
- data/lib/api_recipes/response.rb +125 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +8 -1
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
require 'securerandom'
|
|
4
4
|
require 'api_recipes/cli/upgrade/project'
|
|
5
|
+
require 'api_recipes/cli/upgrade/screen'
|
|
6
|
+
require 'api_recipes/cli/upgrade/fix'
|
|
7
|
+
require 'api_recipes/cli/upgrade/tree_rules'
|
|
5
8
|
|
|
6
9
|
module ApiRecipes
|
|
7
10
|
module CLI
|
|
@@ -34,6 +37,9 @@ module ApiRecipes
|
|
|
34
37
|
'ApiRecipes::Settings was removed: the defaults are listed in the README'),
|
|
35
38
|
Rule.new(:breaks, /\bconfiguration\.setup\b/,
|
|
36
39
|
'configuration.setup was removed: configure loads the configuration'),
|
|
40
|
+
Rule.new(:breaks, /\.apis_files_paths\b/,
|
|
41
|
+
'apis_files_paths was removed: an api is described by a portable recipe, loaded with recipes_paths. ' \
|
|
42
|
+
'Run `api_recipes convert`: it moves every description of the project to a recipe and changes this configuration'),
|
|
37
43
|
Rule.new(:breaks, /ApiRecipes\.print_urls/, 'use config.print_urls inside ApiRecipes.configure', false, :print_urls),
|
|
38
44
|
Rule.new(:breaks, /_aprcps_|set_authorization_for_api|set_basic_auth_for_api|copy_global_authorizations_to_api/,
|
|
39
45
|
'internal method removed'),
|
|
@@ -59,6 +65,17 @@ module ApiRecipes
|
|
|
59
65
|
# The rule about the Hash methods that ActiveSupport defines too.
|
|
60
66
|
HASH_METHODS = RULES.last
|
|
61
67
|
|
|
68
|
+
# The rules on lines about a credential assigned with a writer.
|
|
69
|
+
WRITER_RULES = RULES.select { |rule| %i[global_writer writer].include?(rule.fix) }.freeze
|
|
70
|
+
|
|
71
|
+
# The rule about a rescue that names no class, and what the change
|
|
72
|
+
# that the command offers for it means.
|
|
73
|
+
BARE_RESCUE = RULES.find { |rule| rule.message.start_with?('a bare rescue') }
|
|
74
|
+
RESCUE_NOTE = ['In 2.x this rescue did not catch the errors of api_recipes: they went on to the code that called. ' \
|
|
75
|
+
'In 3.0 it catches them.',
|
|
76
|
+
'y: keep what 2.x did. The errors of api_recipes are raised again before this rescue sees them',
|
|
77
|
+
'n: leave the code as it is. This rescue handles the errors of api_recipes too'].freeze
|
|
78
|
+
|
|
62
79
|
# What stands for the value of an ERB tag in a file that is checked
|
|
63
80
|
# without running its code.
|
|
64
81
|
# Options whose value is a mapping: a tag that gives one gives a part
|
|
@@ -76,39 +93,23 @@ module ApiRecipes
|
|
|
76
93
|
}.freeze
|
|
77
94
|
# Code longer than this is not copied into the code to paste.
|
|
78
95
|
MAX_CODE = 300
|
|
79
|
-
ERB_COMMENT = /<%#.*?%>/m
|
|
80
|
-
ERB_OUTPUT = /<%=.*?%>/m
|
|
81
96
|
|
|
82
97
|
SILENT_CHANGES = [
|
|
83
|
-
'path
|
|
84
|
-
'a / inside a path parameter to cover several segments: use *name or two placeholders',
|
|
98
|
+
'a value of a path parameter that is already encoded when it arrives: %2F would become %252F. The gem warns when it sees one',
|
|
99
|
+
'a / inside the value of a path parameter to cover several segments: use *name or two placeholders. The gem warns when it sees one',
|
|
85
100
|
'debug logs or print_urls output read from STDOUT: set log_to and log_level',
|
|
86
|
-
'a bare rescue
|
|
87
|
-
'credentials assigned on an instance: they now take effect',
|
|
88
|
-
'a credential assigned through a variable that holds the api of the module or of a class (api = ApiRecipes.github, then api.authorization = ...), inside the body of a class (github.authorization = ...), with send, or on a statement split over two lines: the writer raises, give it to config.values or to `api :name, authorization: ...`',
|
|
101
|
+
'a bare rescue in a file that does not name the gem, around code that calls it somewhere else: it now catches the errors of the gem too',
|
|
89
102
|
'the response body read from the message of a verify_with error: read it from error.response',
|
|
90
|
-
'a block passed to run whose return value is used: it is now the Response',
|
|
91
|
-
'apis_configs assigned after the files to discard them: do not assign the files',
|
|
92
|
-
'timeout, on_bad_code, mime_type or verify_with on an endpoint that contains other endpoints: one that does not set the option now finds it there, before the api',
|
|
93
|
-
'an endpoint called with, definition, authorization, basic_auth or variables (on an api), or run, url, with or definition (on an endpoint): reach it with []; the warning at configuration reports it',
|
|
94
103
|
'fill with private setters, or code that relies on swallowed errors: make the setters public, handle the errors',
|
|
95
|
-
'
|
|
96
|
-
'a
|
|
104
|
+
'a rescue of HTTP::Error or of JSON::ParserError around response.data, around response.parse, around fill, or ' \
|
|
105
|
+
'around a run whose verify_with reads data: an empty body now gives nil, and a body that cannot be read raises ApiRecipes::UnreadableResponse',
|
|
97
106
|
'cookies set by a server during a redirect chain: they are no longer carried to the next hop',
|
|
98
|
-
'api names that are methods of Kernel, such as :open or :format: they raise ApiNameClashError, use as:',
|
|
99
107
|
'Endpoint#name, #params, #children, #api and Api#name, #configs, #object were removed: use definition (a search cannot tell them from other methods called like that)',
|
|
100
108
|
'an object restored from a serialized form (Marshal, YAML) has no credential: assign it again',
|
|
101
109
|
'response.uri leaves out the credential query parameter when the api declares auth type query',
|
|
102
110
|
'a redirect to a host with non-ASCII characters needs the addressable gem: without it the request raises InvalidUrl',
|
|
103
|
-
'a
|
|
104
|
-
'
|
|
105
|
-
'response.to_s returns the body, as the http.rb response does: it was the default text of the object',
|
|
106
|
-
'a credential that is not a String, such as a Symbol or a number: it raises InvalidCredential, convert it with to_s',
|
|
107
|
-
'a {word} in a value of default_headers in a configuration given as a Hash: it is now a variable and must be declared',
|
|
108
|
-
'a {word} in the path of an endpoint in a configuration given as a Hash: it is now a variable and must be declared; to keep the braces as text write \\{word}',
|
|
109
|
-
'respond_to? on a response now tells the truth: it answers true for the methods the response forwards and false for the others',
|
|
110
|
-
'serializing an Api, an Endpoint or a Response raises TypeError: serialize response.data or response.body.to_s',
|
|
111
|
-
'a class that includes ApiRecipes and uses a top-level constant named Error, Internals, VERSION or like a new error class of the gem (such as ConfigurationError or InvalidUrl): it now finds the gem\'s constant first, write ::Name in that class; Route and Settings no longer exist, so a top-level Route or Settings is found again'
|
|
111
|
+
'a response that a method receives as an argument: request was removed (read uri or original_response), to_s returns the body, ' \
|
|
112
|
+
'respond_to? tells the truth. Those that the code takes from run are found'
|
|
112
113
|
].freeze
|
|
113
114
|
|
|
114
115
|
# Problems met while reading, as "<file>: <reason>". They are not findings:
|
|
@@ -145,30 +146,43 @@ module ApiRecipes
|
|
|
145
146
|
@uses = 0
|
|
146
147
|
@startup = []
|
|
147
148
|
@configure = {}
|
|
149
|
+
@direct = {}
|
|
150
|
+
@rescues = []
|
|
148
151
|
findings = with_request_hints(@paths.flat_map { |path| scan_path(path) })
|
|
149
152
|
findings += @configs.flat_map { |file| check_config(file) }
|
|
150
153
|
@notes = scope + @notes if @project
|
|
151
154
|
findings
|
|
152
155
|
end
|
|
153
156
|
|
|
154
|
-
# Prints the results on out and the warnings on err. Returns the exit
|
|
155
|
-
|
|
157
|
+
# Prints the results on out and the warnings on err. Returns the exit
|
|
158
|
+
# code. What needs an answer comes last, where a terminal stops: what
|
|
159
|
+
# was checked, what the command cannot find, the findings, what to do.
|
|
160
|
+
# input: where the answers come from. On a terminal the command offers
|
|
161
|
+
# the changes it can make by itself, one at a time.
|
|
162
|
+
def report(out, err, input: nil)
|
|
156
163
|
findings = run
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
end
|
|
164
|
+
screen = Screen.new(out)
|
|
165
|
+
changed = @code && Fix.asks?(input, out) ? offered(screen, input, out, err) : []
|
|
166
|
+
findings = look_again unless changed.empty?
|
|
161
167
|
unless @notes.empty?
|
|
162
|
-
|
|
163
|
-
@notes.each { |note|
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
screen.section('🔎', 'What was checked', plain: false)
|
|
169
|
+
@notes.each { |note| screen.line(note) }
|
|
170
|
+
end
|
|
171
|
+
screen.section('🙈', 'What this command cannot find', "#{SILENT_CHANGES.size} changes")
|
|
172
|
+
SILENT_CHANGES.each { |change| screen.item(change) }
|
|
173
|
+
unless findings.empty?
|
|
174
|
+
screen.section('📌', 'Findings', plain: false)
|
|
175
|
+
findings.each { |finding| screen.finding(finding.kind, where(finding), finding.message, @code ? finding.hint : nil) }
|
|
176
|
+
end
|
|
177
|
+
pasted = @code && code_to_paste(screen)
|
|
178
|
+
unless changed.empty?
|
|
179
|
+
screen.section('✅', 'Changed in the project')
|
|
180
|
+
changed.each { |file, what| screen.item("#{what} #{shown_path(file)}") }
|
|
181
|
+
screen.line('Every change is in `git diff`.')
|
|
182
|
+
end
|
|
170
183
|
@warnings.each { |warning| err.puts warning }
|
|
171
|
-
|
|
184
|
+
screen.summary(summary(findings.size), findings.group_by(&:kind).transform_values(&:size)) if findings.any? || @warnings.empty?
|
|
185
|
+
what_to_do(screen, findings, pasted)
|
|
172
186
|
return 1 if findings.any?
|
|
173
187
|
|
|
174
188
|
@warnings.empty? ? 0 : 2
|
|
@@ -176,6 +190,285 @@ module ApiRecipes
|
|
|
176
190
|
|
|
177
191
|
private
|
|
178
192
|
|
|
193
|
+
CONVERT_NOTE = ['3.0 loads only portable recipes. The description becomes one, and what its ERB tags gave is given by ' \
|
|
194
|
+
'config.values, in the file that calls ApiRecipes.configure.',
|
|
195
|
+
'y: move the api to a recipe now',
|
|
196
|
+
'n: leave it, and run `api_recipes convert` later'].freeze
|
|
197
|
+
|
|
198
|
+
# Offers each change the command can make: first the apis that move to
|
|
199
|
+
# a portable recipe, then the others. The project is read again after
|
|
200
|
+
# a change that was made. Returns the files that were written.
|
|
201
|
+
def offered(screen, input, out, err)
|
|
202
|
+
ours = {}
|
|
203
|
+
fix = Fix.new(screen, input, shown: method(:shown_path), ours: ours)
|
|
204
|
+
declined = []
|
|
205
|
+
written = {}
|
|
206
|
+
loop do
|
|
207
|
+
# Only the whole project is moved to recipes: given some files, the
|
|
208
|
+
# command does not know the others.
|
|
209
|
+
key, title, migration = (@project ? conversions(ours, out, err) : []).find { |one| !declined.include?(one.first) }
|
|
210
|
+
if key
|
|
211
|
+
declined << key
|
|
212
|
+
preview = migration.preview
|
|
213
|
+
next unless fix.confirm(title, CONVERT_NOTE, preview) && migration.finish.zero?
|
|
214
|
+
|
|
215
|
+
preview[:created].each_key { |file| written[file] = 'created' }
|
|
216
|
+
preview[:edited].each_key { |file| written[file] ||= 'edited' }
|
|
217
|
+
preview[:deleted].each { |file| written[file] = 'deleted' }
|
|
218
|
+
look_again
|
|
219
|
+
next
|
|
220
|
+
end
|
|
221
|
+
change = changes.find { |one| !declined.include?(one.key) }
|
|
222
|
+
break unless change
|
|
223
|
+
|
|
224
|
+
files = fix.offer(change)
|
|
225
|
+
declined << change.key
|
|
226
|
+
next unless files
|
|
227
|
+
|
|
228
|
+
files.each { |file| written[file] ||= 'edited' }
|
|
229
|
+
look_again
|
|
230
|
+
end
|
|
231
|
+
written
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# The project as the changes left it: its files are found again.
|
|
235
|
+
def look_again
|
|
236
|
+
if @project
|
|
237
|
+
@project = Project.new
|
|
238
|
+
@paths = @project.ruby_files
|
|
239
|
+
@configs = @project.config_files
|
|
240
|
+
@specific = @all = nil
|
|
241
|
+
end
|
|
242
|
+
run
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# [[key, title, migration]] for each api that a file of the project
|
|
246
|
+
# describes and that the command can move to a portable recipe by
|
|
247
|
+
# itself. The others stay findings: `api_recipes convert` says why.
|
|
248
|
+
def conversions(ours, out, err)
|
|
249
|
+
described = @configs.reject { |file| file.end_with?(Internals::Recipe::SUFFIX) }
|
|
250
|
+
described.sort.flat_map do |file|
|
|
251
|
+
names = begin
|
|
252
|
+
Convert.shape(File.read(file)).keys
|
|
253
|
+
rescue Convert::Refused, Psych::Exception, SystemCallError
|
|
254
|
+
[]
|
|
255
|
+
end
|
|
256
|
+
names.filter_map do |name|
|
|
257
|
+
result = Convert.new(file, name: names.size > 1 ? name : nil).call
|
|
258
|
+
migration = Convert::Migration.new(result, file, out: out, err: err, written: ours)
|
|
259
|
+
next unless migration.prepare.empty?
|
|
260
|
+
|
|
261
|
+
[[:convert, file, result.name], "Move the api #{Recipes::Summary.shown(result.name, 100)} to a portable recipe", migration]
|
|
262
|
+
rescue Convert::Refused, Convert::Unusable, SystemStackError
|
|
263
|
+
nil
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# The changes this command is sure of, in the order they are offered.
|
|
269
|
+
# The rescues of a file come from the last one, so that a change does
|
|
270
|
+
# not move the ones still to offer.
|
|
271
|
+
def changes
|
|
272
|
+
startup_changes + @rescues.sort_by { |item| [item[:file], -item[:line]] }.filter_map { |item| rescue_change(item) }
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
# A value that a line gives when the application starts moves into the
|
|
276
|
+
# block of ApiRecipes.configure, when one file has it and it is read whole.
|
|
277
|
+
def startup_changes
|
|
278
|
+
file, text = @configure.size == 1 ? @configure.first : nil
|
|
279
|
+
block = Code::ConfigureBlock.in(text)
|
|
280
|
+
return [] unless block && !block.problem
|
|
281
|
+
|
|
282
|
+
urls = @startup.select { |item| item[:api].nil? }
|
|
283
|
+
values = @startup.select { |item| item[:api] }
|
|
284
|
+
found = [without_files(file, text, block)]
|
|
285
|
+
if urls.size == 1 && !block.assigned_anywhere?(:print_urls)
|
|
286
|
+
found << change(:print_urls, 'Move print_urls into the block of ApiRecipes.configure', urls, file, text, block,
|
|
287
|
+
["#{block.config}.print_urls = #{urls.first[:code]}"])
|
|
288
|
+
end
|
|
289
|
+
# Two credentials for one api, or one given twice: configure would
|
|
290
|
+
# refuse the first, and which one 2.x kept depends on the order.
|
|
291
|
+
twice = values.group_by { |item| item[:api] }.any? { |_, list| list.size > 1 }
|
|
292
|
+
unless values.empty? || twice || block.assigned_anywhere?(:values)
|
|
293
|
+
found << change(:values, 'Give the credentials of the application to ApiRecipes.configure, as config.values', values, file, text,
|
|
294
|
+
block, ["#{block.config}.values = {", *entries_of(values).map { |entry| " #{entry}" }, '}'])
|
|
295
|
+
end
|
|
296
|
+
found.compact
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# A rescue that names no class gets, before it, a rescue of the errors
|
|
300
|
+
# of the gem that raises them again: what 2.x did. nil for a rescue
|
|
301
|
+
# that does not start a line of its own, as in `x rescue y`.
|
|
302
|
+
def rescue_change(item)
|
|
303
|
+
text = read_ruby(item[:file])
|
|
304
|
+
tree = text && Code.tree(text)
|
|
305
|
+
clause = tree && rescue_at(tree, item[:line])&.last
|
|
306
|
+
line = text && text.lines[item[:line] - 1]
|
|
307
|
+
return nil unless clause && line.match?(/\A[ \t]*rescue\b/) && names_gem?(text)
|
|
308
|
+
return nil unless clause.exceptions.empty? || clause.exceptions.any? { |one| one.slice.delete_prefix('::') == 'StandardError' }
|
|
309
|
+
|
|
310
|
+
indent = line[/\A[ \t]*/]
|
|
311
|
+
written = text.lines.insert(item[:line] - 1, "#{indent}rescue ApiRecipes::Error\n", "#{indent} raise\n").join
|
|
312
|
+
return nil unless gem_errors_handled?(written, item[:line] + 2)
|
|
313
|
+
|
|
314
|
+
# Counted from the end of the file, the place of a rescue stays the
|
|
315
|
+
# same when a change is made above it.
|
|
316
|
+
Fix::Change.new([:rescue, item[:file], text.lines.size - item[:line]],
|
|
317
|
+
"Keep the errors of ApiRecipes out of the rescue at #{shown_path(item[:file])}:#{item[:line]}, as in 2.x",
|
|
318
|
+
{ item[:file] => written }, RESCUE_NOTE)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
# The rescue clause that starts at line number, after the clauses of
|
|
322
|
+
# the same begin that come before it: [[earlier clauses], clause].
|
|
323
|
+
def rescue_at(tree, number)
|
|
324
|
+
Code.nodes(tree) { |node| node.is_a?(Prism::BeginNode) && node.rescue_clause }.each do |owner|
|
|
325
|
+
earlier = []
|
|
326
|
+
clause = owner.rescue_clause
|
|
327
|
+
while clause
|
|
328
|
+
return [earlier, clause] if clause.location.start_line == number
|
|
329
|
+
|
|
330
|
+
earlier << clause
|
|
331
|
+
clause = clause.compact_child_nodes.find { |child| child.is_a?(Prism::RescueNode) }
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
nil
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Whether a clause before the one at line number catches the errors of
|
|
338
|
+
# the gem first: one that names ApiRecipes::Error, Exception or
|
|
339
|
+
# StandardError, or names no class.
|
|
340
|
+
def gem_errors_handled?(text, number)
|
|
341
|
+
tree = Code.tree(text)
|
|
342
|
+
earlier = tree && rescue_at(tree, number)&.first
|
|
343
|
+
return false unless earlier
|
|
344
|
+
|
|
345
|
+
earlier.any? do |clause|
|
|
346
|
+
clause.exceptions.empty? || clause.exceptions.any? { |one| %w[ApiRecipes::Error Exception StandardError].include?(one.slice.delete_prefix('::')) }
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
# Whether the code of a file names the gem: a change is made only there.
|
|
351
|
+
def names_gem?(text)
|
|
352
|
+
return @project.uses_gem?(text) if @project
|
|
353
|
+
|
|
354
|
+
text.each_line.reject { |line| line.lstrip.start_with?('#') }.join.match?(/\bApiRecipes\b/)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# apis_files_paths goes out of the block of configure once no file of
|
|
358
|
+
# the project describes an api: 3.0 refuses the option.
|
|
359
|
+
def without_files(file, text, block)
|
|
360
|
+
given = block.assignments(:apis_files_paths)
|
|
361
|
+
# Only in the whole project the command knows that no description is left.
|
|
362
|
+
return nil if !@project || given.empty? || @configs.any? { |one| !one.end_with?(Internals::Recipe::SUFFIX) }
|
|
363
|
+
# The lines of a heredoc come after the statement that opens it.
|
|
364
|
+
return nil if given.any? { |node| !Code.nodes(node) { |one| one.respond_to?(:opening_loc) && one.opening_loc&.slice&.start_with?('<<') }.empty? }
|
|
365
|
+
|
|
366
|
+
lines = text.lines
|
|
367
|
+
gone = given.flat_map do |node|
|
|
368
|
+
first = node.location.start_line
|
|
369
|
+
last = node.location.end_line
|
|
370
|
+
alone = lines[first - 1].b[0...node.location.start_column].strip.empty? && lines[last - 1].b[node.location.end_column..].to_s.strip.empty?
|
|
371
|
+
return nil unless alone
|
|
372
|
+
|
|
373
|
+
(first..last).to_a
|
|
374
|
+
end
|
|
375
|
+
written = lines.each_with_index.reject { |_, index| gone.include?(index + 1) }.map(&:first).join
|
|
376
|
+
Code.tree(written) ? Fix::Change.new(:files, 'Take apis_files_paths, which 3.0 refuses, out of the block of ApiRecipes.configure', { file => written }) : nil
|
|
377
|
+
end
|
|
378
|
+
|
|
379
|
+
# The texts of the files after the lines of items are taken out and
|
|
380
|
+
# added is written at the end of the block. nil when a line is not a
|
|
381
|
+
# whole statement of its own, or when a result is not valid Ruby.
|
|
382
|
+
def change(key, title, items, file, text, block, added)
|
|
383
|
+
texts = items.group_by { |item| item[:file] }.to_h { |path, list| [path, [path == file ? text : read_ruby(path), list.map { |item| item[:line] }]] }
|
|
384
|
+
texts[file] ||= [text, []]
|
|
385
|
+
return nil unless texts.all? { |path, (content, lines)| content && lines.all? { |number| movable?(content, number, path == file ? block : nil, lines) } }
|
|
386
|
+
|
|
387
|
+
written = texts.to_h do |path, (content, lines)|
|
|
388
|
+
kept = content.lines.each_with_index.reject { |_, index| lines.include?(index + 1) }
|
|
389
|
+
next [path, kept.map(&:first).join] unless path == file
|
|
390
|
+
|
|
391
|
+
inside = lines.select { |number| number.between?(block.first, block.last) }
|
|
392
|
+
now = block.lines(added, without: inside).map { |line| "#{line}\n" }
|
|
393
|
+
[path, kept.flat_map { |line, index| index + 1 == block.first ? now : (index + 1).between?(block.first, block.last) ? [] : [line] }.join]
|
|
394
|
+
end
|
|
395
|
+
written.values.all? { |content| Code.tree(content) } ? Fix::Change.new(key, title, written) : nil
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
MOVED = %i[print_urls= authorization= basic_auth=].freeze
|
|
399
|
+
|
|
400
|
+
# Whether line number of text holds one assignment of a value given
|
|
401
|
+
# when the application starts, and nothing else, whose value the block
|
|
402
|
+
# of configure would give the same: it reads no variable and no method
|
|
403
|
+
# of the file, and no other code that may prepare it
|
|
404
|
+
# runs before it: in the file of the block, none between the block and
|
|
405
|
+
# the line; in another file, which runs at another time, none at all.
|
|
406
|
+
# moved: the lines of the file that the change takes out.
|
|
407
|
+
def movable?(text, number, block, moved)
|
|
408
|
+
tree = Code.tree(text)
|
|
409
|
+
return false unless tree
|
|
410
|
+
|
|
411
|
+
statements = Code.nodes(tree) { |node| node.is_a?(Prism::StatementsNode) }.flat_map(&:body)
|
|
412
|
+
here = statements.select { |node| node.location.start_line == number && node.location.end_line == number }
|
|
413
|
+
return false unless here.size == 1 && text.lines[number - 1].strip == here.first.slice
|
|
414
|
+
|
|
415
|
+
value = assigned(here.first)
|
|
416
|
+
return false unless value && Code.nodes(value) { |one| local_value?(one) }.empty?
|
|
417
|
+
|
|
418
|
+
others = tree.statements.body.reject { |other| moved.include?(other.location.start_line) }
|
|
419
|
+
return others.empty? unless block
|
|
420
|
+
return true unless number > block.last
|
|
421
|
+
|
|
422
|
+
others.none? { |other| other.location.start_line > block.last && other.location.start_line < number }
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# The value that node gives to one of the options moved, or nil.
|
|
426
|
+
def assigned(node)
|
|
427
|
+
if node.is_a?(Prism::CallNode) && MOVED.include?(node.name) && node.arguments&.arguments&.size == 1
|
|
428
|
+
node.arguments.arguments.first
|
|
429
|
+
elsif node.is_a?(Prism::CallOrWriteNode) && MOVED.include?(node.write_name)
|
|
430
|
+
node.value
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# A value that only the place where it is written has.
|
|
435
|
+
def local_value?(node)
|
|
436
|
+
case node
|
|
437
|
+
when Prism::LocalVariableReadNode, Prism::InstanceVariableReadNode, Prism::ClassVariableReadNode, Prism::SelfNode,
|
|
438
|
+
Prism::SourceFileNode, Prism::SourceLineNode then true
|
|
439
|
+
when Prism::CallNode then node.receiver.nil? && !%i[[] !].include?(node.name)
|
|
440
|
+
else false
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
def entries_of(items)
|
|
445
|
+
entries = items.group_by { |item| item[:api] }.map do |api, list|
|
|
446
|
+
"#{api.match?(/\A[a-z_]\w*\z/) ? api : "\"#{api}\""}: { #{list.map { |item| "#{item[:key]}: #{item[:code]}" }.join(', ')} }"
|
|
447
|
+
end
|
|
448
|
+
entries.each_with_index.map { |entry, index| index == entries.size - 1 ? entry : "#{entry}," }
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# What the person does with the results, in order.
|
|
452
|
+
def what_to_do(screen, findings, pasted)
|
|
453
|
+
counts = findings.group_by(&:kind).transform_values(&:size)
|
|
454
|
+
lines = ->(count) { "#{count} line#{'s' unless count == 1}" }
|
|
455
|
+
steps = []
|
|
456
|
+
described = @configs.reject { |file| file.end_with?(Internals::Recipe::SUFFIX) }
|
|
457
|
+
unless described.empty?
|
|
458
|
+
steps << "Run `api_recipes convert`: it moves the #{described.size == 1 ? 'description' : "#{described.size} descriptions"} of the " \
|
|
459
|
+
'project to portable recipes, changes the configuration by itself and says what it did. 3.0 loads no other file'
|
|
460
|
+
end
|
|
461
|
+
other = (counts[:breaks] || 0) - findings.count { |finding| finding.kind == :breaks && (described.include?(finding.file) || finding.message.start_with?('apis_files_paths')) }
|
|
462
|
+
steps << "Fix the #{lines.call(other)} marked breaks#{' that convert does not take care of' unless described.empty?}: that code stops working, and each line says what to use instead#{'. The code to paste, above, does it for the values given when the application starts' if pasted}" if other.positive?
|
|
463
|
+
steps << "Correct what is marked config (#{counts[:config]}): each is a problem in a file that describes an api" if counts[:config]
|
|
464
|
+
steps << "Read the #{lines.call(counts[:check])} marked check: the result changes, and only you can tell whether the code still does what it should. A check line is reported at every run" if counts[:check]
|
|
465
|
+
steps << 'Go through what this command cannot find: look in the code for the changes that concern the project'
|
|
466
|
+
steps << 'Run this command again: go on when only check lines that you have read are left' if counts[:breaks] || counts[:config]
|
|
467
|
+
steps << 'Start the application and run its tests: ApiRecipes.configure checks the whole configuration when it loads, also what an ERB tag gives, and lists every problem at once'
|
|
468
|
+
screen.section('👉', 'What to do now')
|
|
469
|
+
steps.each_with_index { |step, index| screen.item(step, "#{index + 1}.") }
|
|
470
|
+
end
|
|
471
|
+
|
|
179
472
|
def summary(count)
|
|
180
473
|
case count
|
|
181
474
|
when 0 then nothing_found
|
|
@@ -210,10 +503,9 @@ module ApiRecipes
|
|
|
210
503
|
# obey; a path that was given is shown as it was given.
|
|
211
504
|
def shown_path(file) = @project ? Recipes::Summary.shown(file, 300) : file
|
|
212
505
|
|
|
213
|
-
def
|
|
506
|
+
def where(finding)
|
|
214
507
|
file = shown_path(finding.file)
|
|
215
|
-
|
|
216
|
-
"#{where}: [#{finding.kind}] #{finding.message}"
|
|
508
|
+
finding.line ? "#{file}:#{finding.line}" : file
|
|
217
509
|
end
|
|
218
510
|
|
|
219
511
|
def scan_path(path)
|
|
@@ -241,16 +533,32 @@ module ApiRecipes
|
|
|
241
533
|
rules.each do |rule|
|
|
242
534
|
next unless rule.pattern.match?(line)
|
|
243
535
|
|
|
536
|
+
if rule.equal?(BARE_RESCUE)
|
|
537
|
+
# A rescue that comes after one of ApiRecipes::Error never sees them.
|
|
538
|
+
next if gem_errors_handled?(text, number)
|
|
539
|
+
|
|
540
|
+
@rescues << { file: file, line: number }
|
|
541
|
+
end
|
|
244
542
|
found << Finding.new(file: file, line: number, kind: rule.kind, message: rule.message, hint: hint(rule.fix, line, file, number, text))
|
|
245
543
|
end
|
|
246
544
|
end
|
|
247
|
-
found
|
|
545
|
+
(found + in_the_tree(file, text, found)).each_with_index.sort_by { |finding, index| [finding.line, index] }.map(&:first)
|
|
248
546
|
end
|
|
249
547
|
|
|
250
548
|
# ---- the code to paste ----
|
|
251
549
|
|
|
252
550
|
# The code a line assigns, when it can be copied: on one line, short,
|
|
253
551
|
# and with nothing a terminal would obey.
|
|
552
|
+
# What only the tree of the file tells. A writer that a rule on lines
|
|
553
|
+
# found is not said twice.
|
|
554
|
+
def in_the_tree(file, text, found)
|
|
555
|
+
reported = found.select { |finding| WRITER_RULES.any? { |rule| rule.message == finding.message } }.map(&:line)
|
|
556
|
+
uses = !@project || @project.uses_gem?(text)
|
|
557
|
+
TreeRules.findings(text, uses_gem: uses, reported: reported, apis: @project ? @project.api_names : []).map do |one|
|
|
558
|
+
Finding.new(file: file, line: one.line, kind: one.kind, message: one.message)
|
|
559
|
+
end
|
|
560
|
+
end
|
|
561
|
+
|
|
254
562
|
def code_of(match)
|
|
255
563
|
code = without_comment(match[:code].to_s.strip)
|
|
256
564
|
return nil if code.empty? || code.length > MAX_CODE || Code::UNSAFE.match?(code) || !expression?(code)
|
|
@@ -337,8 +645,8 @@ module ApiRecipes
|
|
|
337
645
|
|
|
338
646
|
# The values given when the application starts, in the block of
|
|
339
647
|
# ApiRecipes.configure: the whole block when one file has it.
|
|
340
|
-
def code_to_paste(
|
|
341
|
-
return if @startup.empty?
|
|
648
|
+
def code_to_paste(screen)
|
|
649
|
+
return false if @startup.empty?
|
|
342
650
|
|
|
343
651
|
file, text = @configure.size == 1 ? @configure.first : nil
|
|
344
652
|
block = Code::ConfigureBlock.in(text)
|
|
@@ -347,27 +655,34 @@ module ApiRecipes
|
|
|
347
655
|
inside = @startup.select { |item| block && item[:file] == file && item[:line].between?(block.first, block.last) }
|
|
348
656
|
added = @startup.select { |item| item[:api].nil? }.first(1).map { |item| "#{config}.print_urls = #{item[:code]}" }
|
|
349
657
|
added = [] if block&.line_of(:print_urls)
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
entries =
|
|
658
|
+
# config.values takes one credential for an api: given both, which
|
|
659
|
+
# one the application uses is left to the person.
|
|
660
|
+
both = @startup.select { |item| item[:api] }.group_by { |item| item[:api] }.select { |_, list| list.map { |item| item[:key] }.uniq.size > 1 }
|
|
661
|
+
entries = entries_of(@startup.select { |item| item[:api] && !both.key?(item[:api]) })
|
|
354
662
|
merged = block && block.line_of(:values)
|
|
355
663
|
added += ["#{config}.values = {", *entries.map { |entry| " #{entry}" }, '}'] unless entries.empty? || merged
|
|
356
664
|
|
|
357
|
-
|
|
665
|
+
screen.section('📋', 'Code to paste')
|
|
666
|
+
screen.gap
|
|
358
667
|
if block
|
|
359
|
-
|
|
360
|
-
|
|
668
|
+
screen.line("In #{shown_path(file)}, replace the block of ApiRecipes.configure (lines #{block.first} to #{block.last}) with:")
|
|
669
|
+
screen.code(block.lines(added, without: inside.map { |item| item[:line] }))
|
|
361
670
|
else
|
|
362
|
-
|
|
363
|
-
|
|
671
|
+
screen.line('Inside the block of ApiRecipes.configure:')
|
|
672
|
+
screen.code(added)
|
|
364
673
|
end
|
|
365
674
|
if merged && !entries.empty?
|
|
366
|
-
|
|
367
|
-
|
|
675
|
+
screen.line("and add to the Hash that #{config}.values is given at line #{merged}:")
|
|
676
|
+
screen.code(entries)
|
|
677
|
+
end
|
|
678
|
+
outside = (@startup - inside - both.values.flatten).map { |item| "#{shown_path(item[:file])}:#{item[:line]}" }
|
|
679
|
+
screen.line("Then remove the lines that gave these values: #{outside.join(', ')}") unless outside.empty?
|
|
680
|
+
both.each do |api, list|
|
|
681
|
+
screen.line("#{Recipes::Summary.shown(api, 100)} is given both authorization and basic_auth " \
|
|
682
|
+
"(#{list.map { |item| "#{shown_path(item[:file])}:#{item[:line]}" }.join(', ')}): config.values takes one of them, " \
|
|
683
|
+
'add the one the application uses')
|
|
368
684
|
end
|
|
369
|
-
|
|
370
|
-
out.puts "Then remove the lines that gave these values: #{outside.join(', ')}" unless outside.empty?
|
|
685
|
+
true
|
|
371
686
|
end
|
|
372
687
|
|
|
373
688
|
# Every rule for the paths that were given. In a project, the general
|
|
@@ -407,7 +722,7 @@ module ApiRecipes
|
|
|
407
722
|
|
|
408
723
|
unchecked = []
|
|
409
724
|
tags = false
|
|
410
|
-
apis =
|
|
725
|
+
apis = Description.read(file, erb: @erb) do |text|
|
|
411
726
|
tags = true
|
|
412
727
|
without_erb(file, text)
|
|
413
728
|
end
|
|
@@ -416,20 +731,27 @@ module ApiRecipes
|
|
|
416
731
|
unchecked = erb_values(apis)
|
|
417
732
|
Internals::Compiler.compile_all(apis)
|
|
418
733
|
note_unchecked(file, unchecked)
|
|
419
|
-
[]
|
|
734
|
+
[to_convert(file)] + ConfigRules.findings(apis).map { |message| Finding.new(file: file, line: nil, kind: :check, message: message) }
|
|
420
735
|
rescue RecipeFileError => e
|
|
421
736
|
return not_checked(file) if tags && e.errors.all? { |problem| problem.include?('invalid YAML') }
|
|
422
737
|
|
|
423
|
-
e.errors.map { |problem| config_finding(file, problem.delete_prefix("#{file}: ")) }
|
|
738
|
+
[to_convert(file)] + e.errors.map { |problem| config_finding(file, problem.delete_prefix("#{file}: ")) }
|
|
424
739
|
rescue ConfigurationError => e
|
|
425
740
|
note_unchecked(file, unchecked)
|
|
426
|
-
checked(e.errors, unchecked).map { |problem| config_finding(file, problem) }
|
|
741
|
+
[to_convert(file)] + checked(e.errors, unchecked).map { |problem| config_finding(file, problem) }
|
|
427
742
|
rescue SystemStackError
|
|
428
743
|
[config_finding(file, 'is nested too deeply to be checked')]
|
|
429
744
|
rescue StandardError, ScriptError => e
|
|
430
745
|
[config_finding(file, "could not be evaluated (#{e.class})")]
|
|
431
746
|
end
|
|
432
747
|
|
|
748
|
+
# 3.0 loads no file of the project that describes an api.
|
|
749
|
+
def to_convert(file)
|
|
750
|
+
Finding.new(file: file, line: nil, kind: :breaks,
|
|
751
|
+
message: 'describes an api in a file of the project, which 3.0 no longer loads: an api is described by a ' \
|
|
752
|
+
'portable recipe. `api_recipes convert` moves it to one')
|
|
753
|
+
end
|
|
754
|
+
|
|
433
755
|
# A file whose tags give a name or a part of the structure is not
|
|
434
756
|
# guessed: it is said, and it is not a finding.
|
|
435
757
|
def not_checked(file)
|
|
@@ -474,8 +796,8 @@ module ApiRecipes
|
|
|
474
796
|
# the value of each output tag becomes ERB_VALUE. A tag that is not an
|
|
475
797
|
# output tag can decide which lines the file has, so it is not guessed.
|
|
476
798
|
def without_erb(file, text)
|
|
477
|
-
plain =
|
|
478
|
-
return plain
|
|
799
|
+
plain = Tags.replaced(text, @erb_value)
|
|
800
|
+
return plain if plain
|
|
479
801
|
|
|
480
802
|
raise RecipeFileError.new(file, 'contains ERB that decides the structure of the file (<% ... %>): run again ' \
|
|
481
803
|
'with --erb to evaluate it (this executes the code in the file)')
|