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,819 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
require 'digest'
|
|
5
|
+
require 'tmpdir'
|
|
6
|
+
require 'pathname'
|
|
7
|
+
require 'fileutils'
|
|
8
|
+
|
|
9
|
+
module ApiRecipes
|
|
10
|
+
module CLI
|
|
11
|
+
class Convert
|
|
12
|
+
# Moves one api of a project to a portable recipe, so that the project
|
|
13
|
+
# works afterwards: the recipe is installed, the code of the ERB tags
|
|
14
|
+
# goes to config.values in the file that calls ApiRecipes.configure, and
|
|
15
|
+
# the old description is removed.
|
|
16
|
+
#
|
|
17
|
+
# Ruby is read as a tree, never matched as lines and never run. Every
|
|
18
|
+
# change is worked out in memory, on the text as the changes before it
|
|
19
|
+
# left it, and each result must be valid Ruby. When a step cannot be
|
|
20
|
+
# done safely nothing is written. A write that fails puts every file
|
|
21
|
+
# back as it was. A file that exists is changed only when it is a plain
|
|
22
|
+
# file of the project that git tracks; lines are taken out of one, and
|
|
23
|
+
# one is deleted, only when git has it committed as it is now.
|
|
24
|
+
class Migration
|
|
25
|
+
SIMPLE_CODE = /\AENV(\[\s*(['"])\w+\2\s*\]|\.fetch\(\s*(['"])\w+\3\s*\))\z/
|
|
26
|
+
PLAIN_CODE = /\A("(?:[^"#\\]|\\.)*"|'[^'\\]*'|\d+)\z/
|
|
27
|
+
# git does not run a program that the repository names.
|
|
28
|
+
GIT = ['git', '-c', 'core.fsmonitor=false', '-c', 'core.hooksPath=/dev/null'].freeze
|
|
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: {})
|
|
56
|
+
@result = result
|
|
57
|
+
@source = source
|
|
58
|
+
@out = out
|
|
59
|
+
@err = err
|
|
60
|
+
@root = root
|
|
61
|
+
@written = written
|
|
62
|
+
@blocking = []
|
|
63
|
+
@left = []
|
|
64
|
+
@done = []
|
|
65
|
+
# The text each file will have: nil for a file that is deleted.
|
|
66
|
+
@planned = {}
|
|
67
|
+
@notes = {}
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Returns the exit code: 0 when the project was changed, 1 when it was
|
|
71
|
+
# not, 2 when something could not be written.
|
|
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
|
|
81
|
+
@project = Upgrade::Project.new(@root)
|
|
82
|
+
inside = Pathname.new(File.expand_path(@source)).relative_path_from(Pathname.new(File.expand_path(@root))).to_s
|
|
83
|
+
@source = inside unless inside.start_with?('..')
|
|
84
|
+
@directory = Recipes::DEFAULT_DIRECTORY
|
|
85
|
+
@target = File.join(@directory, "#{@result.name}#{Internals::Recipe::SUFFIX}")
|
|
86
|
+
@lock = File.join(@directory, Internals::RecipeLock::FILE_NAME)
|
|
87
|
+
place_problems
|
|
88
|
+
plan_initializer
|
|
89
|
+
plan_verify_with
|
|
90
|
+
plan_old_description
|
|
91
|
+
plan_valid
|
|
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) }
|
|
125
|
+
return 2 unless apply
|
|
126
|
+
|
|
127
|
+
@planned.each { |file, text| text && kept.include?(file) ? @written[file] = text : @written.delete(file) }
|
|
128
|
+
report
|
|
129
|
+
0
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
private
|
|
133
|
+
|
|
134
|
+
def shown(text) = Recipes::Summary.shown(text, 100)
|
|
135
|
+
|
|
136
|
+
def full(file) = File.join(@root, file)
|
|
137
|
+
|
|
138
|
+
# The text a file has now, with the changes planned so far.
|
|
139
|
+
def text_of(file)
|
|
140
|
+
return @planned[file] if @planned.key?(file)
|
|
141
|
+
|
|
142
|
+
bytes = File.binread(full(file))
|
|
143
|
+
text = bytes.force_encoding(Encoding::UTF_8)
|
|
144
|
+
text.valid_encoding? && !bytes.include?("\0") ? text : nil
|
|
145
|
+
rescue SystemCallError
|
|
146
|
+
nil
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def plan(file, text, note)
|
|
150
|
+
@planned[file] = text
|
|
151
|
+
(@notes[file] ||= []) << note
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# ---- what git knows ----
|
|
155
|
+
|
|
156
|
+
def git(*arguments)
|
|
157
|
+
Open3.capture2e({ 'GIT_OPTIONAL_LOCKS' => '0' }, *GIT, '-C', @root, *arguments)
|
|
158
|
+
rescue SystemCallError
|
|
159
|
+
['', nil]
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Whether file is a plain file of the project: not a symbolic link, not
|
|
163
|
+
# below one, and not outside the project.
|
|
164
|
+
def plain?(file)
|
|
165
|
+
path = full(file)
|
|
166
|
+
return false if File.symlink?(path) || !File.file?(path)
|
|
167
|
+
|
|
168
|
+
File.realpath(path) == File.join(File.realpath(@root), Pathname.new(file).cleanpath.to_s)
|
|
169
|
+
rescue SystemCallError
|
|
170
|
+
false
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Whether git tracks the file as it does any other: `H` is what it
|
|
174
|
+
# says of a file that is neither skipped nor assumed unchanged.
|
|
175
|
+
def tracked?(file)
|
|
176
|
+
out, status = git('ls-files', '-v', '--error-unmatch', '--', file)
|
|
177
|
+
return false unless plain?(file) && status&.success? && out.start_with?('H ')
|
|
178
|
+
|
|
179
|
+
# A file only announced to git (add -N) has nothing to go back to.
|
|
180
|
+
_, known = git('cat-file', '-e', "HEAD:./#{Pathname.new(file).cleanpath}")
|
|
181
|
+
known&.success? ? true : false
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Whether git has the file committed as it is now.
|
|
185
|
+
def committed?(file)
|
|
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]
|
|
189
|
+
|
|
190
|
+
Migration.as_committed?(file, full(file)) { |*arguments| git(*arguments) }
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def git_reason(file)
|
|
194
|
+
_, status = git('--version')
|
|
195
|
+
return 'git was not found: without it a change could not be seen or undone' unless status&.success?
|
|
196
|
+
return "#{shown(file)} is a symbolic link, or is below one: this command changes only the files of the project" unless plain?(file)
|
|
197
|
+
|
|
198
|
+
nil
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Lines are only added to file: git must know it.
|
|
202
|
+
def addable(file)
|
|
203
|
+
return true if tracked?(file) && File.writable?(full(file))
|
|
204
|
+
|
|
205
|
+
@blocking << (git_reason(file) || "#{shown(file)} is not a file that git tracks and that can be written: " \
|
|
206
|
+
'a change to it could not be seen or undone')
|
|
207
|
+
false
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Lines are taken out of file, or the file is deleted: git must have
|
|
211
|
+
# it as it is now.
|
|
212
|
+
def removable(file)
|
|
213
|
+
return true if committed?(file) && File.writable?(full(file)) && File.writable?(File.dirname(full(file)))
|
|
214
|
+
|
|
215
|
+
@blocking << (git_reason(file) || "#{shown(file)} is not committed in git as it is now, or cannot be written: " \
|
|
216
|
+
'what is removed from it could not be brought back')
|
|
217
|
+
false
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# ---- where the recipe goes ----
|
|
221
|
+
|
|
222
|
+
def place_problems
|
|
223
|
+
directory = full(@directory)
|
|
224
|
+
@blocking << "#{@target} already exists" if File.exist?(full(@target)) || File.symlink?(full(@target))
|
|
225
|
+
@blocking << "#{@directory} is a symbolic link, or is below one: the recipe would be written outside the project" unless inside?(directory)
|
|
226
|
+
_, ignored = git('check-ignore', '-q', '--', @target)
|
|
227
|
+
@blocking << "#{@target} would be ignored by git: the recipe would not be committed with the rest" if ignored&.success?
|
|
228
|
+
lock_problem
|
|
229
|
+
also = (@project.config_files - [Pathname.new(@source).cleanpath.to_s]).select do |file|
|
|
230
|
+
!file.end_with?(Internals::Recipe::SUFFIX) && Convert.shape(@project.text(file).to_s).key?(@result.name)
|
|
231
|
+
rescue Refused, Psych::Exception
|
|
232
|
+
false
|
|
233
|
+
end
|
|
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?
|
|
235
|
+
end
|
|
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
|
+
|
|
252
|
+
# A recipes.lock that is there is rewritten with one more line: it must
|
|
253
|
+
# be a plain file that reads as a lock.
|
|
254
|
+
def lock_problem
|
|
255
|
+
return unless File.exist?(full(@lock)) || File.symlink?(full(@lock))
|
|
256
|
+
return @blocking << "#{@lock} is not a plain file of the project" unless plain?(@lock) && File.writable?(full(@lock))
|
|
257
|
+
|
|
258
|
+
Internals::RecipeLock.read(full(@directory))
|
|
259
|
+
rescue ConfigurationError
|
|
260
|
+
@blocking << "#{@lock} cannot be read as a lock: this command would rewrite it"
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# ---- the file that calls ApiRecipes.configure ----
|
|
264
|
+
|
|
265
|
+
def plan_initializer
|
|
266
|
+
files = @project.ruby_files.select { |file| @project.text(file).to_s.include?('ApiRecipes') && Code::ConfigureBlock.in(@project.text(file)) }
|
|
267
|
+
if files.size != 1
|
|
268
|
+
@blocking << (files.empty? ? 'no Ruby file of the project calls ApiRecipes.configure once' : "ApiRecipes.configure is called in #{files.size} files (#{files.map { |file| shown(file) }.join(', ')})")
|
|
269
|
+
return
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
@initializer = files.first
|
|
273
|
+
text = text_of(@initializer)
|
|
274
|
+
block = Code::ConfigureBlock.in(text)
|
|
275
|
+
if block.problem
|
|
276
|
+
@blocking << "#{shown(@initializer)}: the call to ApiRecipes.configure #{block.problem}"
|
|
277
|
+
return
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
problems = @blocking.size
|
|
281
|
+
added = values_edits(block, text) + recipes_edits(block)
|
|
282
|
+
# What is added goes at one place, in this order.
|
|
283
|
+
comma, added = added.partition { |from, _, _| from != block.end_offset }
|
|
284
|
+
edits = comma + files_edits(block, text)
|
|
285
|
+
edits << [block.end_offset, block.end_offset, added.map(&:last).join] unless added.empty?
|
|
286
|
+
return if @blocking.size > problems
|
|
287
|
+
|
|
288
|
+
edits = edits.map { |from, to, added_text| [from, to, added_text.gsub("\n", "\r\n")] } if text.include?("\r\n")
|
|
289
|
+
@proposal = [text, Code.edited(text, edits)]
|
|
290
|
+
taken_out = edits.any? { |from, to, _| to > from }
|
|
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)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# The lines that give the variables of this api, one variable a line.
|
|
296
|
+
# compact: a variable whose code gives nil is not given.
|
|
297
|
+
def entry(indent, unit)
|
|
298
|
+
pairs = @result.expressions.map do |name, code|
|
|
299
|
+
"#{indent}#{unit}#{name}: #{code.match?(SIMPLE_CODE) || code.match?(PLAIN_CODE) ? code : "(#{code})"}"
|
|
300
|
+
end
|
|
301
|
+
"#{indent}#{Snippet.new(@result, @source).key}: { variables: {\n#{pairs.join(",\n")}\n#{indent}}.compact }\n"
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# The edits that give the values: a new config.values, or one more
|
|
305
|
+
# entry in the Hash it is given. That Hash is edited only when the
|
|
306
|
+
# command can read all of it: every key written as a name, none of
|
|
307
|
+
# them this api, and its closing brace alone at the start of a line.
|
|
308
|
+
def values_edits(block, text)
|
|
309
|
+
return [] if @result.expressions.empty?
|
|
310
|
+
|
|
311
|
+
config = block.config
|
|
312
|
+
unit = block.indent.include?("\t") ? "\t" : ' '
|
|
313
|
+
given = block.assignments(:values)
|
|
314
|
+
if given.empty?
|
|
315
|
+
return blocked("#{config}.values is assigned where the block does not always run it") if block.assigned_anywhere?(:values)
|
|
316
|
+
|
|
317
|
+
return [[block.end_offset, block.end_offset, "#{block.indent}#{config}.values = {\n#{entry("#{block.indent}#{unit}", unit)}#{block.indent}}\n"]]
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
hash = given.size == 1 ? given.first.arguments&.arguments&.first : nil
|
|
321
|
+
return blocked("#{config}.values is not given one Hash written between braces: this command does not edit it") unless hash.is_a?(Prism::HashNode)
|
|
322
|
+
|
|
323
|
+
keys = hash.elements.map { |element| key_of(element) }
|
|
324
|
+
if keys.include?(:unknown)
|
|
325
|
+
return blocked("#{config}.values has a key that is not written as a name, or a **: this command cannot tell whether #{@result.name} is among them")
|
|
326
|
+
end
|
|
327
|
+
return blocked("#{config}.values already has #{@result.name}: this command does not merge into it") if keys.include?(@result.name)
|
|
328
|
+
|
|
329
|
+
closing = hash.closing_loc
|
|
330
|
+
line = text.lines[closing.start_line - 1]
|
|
331
|
+
alone = closing.start_line > hash.opening_loc.start_line && line.b[0...closing.start_column].strip.empty?
|
|
332
|
+
return blocked("#{config}.values is given a Hash whose closing brace does not start a line") unless alone
|
|
333
|
+
|
|
334
|
+
indent = line[/\A[ \t]*/]
|
|
335
|
+
start = closing.start_offset - closing.start_column
|
|
336
|
+
last = hash.elements.last
|
|
337
|
+
comma = last && !text.b[last.location.end_offset...start].include?(',') ? [[last.location.end_offset, last.location.end_offset, ',']] : []
|
|
338
|
+
comma + [[start, start, entry("#{indent}#{unit}", unit)]]
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def blocked(reason)
|
|
342
|
+
@blocking << "#{shown(@initializer)}: #{reason}"
|
|
343
|
+
[]
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# The name of a key of a Hash, or nil when it is not written.
|
|
347
|
+
def key_of(element)
|
|
348
|
+
return :unknown unless element.is_a?(Prism::AssocNode)
|
|
349
|
+
|
|
350
|
+
key = element.key
|
|
351
|
+
key.is_a?(Prism::SymbolNode) || key.is_a?(Prism::StringNode) ? key.unescaped : :unknown
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# The folder of the recipes, from the folder of the file.
|
|
355
|
+
def recipes_glob
|
|
356
|
+
relative = Pathname.new(@directory).relative_path_from(Pathname.new(File.dirname(@initializer))).to_s
|
|
357
|
+
"#{relative}/*#{Internals::Recipe::SUFFIX}"
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
def recipes_edits(block)
|
|
361
|
+
given = block.assignments(:recipes_paths)
|
|
362
|
+
if given.empty?
|
|
363
|
+
return blocked("#{block.config}.recipes_paths is assigned where the block does not always run it") if block.assigned_anywhere?(:recipes_paths)
|
|
364
|
+
|
|
365
|
+
return [[block.end_offset, block.end_offset, "#{block.indent}#{block.config}.recipes_paths = Dir[File.expand_path('#{recipes_glob}', __dir__)]\n"]]
|
|
366
|
+
end
|
|
367
|
+
return [] if given.all? { |node| node.slice.include?(Internals::Recipe::SUFFIX) && node.slice.include?(File.basename(@directory)) }
|
|
368
|
+
|
|
369
|
+
blocked("#{block.config}.recipes_paths is assigned at line #{given.first.location.start_line} in a way this command cannot " \
|
|
370
|
+
"read: it must give #{@target}")
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
# When the old file goes, what names it must go too. The command must
|
|
374
|
+
# be able to read how the project loads its files: apis_files_paths
|
|
375
|
+
# given patterns of Dir, or a list written between brackets, from
|
|
376
|
+
# which the one element that names the file is taken out. Anything
|
|
377
|
+
# else, and any other mention of the file in the code, stops it.
|
|
378
|
+
def files_edits(block, text)
|
|
379
|
+
return [] unless whole_file?
|
|
380
|
+
|
|
381
|
+
name = File.basename(@source)
|
|
382
|
+
given = block.assignments(:apis_files_paths)
|
|
383
|
+
if given.empty?
|
|
384
|
+
blocked("#{block.config}.apis_files_paths is not assigned in the block, or only sometimes: this command cannot see how " \
|
|
385
|
+
"the project loads #{shown(name)}, which would be deleted")
|
|
386
|
+
end
|
|
387
|
+
edits = []
|
|
388
|
+
covered = []
|
|
389
|
+
given.each do |node|
|
|
390
|
+
found = named_in(node.arguments&.arguments&.first, name, text)
|
|
391
|
+
if found.nil?
|
|
392
|
+
blocked("#{block.config}.apis_files_paths is given, at line #{node.location.start_line}, in a way this command cannot " \
|
|
393
|
+
"read: it cannot tell whether it names #{shown(name)}, which would be deleted")
|
|
394
|
+
elsif found != :none
|
|
395
|
+
edits << found.first
|
|
396
|
+
covered << found.last
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
edits, covered = without_dead_assignments(block, given, text, edits, covered)
|
|
400
|
+
@project.ruby_files.each do |file|
|
|
401
|
+
@project.text(file).to_s.lines.each_with_index do |line, index|
|
|
402
|
+
next unless line.include?(name)
|
|
403
|
+
next if file == @initializer && covered.any? { |lines| lines.cover?(index + 1) }
|
|
404
|
+
|
|
405
|
+
@blocking << "#{shown(file)}:#{index + 1} names #{shown(name)}, which would be deleted: take the name out first"
|
|
406
|
+
end
|
|
407
|
+
end
|
|
408
|
+
edits
|
|
409
|
+
end
|
|
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
|
+
|
|
520
|
+
# Whether value, given to apis_files_paths, names the file called
|
|
521
|
+
# name. :none when it does not; [edit, lines] to take out the one
|
|
522
|
+
# element that does; nil when the command cannot read value.
|
|
523
|
+
def named_in(value, name, text)
|
|
524
|
+
return :none if pattern?(value)
|
|
525
|
+
|
|
526
|
+
if value.is_a?(Prism::CallNode) && value.name == :+ && value.arguments&.arguments&.size == 1
|
|
527
|
+
sides = [named_in(value.receiver, name, text), named_in(value.arguments.arguments.first, name, text)]
|
|
528
|
+
return nil if sides.include?(nil) || sides.count { |side| side != :none } > 1
|
|
529
|
+
|
|
530
|
+
return sides.find { |side| side != :none } || :none
|
|
531
|
+
end
|
|
532
|
+
return nil unless value.is_a?(Prism::ArrayNode) && value.opening_loc&.slice == '['
|
|
533
|
+
return nil unless value.elements.all? { |element| written?(element) || pattern?(element.is_a?(Prism::SplatNode) ? element.expression : element) }
|
|
534
|
+
|
|
535
|
+
named = value.elements.select { |element| written?(element) && element.slice.include?(name) }
|
|
536
|
+
return :none if named.empty?
|
|
537
|
+
return nil unless named.size == 1
|
|
538
|
+
|
|
539
|
+
edit = removal(value, named.first, text)
|
|
540
|
+
edit && [edit, named.first.location.start_line..named.first.location.end_line]
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
# Dir['...*...'] or Dir.glob('...*...'): files found when the
|
|
544
|
+
# application starts, not named one by one.
|
|
545
|
+
def pattern?(node)
|
|
546
|
+
node.is_a?(Prism::CallNode) && node.receiver.is_a?(Prism::ConstantReadNode) && node.receiver.name == :Dir &&
|
|
547
|
+
%i[[] glob].include?(node.name) && node.block.nil? && node.slice.include?('*') && !node.slice.include?('#{')
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
# One file named with text only: 'a/b.yml', File.expand_path('a/b.yml', __dir__), Rails.root.join('a', 'b.yml').
|
|
551
|
+
def written?(node)
|
|
552
|
+
return true if node.is_a?(Prism::StringNode)
|
|
553
|
+
return false unless node.is_a?(Prism::CallNode) && %i[expand_path join].include?(node.name) && node.block.nil? && node.arguments
|
|
554
|
+
|
|
555
|
+
node.arguments.arguments.all? { |one| one.is_a?(Prism::StringNode) || (one.is_a?(Prism::CallNode) && one.name == :__dir__) }
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
# The edit that takes element out of its list, or nil when it shares
|
|
559
|
+
# its lines with something else.
|
|
560
|
+
def removal(list, element, text)
|
|
561
|
+
others = list.elements - [element]
|
|
562
|
+
if list.location.start_line == list.location.end_line
|
|
563
|
+
return [list.location.start_offset, list.location.end_offset, "[#{others.map(&:slice).join(', ')}]"]
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
first = element.location.start_line
|
|
567
|
+
last = element.location.end_line
|
|
568
|
+
alone = others.none? { |one| one.location.end_line >= first && one.location.start_line <= last } &&
|
|
569
|
+
list.opening_loc.start_line < first && list.closing_loc.start_line > last
|
|
570
|
+
return nil unless alone
|
|
571
|
+
|
|
572
|
+
lines = text.lines
|
|
573
|
+
from = lines[0...first - 1].sum(&:bytesize)
|
|
574
|
+
[from, from + lines[first - 1..last - 1].sum(&:bytesize), '']
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
# ---- verify_with ----
|
|
578
|
+
|
|
579
|
+
# A recipe cannot carry verify_with: the one of the api goes on each
|
|
580
|
+
# `api :name` that a class or a module declares, right after what the
|
|
581
|
+
# call is given. When one of them is written in a way the command
|
|
582
|
+
# cannot read, none is changed and the step is left to the person.
|
|
583
|
+
def plan_verify_with
|
|
584
|
+
@result.verify_with.each do |place, method|
|
|
585
|
+
calls = place == 'verify_with' && Internals::Compiler::METHOD_NAME.match?(method.to_s) ? declarations : []
|
|
586
|
+
files = calls.map(&:first).uniq
|
|
587
|
+
unless !calls.empty? && calls.all? { |_, node| readable?(node) } && files.all? { |file| tracked?(file) && File.writable?(full(file)) }
|
|
588
|
+
@left << "#{shown(place)}: was left out of the recipe and could not be written in the code: give it where a class " \
|
|
589
|
+
"declares the api, as `api :#{@result.name}, verify_with: ...`, or the answers are no longer verified"
|
|
590
|
+
next
|
|
591
|
+
end
|
|
592
|
+
files.each do |file|
|
|
593
|
+
text = text_of(file)
|
|
594
|
+
edits = declarations_in(text).map { |node| [at(node), at(node), ", verify_with: :#{method}"] }
|
|
595
|
+
plan(file, Code.edited(text, edits), 'verify_with on the declaration of the api')
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
# Where the arguments of a call end: before its closing parenthesis.
|
|
601
|
+
def at(node) = node.closing_loc ? node.closing_loc.start_offset : node.arguments.location.end_offset
|
|
602
|
+
|
|
603
|
+
# [file, node] for each `api :name` of a class or a module that gives
|
|
604
|
+
# no verify_with.
|
|
605
|
+
def declarations(planned: true)
|
|
606
|
+
@project.ruby_files.flat_map do |file|
|
|
607
|
+
text = planned ? text_of(file) : @project.text(file)
|
|
608
|
+
text.to_s.include?('api') ? declarations_in(text).map { |node| [file, node] } : []
|
|
609
|
+
end
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
def declarations_in(text)
|
|
613
|
+
tree = Code.tree(text)
|
|
614
|
+
return [] unless tree
|
|
615
|
+
|
|
616
|
+
owners = Code.nodes(tree) { |node| node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode) }
|
|
617
|
+
owners.flat_map { |owner| declared_in(owner.body) }.uniq
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
# The calls `api :name` in the body of a class or a module: not inside
|
|
621
|
+
# a method, a block or another class, which have a body of their own.
|
|
622
|
+
def declared_in(node, found = [])
|
|
623
|
+
return found if node.nil? || [Prism::DefNode, Prism::BlockNode, Prism::LambdaNode, Prism::ClassNode, Prism::ModuleNode].any? { |kind| node.is_a?(kind) }
|
|
624
|
+
|
|
625
|
+
arguments = node.is_a?(Prism::CallNode) && node.name == :api && node.receiver.nil? ? node.arguments&.arguments : nil
|
|
626
|
+
if arguments && arguments.first.is_a?(Prism::SymbolNode) && arguments.first.unescaped == @result.name
|
|
627
|
+
found << node unless options_of(node)&.include?('verify_with')
|
|
628
|
+
end
|
|
629
|
+
node.compact_child_nodes.each { |child| declared_in(child, found) }
|
|
630
|
+
found
|
|
631
|
+
end
|
|
632
|
+
|
|
633
|
+
# The names of the options a call gives, or nil when it gives them in
|
|
634
|
+
# a way the command cannot read: a **, a Hash in braces, a block.
|
|
635
|
+
def options_of(node)
|
|
636
|
+
rest = node.arguments.arguments.drop(1)
|
|
637
|
+
return nil unless node.block.nil? && rest.all? { |one| one.is_a?(Prism::KeywordHashNode) }
|
|
638
|
+
|
|
639
|
+
keys = rest.flat_map(&:elements).map { |element| element.is_a?(Prism::AssocNode) && element.key.is_a?(Prism::SymbolNode) ? element.key.unescaped : nil }
|
|
640
|
+
keys.include?(nil) ? nil : keys
|
|
641
|
+
end
|
|
642
|
+
|
|
643
|
+
def readable?(node) = !options_of(node).nil?
|
|
644
|
+
|
|
645
|
+
# ---- the old description ----
|
|
646
|
+
|
|
647
|
+
def whole_file?
|
|
648
|
+
@whole_file = Convert.shape(File.read(full(@source))).keys == [@result.name] if @whole_file.nil?
|
|
649
|
+
@whole_file
|
|
650
|
+
rescue Refused, Psych::Exception, SystemCallError
|
|
651
|
+
@whole_file = false
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
def plan_old_description
|
|
655
|
+
return unless removable(@source)
|
|
656
|
+
return plan(@source, nil, nil) if whole_file?
|
|
657
|
+
|
|
658
|
+
text = text_of(@source)
|
|
659
|
+
others = Convert.shape(text)
|
|
660
|
+
lines = text.lines
|
|
661
|
+
start = lines.index { |line| line.match?(/\A(['"]?)#{Regexp.escape(@result.name)}\1:/) }
|
|
662
|
+
finish = start && ((start + 1...lines.size).find { |index| lines[index].match?(/\A\S/) } || lines.size)
|
|
663
|
+
rest = start && (lines[0...start] + lines[finish..]).join
|
|
664
|
+
others.delete(@result.name)
|
|
665
|
+
return plan(@source, rest, "the lines of #{@result.name} are gone") if rest && Convert.shape(rest) == others
|
|
666
|
+
|
|
667
|
+
@blocking << "#{shown(@source)} describes other apis too, and the lines of #{@result.name} could not be taken out of it safely"
|
|
668
|
+
rescue Refused, Psych::Exception
|
|
669
|
+
@blocking << "#{shown(@source)} describes other apis too, and the lines of #{@result.name} could not be taken out of it safely"
|
|
670
|
+
end
|
|
671
|
+
|
|
672
|
+
# Every Ruby file must still be Ruby with its changes.
|
|
673
|
+
def plan_valid
|
|
674
|
+
@planned.each do |file, text|
|
|
675
|
+
next if text.nil? || !file.match?(Upgrade::Project::RUBY) || Code.tree(text)
|
|
676
|
+
|
|
677
|
+
@blocking << "#{shown(file)} would not be valid Ruby after the change"
|
|
678
|
+
end
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
# ---- writing ----
|
|
682
|
+
|
|
683
|
+
# Installs the recipe and writes every planned text. When a step
|
|
684
|
+
# fails, whatever the reason, every file is put back as it was.
|
|
685
|
+
def apply
|
|
686
|
+
paths = [@target, @lock, *@planned.keys].uniq
|
|
687
|
+
before = paths.to_h { |path| [path, File.exist?(full(path)) ? [File.binread(full(path)), File.stat(full(path)).mode] : nil] }
|
|
688
|
+
made = !File.directory?(full(@directory))
|
|
689
|
+
install
|
|
690
|
+
@planned.each { |file, text| text.nil? ? File.delete(full(file)) : File.write(full(file), text) }
|
|
691
|
+
true
|
|
692
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
693
|
+
lost = restore(before, made)
|
|
694
|
+
reason = e.is_a?(Refused) ? e.message : e.class.name.split('::').last
|
|
695
|
+
@err.puts "the project could not be changed (#{reason}): " \
|
|
696
|
+
"#{lost.empty? ? 'every file was put back as it was' : "these files could not be put back: #{lost.map { |path| shown(path) }.join(', ')}"}"
|
|
697
|
+
raise unless e.is_a?(SystemCallError) || e.is_a?(Refused)
|
|
698
|
+
|
|
699
|
+
false
|
|
700
|
+
end
|
|
701
|
+
|
|
702
|
+
# Returns the paths that could not be put back.
|
|
703
|
+
def restore(before, made)
|
|
704
|
+
lost = before.reject do |path, saved|
|
|
705
|
+
if saved
|
|
706
|
+
File.binwrite(full(path), saved.first)
|
|
707
|
+
File.chmod(saved.last & 0o7777, full(path))
|
|
708
|
+
else
|
|
709
|
+
FileUtils.rm_f(full(path))
|
|
710
|
+
end
|
|
711
|
+
true
|
|
712
|
+
rescue SystemCallError
|
|
713
|
+
false
|
|
714
|
+
end.keys
|
|
715
|
+
Dir.rmdir(full(@directory)) if made && Dir.exist?(full(@directory)) && Dir.empty?(full(@directory))
|
|
716
|
+
lost
|
|
717
|
+
end
|
|
718
|
+
|
|
719
|
+
def install
|
|
720
|
+
Dir.mktmpdir do |dir|
|
|
721
|
+
file = File.join(dir, "#{@result.name}#{Internals::Recipe::SUFFIX}")
|
|
722
|
+
File.write(file, @result.text)
|
|
723
|
+
quiet = StringIO.new
|
|
724
|
+
code = Recipes.new(out: quiet, err: quiet, input: StringIO.new).install(file, directory: full(@directory), yes: true)
|
|
725
|
+
raise Refused.new(["#{@target} could not be installed"]) unless code.zero?
|
|
726
|
+
end
|
|
727
|
+
end
|
|
728
|
+
|
|
729
|
+
# ---- what is said ----
|
|
730
|
+
|
|
731
|
+
def refused
|
|
732
|
+
@err.puts "#{shown(@source)} can become a portable recipe, but the project was not changed:"
|
|
733
|
+
@blocking.each { |reason| @err.puts " - #{reason}" }
|
|
734
|
+
@err.puts 'Nothing was written.'
|
|
735
|
+
by_hand
|
|
736
|
+
1
|
|
737
|
+
end
|
|
738
|
+
|
|
739
|
+
# What the command would have done, as steps with the code to paste.
|
|
740
|
+
def by_hand
|
|
741
|
+
@out.puts '', 'What to do by hand', '', '1. Write the recipe and install it:'
|
|
742
|
+
Code.show(@out, ["bundle exec api_recipes convert #{shown(@source)} --to #{@directory}",
|
|
743
|
+
"bundle exec api_recipes install #{@target} --yes"], :shell)
|
|
744
|
+
configure_step
|
|
745
|
+
number = 3
|
|
746
|
+
@result.verify_with.each do |place, method|
|
|
747
|
+
next unless place == 'verify_with'
|
|
748
|
+
|
|
749
|
+
places = declarations(planned: false).map { |file, node| "#{shown(file)}:#{node.location.start_line}" }
|
|
750
|
+
@out.puts "#{number}. #{places.empty? ? 'Where a class declares the api' : "Where the api is declared (#{places.join(', ')})"}, give verify_with:"
|
|
751
|
+
Code.show(@out, ["api :#{@result.name}, verify_with: #{Internals::Compiler::METHOD_NAME.match?(method.to_s) ? ":#{method}" : '...'}"])
|
|
752
|
+
number += 1
|
|
753
|
+
end
|
|
754
|
+
@out.puts "#{number}. Remove the old description: #{old_description}"
|
|
755
|
+
end
|
|
756
|
+
|
|
757
|
+
# The block of ApiRecipes.configure as it is after the change, to put
|
|
758
|
+
# in the place of the one that is there; or, when the command could
|
|
759
|
+
# not work it out, the lines to add.
|
|
760
|
+
def configure_step
|
|
761
|
+
before, after = @proposal && @proposal.map { |text| Code::ConfigureBlock.in(text) }
|
|
762
|
+
if before && after && !Code::UNSAFE.match?(after.lines.join("\n"))
|
|
763
|
+
@out.puts "2. In #{shown(@initializer)}, replace the block of ApiRecipes.configure (lines #{before.first} to #{before.last}) with:"
|
|
764
|
+
return Code.show(@out, after.lines)
|
|
765
|
+
end
|
|
766
|
+
|
|
767
|
+
block = @initializer && Code::ConfigureBlock.in(@project.text(@initializer))
|
|
768
|
+
unless block&.config
|
|
769
|
+
@out.puts "2. In #{@initializer ? shown(@initializer) : 'the file that calls ApiRecipes.configure'}, inside the block of ApiRecipes.configure, add:"
|
|
770
|
+
recipes = @initializer ? "Dir[File.expand_path('#{recipes_glob}', __dir__)]" : "Dir['#{@directory}/*#{Internals::Recipe::SUFFIX}']"
|
|
771
|
+
return Code.show(@out, Snippet.new(@result, @source, recipes: recipes).lines)
|
|
772
|
+
end
|
|
773
|
+
|
|
774
|
+
recipes = block.assigned_anywhere?(:recipes_paths) ? nil : "Dir[File.expand_path('#{recipes_glob}', __dir__)]"
|
|
775
|
+
snippet = Snippet.new(@result, @source, config: block.config, recipes: recipes)
|
|
776
|
+
values = block.line_of(:values) || (block.assigned_anywhere?(:values) ? 'another line' : nil)
|
|
777
|
+
values = "line #{values}" if values.is_a?(Integer)
|
|
778
|
+
if values && !@result.expressions.empty?
|
|
779
|
+
@out.puts "2. In #{shown(@initializer)}, inside the block of ApiRecipes.configure (line #{block.first}), add this to the Hash that " \
|
|
780
|
+
"#{block.config}.values is given at #{values}#{', and the line of recipes_paths' if recipes}:"
|
|
781
|
+
Code.show(@out, snippet.entry + (recipes ? ['', "#{block.config}.recipes_paths = #{recipes}"] : []))
|
|
782
|
+
else
|
|
783
|
+
@out.puts "2. In #{shown(@initializer)}, inside the block of ApiRecipes.configure (line #{block.first}), add:"
|
|
784
|
+
Code.show(@out, snippet.lines)
|
|
785
|
+
end
|
|
786
|
+
end
|
|
787
|
+
|
|
788
|
+
def old_description
|
|
789
|
+
return "delete #{shown(@source)}" if whole_file?
|
|
790
|
+
|
|
791
|
+
lines = File.read(full(@source)).lines
|
|
792
|
+
start = lines.index { |line| line.match?(/\A(['"]?)#{Regexp.escape(@result.name)}\1:/) }
|
|
793
|
+
finish = start && ((start + 1...lines.size).find { |index| lines[index].match?(/\A\S/) } || lines.size)
|
|
794
|
+
"take the lines of #{@result.name} out of #{shown(@source)}#{start ? " (lines #{start + 1} to #{finish})" : ''}"
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
def report
|
|
798
|
+
calls = @result.calls
|
|
799
|
+
@out.puts "#{@result.name} #{@result.version} now comes from a portable recipe: #{calls} call#{'s' unless calls == 1}"
|
|
800
|
+
@out.puts '', 'Changed in the project'
|
|
801
|
+
@out.puts " created #{@target}, with its line in #{@lock}"
|
|
802
|
+
@planned.each do |file, text|
|
|
803
|
+
@out.puts(text.nil? ? " deleted #{shown(file)}" : " edited #{shown(file)}: #{@notes[file].compact.join(', and ')}")
|
|
804
|
+
end
|
|
805
|
+
unless @result.changes.empty?
|
|
806
|
+
@out.puts '', 'What changed in the description'
|
|
807
|
+
@result.changes.each { |change| @out.puts " #{change}" }
|
|
808
|
+
end
|
|
809
|
+
unless @left.empty?
|
|
810
|
+
@out.puts '', 'Left to you'
|
|
811
|
+
@left.each { |one| @out.puts " - #{one}" }
|
|
812
|
+
end
|
|
813
|
+
@out.puts '', 'Every change is in `git status` and `git diff`. Check the recipes with `api_recipes check`, then start ' \
|
|
814
|
+
'the application.'
|
|
815
|
+
end
|
|
816
|
+
end
|
|
817
|
+
end
|
|
818
|
+
end
|
|
819
|
+
end
|