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
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'open3'
|
|
4
|
+
require 'digest'
|
|
4
5
|
require 'tmpdir'
|
|
5
6
|
require 'pathname'
|
|
6
7
|
require 'fileutils'
|
|
@@ -26,12 +27,38 @@ module ApiRecipes
|
|
|
26
27
|
# git does not run a program that the repository names.
|
|
27
28
|
GIT = ['git', '-c', 'core.fsmonitor=false', '-c', 'core.hooksPath=/dev/null'].freeze
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
# Whether the file is what git has in its index and in HEAD. The id
|
|
31
|
+
# of the index and the one of HEAD are compared with the id of the
|
|
32
|
+
# bytes of the file, worked out here: unlike `git status`, this runs
|
|
33
|
+
# no filter that the repository is configured with. The bytes with
|
|
34
|
+
# CR LF read as LF count too, as git writes a file with core.autocrlf.
|
|
35
|
+
# path: where the file is read; git is a block that runs git, in the
|
|
36
|
+
# project, with the arguments it is given.
|
|
37
|
+
def self.as_committed?(file, path, &git)
|
|
38
|
+
staged, listed = git.call('ls-files', '-s', '--', file)
|
|
39
|
+
head, found = git.call('rev-parse', '--verify', '-q', "HEAD:./#{Pathname.new(file).cleanpath}")
|
|
40
|
+
format, known = git.call('rev-parse', '--show-object-format')
|
|
41
|
+
return false unless [listed, found].all? { |status| status&.success? }
|
|
42
|
+
|
|
43
|
+
id = staged.lines.size == 1 ? staged.split[1] : nil
|
|
44
|
+
return false unless id && id == head.strip
|
|
45
|
+
|
|
46
|
+
digest = known&.success? && format.strip == 'sha256' ? Digest::SHA256 : Digest::SHA1
|
|
47
|
+
bytes = File.binread(path)
|
|
48
|
+
[bytes, bytes.gsub("\r\n", "\n")].uniq.any? { |one| digest.hexdigest("blob #{one.bytesize}\0#{one}") == id }
|
|
49
|
+
rescue SystemCallError
|
|
50
|
+
false
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# written: { file => text } for the files that the same run of the
|
|
54
|
+
# command already changed, when it converts several apis.
|
|
55
|
+
def initialize(result, source, out:, err:, root: '.', written: {})
|
|
30
56
|
@result = result
|
|
31
57
|
@source = source
|
|
32
58
|
@out = out
|
|
33
59
|
@err = err
|
|
34
60
|
@root = root
|
|
61
|
+
@written = written
|
|
35
62
|
@blocking = []
|
|
36
63
|
@left = []
|
|
37
64
|
@done = []
|
|
@@ -43,6 +70,14 @@ module ApiRecipes
|
|
|
43
70
|
# Returns the exit code: 0 when the project was changed, 1 when it was
|
|
44
71
|
# not, 2 when something could not be written.
|
|
45
72
|
def call
|
|
73
|
+
return refused unless prepare.empty?
|
|
74
|
+
|
|
75
|
+
finish
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Works out every change without writing anything. Returns the
|
|
79
|
+
# reasons why the project cannot be changed: none when it can.
|
|
80
|
+
def prepare
|
|
46
81
|
@project = Upgrade::Project.new(@root)
|
|
47
82
|
inside = Pathname.new(File.expand_path(@source)).relative_path_from(Pathname.new(File.expand_path(@root))).to_s
|
|
48
83
|
@source = inside unless inside.start_with?('..')
|
|
@@ -54,9 +89,42 @@ module ApiRecipes
|
|
|
54
89
|
plan_verify_with
|
|
55
90
|
plan_old_description
|
|
56
91
|
plan_valid
|
|
57
|
-
|
|
92
|
+
# What the changes were worked out from: a file changed after this
|
|
93
|
+
# is not written over.
|
|
94
|
+
@seen = (@planned.keys | [@source, @lock]).select { |file| File.file?(full(file)) }.to_h { |file| [file, File.binread(full(file))] }
|
|
95
|
+
@absent = [@target, @lock].reject { |file| File.exist?(full(file)) || File.symlink?(full(file)) }
|
|
96
|
+
@blocking
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# What prepare worked out: the recipe that is created, the files that
|
|
100
|
+
# are edited, with the text they have and the one they get, and the
|
|
101
|
+
# files that are deleted.
|
|
102
|
+
def preview
|
|
103
|
+
lock = Internals::RecipeLock.read(full(@directory))
|
|
104
|
+
entry = Internals::RecipeLock::Entry.new(version: @result.version, source: nil, commit: nil, sha256: Internals::Recipe.digest(@result.text))
|
|
105
|
+
lock_text = Recipes::LockFile.text(lock.entries.merge(@result.name => entry))
|
|
106
|
+
now = File.exist?(full(@lock)) ? File.read(full(@lock)) : nil
|
|
107
|
+
edited = @planned.select { |_, text| text }.to_h { |file, text| [file, [File.read(full(file)), text]] }
|
|
108
|
+
{ created: { @target => @result.text }.merge(now ? {} : { @lock => lock_text }),
|
|
109
|
+
edited: now ? { @lock => [now, lock_text] }.merge(edited) : edited,
|
|
110
|
+
deleted: @planned.select { |_, text| text.nil? }.keys }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Makes the changes that prepare worked out. Returns the exit code.
|
|
114
|
+
def finish
|
|
115
|
+
changed = @seen.reject { |file, text| File.file?(full(file)) && File.binread(full(file)) == text }.keys +
|
|
116
|
+
@absent.select { |file| File.exist?(full(file)) || File.symlink?(full(file)) }
|
|
117
|
+
unless changed.empty?
|
|
118
|
+
@err.puts "the project was not changed: #{changed.map { |file| shown(file) }.join(', ')} changed after this command " \
|
|
119
|
+
'read it. Nothing was written: run the command again'
|
|
120
|
+
return 2
|
|
121
|
+
end
|
|
122
|
+
# Only a file that git had as it was can be changed again by this
|
|
123
|
+
# run as if it were committed: one that had other changes cannot.
|
|
124
|
+
kept = @planned.keys.select { |file| committed?(file) }
|
|
58
125
|
return 2 unless apply
|
|
59
126
|
|
|
127
|
+
@planned.each { |file, text| text && kept.include?(file) ? @written[file] = text : @written.delete(file) }
|
|
60
128
|
report
|
|
61
129
|
0
|
|
62
130
|
end
|
|
@@ -116,9 +184,10 @@ module ApiRecipes
|
|
|
116
184
|
# Whether git has the file committed as it is now.
|
|
117
185
|
def committed?(file)
|
|
118
186
|
return false unless tracked?(file)
|
|
187
|
+
# As this run left it, after a change made when git had it committed.
|
|
188
|
+
return true if @written.key?(file) && File.read(full(file)) == @written[file]
|
|
119
189
|
|
|
120
|
-
|
|
121
|
-
status&.success? && changes.empty?
|
|
190
|
+
Migration.as_committed?(file, full(file)) { |*arguments| git(*arguments) }
|
|
122
191
|
end
|
|
123
192
|
|
|
124
193
|
def git_reason(file)
|
|
@@ -153,7 +222,7 @@ module ApiRecipes
|
|
|
153
222
|
def place_problems
|
|
154
223
|
directory = full(@directory)
|
|
155
224
|
@blocking << "#{@target} already exists" if File.exist?(full(@target)) || File.symlink?(full(@target))
|
|
156
|
-
@blocking << "#{@directory} is a symbolic link: the recipe would be written outside the project"
|
|
225
|
+
@blocking << "#{@directory} is a symbolic link, or is below one: the recipe would be written outside the project" unless inside?(directory)
|
|
157
226
|
_, ignored = git('check-ignore', '-q', '--', @target)
|
|
158
227
|
@blocking << "#{@target} would be ignored by git: the recipe would not be committed with the rest" if ignored&.success?
|
|
159
228
|
lock_problem
|
|
@@ -165,6 +234,21 @@ module ApiRecipes
|
|
|
165
234
|
@blocking << "#{@result.name} is described in #{also.map { |file| shown(file) }.join(', ')} too: a recipe never takes the place of an api" unless also.empty?
|
|
166
235
|
end
|
|
167
236
|
|
|
237
|
+
# Whether path, or the folder that holds it, is in the project and not
|
|
238
|
+
# below a symbolic link: the nearest part of it that exists is
|
|
239
|
+
# where it is written.
|
|
240
|
+
def inside?(path)
|
|
241
|
+
root = File.expand_path(@root)
|
|
242
|
+
existing = File.expand_path(path)
|
|
243
|
+
existing = File.dirname(existing) until File.exist?(existing) || File.symlink?(existing) || existing == root || existing == File.dirname(existing)
|
|
244
|
+
relative = Pathname.new(existing).relative_path_from(Pathname.new(root)).to_s
|
|
245
|
+
return false if relative.start_with?('..')
|
|
246
|
+
|
|
247
|
+
File.realpath(existing) == (relative == '.' ? File.realpath(root) : File.join(File.realpath(root), relative))
|
|
248
|
+
rescue SystemCallError
|
|
249
|
+
false
|
|
250
|
+
end
|
|
251
|
+
|
|
168
252
|
# A recipes.lock that is there is rewritten with one more line: it must
|
|
169
253
|
# be a plain file that reads as a lock.
|
|
170
254
|
def lock_problem
|
|
@@ -204,7 +288,8 @@ module ApiRecipes
|
|
|
204
288
|
edits = edits.map { |from, to, added_text| [from, to, added_text.gsub("\n", "\r\n")] } if text.include?("\r\n")
|
|
205
289
|
@proposal = [text, Code.edited(text, edits)]
|
|
206
290
|
taken_out = edits.any? { |from, to, _| to > from }
|
|
207
|
-
|
|
291
|
+
note = "recipes_paths and config.values#{'; apis_files_paths, which loads nothing now, is gone' if @gone}"
|
|
292
|
+
plan(@initializer, @proposal.last, note) if taken_out ? removable(@initializer) : addable(@initializer)
|
|
208
293
|
end
|
|
209
294
|
|
|
210
295
|
# The lines that give the variables of this api, one variable a line.
|
|
@@ -311,6 +396,7 @@ module ApiRecipes
|
|
|
311
396
|
covered << found.last
|
|
312
397
|
end
|
|
313
398
|
end
|
|
399
|
+
edits, covered = without_dead_assignments(block, given, text, edits, covered)
|
|
314
400
|
@project.ruby_files.each do |file|
|
|
315
401
|
@project.text(file).to_s.lines.each_with_index do |line, index|
|
|
316
402
|
next unless line.include?(name)
|
|
@@ -322,6 +408,115 @@ module ApiRecipes
|
|
|
322
408
|
edits
|
|
323
409
|
end
|
|
324
410
|
|
|
411
|
+
# When the file that goes was the last one that apis_files_paths
|
|
412
|
+
# loaded, its lines are taken out, if git has the file as it is now;
|
|
413
|
+
# otherwise the person is told, and the lines stay. They stay too when
|
|
414
|
+
# the command cannot tell what the assignment loads.
|
|
415
|
+
def without_dead_assignments(block, given, text, edits, covered)
|
|
416
|
+
return [edits, covered] if given.empty?
|
|
417
|
+
loads = given.map { |node| loads(node.arguments&.arguments&.first) }
|
|
418
|
+
# Gone when nothing it names is left, or when it cannot be read and
|
|
419
|
+
# the project has no other description: never while it names one.
|
|
420
|
+
return [edits, covered] if loads.include?(:something)
|
|
421
|
+
return [edits, covered] unless loads.all?(:nothing) || last_description?
|
|
422
|
+
|
|
423
|
+
lines = text.lines
|
|
424
|
+
whole = given.map do |node|
|
|
425
|
+
first = node.location.start_line
|
|
426
|
+
last = node.location.end_line
|
|
427
|
+
alone = lines[first - 1].b[0...node.location.start_column].strip.empty? && lines[last - 1].b[node.location.end_column..].to_s.strip.empty?
|
|
428
|
+
from = lines[0...first - 1].sum(&:bytesize)
|
|
429
|
+
alone ? [[from, from + lines[first - 1..last - 1].sum(&:bytesize), ''], first..last] : nil
|
|
430
|
+
end
|
|
431
|
+
if whole.all? && committed?(@initializer)
|
|
432
|
+
@gone = true
|
|
433
|
+
return [whole.map(&:first), whole.map(&:last)]
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
@left << "#{block.config}.apis_files_paths, at line #{given.first.location.start_line} of #{shown(@initializer)}, loads no " \
|
|
437
|
+
'file now: you can take it out'
|
|
438
|
+
[edits, covered]
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# Whether the project has no other file that describes an api: what
|
|
442
|
+
# apis_files_paths named is then gone, however it was written.
|
|
443
|
+
def last_description?
|
|
444
|
+
others = @project.config_files.reject { |file| file.end_with?(Internals::Recipe::SUFFIX) }
|
|
445
|
+
whole_file? && others.all? { |file| File.expand_path(file, @root) == File.expand_path(@source, @root) }
|
|
446
|
+
end
|
|
447
|
+
|
|
448
|
+
# What value, given to apis_files_paths, loads once the old file is
|
|
449
|
+
# gone: :nothing, :something (a pattern or a list that gives another
|
|
450
|
+
# description of an api), or :unknown when the command cannot tell.
|
|
451
|
+
def loads(value)
|
|
452
|
+
if pattern?(value)
|
|
453
|
+
found = files_of(value)
|
|
454
|
+
return :unknown if found.nil?
|
|
455
|
+
|
|
456
|
+
return found.any? { |file| description?(file) } ? :something : :nothing
|
|
457
|
+
end
|
|
458
|
+
return combined([loads(value.receiver), loads(value.arguments.arguments.first)]) if value.is_a?(Prism::CallNode) && value.name == :+ && value.arguments&.arguments&.size == 1
|
|
459
|
+
return :unknown unless value.is_a?(Prism::ArrayNode) && value.opening_loc&.slice == '['
|
|
460
|
+
|
|
461
|
+
combined(value.elements.map do |element|
|
|
462
|
+
if written?(element) then named(element)
|
|
463
|
+
else loads(element.is_a?(Prism::SplatNode) ? element.expression : element)
|
|
464
|
+
end
|
|
465
|
+
end)
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# What one file named in a list gives once the old file is gone.
|
|
469
|
+
def named(element)
|
|
470
|
+
return :nothing if element.slice.include?(File.basename(@source))
|
|
471
|
+
|
|
472
|
+
path = written_path(element)
|
|
473
|
+
return :unknown unless path
|
|
474
|
+
|
|
475
|
+
description?(path) ? :something : :nothing
|
|
476
|
+
end
|
|
477
|
+
|
|
478
|
+
# Whether the file describes an api. One that cannot be read is
|
|
479
|
+
# taken as one: the command does not say that it loads nothing.
|
|
480
|
+
def description?(path)
|
|
481
|
+
return false unless File.file?(path)
|
|
482
|
+
|
|
483
|
+
Convert.shape(File.read(path)).values.any? { |options| options.is_a?(Hash) }
|
|
484
|
+
rescue Refused, Psych::Exception, SystemCallError, ArgumentError, EncodingError
|
|
485
|
+
true
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
def combined(states)
|
|
489
|
+
return :something if states.include?(:something)
|
|
490
|
+
|
|
491
|
+
states.all?(:nothing) ? :nothing : :unknown
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# The files that a pattern of Dir finds now, without the recipes and
|
|
495
|
+
# the file that goes; nil when its folder is not written out.
|
|
496
|
+
def files_of(pattern)
|
|
497
|
+
arguments = pattern.arguments&.arguments
|
|
498
|
+
path = arguments&.size == 1 ? written_path(arguments.first) : nil
|
|
499
|
+
return nil unless path
|
|
500
|
+
|
|
501
|
+
Dir.glob(path).select { |file| File.file?(file) }.reject do |file|
|
|
502
|
+
file.end_with?(Internals::Recipe::SUFFIX) || File.expand_path(file) == File.expand_path(full(@source))
|
|
503
|
+
end
|
|
504
|
+
end
|
|
505
|
+
|
|
506
|
+
# The path that node gives, when it is written with text only: 'a/*.yml',
|
|
507
|
+
# File.expand_path('a/*.yml', __dir__), Rails.root.join('a', '*.yml').
|
|
508
|
+
def written_path(node)
|
|
509
|
+
return File.expand_path(node.unescaped, @root) if node.is_a?(Prism::StringNode)
|
|
510
|
+
return nil unless written?(node)
|
|
511
|
+
|
|
512
|
+
texts = node.arguments.arguments.select { |one| one.is_a?(Prism::StringNode) }.map(&:unescaped)
|
|
513
|
+
if node.name == :expand_path
|
|
514
|
+
texts.size == 1 && node.arguments.arguments.size == 2 ? File.expand_path(texts.first, File.dirname(full(@initializer))) : nil
|
|
515
|
+
elsif node.receiver.is_a?(Prism::CallNode) && node.receiver.name == :root && node.receiver.receiver.is_a?(Prism::ConstantReadNode) && node.receiver.receiver.name == :Rails
|
|
516
|
+
File.join(File.expand_path(@root), *texts)
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
325
520
|
# Whether value, given to apis_files_paths, names the file called
|
|
326
521
|
# name. :none when it does not; [edit, lines] to take out the one
|
|
327
522
|
# element that does; nil when the command cannot read value.
|
|
@@ -53,10 +53,12 @@ module ApiRecipes
|
|
|
53
53
|
default_headers default_params].freeze
|
|
54
54
|
ENDPOINT_ORDER = %w[path verb route ok_code resource timeout on_bad_code mime_type default_headers default_params].freeze
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
# calls: write beside each endpoint how it is called in Ruby.
|
|
57
|
+
def initialize(file, name: nil, version: DEFAULT_VERSION, calls: false)
|
|
57
58
|
@file = file
|
|
58
59
|
@wanted = name
|
|
59
60
|
@version = version
|
|
61
|
+
@calls = calls
|
|
60
62
|
# The mark of a tag: random, so that no file can write one.
|
|
61
63
|
@mark = "__API_RECIPES_ERB_#{SecureRandom.hex(8)}_"
|
|
62
64
|
@token = /#{@mark}(\d+)__/
|
|
@@ -75,6 +77,7 @@ module ApiRecipes
|
|
|
75
77
|
@secret = []
|
|
76
78
|
@given = {}
|
|
77
79
|
@refs = {}
|
|
80
|
+
@segments = false
|
|
78
81
|
name, options = api(parsed(plain(source)))
|
|
79
82
|
@name = name
|
|
80
83
|
declared = options['variables']
|
|
@@ -84,11 +87,20 @@ module ApiRecipes
|
|
|
84
87
|
refuse('already has the key recipe: rename the file to NAME.recipe.yml and check it') if options.key?('recipe')
|
|
85
88
|
|
|
86
89
|
body = api_options(options)
|
|
87
|
-
body = { 'recipe' => { 'name' => name, 'version' => @version, 'format' => Internals::Recipe::
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
body = { 'recipe' => { 'name' => name, 'version' => @version, 'format' => @segments ? Internals::Recipe::SEGMENTS_FORMAT : Internals::Recipe::FORMATS.first } }.merge(body)
|
|
91
|
+
# A variable that has no default needs no declaration: the recipe
|
|
92
|
+
# uses it, and that makes it a variable that a value must be given to.
|
|
93
|
+
written = @variables.reject { |_, spec| spec.empty? }
|
|
94
|
+
body['variables'] = (body['variables'] || {}).merge(written) unless written.empty?
|
|
95
|
+
options = ordered(body, API_ORDER)
|
|
96
|
+
text = Text.new(name, options).to_yaml
|
|
97
|
+
definition = checked(name, text)
|
|
98
|
+
# The calls are those of the recipe just checked. The text is read
|
|
99
|
+
# back when it is written: the comments leave the data as it is.
|
|
100
|
+
text = Text.new(name, options, Calls.of(definition)).to_yaml if @calls
|
|
101
|
+
Result.new(name: name, version: @version, text: text, calls: definition.routes.size, changes: @changes,
|
|
102
|
+
variables: env_of(false).merge(@given), defaults: env_of(true).slice(*@expressions.keys), expressions: @expressions,
|
|
103
|
+
verify_with: @verify_with,
|
|
92
104
|
refs: @refs.to_h { |variable, ref| [variable, ref.merge(credential: ref[:credential] || @secret.include?(variable))] })
|
|
93
105
|
end
|
|
94
106
|
|
|
@@ -255,7 +267,8 @@ module ApiRecipes
|
|
|
255
267
|
case key
|
|
256
268
|
when 'verify_with' then left_out(key, value)
|
|
257
269
|
when 'variables' then out[key] = declared_variables(value)
|
|
258
|
-
when 'host'
|
|
270
|
+
when 'host' then out[key] = with_variables(value, key, key)
|
|
271
|
+
when 'base_url' then out[key] = with_variables(as_text(value, key), key, key, star: true)
|
|
259
272
|
when *OPTION_KEYS then out[key] = option(value, key, key)
|
|
260
273
|
when *SENT then out[key] = sent(value, key)
|
|
261
274
|
when 'endpoints' then out[key] = endpoints(value, key)
|
|
@@ -296,7 +309,7 @@ module ApiRecipes
|
|
|
296
309
|
here = "#{where}.#{key}"
|
|
297
310
|
case key
|
|
298
311
|
when 'verify_with' then left_out(here, value)
|
|
299
|
-
when 'path' then out[key] = with_variables(value, here, "#{name}_path")
|
|
312
|
+
when 'path' then out[key] = with_variables(as_text(value, here), here, "#{name}_path", star: true)
|
|
300
313
|
when *OPTION_KEYS then out[key] = option(value, here, "#{name}_#{key}")
|
|
301
314
|
when *SENT then out[key] = sent(value, here)
|
|
302
315
|
when 'endpoints' then out[key] = endpoints(value, here)
|
|
@@ -328,12 +341,58 @@ module ApiRecipes
|
|
|
328
341
|
end
|
|
329
342
|
if secret
|
|
330
343
|
[name, credential(content, here, name)]
|
|
344
|
+
elsif text_in_braces?(content, escaped: false)
|
|
345
|
+
[name, braces_kept(content, here, name)]
|
|
331
346
|
else
|
|
332
347
|
[name, with_variables(content, here, name)]
|
|
333
348
|
end
|
|
334
349
|
end
|
|
335
350
|
end
|
|
336
351
|
|
|
352
|
+
# A word in braces that the file does not declare as a variable: 2.x
|
|
353
|
+
# sent it as text, and 3.0 would read it as a variable. In a path and
|
|
354
|
+
# in the base_url a backslash before the brace already keeps it text;
|
|
355
|
+
# a header and a parameter have no such escape. With a star, {*word},
|
|
356
|
+
# it was text in every version before, declared or not: in a path and
|
|
357
|
+
# in the base_url 3.0 reads it as a variable of several segments.
|
|
358
|
+
TEXT_IN_BRACES = /(?<!\\)\{(\*?)([A-Za-z_]\w*)\}/
|
|
359
|
+
|
|
360
|
+
def text_in_braces?(value, escaped: true)
|
|
361
|
+
return false unless value.is_a?(String)
|
|
362
|
+
return value.scan(Internals::Template::VARIABLE).flatten.any? { |name| @origins[name] != :declared } unless escaped
|
|
363
|
+
|
|
364
|
+
value.scan(TEXT_IN_BRACES).any? { |star, name| !star.empty? || @origins[name] != :declared }
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# In a path and in the base_url a brace is kept as text with a backslash.
|
|
368
|
+
def as_text(value, where)
|
|
369
|
+
return value unless text_in_braces?(value)
|
|
370
|
+
|
|
371
|
+
value.gsub(TEXT_IN_BRACES) do
|
|
372
|
+
star, name = Regexp.last_match.captures
|
|
373
|
+
next Regexp.last_match(0) if star.empty? && @origins[name] == :declared
|
|
374
|
+
|
|
375
|
+
word = "{#{star}#{shown(name)}}"
|
|
376
|
+
@changes << "#{shown(where)}: #{word} was text in 2.x and would be a variable: it is written \\#{word}"
|
|
377
|
+
"\\{#{star}#{name}}"
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# A header or a parameter has no way to write a brace as text: the value
|
|
382
|
+
# becomes the default of a variable, which is sent as it is.
|
|
383
|
+
def braces_kept(content, where, name)
|
|
384
|
+
if @token.match?(content)
|
|
385
|
+
refuse("#{shown(where)}: holds a word in braces, which was text in 2.x, and an ERB tag: a recipe cannot keep both " \
|
|
386
|
+
'as they were; write the value by hand')
|
|
387
|
+
end
|
|
388
|
+
# Its own kind of code: a credential with the same text never shares
|
|
389
|
+
# this variable, nor its default.
|
|
390
|
+
variable = declare(variable_name(name), [:text, content], nil, content)
|
|
391
|
+
@changes << "#{shown(where)}: a word in braces was text in 2.x and would be a variable: the value is the default of " \
|
|
392
|
+
"the variable {#{variable}}"
|
|
393
|
+
"{#{variable}}"
|
|
394
|
+
end
|
|
395
|
+
|
|
337
396
|
# A value that is already made of variables only, or of text and
|
|
338
397
|
# variables the file declares, holds nothing to hide.
|
|
339
398
|
def given_by_variables?(content)
|
|
@@ -359,18 +418,38 @@ module ApiRecipes
|
|
|
359
418
|
end
|
|
360
419
|
|
|
361
420
|
# A value where a recipe can have a variable.
|
|
362
|
-
|
|
421
|
+
# star: in the base_url and in a path, where 2.x put the value of the
|
|
422
|
+
# tag as it was, slashes included: the variable spans several segments,
|
|
423
|
+
# and the recipe asks for the format that knows {*name}.
|
|
424
|
+
def with_variables(value, where, fallback, star: false)
|
|
363
425
|
return fixed(value, where) unless value.is_a?(String) && @token.match?(value)
|
|
364
426
|
|
|
365
427
|
value.gsub(@token) do
|
|
366
428
|
tag = @tags[Regexp.last_match(1).to_i]
|
|
367
429
|
variable = variable_for(tag, fallback, where, default: true)
|
|
368
430
|
kept = @variables[variable].key?('default') ? ', with the default written in the tag' : ''
|
|
369
|
-
@
|
|
370
|
-
"{#{variable}}"
|
|
431
|
+
@segments ||= star
|
|
432
|
+
as_variable = "{#{'*' if star}#{variable}}"
|
|
433
|
+
@changes << "#{shown(where)}: the ERB tag is now the variable #{as_variable}#{kept}"
|
|
434
|
+
refused_default(where, variable) if star
|
|
435
|
+
as_variable
|
|
371
436
|
end
|
|
372
437
|
end
|
|
373
438
|
|
|
439
|
+
# 2.x joined the slashes of a value; a {*name} refuses a value that
|
|
440
|
+
# starts or ends with one, has two in a row or is empty. Said with the
|
|
441
|
+
# place and the name, never with the value.
|
|
442
|
+
def refused_default(where, variable)
|
|
443
|
+
spec = @variables[variable]
|
|
444
|
+
return unless spec.key?('default')
|
|
445
|
+
|
|
446
|
+
Internals::Template.segments(spec['default'], variable, @name)
|
|
447
|
+
rescue InvalidVariable
|
|
448
|
+
@changes << "#{shown(where)}: the default written in the tag is not a value that {*#{variable}} takes: one or more " \
|
|
449
|
+
'segments, with no slash at the start or at the end and no two in a row. Correct it in the recipe ' \
|
|
450
|
+
'and in the code of the tag, which config.values gives'
|
|
451
|
+
end
|
|
452
|
+
|
|
374
453
|
def variable_for(tag, fallback, where, default:)
|
|
375
454
|
# What is named like a credential in the environment has no default either.
|
|
376
455
|
default &&= !CREDENTIAL.match?(tag.env.to_s)
|
|
@@ -467,10 +546,10 @@ module ApiRecipes
|
|
|
467
546
|
(first + (options.keys - first - ['endpoints']) + (options.key?('endpoints') ? ['endpoints'] : [])).to_h { |key| [key, options[key]] }
|
|
468
547
|
end
|
|
469
548
|
|
|
470
|
-
# The result must be a portable recipe. Returns
|
|
549
|
+
# The result must be a portable recipe. Returns its compiled tree.
|
|
471
550
|
def checked(name, text)
|
|
472
551
|
file_name = "#{name}#{Internals::Recipe::SUFFIX}"
|
|
473
|
-
Internals::Recipe.parse(text, file_name, file_name: file_name).definition
|
|
552
|
+
Internals::Recipe.parse(text, file_name, file_name: file_name).definition
|
|
474
553
|
rescue ConfigurationError => e
|
|
475
554
|
problems = e.errors.map { |problem| problem.delete_prefix("#{file_name}: ") }
|
|
476
555
|
raise Refused.new(["#{shown(@file)} cannot become a portable recipe:"] + problems.map { |problem| " #{problem}" })
|
|
@@ -482,14 +561,20 @@ module ApiRecipes
|
|
|
482
561
|
class Text
|
|
483
562
|
INTERNAL = 'internal error: the recipe does not read back as it was written, so nothing was written'
|
|
484
563
|
|
|
485
|
-
|
|
564
|
+
COMMENT_COLUMN = 34
|
|
565
|
+
|
|
566
|
+
# calls: { names of an endpoint => how it is called }, written in a
|
|
567
|
+
# comment beside the endpoint.
|
|
568
|
+
def initialize(name, options, calls = {})
|
|
486
569
|
@name = name
|
|
487
570
|
@options = options
|
|
571
|
+
@calls = calls
|
|
488
572
|
end
|
|
489
573
|
|
|
490
574
|
def to_yaml
|
|
491
575
|
@lines = []
|
|
492
|
-
|
|
576
|
+
line(0, "#{scalar(@name)}:")
|
|
577
|
+
options(1, @options, [])
|
|
493
578
|
text = "#{@lines.join("\n")}\n"
|
|
494
579
|
raise Refused.new([INTERNAL]) unless YAML.safe_load(text) == { @name => @options }
|
|
495
580
|
|
|
@@ -500,23 +585,45 @@ module ApiRecipes
|
|
|
500
585
|
|
|
501
586
|
private
|
|
502
587
|
|
|
503
|
-
|
|
504
|
-
|
|
588
|
+
# The options of the api, or of the endpoint that names holds from the
|
|
589
|
+
# api down. Only the entries of its `endpoints` are endpoints: a
|
|
590
|
+
# header or a parameter with the name of one gets no call.
|
|
591
|
+
def options(indent, body, names)
|
|
592
|
+
body.each do |key, value|
|
|
593
|
+
next write(indent, key, value) unless key.to_s == 'endpoints' && value.is_a?(Hash) && !value.empty?
|
|
594
|
+
|
|
595
|
+
line(indent, "#{scalar(key)}:")
|
|
596
|
+
value.each { |name, inner| endpoint(indent + 1, name, inner, names + [name.to_s]) }
|
|
597
|
+
end
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
def endpoint(indent, name, body, names)
|
|
601
|
+
return write(indent, name, body, @calls[names]) unless body.is_a?(Hash) && !body.empty?
|
|
602
|
+
|
|
603
|
+
line(indent, "#{scalar(name)}:", @calls[names])
|
|
604
|
+
options(indent + 1, body, names)
|
|
605
|
+
end
|
|
606
|
+
|
|
607
|
+
def write(indent, key, value, comment = nil)
|
|
608
|
+
written = scalar(key)
|
|
505
609
|
case value
|
|
506
610
|
when Hash
|
|
507
611
|
if value.empty?
|
|
508
|
-
line(indent, "#{
|
|
612
|
+
line(indent, "#{written}: {}", comment)
|
|
509
613
|
else
|
|
510
|
-
line(indent, "#{
|
|
614
|
+
line(indent, "#{written}:", comment)
|
|
511
615
|
value.each { |name, inner| write(indent + 1, name, inner) }
|
|
512
616
|
end
|
|
513
|
-
when Array then line(indent, "#{
|
|
514
|
-
when nil then line(indent, "#{
|
|
515
|
-
else line(indent, "#{
|
|
617
|
+
when Array then line(indent, "#{written}: [#{value.map { |item| scalar(item) }.join(', ')}]", comment)
|
|
618
|
+
when nil then line(indent, "#{written}:", comment)
|
|
619
|
+
else line(indent, "#{written}: #{scalar(value)}", comment)
|
|
516
620
|
end
|
|
517
621
|
end
|
|
518
622
|
|
|
519
|
-
def line(indent, text
|
|
623
|
+
def line(indent, text, comment = nil)
|
|
624
|
+
text = "#{' ' * indent}#{text}"
|
|
625
|
+
@lines << (comment ? "#{text.ljust(COMMENT_COLUMN - 1)} # #{comment}" : text)
|
|
626
|
+
end
|
|
520
627
|
|
|
521
628
|
def scalar(content)
|
|
522
629
|
plain = content.is_a?(String) || content.is_a?(Integer) || content.is_a?(Float) || content == true || content == false
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'yaml'
|
|
5
|
+
|
|
6
|
+
module ApiRecipes
|
|
7
|
+
module CLI
|
|
8
|
+
# A file of a 2.x project that describes apis: YAML, which may hold ERB.
|
|
9
|
+
# The gem no longer loads such a file: the commands read it to check it,
|
|
10
|
+
# to list its calls and to convert it into a portable recipe.
|
|
11
|
+
module Description
|
|
12
|
+
# Returns the apis of the file as { name => description }. ERB is
|
|
13
|
+
# evaluated only with erb: true; without it a file that contains ERB
|
|
14
|
+
# is given to the block, which returns the text to read, or is refused
|
|
15
|
+
# when there is no block. Nothing in it is run.
|
|
16
|
+
def self.read(path, erb: false)
|
|
17
|
+
unless (path.is_a?(String) || path.is_a?(Pathname)) && !path.to_s.include?("\0")
|
|
18
|
+
raise ConfigurationError.new(['every path must be a String or a Pathname, without null bytes'])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
full_path = File.expand_path(path)
|
|
22
|
+
if shared_recipe?(full_path)
|
|
23
|
+
raise RecipeFileError.new(path, 'is a portable recipe (.recipe.yml): it is read as data only')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
text = read_text(path, full_path)
|
|
27
|
+
if erb
|
|
28
|
+
text = ERB.new(text).result
|
|
29
|
+
elsif text.include?('<%')
|
|
30
|
+
unless block_given?
|
|
31
|
+
raise RecipeFileError.new(path, 'contains ERB: run again with --erb to evaluate it ' \
|
|
32
|
+
'(this executes the code in the file)')
|
|
33
|
+
end
|
|
34
|
+
text = yield(text)
|
|
35
|
+
end
|
|
36
|
+
data = begin
|
|
37
|
+
YAML.safe_load(text, permitted_classes: [Symbol], aliases: true)
|
|
38
|
+
rescue Psych::Exception => e
|
|
39
|
+
raise RecipeFileError.new(path, "invalid YAML: #{e.message}")
|
|
40
|
+
end
|
|
41
|
+
raise RecipeFileError.new(path, 'file is empty') if data.nil?
|
|
42
|
+
raise RecipeFileError.new(path, 'must contain a mapping of api names') unless data.is_a?(Hash)
|
|
43
|
+
|
|
44
|
+
Internals::Compiler.normalize(data, :options_map)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.read_text(path, full_path)
|
|
48
|
+
text = File.read(full_path, encoding: 'UTF-8')
|
|
49
|
+
raise RecipeFileError.new(path, 'file is not valid UTF-8') unless text.valid_encoding?
|
|
50
|
+
|
|
51
|
+
text
|
|
52
|
+
rescue Errno::ENOENT
|
|
53
|
+
raise RecipeFileError.new(path, 'file not found')
|
|
54
|
+
rescue SystemCallError => e
|
|
55
|
+
raise RecipeFileError.new(path, "cannot read file (#{e.class.name.split('::').last})")
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def self.shared_recipe?(full_path)
|
|
59
|
+
names = [full_path]
|
|
60
|
+
names << File.realpath(full_path) if File.exist?(full_path)
|
|
61
|
+
names.any? { |name| name.downcase.end_with?('.recipe.yml') }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -5,7 +5,8 @@ module ApiRecipes
|
|
|
5
5
|
class Import
|
|
6
6
|
# One entry of `servers` as the options of an api: protocol, host, port,
|
|
7
7
|
# base_url and variables. Only what differs from the defaults of the gem
|
|
8
|
-
# is returned.
|
|
8
|
+
# is returned. A recipe needs a host: one that the description does not
|
|
9
|
+
# give is a variable, which the application gives.
|
|
9
10
|
class Server
|
|
10
11
|
Result = Data.define(:options, :warnings)
|
|
11
12
|
|
|
@@ -28,7 +29,7 @@ module ApiRecipes
|
|
|
28
29
|
|
|
29
30
|
def read
|
|
30
31
|
server = chosen
|
|
31
|
-
return Result.new(options: {}, warnings: @warnings) if server.nil?
|
|
32
|
+
return Result.new(options: { host: host_variable('the description names no server') }, warnings: @warnings) if server.nil?
|
|
32
33
|
|
|
33
34
|
@where = "servers[#{@index}]"
|
|
34
35
|
@declared = server['variables'].is_a?(Hash) ? server['variables'] : {}
|
|
@@ -49,7 +50,6 @@ module ApiRecipes
|
|
|
49
50
|
if servers.empty?
|
|
50
51
|
raise Error, "--server #{@index}: the description names no server" unless @index.zero?
|
|
51
52
|
|
|
52
|
-
@warnings << 'the description names no server: set host in the recipe'
|
|
53
53
|
return nil
|
|
54
54
|
end
|
|
55
55
|
unless @index.between?(0, servers.size - 1)
|
|
@@ -63,8 +63,18 @@ module ApiRecipes
|
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def relative(path)
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
options = with_variables({ base_url: base_url(path) }.compact)
|
|
67
|
+
{ host: host_variable('the server address is relative') }.merge(options)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The variable that stands for the host, named so that it is not one
|
|
71
|
+
# of the variables of the address.
|
|
72
|
+
def host_variable(why)
|
|
73
|
+
taken = @names.values
|
|
74
|
+
name = %w[host api_host].find { |one| !taken.include?(one) }
|
|
75
|
+
name ||= (2..).lazy.map { |number| "api_host_#{number}" }.find { |one| !taken.include?(one) }
|
|
76
|
+
@warnings << "#{why}: the host is the variable {#{name}}, to give with config.values"
|
|
77
|
+
"{#{name}}"
|
|
68
78
|
end
|
|
69
79
|
|
|
70
80
|
def absolute(scheme, authority, path)
|
|
@@ -140,11 +150,12 @@ module ApiRecipes
|
|
|
140
150
|
end
|
|
141
151
|
end
|
|
142
152
|
|
|
143
|
-
# Adds the variables that host and base_url use,
|
|
153
|
+
# Adds the declarations of the variables that host and base_url use,
|
|
154
|
+
# in the order they appear. A variable with no default and no
|
|
155
|
+
# description needs none: the recipe uses it.
|
|
144
156
|
def with_variables(options)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
options.merge(variables: @names.to_h { |original, name| [name, declaration(original)] })
|
|
157
|
+
declared = @names.to_h { |original, name| [name, declaration(original)] }.reject { |_, spec| spec.empty? }
|
|
158
|
+
declared.empty? ? options : options.merge(variables: declared)
|
|
148
159
|
end
|
|
149
160
|
|
|
150
161
|
def declaration(original)
|