api_recipes 3.0.0.beta3 → 3.0.0.beta5
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 +377 -76
- data/UPGRADING.md +223 -59
- data/lib/api_recipes/cli/calls.rb +27 -0
- data/lib/api_recipes/cli/code.rb +153 -0
- data/lib/api_recipes/cli/convert/migration.rb +819 -0
- data/lib/api_recipes/cli/convert/snippet.rb +67 -0
- data/lib/api_recipes/cli/convert.rb +614 -0
- 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 +25 -6
- data/lib/api_recipes/cli/recipes.rb +5 -2
- data/lib/api_recipes/cli/routes.rb +63 -8
- 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/project.rb +194 -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 +706 -57
- data/lib/api_recipes/cli.rb +171 -17
- data/lib/api_recipes/configuration.rb +17 -95
- data/lib/api_recipes/endpoint.rb +1 -1
- data/lib/api_recipes/internals/compiler.rb +102 -67
- data/lib/api_recipes/internals/context.rb +35 -6
- data/lib/api_recipes/internals/declaration.rb +17 -15
- data/lib/api_recipes/internals/definition.rb +16 -3
- data/lib/api_recipes/internals/option_value.rb +92 -0
- data/lib/api_recipes/internals/recipe.rb +1 -1
- data/lib/api_recipes/internals/recipe_lock.rb +12 -9
- data/lib/api_recipes/internals/request.rb +17 -6
- data/lib/api_recipes/version.rb +1 -1
- data/lib/api_recipes.rb +1 -0
- metadata +13 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module CLI
|
|
5
|
+
class Convert
|
|
6
|
+
# The code to paste where the command did not write it: the lines for the
|
|
7
|
+
# block of ApiRecipes.configure, with the code of each ERB tag and each
|
|
8
|
+
# value as they are written in the file, so that what is pasted works.
|
|
9
|
+
# Only what a terminal would obey is left out: that value is given as
|
|
10
|
+
# nil, with a comment that says where to copy it from.
|
|
11
|
+
class Snippet
|
|
12
|
+
ENV_ONLY = /\AENV(\[\s*(['"])\w+\2\s*\]|\.fetch\(\s*(['"])\w+\3\s*\))\z/
|
|
13
|
+
PLAIN = /\A("(?:[^"#\\]|\\.)*"|'[^'\\]*'|\d+)\z/
|
|
14
|
+
|
|
15
|
+
# config: the name of the block parameter. recipes: the code that
|
|
16
|
+
# gives recipes_paths, or nil when the file assigns it already.
|
|
17
|
+
def initialize(result, source, config: 'config', recipes: "Dir['config/api_recipes/*.recipe.yml']")
|
|
18
|
+
@result = result
|
|
19
|
+
@source = source
|
|
20
|
+
@config = config
|
|
21
|
+
@recipes = recipes
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The lines to add inside the block.
|
|
25
|
+
def lines
|
|
26
|
+
out = []
|
|
27
|
+
out << "#{@config}.recipes_paths = #{@recipes}" if @recipes
|
|
28
|
+
return out if @result.expressions.empty?
|
|
29
|
+
|
|
30
|
+
out + ["#{@config}.values = {", *entry(' '), '}']
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The lines of this api inside the Hash given to config.values.
|
|
34
|
+
def entry(indent = '')
|
|
35
|
+
names = @result.expressions.keys
|
|
36
|
+
pairs = names.each_with_index.map do |name, index|
|
|
37
|
+
code, comment = value(name)
|
|
38
|
+
"#{indent} #{name}: #{code}#{',' unless index == names.size - 1}#{" # #{comment}" if comment}"
|
|
39
|
+
end
|
|
40
|
+
["#{indent}#{key}: { variables: {", *pairs, "#{indent}}.compact }"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# The name of the api as a key of a Hash.
|
|
44
|
+
def key
|
|
45
|
+
name = @result.name
|
|
46
|
+
name.match?(/\A[a-z_]\w*\z/) ? name : "\"#{name}\""
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
# [code, comment]: the code of the tag, or nil and where to copy from.
|
|
52
|
+
def value(name)
|
|
53
|
+
code = @result.expressions[name]
|
|
54
|
+
ref = @result.refs[name] || {}
|
|
55
|
+
return [ENV_ONLY.match?(code) || PLAIN.match?(code) ? code : "(#{code})", nil] if shown?(code, ref)
|
|
56
|
+
|
|
57
|
+
what = ref[:literal] ? 'the value written' : 'the code of the tag'
|
|
58
|
+
where = [ref[:place] && "for #{Recipes::Summary.shown(ref[:place], 100)}", ref[:line] && "at line #{ref[:line]}"].compact.join(', ')
|
|
59
|
+
['nil', "paste here #{what} #{where} of #{Recipes::Summary.shown(@source, 100)}"]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Everything is shown but what would change what the terminal shows.
|
|
63
|
+
def shown?(code, _ref) = !Code::UNSAFE.match?(code)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yaml'
|
|
4
|
+
require 'securerandom'
|
|
5
|
+
|
|
6
|
+
module ApiRecipes
|
|
7
|
+
module CLI
|
|
8
|
+
# The convert command: from a configuration file of a project to the text
|
|
9
|
+
# of a portable recipe. The file is read and never run: an ERB tag becomes
|
|
10
|
+
# a variable where a recipe can have one, and is left out elsewhere. What
|
|
11
|
+
# is named like a credential is never copied. The result is checked as a
|
|
12
|
+
# portable recipe before it is returned.
|
|
13
|
+
class Convert
|
|
14
|
+
# The file cannot become a recipe. reasons: one line each.
|
|
15
|
+
class Refused < StandardError
|
|
16
|
+
attr_reader :reasons
|
|
17
|
+
|
|
18
|
+
def initialize(reasons)
|
|
19
|
+
@reasons = Array(reasons)
|
|
20
|
+
super(@reasons.join("\n"))
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The arguments are wrong, or the file cannot be read.
|
|
25
|
+
class Unusable < StandardError; end
|
|
26
|
+
|
|
27
|
+
# variables: { name => environment variable or nil } for those without a
|
|
28
|
+
# default; defaults: the same for those that have one. expressions:
|
|
29
|
+
# { name => Ruby code } for every variable the conversion made: the code
|
|
30
|
+
# of the tag it comes from, or the value that was written, as a literal.
|
|
31
|
+
# verify_with: where one was left out, as [place, method name].
|
|
32
|
+
# refs: { name => { place:, line:, credential:, literal: } }, where the
|
|
33
|
+
# value of each of those variables is in the file.
|
|
34
|
+
Result = Data.define(:name, :version, :text, :calls, :changes, :variables, :defaults, :expressions, :verify_with, :refs)
|
|
35
|
+
|
|
36
|
+
Tag = Data.define(:line, :env, :default, :code)
|
|
37
|
+
|
|
38
|
+
DEFAULT_VERSION = '1.0.0'
|
|
39
|
+
COMMENT = /<%#.*?%>/m
|
|
40
|
+
OUTPUT = /<%=(.*?)-?%>/m
|
|
41
|
+
# A tag longer than this is not read as a read of the environment.
|
|
42
|
+
MAX_TAG = 300
|
|
43
|
+
# More values than a recipe can hold: aliases can make them from a few lines.
|
|
44
|
+
MAX_VALUES = 200_000
|
|
45
|
+
ENV_FETCH = /\AENV\.fetch\(\s*(?:'([A-Za-z_]\w*)'|"([A-Za-z_]\w*)")\s*(?:,\s*([^,()]+?)\s*)?\)\z/
|
|
46
|
+
ENV_INDEX = /\AENV\[\s*(?:'([A-Za-z_]\w*)'|"([A-Za-z_]\w*)")\s*\](?:\s*\|\|\s*([^|&]+?))?\z/
|
|
47
|
+
# A header or a parameter with such a name holds a credential.
|
|
48
|
+
CREDENTIAL = /auth|key|token|secret|passw|credential|signature|cookie|session/i
|
|
49
|
+
SENT = %w[default_headers default_params].freeze
|
|
50
|
+
# The options of a request that a variable can give whole.
|
|
51
|
+
OPTION_KEYS = Internals::OptionValue::KEYS.map(&:to_s).freeze
|
|
52
|
+
API_ORDER = %w[recipe auth variables protocol host port base_url timeout on_bad_code mime_type
|
|
53
|
+
default_headers default_params].freeze
|
|
54
|
+
ENDPOINT_ORDER = %w[path verb route ok_code resource timeout on_bad_code mime_type default_headers default_params].freeze
|
|
55
|
+
|
|
56
|
+
# calls: write beside each endpoint how it is called in Ruby.
|
|
57
|
+
def initialize(file, name: nil, version: DEFAULT_VERSION, calls: false)
|
|
58
|
+
@file = file
|
|
59
|
+
@wanted = name
|
|
60
|
+
@version = version
|
|
61
|
+
@calls = calls
|
|
62
|
+
# The mark of a tag: random, so that no file can write one.
|
|
63
|
+
@mark = "__API_RECIPES_ERB_#{SecureRandom.hex(8)}_"
|
|
64
|
+
@token = /#{@mark}(\d+)__/
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def call
|
|
68
|
+
raise Unusable, '--version: must be made of digits, letters and dots, such as 1.2.0' unless version_valid?
|
|
69
|
+
raise Unusable, "#{shown(@file)}: is already called #{Internals::Recipe::SUFFIX}: check it with `api_recipes check`" if @file.end_with?(Internals::Recipe::SUFFIX)
|
|
70
|
+
|
|
71
|
+
@changes = []
|
|
72
|
+
@variables = {}
|
|
73
|
+
@origins = {}
|
|
74
|
+
@envs = {}
|
|
75
|
+
@expressions = {}
|
|
76
|
+
@verify_with = []
|
|
77
|
+
@secret = []
|
|
78
|
+
@given = {}
|
|
79
|
+
@refs = {}
|
|
80
|
+
name, options = api(parsed(plain(source)))
|
|
81
|
+
@name = name
|
|
82
|
+
declared = options['variables']
|
|
83
|
+
refuse('variables: must be a mapping of names') unless declared.nil? || declared.is_a?(Hash)
|
|
84
|
+
(declared || {}).each_key { |variable| @origins[variable.to_s] = :declared }
|
|
85
|
+
refuse('the name of the api must be made of lowercase letters, digits, - and _ to be the name of a recipe') unless Internals::Recipe::NAME.match?(name)
|
|
86
|
+
refuse('already has the key recipe: rename the file to NAME.recipe.yml and check it') if options.key?('recipe')
|
|
87
|
+
|
|
88
|
+
body = api_options(options)
|
|
89
|
+
body = { 'recipe' => { 'name' => name, 'version' => @version, 'format' => Internals::Recipe::FORMAT } }.merge(body)
|
|
90
|
+
# A variable that has no default needs no declaration: the recipe
|
|
91
|
+
# uses it, and that makes it a variable that a value must be given to.
|
|
92
|
+
written = @variables.reject { |_, spec| spec.empty? }
|
|
93
|
+
body['variables'] = (body['variables'] || {}).merge(written) unless written.empty?
|
|
94
|
+
options = ordered(body, API_ORDER)
|
|
95
|
+
text = Text.new(name, options).to_yaml
|
|
96
|
+
definition = checked(name, text)
|
|
97
|
+
# The calls are those of the recipe just checked. The text is read
|
|
98
|
+
# back when it is written: the comments leave the data as it is.
|
|
99
|
+
text = Text.new(name, options, Calls.of(definition)).to_yaml if @calls
|
|
100
|
+
Result.new(name: name, version: @version, text: text, calls: definition.routes.size, changes: @changes,
|
|
101
|
+
variables: env_of(false).merge(@given), defaults: env_of(true).slice(*@expressions.keys), expressions: @expressions,
|
|
102
|
+
verify_with: @verify_with,
|
|
103
|
+
refs: @refs.to_h { |variable, ref| [variable, ref.merge(credential: ref[:credential] || @secret.include?(variable))] })
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# What is said after the recipe is written in path: what changed from
|
|
107
|
+
# the file, and what the application gives from now on. It holds names
|
|
108
|
+
# only, never a value of the file.
|
|
109
|
+
def self.report(result, source, path, out)
|
|
110
|
+
calls = result.calls
|
|
111
|
+
out.puts "#{result.name} #{result.version} is written in #{path}: #{calls} call#{'s' unless calls == 1}"
|
|
112
|
+
unless result.changes.empty?
|
|
113
|
+
out.puts '', "What changed from #{Recipes::Summary.shown(source, 100)}"
|
|
114
|
+
result.changes.each { |change| out.puts " #{change}" }
|
|
115
|
+
end
|
|
116
|
+
out.puts '', 'In the file that calls ApiRecipes.configure, inside its block:'
|
|
117
|
+
Code.show(out, Snippet.new(result, source).lines)
|
|
118
|
+
unless result.defaults.empty?
|
|
119
|
+
out.puts "Variables that have a default in the recipe: #{result.defaults.keys.join(', ')}. .compact leaves out " \
|
|
120
|
+
'the ones whose code gives nil.', ''
|
|
121
|
+
end
|
|
122
|
+
out.puts "Read the file, then check it: api_recipes check #{path}"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The apis that text describes, with every ERB tag read as the same
|
|
126
|
+
# word: what two texts must share to describe the same thing.
|
|
127
|
+
def self.shape(text)
|
|
128
|
+
plain = text.gsub(COMMENT, '').gsub(OUTPUT, '__API_RECIPES_ERB__')
|
|
129
|
+
raise Refused.new(['an ERB tag decides the structure of the file']) if plain.gsub('<%%', '').include?('<%')
|
|
130
|
+
|
|
131
|
+
data = YAML.safe_load(plain, permitted_classes: [Symbol], aliases: true)
|
|
132
|
+
data.is_a?(Hash) ? data.transform_keys(&:to_s) : {}
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
private
|
|
136
|
+
|
|
137
|
+
def shown(text) = Recipes::Summary.shown(text, 100)
|
|
138
|
+
|
|
139
|
+
def refuse(reasons) = raise(Refused.new(Array(reasons).map { |reason| "#{shown(@file)}: #{reason}" }))
|
|
140
|
+
|
|
141
|
+
def version_valid?
|
|
142
|
+
@version.is_a?(String) && @version.length <= Internals::Recipe::MAX_VERSION && Internals::Recipe::VERSION.match?(@version)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def source
|
|
146
|
+
raise Unusable, "#{shown(@file)}: no such file" unless File.exist?(@file)
|
|
147
|
+
raise Unusable, "#{shown(@file)}: is not a file that can be read" unless File.file?(@file) && File.readable?(@file)
|
|
148
|
+
raise Unusable, "#{shown(@file)}: is larger than a recipe can be" if File.size(@file) > Internals::Recipe::MAX_BYTES
|
|
149
|
+
|
|
150
|
+
text = File.binread(@file).force_encoding(Encoding::UTF_8)
|
|
151
|
+
refuse('is not text') if text.include?("\0") || !text.valid_encoding?
|
|
152
|
+
text
|
|
153
|
+
rescue SystemCallError => e
|
|
154
|
+
raise Unusable, "#{shown(@file)}: cannot be read (#{e.class.name.split('::').last})"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The text with each output tag replaced by a token, and the tags kept
|
|
158
|
+
# aside. Nothing of a tag is run.
|
|
159
|
+
def plain(text)
|
|
160
|
+
@tags = []
|
|
161
|
+
plain = text.gsub(COMMENT) { |comment| comment.gsub(/[^\n]/, '') }
|
|
162
|
+
plain = plain.gsub(OUTPUT) do
|
|
163
|
+
code = Regexp.last_match(1)
|
|
164
|
+
@tags << tag(code, Regexp.last_match.pre_match.count("\n") + 1)
|
|
165
|
+
"#{@mark}#{@tags.size - 1}__#{"\n" * code.count("\n")}"
|
|
166
|
+
end
|
|
167
|
+
left = plain.gsub('<%%', '')
|
|
168
|
+
return plain.gsub('<%%', '<%') unless left.include?('<%')
|
|
169
|
+
|
|
170
|
+
lines = left.each_line.with_index(1).select { |line, _| line.include?('<%') }.map(&:last)
|
|
171
|
+
raise Refused.new(lines.map do |line|
|
|
172
|
+
"#{shown(@file)}:#{line}: an ERB tag that is not <%= ... %> decides the structure of the file: " \
|
|
173
|
+
'write the file without it first'
|
|
174
|
+
end)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# What can be known of a tag without running it: the environment
|
|
178
|
+
# variable it reads and the default written beside it.
|
|
179
|
+
def tag(code, line)
|
|
180
|
+
code = code.strip
|
|
181
|
+
match = code.length <= MAX_TAG && (ENV_FETCH.match(code) || ENV_INDEX.match(code))
|
|
182
|
+
return Tag.new(line: line, env: nil, default: nil, code: code) unless match
|
|
183
|
+
|
|
184
|
+
Tag.new(line: line, env: match[1] || match[2], default: literal(match[3]), code: code)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def literal(code)
|
|
188
|
+
case code
|
|
189
|
+
when /\A'([^'\\]*)'\z/, /\A"([^"\\#]*)"\z/ then Regexp.last_match(1)
|
|
190
|
+
when /\A\d+\z/ then code.to_i
|
|
191
|
+
when /\A\d+\.\d+\z/ then code.to_f
|
|
192
|
+
when 'true' then true
|
|
193
|
+
when 'false' then false
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def parsed(text)
|
|
198
|
+
data = YAML.safe_load(text, permitted_classes: [Symbol], aliases: true)
|
|
199
|
+
refuse('does not describe an api') unless data.is_a?(Hash) && !data.empty?
|
|
200
|
+
bounded(data)
|
|
201
|
+
named = names_from_tags(data)
|
|
202
|
+
unless named.empty?
|
|
203
|
+
raise Refused.new(named.map { |line| "#{shown(@file)}:#{line}: an ERB tag gives a name: a recipe needs the name written" })
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
stringify(data)
|
|
207
|
+
rescue Psych::SyntaxError => e
|
|
208
|
+
refuse("is not valid YAML, at line #{e.line}")
|
|
209
|
+
rescue Psych::Exception
|
|
210
|
+
refuse('is not valid YAML')
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Aliases make a huge structure from a few lines: it is counted before
|
|
214
|
+
# anything walks it whole.
|
|
215
|
+
def bounded(value, seen = [0])
|
|
216
|
+
seen[0] += 1
|
|
217
|
+
refuse('describes more values than a recipe can hold') if seen[0] > MAX_VALUES
|
|
218
|
+
case value
|
|
219
|
+
when Hash then value.each { |key, inner| bounded(key, seen) && bounded(inner, seen) }
|
|
220
|
+
when Array then value.each { |inner| bounded(inner, seen) }
|
|
221
|
+
end
|
|
222
|
+
true
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# The lines of the tags that stand where a name does.
|
|
226
|
+
def names_from_tags(value, found = [])
|
|
227
|
+
case value
|
|
228
|
+
when Hash
|
|
229
|
+
value.each do |key, inner|
|
|
230
|
+
key.to_s.scan(@token) { found << @tags[Regexp.last_match(1).to_i].line }
|
|
231
|
+
names_from_tags(inner, found)
|
|
232
|
+
end
|
|
233
|
+
when Array then value.each { |inner| names_from_tags(inner, found) }
|
|
234
|
+
end
|
|
235
|
+
found
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def stringify(value)
|
|
239
|
+
case value
|
|
240
|
+
when Hash then value.to_h { |key, inner| [key.to_s, stringify(inner)] }
|
|
241
|
+
when Array then value.map { |inner| stringify(inner) }
|
|
242
|
+
when Symbol then value.to_s
|
|
243
|
+
else value
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def api(data)
|
|
248
|
+
names = data.keys
|
|
249
|
+
if @wanted
|
|
250
|
+
raise Unusable, "#{shown(@file)}: does not describe an api called #{shown(@wanted)}" unless data.key?(@wanted)
|
|
251
|
+
|
|
252
|
+
refuse('does not describe an api') unless data[@wanted].nil? || data[@wanted].is_a?(Hash)
|
|
253
|
+
return [@wanted, data[@wanted] || {}]
|
|
254
|
+
end
|
|
255
|
+
unless names.size == 1
|
|
256
|
+
raise Unusable, "#{shown(@file)}: describes #{names.size} apis (#{names.map { |name| shown(name) }.join(', ')}): choose one with --name"
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
refuse('does not describe an api') unless data[names.first].is_a?(Hash)
|
|
260
|
+
[names.first, data[names.first]]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def api_options(options)
|
|
264
|
+
out = {}
|
|
265
|
+
options.each do |key, value|
|
|
266
|
+
case key
|
|
267
|
+
when 'verify_with' then left_out(key, value)
|
|
268
|
+
when 'variables' then out[key] = declared_variables(value)
|
|
269
|
+
when 'host' then out[key] = with_variables(value, key, key)
|
|
270
|
+
when 'base_url' then out[key] = with_variables(as_text(value, key), key, key)
|
|
271
|
+
when *OPTION_KEYS then out[key] = option(value, key, key)
|
|
272
|
+
when *SENT then out[key] = sent(value, key)
|
|
273
|
+
when 'endpoints' then out[key] = endpoints(value, key)
|
|
274
|
+
else keep(out, key, value, key)
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
out
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# The variables the file declares already. A default that a tag gives
|
|
281
|
+
# is given by the application from now on.
|
|
282
|
+
def declared_variables(value)
|
|
283
|
+
value.to_h do |name, spec|
|
|
284
|
+
next [name, spec] unless spec.is_a?(Hash) && holds_tag?(spec['default'])
|
|
285
|
+
|
|
286
|
+
match = /\A#{@token.source}\z/.match(spec['default'].to_s)
|
|
287
|
+
refuse("variables.#{shown(name)}.default: an ERB tag gives a part of the value: a recipe needs it written") unless match
|
|
288
|
+
@expressions[name.to_s] = @tags[match[1].to_i].code
|
|
289
|
+
@given[name.to_s] = @tags[match[1].to_i].env
|
|
290
|
+
@refs[name.to_s] = { place: "variables.#{name}.default", line: @tags[match[1].to_i].line, credential: false }
|
|
291
|
+
@changes << "variables.#{shown(name)}.default: came from an ERB tag: the application gives the variable from now on"
|
|
292
|
+
[name, spec.reject { |key, _| key == 'default' }]
|
|
293
|
+
end
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def endpoints(value, where)
|
|
297
|
+
return value unless value.is_a?(Hash)
|
|
298
|
+
|
|
299
|
+
value.to_h do |name, options|
|
|
300
|
+
here = "#{where}.#{name}"
|
|
301
|
+
[name, options.is_a?(Hash) ? ordered(endpoint(options, here, name), ENDPOINT_ORDER) : fixed(options, here)]
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def endpoint(options, where, name)
|
|
306
|
+
out = {}
|
|
307
|
+
options.each do |key, value|
|
|
308
|
+
here = "#{where}.#{key}"
|
|
309
|
+
case key
|
|
310
|
+
when 'verify_with' then left_out(here, value)
|
|
311
|
+
when 'path' then out[key] = with_variables(as_text(value, here), here, "#{name}_path")
|
|
312
|
+
when *OPTION_KEYS then out[key] = option(value, here, "#{name}_#{key}")
|
|
313
|
+
when *SENT then out[key] = sent(value, here)
|
|
314
|
+
when 'endpoints' then out[key] = endpoints(value, here)
|
|
315
|
+
else keep(out, key, value, here)
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
out
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def left_out(where, value)
|
|
322
|
+
@verify_with << [where, holds_tag?(value) ? nil : value.to_s]
|
|
323
|
+
@changes << "#{shown(where)}: left out, a recipe cannot carry it: give it where a class declares the api, " \
|
|
324
|
+
"as `api :#{@name}, verify_with: ...`"
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# Headers and parameters: a tag becomes a variable, and what is named
|
|
328
|
+
# like a credential is never copied.
|
|
329
|
+
def sent(value, where)
|
|
330
|
+
return fixed(value, where) unless value.is_a?(Hash)
|
|
331
|
+
|
|
332
|
+
value.to_h do |name, content|
|
|
333
|
+
here = "#{where}.#{name}"
|
|
334
|
+
if content.to_s.length > Internals::Recipe::MAX_LENGTH
|
|
335
|
+
refuse("#{shown(here)}: is longer than the #{Internals::Recipe::MAX_LENGTH} characters a recipe allows for a value")
|
|
336
|
+
end
|
|
337
|
+
secret = CREDENTIAL.match?(name) && !given_by_variables?(content)
|
|
338
|
+
if !secret && content.is_a?(String) && Code::UNSAFE.match?(content.gsub(@token, ''))
|
|
339
|
+
refuse("#{shown(here)}: holds a character that is not printed: a recipe holds visible text only")
|
|
340
|
+
end
|
|
341
|
+
if secret
|
|
342
|
+
[name, credential(content, here, name)]
|
|
343
|
+
elsif text_in_braces?(content, escaped: false)
|
|
344
|
+
[name, braces_kept(content, here, name)]
|
|
345
|
+
else
|
|
346
|
+
[name, with_variables(content, here, name)]
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# A word in braces that the file does not declare as a variable: 2.x
|
|
352
|
+
# sent it as text, and 3.0 would read it as a variable. In a path and
|
|
353
|
+
# in the base_url a backslash before the brace already keeps it text;
|
|
354
|
+
# a header and a parameter have no such escape.
|
|
355
|
+
TEXT_IN_BRACES = /(?<!\\)\{([A-Za-z_]\w*)\}/
|
|
356
|
+
|
|
357
|
+
def text_in_braces?(value, escaped: true)
|
|
358
|
+
pattern = escaped ? TEXT_IN_BRACES : Internals::Template::VARIABLE
|
|
359
|
+
value.is_a?(String) && value.scan(pattern).flatten.any? { |name| @origins[name] != :declared }
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# In a path and in the base_url a brace is kept as text with a backslash.
|
|
363
|
+
def as_text(value, where)
|
|
364
|
+
return value unless text_in_braces?(value)
|
|
365
|
+
|
|
366
|
+
value.gsub(TEXT_IN_BRACES) do
|
|
367
|
+
name = Regexp.last_match(1)
|
|
368
|
+
next Regexp.last_match(0) if @origins[name] == :declared
|
|
369
|
+
|
|
370
|
+
@changes << "#{shown(where)}: {#{shown(name)}} was text in 2.x and would be a variable: it is written \\{#{shown(name)}}"
|
|
371
|
+
"\\{#{name}}"
|
|
372
|
+
end
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
# A header or a parameter has no way to write a brace as text: the value
|
|
376
|
+
# becomes the default of a variable, which is sent as it is.
|
|
377
|
+
def braces_kept(content, where, name)
|
|
378
|
+
if @token.match?(content)
|
|
379
|
+
refuse("#{shown(where)}: holds a word in braces, which was text in 2.x, and an ERB tag: a recipe cannot keep both " \
|
|
380
|
+
'as they were; write the value by hand')
|
|
381
|
+
end
|
|
382
|
+
# Its own kind of code: a credential with the same text never shares
|
|
383
|
+
# this variable, nor its default.
|
|
384
|
+
variable = declare(variable_name(name), [:text, content], nil, content)
|
|
385
|
+
@changes << "#{shown(where)}: a word in braces was text in 2.x and would be a variable: the value is the default of " \
|
|
386
|
+
"the variable {#{variable}}"
|
|
387
|
+
"{#{variable}}"
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# A value that is already made of variables only, or of text and
|
|
391
|
+
# variables the file declares, holds nothing to hide.
|
|
392
|
+
def given_by_variables?(content)
|
|
393
|
+
return false unless content.is_a?(String) && !@token.match?(content)
|
|
394
|
+
|
|
395
|
+
names = content.scan(Internals::Template::VARIABLE).flatten
|
|
396
|
+
!names.empty? && names.all? { |name| @origins[name] == :declared }
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def credential(content, where, name)
|
|
400
|
+
tags = content.to_s.scan(@token).flatten.map { |index| @tags[index.to_i] }
|
|
401
|
+
if tags.size == 1 && content.to_s.match?(/\A#{@token.source}\z/)
|
|
402
|
+
variable = variable_for(tags.first, name, where, default: false)
|
|
403
|
+
@changes << "#{shown(where)}: the ERB tag is now the variable {#{variable}}; the value is not in the recipe"
|
|
404
|
+
else
|
|
405
|
+
code = written(content)
|
|
406
|
+
variable = declare(variable_name(name), code, nil, nil)
|
|
407
|
+
@expressions[variable] ||= code
|
|
408
|
+
@refs[variable] ||= { place: where, line: nil, credential: true, literal: true }
|
|
409
|
+
@changes << "#{shown(where)}: looks like a credential: its value is not copied, it is now the variable {#{variable}}"
|
|
410
|
+
end
|
|
411
|
+
"{#{variable}}"
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# A value where a recipe can have a variable.
|
|
415
|
+
def with_variables(value, where, fallback)
|
|
416
|
+
return fixed(value, where) unless value.is_a?(String) && @token.match?(value)
|
|
417
|
+
|
|
418
|
+
value.gsub(@token) do
|
|
419
|
+
tag = @tags[Regexp.last_match(1).to_i]
|
|
420
|
+
variable = variable_for(tag, fallback, where, default: true)
|
|
421
|
+
kept = @variables[variable].key?('default') ? ', with the default written in the tag' : ''
|
|
422
|
+
@changes << "#{shown(where)}: the ERB tag is now the variable {#{variable}}#{kept}"
|
|
423
|
+
"{#{variable}}"
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def variable_for(tag, fallback, where, default:)
|
|
428
|
+
# What is named like a credential in the environment has no default either.
|
|
429
|
+
default &&= !CREDENTIAL.match?(tag.env.to_s)
|
|
430
|
+
name = declare(variable_name(tag.env || fallback), tag.code, tag.env, default ? tag.default : nil)
|
|
431
|
+
@refs[name] ||= { place: where, line: tag.line, credential: !default }
|
|
432
|
+
# A variable that gives a credential has no default, also when the
|
|
433
|
+
# same environment variable is read elsewhere with one.
|
|
434
|
+
@secret << name unless default
|
|
435
|
+
@variables[name] = {} if @secret.include?(name)
|
|
436
|
+
@expressions[name] ||= tag.code
|
|
437
|
+
name
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
# A value written in the file, as the Ruby code that gives it again. A
|
|
441
|
+
# tag inside it stays what it was: an interpolation.
|
|
442
|
+
def written(content)
|
|
443
|
+
return content.to_s.inspect unless content.is_a?(String) && @token.match?(content)
|
|
444
|
+
|
|
445
|
+
parts = content.split(@token).each_slice(2).flat_map do |text, index|
|
|
446
|
+
[text.inspect[1..-2], index && "\#{#{@tags[index.to_i].code}}"]
|
|
447
|
+
end
|
|
448
|
+
"\"#{parts.compact.join}\""
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# One of the options of a request: a tag that gives it whole becomes a
|
|
452
|
+
# variable, which is all a recipe can have there.
|
|
453
|
+
def option(value, where, fallback)
|
|
454
|
+
return value unless holds_tag?(value)
|
|
455
|
+
return with_variables(value, where, fallback) if value.is_a?(String) && value.match?(/\A#{@token.source}\z/)
|
|
456
|
+
|
|
457
|
+
refuse("#{shown(where)}: an ERB tag gives a part of the value: a recipe needs it written, or given whole by one tag")
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
def variable_name(text)
|
|
461
|
+
name = text.to_s.downcase.gsub(/[^a-z0-9_]+/, '_').gsub(/\A_+|_+\z/, '')
|
|
462
|
+
name = "v_#{name}" unless name.match?(/\A[a-z_]/)
|
|
463
|
+
name
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# Declares the variable and returns its name. The same code gives the
|
|
467
|
+
# same variable; another code gives another one, also at the same place
|
|
468
|
+
# or for the same environment variable, so that nothing is lost.
|
|
469
|
+
def declare(name, code, env, default)
|
|
470
|
+
taken = @origins.key(code)
|
|
471
|
+
return taken if taken
|
|
472
|
+
|
|
473
|
+
base = name
|
|
474
|
+
number = 1
|
|
475
|
+
name = "#{base}_#{number += 1}" while @origins.key?(name)
|
|
476
|
+
@origins[name] = code
|
|
477
|
+
@variables[name] = default.nil? ? {} : { 'default' => default.to_s }
|
|
478
|
+
@envs[name] = env
|
|
479
|
+
name
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
def env_of(with_default)
|
|
483
|
+
@variables.select { |_, spec| spec.key?('default') == with_default }.keys.sort.to_h { |name| [name, @envs[name]] }
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# Any other option: a recipe cannot have a variable there.
|
|
487
|
+
def keep(out, key, value, where)
|
|
488
|
+
unless holds_tag?(value)
|
|
489
|
+
out[key] = value
|
|
490
|
+
return
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
tag = value.is_a?(String) && (match = /\A#{@token.source}\z/.match(value)) ? @tags[match[1].to_i] : nil
|
|
494
|
+
if tag && !tag.default.nil?
|
|
495
|
+
out[key] = tag.default
|
|
496
|
+
@changes << "#{shown(where)}: came from an ERB tag: the recipe has the default written in the tag"
|
|
497
|
+
else
|
|
498
|
+
@changes << "#{shown(where)}: came from an ERB tag with no default that can be read without running it: " \
|
|
499
|
+
'left out, so the default of the gem applies; write the value in the recipe if it is always the same'
|
|
500
|
+
end
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
def fixed(value, where)
|
|
504
|
+
return value unless holds_tag?(value)
|
|
505
|
+
|
|
506
|
+
refuse("#{shown(where)}: an ERB tag gives a value that a recipe needs written")
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
def holds_tag?(value)
|
|
510
|
+
case value
|
|
511
|
+
when Hash then value.any? { |key, inner| @token.match?(key.to_s) || holds_tag?(inner) }
|
|
512
|
+
when Array then value.any? { |inner| holds_tag?(inner) }
|
|
513
|
+
when String then @token.match?(value)
|
|
514
|
+
else false
|
|
515
|
+
end
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
def ordered(options, order)
|
|
519
|
+
first = order.select { |key| options.key?(key) }
|
|
520
|
+
(first + (options.keys - first - ['endpoints']) + (options.key?('endpoints') ? ['endpoints'] : [])).to_h { |key| [key, options[key]] }
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
# The result must be a portable recipe. Returns its compiled tree.
|
|
524
|
+
def checked(name, text)
|
|
525
|
+
file_name = "#{name}#{Internals::Recipe::SUFFIX}"
|
|
526
|
+
Internals::Recipe.parse(text, file_name, file_name: file_name).definition
|
|
527
|
+
rescue ConfigurationError => e
|
|
528
|
+
problems = e.errors.map { |problem| problem.delete_prefix("#{file_name}: ") }
|
|
529
|
+
raise Refused.new(["#{shown(@file)} cannot become a portable recipe:"] + problems.map { |problem| " #{problem}" })
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
# The text of the recipe. Every name and every value is written by Ruby's
|
|
533
|
+
# YAML generator, on one line, and the text is read back and compared
|
|
534
|
+
# with the data before it is returned.
|
|
535
|
+
class Text
|
|
536
|
+
INTERNAL = 'internal error: the recipe does not read back as it was written, so nothing was written'
|
|
537
|
+
|
|
538
|
+
COMMENT_COLUMN = 34
|
|
539
|
+
|
|
540
|
+
# calls: { names of an endpoint => how it is called }, written in a
|
|
541
|
+
# comment beside the endpoint.
|
|
542
|
+
def initialize(name, options, calls = {})
|
|
543
|
+
@name = name
|
|
544
|
+
@options = options
|
|
545
|
+
@calls = calls
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
def to_yaml
|
|
549
|
+
@lines = []
|
|
550
|
+
line(0, "#{scalar(@name)}:")
|
|
551
|
+
options(1, @options, [])
|
|
552
|
+
text = "#{@lines.join("\n")}\n"
|
|
553
|
+
raise Refused.new([INTERNAL]) unless YAML.safe_load(text) == { @name => @options }
|
|
554
|
+
|
|
555
|
+
text
|
|
556
|
+
rescue Psych::Exception
|
|
557
|
+
raise Refused.new([INTERNAL])
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
private
|
|
561
|
+
|
|
562
|
+
# The options of the api, or of the endpoint that names holds from the
|
|
563
|
+
# api down. Only the entries of its `endpoints` are endpoints: a
|
|
564
|
+
# header or a parameter with the name of one gets no call.
|
|
565
|
+
def options(indent, body, names)
|
|
566
|
+
body.each do |key, value|
|
|
567
|
+
next write(indent, key, value) unless key.to_s == 'endpoints' && value.is_a?(Hash) && !value.empty?
|
|
568
|
+
|
|
569
|
+
line(indent, "#{scalar(key)}:")
|
|
570
|
+
value.each { |name, inner| endpoint(indent + 1, name, inner, names + [name.to_s]) }
|
|
571
|
+
end
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def endpoint(indent, name, body, names)
|
|
575
|
+
return write(indent, name, body, @calls[names]) unless body.is_a?(Hash) && !body.empty?
|
|
576
|
+
|
|
577
|
+
line(indent, "#{scalar(name)}:", @calls[names])
|
|
578
|
+
options(indent + 1, body, names)
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
def write(indent, key, value, comment = nil)
|
|
582
|
+
written = scalar(key)
|
|
583
|
+
case value
|
|
584
|
+
when Hash
|
|
585
|
+
if value.empty?
|
|
586
|
+
line(indent, "#{written}: {}", comment)
|
|
587
|
+
else
|
|
588
|
+
line(indent, "#{written}:", comment)
|
|
589
|
+
value.each { |name, inner| write(indent + 1, name, inner) }
|
|
590
|
+
end
|
|
591
|
+
when Array then line(indent, "#{written}: [#{value.map { |item| scalar(item) }.join(', ')}]", comment)
|
|
592
|
+
when nil then line(indent, "#{written}:", comment)
|
|
593
|
+
else line(indent, "#{written}: #{scalar(value)}", comment)
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
def line(indent, text, comment = nil)
|
|
598
|
+
text = "#{' ' * indent}#{text}"
|
|
599
|
+
@lines << (comment ? "#{text.ljust(COMMENT_COLUMN - 1)} # #{comment}" : text)
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
def scalar(content)
|
|
603
|
+
plain = content.is_a?(String) || content.is_a?(Integer) || content.is_a?(Float) || content == true || content == false
|
|
604
|
+
raise Refused.new(['a value of the file is not text, a number, true or false: a recipe holds nothing else']) unless plain
|
|
605
|
+
|
|
606
|
+
written = YAML.dump([content], line_width: -1)[/\A---\n- (.*)\n\z/m, 1]
|
|
607
|
+
raise Refused.new(['a name or a value of the file takes more than one line: a recipe holds one line each']) if written.nil? || written.include?("\n")
|
|
608
|
+
|
|
609
|
+
written
|
|
610
|
+
end
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
end
|
|
614
|
+
end
|