api_recipes 3.0.0.beta2 → 3.0.0.beta4
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 +704 -81
- data/UPGRADING.md +149 -46
- data/lib/api_recipes/api.rb +14 -10
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +624 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +533 -0
- data/lib/api_recipes/cli/recipes/git.rb +197 -0
- data/lib/api_recipes/cli/recipes/lock_file.rb +34 -0
- data/lib/api_recipes/cli/recipes/summary.rb +245 -0
- data/lib/api_recipes/cli/recipes.rb +287 -0
- data/lib/api_recipes/cli/routes.rb +8 -3
- data/lib/api_recipes/cli/upgrade/project.rb +194 -0
- data/lib/api_recipes/cli/upgrade.rb +361 -24
- data/lib/api_recipes/cli.rb +161 -8
- data/lib/api_recipes/configuration.rb +68 -18
- data/lib/api_recipes/endpoint.rb +10 -4
- data/lib/api_recipes/errors.rb +2 -1
- data/lib/api_recipes/internals/compiler.rb +174 -33
- data/lib/api_recipes/internals/context.rb +87 -20
- data/lib/api_recipes/internals/declaration.rb +18 -18
- data/lib/api_recipes/internals/definition.rb +76 -23
- data/lib/api_recipes/internals/level.rb +54 -43
- data/lib/api_recipes/internals/option_value.rb +92 -0
- data/lib/api_recipes/internals/path_template.rb +29 -4
- data/lib/api_recipes/internals/plain_yaml.rb +133 -0
- data/lib/api_recipes/internals/recipe.rb +297 -0
- data/lib/api_recipes/internals/recipe_lock.rb +115 -0
- data/lib/api_recipes/internals/registry.rb +61 -15
- data/lib/api_recipes/internals/request.rb +42 -14
- data/lib/api_recipes/internals/template.rb +17 -5
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +8 -3
- metadata +14 -1
|
@@ -1,53 +1,95 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
require 'api_recipes/cli/upgrade/project'
|
|
5
|
+
|
|
3
6
|
module ApiRecipes
|
|
4
7
|
module CLI
|
|
5
8
|
# Looks for what api_recipes 3.0 removed or changed. It reads files and
|
|
6
9
|
# never writes one. A result carries the file, the line, the kind and a fixed
|
|
7
10
|
# message: never the text of the line, which can hold a secret.
|
|
8
11
|
class Upgrade
|
|
9
|
-
|
|
12
|
+
# hint: lines of code shown under the finding, or nil.
|
|
13
|
+
Finding = Data.define(:file, :line, :kind, :message, :hint) do
|
|
14
|
+
def initialize(file:, line:, kind:, message:, hint: nil) = super
|
|
15
|
+
end
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
# general: the pattern also matches code that has nothing to do with the
|
|
18
|
+
# gem. In a whole project such a rule is applied only to the files that
|
|
19
|
+
# use the gem.
|
|
20
|
+
# fix: which code to paste the line gives, when it gives one.
|
|
21
|
+
Rule = Data.define(:kind, :pattern, :message, :general, :fix) do
|
|
22
|
+
def initialize(kind:, pattern:, message:, general: false, fix: nil) = super
|
|
23
|
+
end
|
|
12
24
|
|
|
13
25
|
RULES = [
|
|
14
|
-
Rule.new(:breaks, /\.route\b/, 'endpoint.route was removed: use response.fill or endpoint.definition'),
|
|
26
|
+
Rule.new(:breaks, /\.route\b/, 'endpoint.route was removed: use response.fill or endpoint.definition', false, :route),
|
|
15
27
|
Rule.new(:breaks, /RouteNameClashWithExistentMethod/,
|
|
16
28
|
'this error class was removed: a name clash is now a warning'),
|
|
17
29
|
Rule.new(:breaks, /RouteAndResourceNamesClashError|MissingRouteAttribute|ProvidedObjectNotAsResponseData|NoConfigurationGivenForEndpoint/,
|
|
18
30
|
'this error class was removed: nothing raised it, so the rescue clause can go'),
|
|
19
|
-
Rule.new(:breaks, /\.base_configs\b/, 'Api#base_configs was removed: use api.definition'),
|
|
31
|
+
Rule.new(:breaks, /\.base_configs\b/, 'Api#base_configs was removed: use api.definition', false, :base_configs),
|
|
20
32
|
Rule.new(:breaks, /ApiRecipes::Route\b/, 'ApiRecipes::Route was removed: a request is sent with run'),
|
|
21
33
|
Rule.new(:breaks, /ApiRecipes::Settings\b/,
|
|
22
34
|
'ApiRecipes::Settings was removed: the defaults are listed in the README'),
|
|
23
35
|
Rule.new(:breaks, /\bconfiguration\.setup\b/,
|
|
24
36
|
'configuration.setup was removed: configure loads the configuration'),
|
|
25
|
-
Rule.new(:breaks, /ApiRecipes\.print_urls/, 'use config.print_urls inside ApiRecipes.configure'),
|
|
37
|
+
Rule.new(:breaks, /ApiRecipes\.print_urls/, 'use config.print_urls inside ApiRecipes.configure', false, :print_urls),
|
|
26
38
|
Rule.new(:breaks, /_aprcps_|set_authorization_for_api|set_basic_auth_for_api|copy_global_authorizations_to_api/,
|
|
27
39
|
'internal method removed'),
|
|
40
|
+
Rule.new(:breaks, /\bApiRecipes((\.|::)\w+|\[[^\]]*\])\.(authorization|basic_auth)\s*(\|\|)?=(?![=~])/,
|
|
41
|
+
'a credential of the whole application is given to ApiRecipes.configure, as ' \
|
|
42
|
+
'config.values = { name: { authorization: ... } }: this writer now raises', false, :global_writer),
|
|
43
|
+
Rule.new(:check, /(?<!\bApiRecipes)\.\w+\.(authorization|basic_auth)\s*(\|\|)?=(?![=~])/,
|
|
44
|
+
'authorization= and basic_auth= now work on an object only: on a class they raise, ' \
|
|
45
|
+
'give the credential to `api :name, authorization: ...`, or to with(...) for one request', false, :writer),
|
|
28
46
|
Rule.new(:check, /url_encode|CGI\.escape|encode_www_form_component/,
|
|
29
|
-
'path parameters are now encoded by ApiRecipes: remove manual encoding of values passed to endpoints'),
|
|
47
|
+
'path parameters are now encoded by ApiRecipes: remove manual encoding of values passed to endpoints', true),
|
|
30
48
|
Rule.new(:check, /\brescue\s*(=>.*)?$|rescue StandardError/,
|
|
31
|
-
'a bare rescue now catches ApiRecipes errors too'),
|
|
32
|
-
Rule.new(:check, /\bfollow:/, 'credentials are no longer sent to another origin after a redirect'),
|
|
49
|
+
'a bare rescue now catches ApiRecipes errors too', true),
|
|
50
|
+
Rule.new(:check, /\bfollow:/, 'credentials are no longer sent to another origin after a redirect', true),
|
|
33
51
|
Rule.new(:check, /\bresponse\.request\b/,
|
|
34
52
|
'response.request was removed, because the request holds the credentials: ' \
|
|
35
|
-
'read response.uri, or response.original_response'),
|
|
53
|
+
'read response.uri, or response.original_response', true, :request),
|
|
36
54
|
Rule.new(:check, /\.(deep_merge|deep_symbolize_keys|deep_stringify_keys|symbolize_keys|stringify_keys|extract_options!|assert_valid_keys)\b/,
|
|
37
55
|
'this method came from api_recipes 2.x unless ActiveSupport is loaded: ' \
|
|
38
|
-
'define it or add ActiveSupport')
|
|
56
|
+
'define it or add ActiveSupport', true)
|
|
39
57
|
].freeze
|
|
40
58
|
|
|
59
|
+
# The rule about the Hash methods that ActiveSupport defines too.
|
|
60
|
+
HASH_METHODS = RULES.last
|
|
61
|
+
|
|
62
|
+
# What stands for the value of an ERB tag in a file that is checked
|
|
63
|
+
# without running its code.
|
|
64
|
+
# Options whose value is a mapping: a tag that gives one gives a part
|
|
65
|
+
# of the structure, which is not guessed.
|
|
66
|
+
CONTAINERS = %w[variables endpoints default_headers default_params auth recipe resource].freeze
|
|
67
|
+
|
|
68
|
+
# What a line assigns, read without running it, to give the code to paste.
|
|
69
|
+
WRITTEN = '\\s*(?:\\|\\|)?=(?![=~])\\s*(?<code>.+)\\z'
|
|
70
|
+
FIXES = {
|
|
71
|
+
print_urls: /\bApiRecipes\.print_urls#{WRITTEN}/,
|
|
72
|
+
global_writer: /\bApiRecipes(?:(?:\.|::)(?<api>\w+)|\[\s*[:'"](?<api>\w+)['"]?\s*\])\.(?<key>authorization|basic_auth)#{WRITTEN}/,
|
|
73
|
+
writer: /(?<![^\s(=,])(?<receiver>[\w.@:\[\]'"]+)\.(?<api>\w+)\.(?<key>authorization|basic_auth)#{WRITTEN}/,
|
|
74
|
+
base_configs: /(?<![^\s(=,])(?<receiver>[\w.@:\[\]'"]+)\.base_configs\b/,
|
|
75
|
+
route: /\.route\.fill\((?<code>.*)\)/
|
|
76
|
+
}.freeze
|
|
77
|
+
# Code longer than this is not copied into the code to paste.
|
|
78
|
+
MAX_CODE = 300
|
|
79
|
+
ERB_COMMENT = /<%#.*?%>/m
|
|
80
|
+
ERB_OUTPUT = /<%=.*?%>/m
|
|
81
|
+
|
|
41
82
|
SILENT_CHANGES = [
|
|
42
83
|
'path parameters encoded by hand: %2F would become %252F, remove the encoding',
|
|
43
84
|
'a / inside a path parameter to cover several segments: use *name or two placeholders',
|
|
44
85
|
'debug logs or print_urls output read from STDOUT: set log_to and log_level',
|
|
45
86
|
'a bare rescue around a call: it now catches the errors of the gem too',
|
|
46
|
-
'credentials assigned on
|
|
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: ...`',
|
|
47
89
|
'the response body read from the message of a verify_with error: read it from error.response',
|
|
48
90
|
'a block passed to run whose return value is used: it is now the Response',
|
|
49
91
|
'apis_configs assigned after the files to discard them: do not assign the files',
|
|
50
|
-
'
|
|
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',
|
|
51
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',
|
|
52
94
|
'fill with private setters, or code that relies on swallowed errors: make the setters public, handle the errors',
|
|
53
95
|
'follow: true together with a credential: the credential no longer follows to another host',
|
|
@@ -63,6 +105,7 @@ module ApiRecipes
|
|
|
63
105
|
'response.to_s returns the body, as the http.rb response does: it was the default text of the object',
|
|
64
106
|
'a credential that is not a String, such as a Symbol or a number: it raises InvalidCredential, convert it with to_s',
|
|
65
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}',
|
|
66
109
|
'respond_to? on a response now tells the truth: it answers true for the methods the response forwards and false for the others',
|
|
67
110
|
'serializing an Api, an Endpoint or a Response raises TypeError: serialize response.data or response.body.to_s',
|
|
68
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'
|
|
@@ -72,23 +115,54 @@ module ApiRecipes
|
|
|
72
115
|
# the file could not be checked.
|
|
73
116
|
attr_reader :warnings
|
|
74
117
|
|
|
75
|
-
|
|
118
|
+
# What was checked and how, said after the findings. Not findings.
|
|
119
|
+
attr_reader :notes
|
|
120
|
+
|
|
121
|
+
# project: the Project the paths and the files were found in, when the
|
|
122
|
+
# command was given none.
|
|
123
|
+
# code: false leaves out the code to paste, as in a CI whose output is kept.
|
|
124
|
+
def initialize(paths, config: [], erb: false, project: nil, code: true)
|
|
125
|
+
@code = code
|
|
76
126
|
@paths = Array(paths)
|
|
77
127
|
@configs = Array(config)
|
|
78
128
|
@erb = erb
|
|
129
|
+
@project = project
|
|
79
130
|
@warnings = []
|
|
131
|
+
@notes = []
|
|
132
|
+
# What stands for the value of an ERB tag in a file that is checked
|
|
133
|
+
# without running its code: random, so that no file can write it.
|
|
134
|
+
@erb_value = "API_RECIPES_ERB_VALUE_#{SecureRandom.hex(6)}"
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The command without arguments: the project in the current directory.
|
|
138
|
+
def self.for_project(project, erb: false, code: true)
|
|
139
|
+
new(project.ruby_files, config: project.config_files, erb: erb, project: project, code: code)
|
|
80
140
|
end
|
|
81
141
|
|
|
82
142
|
def run
|
|
83
|
-
@warnings = []
|
|
84
|
-
|
|
85
|
-
|
|
143
|
+
@warnings = @project ? @project.warnings.dup : []
|
|
144
|
+
@notes = []
|
|
145
|
+
@uses = 0
|
|
146
|
+
@startup = []
|
|
147
|
+
@configure = {}
|
|
148
|
+
findings = with_request_hints(@paths.flat_map { |path| scan_path(path) })
|
|
149
|
+
findings += @configs.flat_map { |file| check_config(file) }
|
|
150
|
+
@notes = scope + @notes if @project
|
|
151
|
+
findings
|
|
86
152
|
end
|
|
87
153
|
|
|
88
154
|
# Prints the results on out and the warnings on err. Returns the exit code.
|
|
89
155
|
def report(out, err)
|
|
90
156
|
findings = run
|
|
91
|
-
findings.each
|
|
157
|
+
findings.each do |finding|
|
|
158
|
+
out.puts format_finding(finding)
|
|
159
|
+
Code.show(out, finding.hint) if @code && finding.hint
|
|
160
|
+
end
|
|
161
|
+
unless @notes.empty?
|
|
162
|
+
out.puts unless findings.empty?
|
|
163
|
+
@notes.each { |note| out.puts note }
|
|
164
|
+
end
|
|
165
|
+
code_to_paste(out) if @code
|
|
92
166
|
out.puts
|
|
93
167
|
out.puts 'What this command cannot find'
|
|
94
168
|
SILENT_CHANGES.each { |change| out.puts " - #{change}" }
|
|
@@ -104,20 +178,47 @@ module ApiRecipes
|
|
|
104
178
|
|
|
105
179
|
def summary(count)
|
|
106
180
|
case count
|
|
107
|
-
when 0 then
|
|
181
|
+
when 0 then nothing_found
|
|
108
182
|
when 1 then '1 finding'
|
|
109
183
|
else "#{count} findings"
|
|
110
184
|
end
|
|
111
185
|
end
|
|
112
186
|
|
|
187
|
+
# "Nothing to change" is said only of what was checked.
|
|
188
|
+
def nothing_found
|
|
189
|
+
return 'Nothing to change' unless @project && @configs.empty?
|
|
190
|
+
|
|
191
|
+
'Nothing to change in the Ruby files. No configuration file was found: if the apis are described in ' \
|
|
192
|
+
'YAML files, name each one with --config FILE'
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# What a run on a project looked at.
|
|
196
|
+
def scope
|
|
197
|
+
ruby = @paths.size
|
|
198
|
+
lines = ["Checked #{ruby} Ruby file#{'s' unless ruby == 1}, #{@uses} of them use#{'s' if @uses == 1} api_recipes"]
|
|
199
|
+
lines << if @configs.empty?
|
|
200
|
+
'No configuration file was found'
|
|
201
|
+
else
|
|
202
|
+
"Checked #{@configs.size} configuration file#{'s' unless @configs.size == 1}: #{@configs.map { |file| shown_path(file) }.join(', ')}"
|
|
203
|
+
end
|
|
204
|
+
lines + @project.undescribed.map do |declared|
|
|
205
|
+
"#{shown_path(declared.file)}:#{declared.line}: declares the api '#{declared.api}', and no configuration file found here describes it"
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# A name found in the project is shown without what a terminal would
|
|
210
|
+
# obey; a path that was given is shown as it was given.
|
|
211
|
+
def shown_path(file) = @project ? Recipes::Summary.shown(file, 300) : file
|
|
212
|
+
|
|
113
213
|
def format_finding(finding)
|
|
114
|
-
|
|
214
|
+
file = shown_path(finding.file)
|
|
215
|
+
where = finding.line ? "#{file}:#{finding.line}" : file
|
|
115
216
|
"#{where}: [#{finding.kind}] #{finding.message}"
|
|
116
217
|
end
|
|
117
218
|
|
|
118
219
|
def scan_path(path)
|
|
119
220
|
if File.directory?(path)
|
|
120
|
-
Dir.glob('**/*.rb', base: path).sort.flat_map do |relative|
|
|
221
|
+
Dir.glob('**/*.{rb,rake}', base: path).sort.flat_map do |relative|
|
|
121
222
|
file = File.join(path, relative)
|
|
122
223
|
File.directory?(file) ? [] : scan_file(file)
|
|
123
224
|
end
|
|
@@ -133,15 +234,156 @@ module ApiRecipes
|
|
|
133
234
|
text = read_ruby(file)
|
|
134
235
|
return [] unless text
|
|
135
236
|
|
|
237
|
+
rules = rules_for(text)
|
|
238
|
+
@configure[file] = text if text.match?(/\bApiRecipes\.configure\b/)
|
|
136
239
|
found = []
|
|
137
240
|
text.each_line(chomp: true).with_index(1) do |line, number|
|
|
138
|
-
|
|
139
|
-
|
|
241
|
+
rules.each do |rule|
|
|
242
|
+
next unless rule.pattern.match?(line)
|
|
243
|
+
|
|
244
|
+
found << Finding.new(file: file, line: number, kind: rule.kind, message: rule.message, hint: hint(rule.fix, line, file, number, text))
|
|
140
245
|
end
|
|
141
246
|
end
|
|
142
247
|
found
|
|
143
248
|
end
|
|
144
249
|
|
|
250
|
+
# ---- the code to paste ----
|
|
251
|
+
|
|
252
|
+
# The code a line assigns, when it can be copied: on one line, short,
|
|
253
|
+
# and with nothing a terminal would obey.
|
|
254
|
+
def code_of(match)
|
|
255
|
+
code = without_comment(match[:code].to_s.strip)
|
|
256
|
+
return nil if code.empty? || code.length > MAX_CODE || Code::UNSAFE.match?(code) || !expression?(code)
|
|
257
|
+
|
|
258
|
+
code
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# code up to a comment that follows it on the line.
|
|
262
|
+
def without_comment(code)
|
|
263
|
+
quote = nil
|
|
264
|
+
code.each_char.with_index do |char, index|
|
|
265
|
+
if quote
|
|
266
|
+
quote = nil if char == quote && code[index - 1] != '\\'
|
|
267
|
+
elsif ["'", '"', '`'].include?(char)
|
|
268
|
+
quote = char
|
|
269
|
+
elsif char == '#' && (index.zero? || code[index - 1].match?(/\s/))
|
|
270
|
+
return code[0...index].rstrip
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
code
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Whether code is one whole Ruby expression, and not one that a word
|
|
277
|
+
# after it changes (if, unless, rescue, and, or). It is read, never run.
|
|
278
|
+
def expression?(code)
|
|
279
|
+
tree = Code.tree(code)
|
|
280
|
+
return false unless tree && tree.statements.body.size == 1
|
|
281
|
+
|
|
282
|
+
node = tree.statements.body.first
|
|
283
|
+
return false if [Prism::IfNode, Prism::UnlessNode, Prism::RescueModifierNode, Prism::WhileNode, Prism::UntilNode].any? { |kind| node.is_a?(kind) }
|
|
284
|
+
|
|
285
|
+
!((node.is_a?(Prism::AndNode) || node.is_a?(Prism::OrNode)) && %w[and or].include?(node.operator_loc.slice))
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Whether the statement on line is always run when its file is: a
|
|
289
|
+
# statement of the file itself, or of the block of ApiRecipes.configure,
|
|
290
|
+
# not one inside a condition or another block.
|
|
291
|
+
def always_run?(file, text, line)
|
|
292
|
+
@direct ||= {}
|
|
293
|
+
@direct[file] ||= begin
|
|
294
|
+
tree = Code.tree(text)
|
|
295
|
+
top = tree ? tree.statements.body.map { |node| node.location.start_line } : []
|
|
296
|
+
block = Code::ConfigureBlock.in(text)
|
|
297
|
+
top + (block ? block.statement_lines : [])
|
|
298
|
+
end
|
|
299
|
+
@direct[file].include?(line)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# The lines shown under a finding. A value given when the application
|
|
303
|
+
# starts is kept aside: all of them go in one block of configure.
|
|
304
|
+
def hint(fix, line, file, number, text)
|
|
305
|
+
match = FIXES[fix]&.match(line)
|
|
306
|
+
case fix
|
|
307
|
+
when :request then ['response.uri # the address of the request', 'response.original_response # the http.rb response']
|
|
308
|
+
when :base_configs then match && !Code::UNSAFE.match?(match[:receiver]) ? ["#{match[:receiver]}.definition"] : nil
|
|
309
|
+
when :route then (code = match && code_of(match)) ? ["response.fill(#{code}) # response is what run returned"] : nil
|
|
310
|
+
when :writer
|
|
311
|
+
code = match && code_of(match)
|
|
312
|
+
return nil unless code && !Code::UNSAFE.match?(match[:receiver])
|
|
313
|
+
|
|
314
|
+
['# for a class, where it declares the api:', "api :#{match[:api]}, #{match[:key]}: #{code}", '# for one request:',
|
|
315
|
+
"#{match[:receiver]}.#{match[:api]}.with(#{match[:key]}: #{code})"]
|
|
316
|
+
when :print_urls, :global_writer
|
|
317
|
+
code = match && code_of(match)
|
|
318
|
+
return nil unless code && always_run?(file, text, number)
|
|
319
|
+
|
|
320
|
+
@startup << { file: file, line: number, api: fix == :print_urls ? nil : match[:api], key: fix == :print_urls ? 'print_urls' : match[:key], code: code }
|
|
321
|
+
nil
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# A credential of the application assigned in more than one place is
|
|
326
|
+
# assigned for each request: each of those lines becomes a with.
|
|
327
|
+
def with_request_hints(findings)
|
|
328
|
+
repeated = @startup.group_by { |item| [item[:api], item[:key]] }.select { |(api, _), items| api && items.size > 1 }.values.flatten
|
|
329
|
+
@startup -= repeated
|
|
330
|
+
findings.map do |finding|
|
|
331
|
+
item = repeated.find { |one| one[:file] == finding.file && one[:line] == finding.line }
|
|
332
|
+
next finding unless item && finding.hint.nil? && FIXES[:global_writer].match?('ApiRecipes.x.' + item[:key] + ' = 1') && finding.message.include?('config.values')
|
|
333
|
+
|
|
334
|
+
finding.with(hint: ["ApiRecipes.#{item[:api]}.with(#{item[:key]}: #{item[:code]}) # use the api it returns for the calls of this request"])
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# The values given when the application starts, in the block of
|
|
339
|
+
# ApiRecipes.configure: the whole block when one file has it.
|
|
340
|
+
def code_to_paste(out)
|
|
341
|
+
return if @startup.empty?
|
|
342
|
+
|
|
343
|
+
file, text = @configure.size == 1 ? @configure.first : nil
|
|
344
|
+
block = Code::ConfigureBlock.in(text)
|
|
345
|
+
block = nil if block&.problem
|
|
346
|
+
config = block ? block.config : 'config'
|
|
347
|
+
inside = @startup.select { |item| block && item[:file] == file && item[:line].between?(block.first, block.last) }
|
|
348
|
+
added = @startup.select { |item| item[:api].nil? }.first(1).map { |item| "#{config}.print_urls = #{item[:code]}" }
|
|
349
|
+
added = [] if block&.line_of(:print_urls)
|
|
350
|
+
entries = @startup.select { |item| item[:api] }.group_by { |item| item[:api] }.map do |api, items|
|
|
351
|
+
"#{api.match?(/\A[a-z_]\w*\z/) ? api : "\"#{api}\""}: { #{items.map { |item| "#{item[:key]}: #{item[:code]}" }.join(', ')} }"
|
|
352
|
+
end
|
|
353
|
+
entries = entries.each_with_index.map { |entry, index| index == entries.size - 1 ? entry : "#{entry}," }
|
|
354
|
+
merged = block && block.line_of(:values)
|
|
355
|
+
added += ["#{config}.values = {", *entries.map { |entry| " #{entry}" }, '}'] unless entries.empty? || merged
|
|
356
|
+
|
|
357
|
+
out.puts '', 'Code to paste', ''
|
|
358
|
+
if block
|
|
359
|
+
out.puts "In #{shown_path(file)}, replace the block of ApiRecipes.configure (lines #{block.first} to #{block.last}) with:"
|
|
360
|
+
Code.show(out, block.lines(added, without: inside.map { |item| item[:line] }))
|
|
361
|
+
else
|
|
362
|
+
out.puts 'Inside the block of ApiRecipes.configure:'
|
|
363
|
+
Code.show(out, added)
|
|
364
|
+
end
|
|
365
|
+
if merged && !entries.empty?
|
|
366
|
+
out.puts "and add to the Hash that #{config}.values is given at line #{merged}:"
|
|
367
|
+
Code.show(out, entries)
|
|
368
|
+
end
|
|
369
|
+
outside = (@startup - inside).map { |item| "#{shown_path(item[:file])}:#{item[:line]}" }
|
|
370
|
+
out.puts "Then remove the lines that gave these values: #{outside.join(', ')}" unless outside.empty?
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# Every rule for the paths that were given. In a project, the general
|
|
374
|
+
# ones only for a file that uses the gem, and never the one about the
|
|
375
|
+
# methods that ActiveSupport defines when the project has it.
|
|
376
|
+
def rules_for(text)
|
|
377
|
+
return RULES unless @project
|
|
378
|
+
|
|
379
|
+
@specific ||= RULES.reject(&:general)
|
|
380
|
+
@all ||= @project.active_support? ? RULES - [HASH_METHODS] : RULES
|
|
381
|
+
return @specific unless @project.uses_gem?(text)
|
|
382
|
+
|
|
383
|
+
@uses += 1
|
|
384
|
+
@all
|
|
385
|
+
end
|
|
386
|
+
|
|
145
387
|
# Returns the text, or nil after recording why the file was not checked.
|
|
146
388
|
def read_ruby(file)
|
|
147
389
|
bytes = File.binread(file)
|
|
@@ -160,17 +402,112 @@ module ApiRecipes
|
|
|
160
402
|
end
|
|
161
403
|
|
|
162
404
|
def check_config(file)
|
|
163
|
-
|
|
405
|
+
return check_recipe(file) if file.end_with?(Internals::Recipe::SUFFIX)
|
|
406
|
+
return [config_finding(file, 'describes more values than can be checked: aliases make them from a few lines')] if too_many_values?(file)
|
|
407
|
+
|
|
408
|
+
unchecked = []
|
|
409
|
+
tags = false
|
|
410
|
+
apis = Configuration.new.read_recipe_file(file, erb: @erb) do |text|
|
|
411
|
+
tags = true
|
|
412
|
+
without_erb(file, text)
|
|
413
|
+
end
|
|
414
|
+
return not_checked(file) if tags && structural_tag?(apis)
|
|
415
|
+
|
|
416
|
+
unchecked = erb_values(apis)
|
|
164
417
|
Internals::Compiler.compile_all(apis)
|
|
418
|
+
note_unchecked(file, unchecked)
|
|
165
419
|
[]
|
|
166
420
|
rescue RecipeFileError => e
|
|
421
|
+
return not_checked(file) if tags && e.errors.all? { |problem| problem.include?('invalid YAML') }
|
|
422
|
+
|
|
167
423
|
e.errors.map { |problem| config_finding(file, problem.delete_prefix("#{file}: ")) }
|
|
168
424
|
rescue ConfigurationError => e
|
|
169
|
-
|
|
425
|
+
note_unchecked(file, unchecked)
|
|
426
|
+
checked(e.errors, unchecked).map { |problem| config_finding(file, problem) }
|
|
427
|
+
rescue SystemStackError
|
|
428
|
+
[config_finding(file, 'is nested too deeply to be checked')]
|
|
170
429
|
rescue StandardError, ScriptError => e
|
|
171
430
|
[config_finding(file, "could not be evaluated (#{e.class})")]
|
|
172
431
|
end
|
|
173
432
|
|
|
433
|
+
# A file whose tags give a name or a part of the structure is not
|
|
434
|
+
# guessed: it is said, and it is not a finding.
|
|
435
|
+
def not_checked(file)
|
|
436
|
+
@notes << "#{shown_path(file)}: an ERB tag gives a name or a part of the structure: not checked without --erb"
|
|
437
|
+
[]
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
def structural_tag?(value)
|
|
441
|
+
case value
|
|
442
|
+
when Hash
|
|
443
|
+
value.any? do |key, inner|
|
|
444
|
+
key.to_s.include?(@erb_value) || (CONTAINERS.include?(key.to_s) && inner.to_s == @erb_value) || structural_tag?(inner)
|
|
445
|
+
end
|
|
446
|
+
when Array then value.any? { |inner| structural_tag?(inner) }
|
|
447
|
+
else false
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Aliases can make millions of values from a few lines: the file is
|
|
452
|
+
# measured, with its tags read as a word, before anything walks it.
|
|
453
|
+
def too_many_values?(file)
|
|
454
|
+
text = File.binread(file).force_encoding(Encoding::UTF_8)
|
|
455
|
+
return false unless text.valid_encoding? && text.include?('*')
|
|
456
|
+
|
|
457
|
+
data = YAML.safe_load(text.gsub(/<%.*?%>/m, 'erb'), permitted_classes: [Symbol], aliases: true)
|
|
458
|
+
!Project.within?(data, Project::MAX_VALUES)
|
|
459
|
+
rescue SystemCallError, Psych::Exception, SystemStackError
|
|
460
|
+
false
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
# A file called NAME.recipe.yml is a portable recipe, read as data only.
|
|
464
|
+
def check_recipe(file)
|
|
465
|
+
Internals::Recipe.read(file)
|
|
466
|
+
[]
|
|
467
|
+
rescue ConfigurationError => e
|
|
468
|
+
e.errors.map { |problem| config_finding(file, problem.delete_prefix("#{file}: ")) }
|
|
469
|
+
rescue StandardError => e
|
|
470
|
+
[config_finding(file, "could not be read (#{e.class})")]
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
# The text of a file with ERB, made checkable without running anything:
|
|
474
|
+
# the value of each output tag becomes ERB_VALUE. A tag that is not an
|
|
475
|
+
# output tag can decide which lines the file has, so it is not guessed.
|
|
476
|
+
def without_erb(file, text)
|
|
477
|
+
plain = text.gsub(ERB_COMMENT, '').gsub(ERB_OUTPUT, @erb_value)
|
|
478
|
+
return plain.gsub('<%%', '<%') unless plain.gsub('<%%', '').include?('<%')
|
|
479
|
+
|
|
480
|
+
raise RecipeFileError.new(file, 'contains ERB that decides the structure of the file (<% ... %>): run again ' \
|
|
481
|
+
'with --erb to evaluate it (this executes the code in the file)')
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# The places, as the compiler names them, whose value holds the value
|
|
485
|
+
# of a tag.
|
|
486
|
+
def erb_values(value, path = nil, found = [])
|
|
487
|
+
case value
|
|
488
|
+
when Hash then value.each { |key, inner| erb_values(inner, [path, place(key)].compact.join('.'), found) }
|
|
489
|
+
when Array then found << path if value.any? { |item| item.to_s.include?(@erb_value) }
|
|
490
|
+
else found << path if value.to_s.include?(@erb_value)
|
|
491
|
+
end
|
|
492
|
+
found
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
# A name as the compiler writes it in the place of a problem.
|
|
496
|
+
def place(name) = name.to_s.gsub(/[^[:print:]]/, '?')[0, 40]
|
|
497
|
+
|
|
498
|
+
# The problems that are not about a value that an ERB tag gives.
|
|
499
|
+
def checked(problems, unchecked)
|
|
500
|
+
problems.reject { |problem| unchecked.any? { |path| problem.start_with?("#{path}: ") } }
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
def note_unchecked(file, unchecked)
|
|
504
|
+
return if unchecked.empty?
|
|
505
|
+
|
|
506
|
+
count = unchecked.size
|
|
507
|
+
@notes << "#{shown_path(file)}: #{count} value#{'s' unless count == 1} come#{'s' if count == 1} from ERB and " \
|
|
508
|
+
"#{count == 1 ? 'was' : 'were'} not checked"
|
|
509
|
+
end
|
|
510
|
+
|
|
174
511
|
def config_finding(file, message)
|
|
175
512
|
Finding.new(file: file, line: nil, kind: :config, message: message)
|
|
176
513
|
end
|