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
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'api_recipes/cli/recipes/summary'
|
|
5
|
+
require 'api_recipes/cli/recipes/git'
|
|
6
|
+
require 'api_recipes/cli/recipes/lock_file'
|
|
7
|
+
|
|
8
|
+
module ApiRecipes
|
|
9
|
+
module CLI
|
|
10
|
+
# The commands for portable recipes: check, install and update.
|
|
11
|
+
class Recipes
|
|
12
|
+
DEFAULT_DIRECTORY = 'config/api_recipes'
|
|
13
|
+
DEFAULT_SOURCE = 'https://github.com/madAle/api_recipes-cookbook'
|
|
14
|
+
Recipe = Internals::Recipe
|
|
15
|
+
RecipeLock = Internals::RecipeLock
|
|
16
|
+
|
|
17
|
+
# A recipe about to be installed: where it comes from, for the lock
|
|
18
|
+
# (source and commit, nil for a file) and for the reader (origin).
|
|
19
|
+
Incoming = Data.define(:recipe, :source, :commit, :origin)
|
|
20
|
+
|
|
21
|
+
# The command ends here, with this exit code and this message.
|
|
22
|
+
class Stop < StandardError
|
|
23
|
+
attr_reader :code
|
|
24
|
+
|
|
25
|
+
def initialize(code, message)
|
|
26
|
+
@code = code
|
|
27
|
+
super(message)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def initialize(out:, err:, input: nil)
|
|
32
|
+
@out = out
|
|
33
|
+
@err = err
|
|
34
|
+
@input = input
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Validates each file as a portable recipe. Without files, compares the
|
|
38
|
+
# recipes of directory with its recipes.lock, as configure does. Returns
|
|
39
|
+
# the exit code: 2 if something could not be read, else 1 if something is
|
|
40
|
+
# not valid or does not match, else 0.
|
|
41
|
+
def check(files, directory: DEFAULT_DIRECTORY)
|
|
42
|
+
return check_directory(directory) if files.empty?
|
|
43
|
+
|
|
44
|
+
files.map { |file| check_file(file) }.max
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Installs the recipe called target from the repository at from, at
|
|
48
|
+
# version or at the head; or the file at target, when target is not the
|
|
49
|
+
# name of a recipe. Shows what the recipe does and asks before writing
|
|
50
|
+
# in directory. Returns the exit code: 1 if the recipe is not acceptable,
|
|
51
|
+
# is not found or the answer is no, 2 if the arguments are wrong or
|
|
52
|
+
# something could not be read or written, else 0.
|
|
53
|
+
def install(target, version: nil, from: nil, directory: DEFAULT_DIRECTORY, yes: false)
|
|
54
|
+
guarded do
|
|
55
|
+
incoming = Recipe::NAME.match?(target) ? fetched(target, version, from || DEFAULT_SOURCE, '--from') : local(target, version, from)
|
|
56
|
+
place(incoming, directory, yes, 'Install')
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Replaces an installed recipe with the one its repository has now, or
|
|
61
|
+
# has at version, after showing what changes. Same exit codes as install.
|
|
62
|
+
def update(name, version: nil, directory: DEFAULT_DIRECTORY, yes: false)
|
|
63
|
+
guarded do
|
|
64
|
+
raise Stop.new(2, "#{Summary.shown(name, 80)} is not the name of a recipe") unless Recipe::NAME.match?(name)
|
|
65
|
+
|
|
66
|
+
readable!(File.join(directory, RecipeLock::FILE_NAME))
|
|
67
|
+
entry = RecipeLock.read(directory)[name]
|
|
68
|
+
raise Stop.new(1, "#{name} is not installed in #{directory}") if entry.nil?
|
|
69
|
+
if entry.source.nil?
|
|
70
|
+
raise Stop.new(1, "#{name} was installed from a file: install the new file with `api_recipes install FILE`")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
place(fetched(name, version, entry.source, "#{File.join(directory, RecipeLock::FILE_NAME)}: the source of #{name}"),
|
|
74
|
+
directory, yes, 'Update')
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def guarded
|
|
81
|
+
yield
|
|
82
|
+
rescue Stop => e
|
|
83
|
+
@err.puts e.message
|
|
84
|
+
e.code
|
|
85
|
+
rescue ConfigurationError => e
|
|
86
|
+
e.errors.each { |problem| @err.puts problem }
|
|
87
|
+
1
|
|
88
|
+
rescue Git::NotFound => e
|
|
89
|
+
@err.puts e.message
|
|
90
|
+
1
|
|
91
|
+
rescue Git::Failure => e
|
|
92
|
+
@err.puts e.message
|
|
93
|
+
2
|
|
94
|
+
rescue SystemCallError => e
|
|
95
|
+
@err.puts "could not write (#{e.class.name.split('::').last}): check the folder and its recipes.lock"
|
|
96
|
+
2
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def fetched(name, version, address, what)
|
|
100
|
+
problem = Git.address_problem(address)
|
|
101
|
+
raise Stop.new(2, "#{what} #{problem}") if problem
|
|
102
|
+
unless version.nil? || (version.length <= Recipe::MAX_VERSION && Recipe::VERSION.match?(version))
|
|
103
|
+
raise Stop.new(2, '--version must be made of digits, letters and dots')
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Said before git connects: the address may come from the lock, and
|
|
107
|
+
# git or ssh may ask something on the terminal for it.
|
|
108
|
+
@out.puts "reading #{name} from #{address}"
|
|
109
|
+
@out.flush if @out.respond_to?(:flush)
|
|
110
|
+
found = Git.new(address, ask: terminal?).fetch(name, version)
|
|
111
|
+
recipe = Recipe.parse(found.text, "#{name} in #{address}", file_name: "#{name}#{Recipe::SUFFIX}")
|
|
112
|
+
if version && recipe.version != version
|
|
113
|
+
raise Stop.new(1, "the tag #{name}-#{version} of #{address} holds version #{recipe.version}: nothing was written")
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
Incoming.new(recipe: recipe, source: address, commit: found.commit, origin: "#{address}, commit #{found.commit[0, 12]}")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Whether someone is there to answer: the input of the command is a terminal.
|
|
120
|
+
def terminal? = @input.respond_to?(:tty?) && @input.tty? ? true : false
|
|
121
|
+
|
|
122
|
+
def local(path, version, from)
|
|
123
|
+
raise Stop.new(2, '--from and --version go with the name of a recipe, not with a file') if version || from
|
|
124
|
+
unless File.file?(path) && File.readable?(path)
|
|
125
|
+
shown = Summary.shown(path)
|
|
126
|
+
raise Stop.new(2, "#{shown}: #{File.file?(path) ? 'cannot be read' : (File.exist?(path) ? 'is not a file' : 'no such file')}")
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
Incoming.new(recipe: Recipe.read(path), source: nil, commit: nil, origin: "the file #{Summary.shown(path)}")
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Shows the recipe, asks, then writes its file and the lock of directory.
|
|
133
|
+
def place(incoming, directory, yes, action)
|
|
134
|
+
recipe = incoming.recipe
|
|
135
|
+
destination = File.join(directory, "#{recipe.name}#{Recipe::SUFFIX}")
|
|
136
|
+
lock_path = File.join(directory, RecipeLock::FILE_NAME)
|
|
137
|
+
[destination, lock_path].each do |path|
|
|
138
|
+
raise Stop.new(2, "#{path} is a link: api_recipes does not write through links") if File.symlink?(path)
|
|
139
|
+
end
|
|
140
|
+
raise Stop.new(2, "#{directory} is not a folder") if File.exist?(directory) && !File.directory?(directory)
|
|
141
|
+
|
|
142
|
+
[destination, lock_path].each { |path| readable!(path) }
|
|
143
|
+
lock = RecipeLock.read(directory)
|
|
144
|
+
entry = RecipeLock::Entry.new(version: recipe.version, source: incoming.source, commit: incoming.commit,
|
|
145
|
+
sha256: recipe.sha256)
|
|
146
|
+
current = installed_text(destination)
|
|
147
|
+
summary = Summary.new(recipe, origin: incoming.origin)
|
|
148
|
+
if current == recipe.text && lock[recipe.name] == entry
|
|
149
|
+
@out.puts "#{summary.title} is already installed in #{directory}: nothing to do"
|
|
150
|
+
return 0
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
show(summary, destination, current, lock[recipe.name])
|
|
154
|
+
confirm("#{action} #{summary.title} in #{directory}?") unless yes
|
|
155
|
+
FileUtils.mkdir_p(directory)
|
|
156
|
+
write(destination, recipe.text) unless current == recipe.text
|
|
157
|
+
write(lock_path, LockFile.text(lock.entries.merge(recipe.name => entry)))
|
|
158
|
+
@out.puts "#{recipe.name} #{recipe.version} is installed in #{destination}"
|
|
159
|
+
0
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def installed_text(destination)
|
|
163
|
+
return nil unless File.exist?(destination)
|
|
164
|
+
raise Stop.new(2, "#{destination} is not a file") unless File.file?(destination)
|
|
165
|
+
|
|
166
|
+
Recipe.read_text(destination).first
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def show(summary, destination, current, entry)
|
|
170
|
+
summary.lines.each { |line| @out.puts line }
|
|
171
|
+
summary.warnings.each { |warning| @out.puts " warning: #{warning}" }
|
|
172
|
+
return if current.nil? || current == summary.text
|
|
173
|
+
|
|
174
|
+
if entry && entry.sha256 != Recipe.digest(current)
|
|
175
|
+
@out.puts " #{destination} was changed after it was installed: those changes will be lost"
|
|
176
|
+
end
|
|
177
|
+
summary.changes_from(installed_summary(current, destination)).each { |line| @out.puts line }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# The summary of the recipe that is installed now; nil when it is not a
|
|
181
|
+
# valid recipe.
|
|
182
|
+
def installed_summary(text, destination)
|
|
183
|
+
Summary.new(Recipe.parse(text, destination))
|
|
184
|
+
rescue ConfigurationError
|
|
185
|
+
nil
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def confirm(question)
|
|
189
|
+
@out.print "#{question} [y/N] "
|
|
190
|
+
@out.flush if @out.respond_to?(:flush)
|
|
191
|
+
answer = @input&.gets.to_s.strip.downcase
|
|
192
|
+
raise Stop.new(1, 'nothing was written') unless %w[y yes].include?(answer)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# The new content takes the place of the file in one step. The bytes are
|
|
196
|
+
# written as they are, on every system: the checksum is of the bytes.
|
|
197
|
+
def write(path, text)
|
|
198
|
+
temporary = "#{path}.#{Process.pid}.tmp"
|
|
199
|
+
file = File.open(temporary, File::WRONLY | File::CREAT | File::EXCL | File::BINARY, 0o644)
|
|
200
|
+
begin
|
|
201
|
+
file.write(text)
|
|
202
|
+
file.close
|
|
203
|
+
File.rename(temporary, path)
|
|
204
|
+
ensure
|
|
205
|
+
file.close unless file.closed?
|
|
206
|
+
File.delete(temporary) if File.exist?(temporary)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# A file that is there and cannot be read stops the command with 2.
|
|
211
|
+
def readable!(path)
|
|
212
|
+
raise Stop.new(2, "#{path}: cannot be read") if File.exist?(path) && !File.readable?(path)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def check_file(file)
|
|
216
|
+
unless File.file?(file) && File.readable?(file)
|
|
217
|
+
@err.puts "#{file}: #{File.file?(file) ? 'cannot be read' : (File.exist?(file) ? 'is not a file' : 'no such file')}"
|
|
218
|
+
return 2
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
describe(file, Recipe.read(file))
|
|
222
|
+
0
|
|
223
|
+
rescue ConfigurationError => e
|
|
224
|
+
e.errors.each { |problem| @err.puts problem }
|
|
225
|
+
1
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
def describe(file, recipe)
|
|
229
|
+
summary = Summary.new(recipe)
|
|
230
|
+
calls = recipe.definition.routes.size
|
|
231
|
+
@out.puts "#{file}: #{summary.title}, #{calls} call#{'s' unless calls == 1}"
|
|
232
|
+
summary.warnings.each { |warning| @err.puts "#{file}: warning: #{warning}" }
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def check_directory(directory)
|
|
236
|
+
unless File.directory?(directory)
|
|
237
|
+
@err.puts "#{directory}: no such folder"
|
|
238
|
+
return 2
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
readable!(File.join(directory, RecipeLock::FILE_NAME))
|
|
242
|
+
lock = RecipeLock.read(directory)
|
|
243
|
+
names = Dir.children(directory).select { |name| name.end_with?(Recipe::SUFFIX) }.sort
|
|
244
|
+
codes = names.map { |name| check_installed(File.join(directory, name), name.delete_suffix(Recipe::SUFFIX), lock) }
|
|
245
|
+
(lock.entries.keys - names.map { |name| name.delete_suffix(Recipe::SUFFIX) }).sort.each do |name|
|
|
246
|
+
@err.puts "#{lock.path}: #{name} is in the lock, but #{File.join(directory, "#{name}#{Recipe::SUFFIX}")} is missing"
|
|
247
|
+
codes << 1
|
|
248
|
+
end
|
|
249
|
+
@out.puts "no recipes in #{directory}" if codes.empty?
|
|
250
|
+
codes.max || 0
|
|
251
|
+
rescue Stop => e
|
|
252
|
+
@err.puts e.message
|
|
253
|
+
e.code
|
|
254
|
+
rescue ConfigurationError => e
|
|
255
|
+
e.errors.each { |problem| @err.puts problem }
|
|
256
|
+
1
|
|
257
|
+
rescue SystemCallError => e
|
|
258
|
+
@err.puts "#{directory}: cannot be read (#{e.class.name.split('::').last})"
|
|
259
|
+
2
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def check_installed(file, name, lock)
|
|
263
|
+
unless File.readable?(file)
|
|
264
|
+
@err.puts "#{file}: cannot be read"
|
|
265
|
+
return 2
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
text, = Recipe.read_text(file)
|
|
269
|
+
problem = lock.problem_for(name, Recipe.digest(text))
|
|
270
|
+
if problem
|
|
271
|
+
@err.puts "#{file}: #{problem} `api_recipes install #{file}`"
|
|
272
|
+
return 1
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
recipe = Recipe.parse(text, file, file_name: File.basename(file))
|
|
276
|
+
describe(file, recipe)
|
|
277
|
+
return 0 if lock[name].version == recipe.version
|
|
278
|
+
|
|
279
|
+
@err.puts "#{lock.path}: #{name} is version #{lock[name].version} in the lock, but the file says #{recipe.version}"
|
|
280
|
+
1
|
|
281
|
+
rescue ConfigurationError => e
|
|
282
|
+
e.errors.each { |one| @err.puts one }
|
|
283
|
+
1
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
module ApiRecipes
|
|
4
4
|
module CLI
|
|
5
5
|
# The routes command: the calls that the recipes in some files offer, one
|
|
6
|
-
# per line, with the verb and the address.
|
|
6
|
+
# per line, with the verb and the address. A file called NAME.recipe.yml is
|
|
7
|
+
# a portable recipe, read as data only.
|
|
7
8
|
class Routes
|
|
8
9
|
def initialize(files, erb: false)
|
|
9
10
|
@files = Array(files)
|
|
@@ -30,9 +31,13 @@ module ApiRecipes
|
|
|
30
31
|
return 2
|
|
31
32
|
end
|
|
32
33
|
|
|
33
|
-
apis =
|
|
34
|
+
apis = if file.end_with?(Internals::Recipe::SUFFIX)
|
|
35
|
+
Internals::Recipe.read(file, compile: false).apis
|
|
36
|
+
else
|
|
37
|
+
Configuration.new.read_recipe_file(file, erb: @erb)
|
|
38
|
+
end
|
|
34
39
|
Internals::Compiler.compile_all(apis).each_value do |api|
|
|
35
|
-
api.routes.each { |route| rows << [route.call, route.verb
|
|
40
|
+
api.routes.each { |route| rows << [route.call, Internals::OptionValue.shown_verb(route.verb), route.url] }
|
|
36
41
|
end
|
|
37
42
|
0
|
|
38
43
|
rescue RecipeFileError => e
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yaml'
|
|
4
|
+
|
|
5
|
+
module ApiRecipes
|
|
6
|
+
module CLI
|
|
7
|
+
class Upgrade
|
|
8
|
+
# The project in a directory, as `api_recipes upgrade` without arguments
|
|
9
|
+
# sees it: its Ruby files, the files that describe an api and the names
|
|
10
|
+
# of its apis. It reads files and never runs one, and it does not follow
|
|
11
|
+
# a symbolic link.
|
|
12
|
+
class Project
|
|
13
|
+
# Top-level folders that hold no code of the project.
|
|
14
|
+
SKIPPED = %w[vendor node_modules tmp log coverage public storage].freeze
|
|
15
|
+
# Folders skipped wherever they are.
|
|
16
|
+
SKIPPED_ANYWHERE = %w[node_modules].freeze
|
|
17
|
+
# Top-level folders whose YAML files are fixtures, not the configuration.
|
|
18
|
+
NO_CONFIGURATION = %w[spec test features].freeze
|
|
19
|
+
RUBY = /\.(rb|rake)\z/
|
|
20
|
+
YAML_FILE = /\.(yml|yaml)(\.erb)?\z/
|
|
21
|
+
# The line of a file that ERB shapes, which cannot be parsed: enough
|
|
22
|
+
# to report it.
|
|
23
|
+
ENDPOINTS_LINE = /^[ \t]+endpoints:[ \t]*(#.*)?$/
|
|
24
|
+
DECLARATION = /^[ \t]*api[ \t(]+:([A-Za-z_]\w*)(?:.*?\bas:[ \t]*[:'"]([A-Za-z_]\w*))?/
|
|
25
|
+
# A file larger than this is not a description of an api.
|
|
26
|
+
LARGEST = 2 * 1024 * 1024
|
|
27
|
+
# More values than a description holds: aliases make them from a few lines.
|
|
28
|
+
MAX_VALUES = 200_000
|
|
29
|
+
|
|
30
|
+
# api: the name given to `api`; method: the name it is called by.
|
|
31
|
+
Declared = Data.define(:file, :line, :api, :method)
|
|
32
|
+
|
|
33
|
+
# Paths relative to root, sorted. warnings: what could not be read.
|
|
34
|
+
attr_reader :ruby_files, :config_files, :warnings
|
|
35
|
+
|
|
36
|
+
# Whether data holds no more than limit values, aliases counted each
|
|
37
|
+
# time they are used. It stops as soon as the limit is passed.
|
|
38
|
+
def self.within?(data, limit, seen = [0])
|
|
39
|
+
seen[0] += 1
|
|
40
|
+
return false if seen[0] > limit
|
|
41
|
+
|
|
42
|
+
case data
|
|
43
|
+
when Hash then data.all? { |key, inner| within?(key, limit, seen) && within?(inner, limit, seen) }
|
|
44
|
+
when Array then data.all? { |inner| within?(inner, limit, seen) }
|
|
45
|
+
else true
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def initialize(root = '.')
|
|
50
|
+
@root = root
|
|
51
|
+
@ruby_files = []
|
|
52
|
+
@config_files = []
|
|
53
|
+
@warnings = []
|
|
54
|
+
@texts = {}
|
|
55
|
+
@described = []
|
|
56
|
+
walk(nil)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Rails and most applications load ActiveSupport, which defines the
|
|
60
|
+
# Hash methods that api_recipes 2.x also added.
|
|
61
|
+
def active_support?
|
|
62
|
+
lock = File.join(@root, 'Gemfile.lock')
|
|
63
|
+
File.file?(lock) && File.foreach(lock).any? { |line| line.match?(/\A\s+activesupport \(/) }
|
|
64
|
+
rescue SystemCallError, ArgumentError
|
|
65
|
+
false
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The text of a file of the project, or nil when it is not text.
|
|
69
|
+
def text(file)
|
|
70
|
+
return @texts[file] if @texts.key?(file)
|
|
71
|
+
|
|
72
|
+
@texts[file] = read(file)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The names an api is called by: those the files describe, and those
|
|
76
|
+
# given to `api` in the code, with `as:` when there is one.
|
|
77
|
+
def api_names
|
|
78
|
+
@api_names ||= (described + declarations.map(&:method)).uniq.sort
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The apis the files of the project describe.
|
|
82
|
+
def described = @described.uniq
|
|
83
|
+
|
|
84
|
+
# Where the code declares an api: a line that starts with `api :name`.
|
|
85
|
+
def declarations
|
|
86
|
+
@declarations ||= @ruby_files.flat_map do |file|
|
|
87
|
+
found = []
|
|
88
|
+
text(file).to_s.each_line.with_index(1) do |line, number|
|
|
89
|
+
match = DECLARATION.match(line)
|
|
90
|
+
found << Declared.new(file: file, line: number, api: match[1], method: match[2] || match[1]) if match
|
|
91
|
+
end
|
|
92
|
+
found
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The declarations of an api that no file found here describes, and
|
|
97
|
+
# that do not describe it themselves.
|
|
98
|
+
def undescribed
|
|
99
|
+
return [] if @ruby_files.any? { |file| text(file).to_s.include?('apis_configs') }
|
|
100
|
+
|
|
101
|
+
declarations.reject do |declared|
|
|
102
|
+
described.include?(declared.api) || text(declared.file).to_s.lines[declared.line - 1].to_s.match?(/\b(endpoints|host):/)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Whether the code of a Ruby file names the gem or calls one of the
|
|
107
|
+
# apis. Comment lines are not code.
|
|
108
|
+
def uses_gem?(text)
|
|
109
|
+
code = text.each_line.reject { |line| line.lstrip.start_with?('#') }.join
|
|
110
|
+
return true if code.match?(/\bApiRecipes\b/)
|
|
111
|
+
|
|
112
|
+
names = api_names
|
|
113
|
+
!names.empty? && code.match?(%r{(?<![\w/])(?:#{names.map { |name| Regexp.escape(name) }.join('|')})\b[ \t]*(?:\.[a-z_]|\[)})
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
def walk(directory)
|
|
119
|
+
entries = Dir.children(directory ? File.join(@root, directory) : @root).sort
|
|
120
|
+
entries.each do |name|
|
|
121
|
+
next if name.start_with?('.') || SKIPPED_ANYWHERE.include?(name)
|
|
122
|
+
next if directory.nil? && SKIPPED.include?(name)
|
|
123
|
+
|
|
124
|
+
relative = directory ? File.join(directory, name) : name
|
|
125
|
+
full = File.join(@root, relative)
|
|
126
|
+
next if File.symlink?(full)
|
|
127
|
+
|
|
128
|
+
if File.directory?(full)
|
|
129
|
+
walk(relative)
|
|
130
|
+
elsif File.file?(full)
|
|
131
|
+
keep(relative, name)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
rescue SystemCallError => e
|
|
135
|
+
@warnings << "#{directory || @root}: cannot read (#{e.class.name.split('::').last})"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def keep(relative, name)
|
|
139
|
+
if RUBY.match?(name)
|
|
140
|
+
@ruby_files << relative
|
|
141
|
+
elsif name.end_with?(Internals::Recipe::SUFFIX)
|
|
142
|
+
@config_files << relative
|
|
143
|
+
@described.concat(apis_in(text(relative)) || [])
|
|
144
|
+
elsif YAML_FILE.match?(name) && !NO_CONFIGURATION.include?(relative.split('/').first)
|
|
145
|
+
names = apis_in(text(relative))
|
|
146
|
+
return if names.nil?
|
|
147
|
+
|
|
148
|
+
@config_files << relative
|
|
149
|
+
@described.concat(names)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The names of the apis that text describes, or nil when it describes
|
|
154
|
+
# none. An api is a name with endpoints, each with its options or with
|
|
155
|
+
# none. Tags are read as a word, never run.
|
|
156
|
+
def apis_in(text)
|
|
157
|
+
return nil if text.nil? || !text.include?('endpoints')
|
|
158
|
+
|
|
159
|
+
plain = text.gsub(/<%#.*?%>/m, '').gsub(/<%=.*?%>/m, 'erb')
|
|
160
|
+
return (ENDPOINTS_LINE.match?(text) ? [] : nil) if plain.gsub('<%%', '').include?('<%')
|
|
161
|
+
|
|
162
|
+
data = YAML.safe_load(plain, permitted_classes: [Symbol], aliases: true)
|
|
163
|
+
return nil unless data.is_a?(Hash) && self.class.within?(data, MAX_VALUES)
|
|
164
|
+
|
|
165
|
+
names = data.select { |_, options| api?(options) }.keys.map(&:to_s)
|
|
166
|
+
names.empty? ? nil : names
|
|
167
|
+
rescue Psych::Exception, SystemStackError
|
|
168
|
+
ENDPOINTS_LINE.match?(text) ? [] : nil
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def api?(options)
|
|
172
|
+
return false unless options.is_a?(Hash)
|
|
173
|
+
|
|
174
|
+
endpoints = options['endpoints'] || options[:endpoints]
|
|
175
|
+
endpoints.is_a?(Hash) && endpoints.values.all? { |one| one.nil? || one.is_a?(Hash) }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def read(file)
|
|
179
|
+
full = File.join(@root, file)
|
|
180
|
+
return nil if File.size(full) > LARGEST
|
|
181
|
+
|
|
182
|
+
bytes = File.binread(full)
|
|
183
|
+
return nil if bytes.include?("\0")
|
|
184
|
+
|
|
185
|
+
text = bytes.force_encoding(Encoding::UTF_8)
|
|
186
|
+
text.valid_encoding? ? text : nil
|
|
187
|
+
rescue SystemCallError => e
|
|
188
|
+
@warnings << "#{file}: cannot read (#{e.class.name.split('::').last})"
|
|
189
|
+
nil
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|