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
|
@@ -10,7 +10,7 @@ module ApiRecipes
|
|
|
10
10
|
# Ruby's YAML generator. Before the text is returned it is read back and
|
|
11
11
|
# compared with the data it was written from.
|
|
12
12
|
class Writer
|
|
13
|
-
API_KEYS = %i[protocol host port base_url auth variables].freeze
|
|
13
|
+
API_KEYS = %i[recipe protocol host port base_url auth variables].freeze
|
|
14
14
|
ENDPOINT_KEYS = %i[path verb route ok_code].freeze
|
|
15
15
|
COMMENT_COLUMN = 34
|
|
16
16
|
MAX_NAME = 1000
|
|
@@ -18,11 +18,14 @@ module ApiRecipes
|
|
|
18
18
|
LINE_BREAKING = /[[:cntrl:]\u2028\u2029]/
|
|
19
19
|
INTERNAL = 'internal error: the recipe does not read back as it was written, so nothing was written'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
# calls: { names of an endpoint => how it is called }, written in a
|
|
22
|
+
# comment under the endpoint; nil to write none.
|
|
23
|
+
def initialize(name:, options:, nodes:, header: nil, calls: nil)
|
|
22
24
|
@name = name
|
|
23
25
|
@options = options
|
|
24
26
|
@nodes = nodes
|
|
25
27
|
@header = header
|
|
28
|
+
@calls = calls
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
def to_yaml
|
|
@@ -57,11 +60,13 @@ module ApiRecipes
|
|
|
57
60
|
body.empty? ? nil : body
|
|
58
61
|
end
|
|
59
62
|
|
|
60
|
-
def nodes(list, indent, where)
|
|
63
|
+
def nodes(list, indent, where, names = [])
|
|
61
64
|
list.to_h do |node|
|
|
62
65
|
name = scalar(node.name, where, 'the name of an endpoint')
|
|
63
66
|
comment = Import.one_line(node.comment)
|
|
64
67
|
line(indent, "#{name}:", comment.empty? ? nil : comment)
|
|
68
|
+
called = names + [node.name.to_s]
|
|
69
|
+
call_line(@calls[called]) if @calls && @calls[called]
|
|
65
70
|
here = "#{where}.#{node.name}"
|
|
66
71
|
body = {}
|
|
67
72
|
ENDPOINT_KEYS.each do |key|
|
|
@@ -69,7 +74,7 @@ module ApiRecipes
|
|
|
69
74
|
end
|
|
70
75
|
unless node.children.empty?
|
|
71
76
|
line(indent + 1, 'endpoints:')
|
|
72
|
-
body['endpoints'] = nodes(node.children, indent + 2, "#{here}.endpoints")
|
|
77
|
+
body['endpoints'] = nodes(node.children, indent + 2, "#{here}.endpoints", called)
|
|
73
78
|
end
|
|
74
79
|
[node.name, body.empty? ? nil : body]
|
|
75
80
|
end
|
|
@@ -96,6 +101,13 @@ module ApiRecipes
|
|
|
96
101
|
end
|
|
97
102
|
end
|
|
98
103
|
|
|
104
|
+
# A comment of its own under the line just written, where the comment
|
|
105
|
+
# of that line starts: how the endpoint is called.
|
|
106
|
+
def call_line(call)
|
|
107
|
+
column = @lines.last.index(' # ') || [COMMENT_COLUMN - 1, @lines.last.length].max
|
|
108
|
+
@lines << "#{' ' * column} # #{Import.one_line(call)}"
|
|
109
|
+
end
|
|
110
|
+
|
|
99
111
|
def line(indent, text, comment = nil)
|
|
100
112
|
text = "#{' ' * indent}#{text}"
|
|
101
113
|
text = "#{text.ljust(COMMENT_COLUMN - 1)} # #{comment}" if comment
|
|
@@ -10,15 +10,19 @@ module ApiRecipes
|
|
|
10
10
|
# The description cannot be imported. The message says where and why.
|
|
11
11
|
class Error < ApiRecipes::Error; end
|
|
12
12
|
|
|
13
|
-
Result = Data.define(:text, :warnings, :summary)
|
|
13
|
+
Result = Data.define(:name, :text, :warnings, :summary)
|
|
14
14
|
MAX_WARNINGS = 100
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
# calls: write beside each endpoint how it is called in Ruby.
|
|
17
|
+
# version: the version of the recipe that is written.
|
|
18
|
+
def initialize(path, name: nil, only: [], except: [], server: 0, calls: false, version: Convert::DEFAULT_VERSION)
|
|
19
|
+
@version = version
|
|
17
20
|
@path = path
|
|
18
21
|
@name = name
|
|
19
22
|
@only = only
|
|
20
23
|
@except = except
|
|
21
24
|
@server = server
|
|
25
|
+
@calls = calls
|
|
22
26
|
end
|
|
23
27
|
|
|
24
28
|
# Reads the description and returns the text of the recipe with the
|
|
@@ -30,14 +34,22 @@ module ApiRecipes
|
|
|
30
34
|
server = Server.read(document, @server)
|
|
31
35
|
security = Security.read(document)
|
|
32
36
|
tree = Tree.build(document, only: @only, except: @except)
|
|
33
|
-
|
|
37
|
+
raise Error, '--version: must be made of digits, letters and dots, such as 1.2.0' unless Internals::Recipe::VERSION.match?(@version.to_s)
|
|
38
|
+
|
|
39
|
+
options = { recipe: { 'name' => name, 'version' => @version, 'format' => Internals::Recipe::FORMATS.first } }.merge(server.options)
|
|
34
40
|
options[:auth] = security.auth if security.auth
|
|
35
41
|
header = "Generated by api_recipes import from #{document.title} #{document.version}"
|
|
36
42
|
text = Writer.new(name: name, options: options, nodes: tree.nodes, header: header).to_yaml
|
|
37
|
-
verify(text)
|
|
43
|
+
compiled = verify(text)
|
|
44
|
+
if @calls
|
|
45
|
+
# The calls are those of the recipe just written and checked. The
|
|
46
|
+
# writer reads its text back: the comments leave the data as it is.
|
|
47
|
+
calls = Calls.of(compiled)
|
|
48
|
+
text = Writer.new(name: name, options: options, nodes: tree.nodes, header: header, calls: calls).to_yaml
|
|
49
|
+
end
|
|
38
50
|
warnings += server.warnings + security.warnings + tree.warnings
|
|
39
51
|
warnings << 'no operation was imported' if tree.operations.zero?
|
|
40
|
-
Result.new(text: text, warnings: shown_warnings(warnings), summary: summary(tree, warnings.size))
|
|
52
|
+
Result.new(name: name, text: text, warnings: shown_warnings(warnings), summary: summary(tree, warnings.size))
|
|
41
53
|
end
|
|
42
54
|
|
|
43
55
|
private
|
|
@@ -51,8 +63,8 @@ module ApiRecipes
|
|
|
51
63
|
name = 'api' if name.empty?
|
|
52
64
|
name = "api_#{name}" if name.match?(/\A\d/)
|
|
53
65
|
end
|
|
54
|
-
unless name.is_a?(String) && Internals::Compiler::METHOD_NAME.match?(name)
|
|
55
|
-
raise Error, '--name: must be the name of a method: letters, digits and _, not starting with a digit'
|
|
66
|
+
unless name.is_a?(String) && Internals::Compiler::METHOD_NAME.match?(name) && Internals::Recipe::NAME.match?(name)
|
|
67
|
+
raise Error, '--name: must be the name of a recipe and of a method: lowercase letters, digits and _, not starting with a digit'
|
|
56
68
|
end
|
|
57
69
|
|
|
58
70
|
warnings = []
|
|
@@ -70,9 +82,11 @@ module ApiRecipes
|
|
|
70
82
|
end
|
|
71
83
|
|
|
72
84
|
# The text must be a recipe that the gem compiles.
|
|
85
|
+
# What is written is a portable recipe: it is checked as one. Returns
|
|
86
|
+
# its compiled tree.
|
|
73
87
|
def verify(text)
|
|
74
|
-
|
|
75
|
-
Internals::
|
|
88
|
+
file_name = "#{YAML.safe_load(text).keys.first}#{Internals::Recipe::SUFFIX}"
|
|
89
|
+
Internals::Recipe.parse(text, file_name, file_name: file_name).definition
|
|
76
90
|
rescue ConfigurationError => e
|
|
77
91
|
raise Error, "the recipe would not be valid, so nothing was written:\n#{e.errors.map { |problem| " #{problem}" }.join("\n")}"
|
|
78
92
|
end
|
|
@@ -18,7 +18,7 @@ module ApiRecipes
|
|
|
18
18
|
|
|
19
19
|
# A header or a parameter sent with every request. where: the name of
|
|
20
20
|
# the endpoint that adds it, nil for the api; below: whether that
|
|
21
|
-
# endpoint
|
|
21
|
+
# endpoint contains others, whose requests find it there too.
|
|
22
22
|
Sent = Data.define(:name, :value, :where, :below) do
|
|
23
23
|
def key = [name, where]
|
|
24
24
|
end
|
|
@@ -72,7 +72,7 @@ module ApiRecipes
|
|
|
72
72
|
def address(fill = false)
|
|
73
73
|
host = fill ? filled(@api.host) : @api.host
|
|
74
74
|
base = @api.base_url.to_s
|
|
75
|
-
base = filled(base) if fill
|
|
75
|
+
base = filled(base, Internals::Template::PATH_VARIABLE) if fill
|
|
76
76
|
path = "/#{base}".gsub(%r{/+}, '/').chomp('/')
|
|
77
77
|
protocol = fill ? filled(@api.protocol.to_s) : @api.protocol
|
|
78
78
|
port = fill && @api.port ? filled(@api.port.to_s) : @api.port
|
|
@@ -166,7 +166,7 @@ module ApiRecipes
|
|
|
166
166
|
def sent_row(one) = "#{clip(one.name, PART)}: #{clip(one.value, PART)}#{place(one, PART)}"
|
|
167
167
|
|
|
168
168
|
def place(one, limit)
|
|
169
|
-
one.where ? " (#{clip(one.where, limit)}#{' and
|
|
169
|
+
one.where ? " (#{clip(one.where, limit)}#{' and the endpoints it contains' if one.below})" : ''
|
|
170
170
|
end
|
|
171
171
|
|
|
172
172
|
# What changes between two lists of headers or of parameters, a line
|
|
@@ -216,9 +216,11 @@ module ApiRecipes
|
|
|
216
216
|
found
|
|
217
217
|
end
|
|
218
218
|
|
|
219
|
-
|
|
219
|
+
# The base_url reads {name} and {*name}, with PATH_VARIABLE; the rest
|
|
220
|
+
# reads {name} only. The name is the last group of both.
|
|
221
|
+
def filled(text, pattern = Internals::Template::VARIABLE)
|
|
220
222
|
defaults = @api.variables.transform_values { |spec| spec[:default] }.compact
|
|
221
|
-
text.gsub(
|
|
223
|
+
text.gsub(pattern) { defaults.fetch(Regexp.last_match[-1].to_sym, Regexp.last_match(0)) }
|
|
222
224
|
end
|
|
223
225
|
|
|
224
226
|
# One line for each variable: its default, or that it must be set.
|
|
@@ -171,7 +171,9 @@ module ApiRecipes
|
|
|
171
171
|
summary.warnings.each { |warning| @out.puts " warning: #{warning}" }
|
|
172
172
|
return if current.nil? || current == summary.text
|
|
173
173
|
|
|
174
|
-
if entry
|
|
174
|
+
if entry.nil?
|
|
175
|
+
@out.puts " #{destination} is a recipe that recipes.lock does not name: what it holds now will be lost"
|
|
176
|
+
elsif entry.sha256 != Recipe.digest(current)
|
|
175
177
|
@out.puts " #{destination} was changed after it was installed: those changes will be lost"
|
|
176
178
|
end
|
|
177
179
|
summary.changes_from(installed_summary(current, destination)).each { |line| @out.puts line }
|
|
@@ -274,7 +276,8 @@ module ApiRecipes
|
|
|
274
276
|
|
|
275
277
|
recipe = Recipe.parse(text, file, file_name: File.basename(file))
|
|
276
278
|
describe(file, recipe)
|
|
277
|
-
|
|
279
|
+
# A recipe of the project has its version in the file, and nowhere else.
|
|
280
|
+
return 0 if !lock.from_a_repository?(name) || lock[name].version == recipe.version
|
|
278
281
|
|
|
279
282
|
@err.puts "#{lock.path}: #{name} is version #{lock[name].version} in the lock, but the file says #{recipe.version}"
|
|
280
283
|
1
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
3
5
|
module ApiRecipes
|
|
4
6
|
module CLI
|
|
5
7
|
# The routes command: the calls that the recipes in some files offer, one
|
|
6
8
|
# per line, with the verb and the address. A file called NAME.recipe.yml is
|
|
7
9
|
# a portable recipe, read as data only.
|
|
8
10
|
class Routes
|
|
9
|
-
|
|
11
|
+
STRUCTURE = 'contains ERB that decides the structure of the file (<% ... %>): run again with --erb to evaluate it ' \
|
|
12
|
+
'(this executes the code in the file)'
|
|
13
|
+
|
|
14
|
+
# project: the files were found in the project, not given: their names
|
|
15
|
+
# are shown without what a terminal would obey.
|
|
16
|
+
def initialize(files, erb: false, project: false)
|
|
10
17
|
@files = Array(files)
|
|
11
18
|
@erb = erb
|
|
19
|
+
@project = project
|
|
20
|
+
# What stands for the value of an ERB tag: random, so that no file can write it.
|
|
21
|
+
@mark = "API_RECIPES_ERB_#{SecureRandom.hex(6)}"
|
|
12
22
|
end
|
|
13
23
|
|
|
14
24
|
# Prints the calls on out and the problems on err. Returns the exit code:
|
|
@@ -24,32 +34,77 @@ module ApiRecipes
|
|
|
24
34
|
|
|
25
35
|
private
|
|
26
36
|
|
|
37
|
+
def shown(file) = @project ? Recipes::Summary.shown(file, 300) : file
|
|
38
|
+
|
|
27
39
|
# Adds the calls of file to rows. Returns the exit code for this file.
|
|
28
40
|
def read(file, rows, err)
|
|
29
41
|
unless File.file?(file) && File.readable?(file)
|
|
30
|
-
err.puts "#{file}: #{File.file?(file) ? 'cannot be read' : (File.exist?(file) ? 'is not a file' : 'no such file')}"
|
|
42
|
+
err.puts "#{shown(file)}: #{File.file?(file) ? 'cannot be read' : (File.exist?(file) ? 'is not a file' : 'no such file')}"
|
|
31
43
|
return 2
|
|
32
44
|
end
|
|
33
45
|
|
|
46
|
+
tags = false
|
|
34
47
|
apis = if file.end_with?(Internals::Recipe::SUFFIX)
|
|
35
48
|
Internals::Recipe.read(file, compile: false).apis
|
|
36
49
|
else
|
|
37
|
-
|
|
50
|
+
Description.read(file, erb: @erb) do |text|
|
|
51
|
+
tags = true
|
|
52
|
+
Tags.replaced(text, @mark) || raise(RecipeFileError.new(file, STRUCTURE))
|
|
53
|
+
end
|
|
38
54
|
end
|
|
55
|
+
shown_as = []
|
|
56
|
+
apis = with_tags_shown(file, apis, shown_as) if tags
|
|
39
57
|
Internals::Compiler.compile_all(apis).each_value do |api|
|
|
40
58
|
api.routes.each { |route| rows << [route.call, Internals::OptionValue.shown_verb(route.verb), route.url] }
|
|
41
59
|
end
|
|
60
|
+
if tags
|
|
61
|
+
err.puts "#{shown(file)}: what an ERB tag gives is shown as #{(shown_as.empty? ? ['erb'] : shown_as.uniq).map { |name| "{#{name}}" }.join(' and ')}; " \
|
|
62
|
+
'--erb evaluates the file, which runs the code in it'
|
|
63
|
+
end
|
|
42
64
|
0
|
|
43
65
|
rescue RecipeFileError => e
|
|
44
|
-
e.errors.each { |problem| err.puts problem }
|
|
66
|
+
e.errors.each { |problem| err.puts(@project ? "#{shown(file)}: #{problem.delete_prefix("#{file}: ")}" : problem) }
|
|
45
67
|
1
|
|
46
68
|
rescue ConfigurationError => e
|
|
47
|
-
e.errors.each { |problem| err.puts "#{file}: #{problem}" }
|
|
69
|
+
e.errors.each { |problem| err.puts "#{shown(file)}: #{problem}" }
|
|
48
70
|
1
|
|
49
|
-
rescue StandardError, ScriptError => e
|
|
50
|
-
err.puts "#{file}: could not be evaluated (#{e.class})"
|
|
71
|
+
rescue StandardError, ScriptError, SystemStackError => e
|
|
72
|
+
err.puts "#{shown(file)}: could not be evaluated (#{e.class})"
|
|
51
73
|
1
|
|
52
74
|
end
|
|
75
|
+
|
|
76
|
+
# The apis with what each ERB tag gives written as a variable, which
|
|
77
|
+
# every api declares for it: the calls are listed, the tags are not run.
|
|
78
|
+
# The name of the variable is added to shown_as.
|
|
79
|
+
def with_tags_shown(file, apis, shown_as)
|
|
80
|
+
apis.to_h do |name, options|
|
|
81
|
+
raise RecipeFileError.new(file, 'an ERB tag gives a name: run again with --erb to evaluate it (this executes the code in the file)') if names_a_key?(name => options)
|
|
82
|
+
next [name, options] unless options.is_a?(Hash) && options.to_s.include?(@mark)
|
|
83
|
+
|
|
84
|
+
declared = options[:variables].is_a?(Hash) ? options[:variables] : {}
|
|
85
|
+
# A variable called erb that the api declares or uses is another one.
|
|
86
|
+
variable = declared.key?(:erb) || options.to_s.include?('{erb}') ? 'erb_tag' : 'erb'
|
|
87
|
+
shown_as << variable
|
|
88
|
+
[name, shown_as(options, "{#{variable}}").merge(variables: declared.merge(variable.to_sym => nil))]
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def names_a_key?(value)
|
|
93
|
+
case value
|
|
94
|
+
when Hash then value.any? { |key, inner| key.to_s.include?(@mark) || names_a_key?(inner) }
|
|
95
|
+
when Array then value.any? { |inner| names_a_key?(inner) }
|
|
96
|
+
else false
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def shown_as(value, variable)
|
|
101
|
+
case value
|
|
102
|
+
when Hash then value.transform_values { |inner| shown_as(inner, variable) }
|
|
103
|
+
when Array then value.map { |inner| shown_as(inner, variable) }
|
|
104
|
+
when String then value.include?(@mark) ? value.gsub(@mark, variable) : value
|
|
105
|
+
else value
|
|
106
|
+
end
|
|
107
|
+
end
|
|
53
108
|
end
|
|
54
109
|
end
|
|
55
110
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ApiRecipes
|
|
4
|
+
module CLI
|
|
5
|
+
# The ERB tags of a file that is read without running its code.
|
|
6
|
+
module Tags
|
|
7
|
+
COMMENT = /<%#.*?%>/m
|
|
8
|
+
OUTPUT = /<%=.*?%>/m
|
|
9
|
+
|
|
10
|
+
# text with each output tag replaced by mark and each comment tag
|
|
11
|
+
# removed; nil when another tag is left, which can decide the lines the
|
|
12
|
+
# file has.
|
|
13
|
+
def self.replaced(text, mark)
|
|
14
|
+
plain = text.gsub(COMMENT, '').gsub(OUTPUT, mark)
|
|
15
|
+
plain.gsub('<%%', '').include?('<%') ? nil : plain.gsub('<%%', '<%')
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
require 'pathname'
|
|
5
|
+
|
|
6
|
+
module ApiRecipes
|
|
7
|
+
module CLI
|
|
8
|
+
class Upgrade
|
|
9
|
+
# The changes `upgrade` makes by itself. Each is shown as a diff and is
|
|
10
|
+
# made only when the person answers yes, on a terminal. A change
|
|
11
|
+
# touches only files that git has committed as they are now, so that
|
|
12
|
+
# it can be seen and undone, and writes all its files or none.
|
|
13
|
+
class Fix
|
|
14
|
+
# key: what the change is, said once. texts: { file => its new text }.
|
|
15
|
+
# note: what yes and no mean, when the title does not say it: a line
|
|
16
|
+
# of text, then one entry for each answer.
|
|
17
|
+
Change = Data.define(:key, :title, :texts, :note) do
|
|
18
|
+
def initialize(key:, title:, texts:, note: nil) = super
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Whether someone is there to read the question and answer it: the
|
|
22
|
+
# answers come from a terminal, and the results go to one.
|
|
23
|
+
def self.asks?(input, out) = [input, out].all? { |io| io.respond_to?(:tty?) && io.tty? }
|
|
24
|
+
|
|
25
|
+
# shown: how the name of a file is shown. ours: { file => text } for
|
|
26
|
+
# the files that this run of the command already changed.
|
|
27
|
+
def initialize(screen, input, shown:, root: '.', ours: {})
|
|
28
|
+
@screen = screen
|
|
29
|
+
@input = input
|
|
30
|
+
@shown = shown
|
|
31
|
+
@root = root
|
|
32
|
+
@ours = ours
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Shows a change that another part of the command works out and
|
|
36
|
+
# makes: the files it creates, edits and deletes. Returns whether
|
|
37
|
+
# the answer was yes.
|
|
38
|
+
def confirm(title, note, preview)
|
|
39
|
+
@screen.section('🔧', title)
|
|
40
|
+
if note
|
|
41
|
+
@screen.line(note.first)
|
|
42
|
+
note.drop(1).each { |answer| @screen.item(answer) }
|
|
43
|
+
end
|
|
44
|
+
preview[:created].each { |file, text| @screen.created(@shown.call(file), text) }
|
|
45
|
+
preview[:edited].each { |file, (before, after)| @screen.diff(@shown.call(file), before, after) }
|
|
46
|
+
preview[:deleted].each { |file| @screen.deleted(@shown.call(file)) }
|
|
47
|
+
@screen.ask('Make this change?', @input)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Shows the change and asks. Returns the files it wrote, or nil when
|
|
51
|
+
# the change was not made.
|
|
52
|
+
def offer(change)
|
|
53
|
+
@screen.section('🔧', change.title)
|
|
54
|
+
reason = blocked(change)
|
|
55
|
+
if reason
|
|
56
|
+
@screen.line("This command does not make the change: #{reason}. The code to paste is below.")
|
|
57
|
+
return nil
|
|
58
|
+
end
|
|
59
|
+
if change.note
|
|
60
|
+
@screen.line(change.note.first)
|
|
61
|
+
change.note.drop(1).each { |answer| @screen.item(answer) }
|
|
62
|
+
@screen.gap
|
|
63
|
+
end
|
|
64
|
+
before = change.texts.keys.to_h { |file| [file, File.read(full(file))] }
|
|
65
|
+
change.texts.each { |file, text| @screen.diff(@shown.call(file), before[file], text) }
|
|
66
|
+
return nil unless @screen.ask('Make this change?', @input)
|
|
67
|
+
|
|
68
|
+
changed = change.texts.keys.find { |file| File.read(full(file)) != before[file] }
|
|
69
|
+
if changed
|
|
70
|
+
@screen.line("#{@shown.call(changed)} changed while the question waited: nothing was written. Run the command again.")
|
|
71
|
+
return nil
|
|
72
|
+
end
|
|
73
|
+
write(change, before) ? change.texts.keys : nil
|
|
74
|
+
rescue SystemCallError => e
|
|
75
|
+
@screen.line("The change could not be written (#{e.class.name.split('::').last}): nothing was written")
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def full(file) = File.join(@root, file)
|
|
82
|
+
|
|
83
|
+
def git(*arguments)
|
|
84
|
+
Open3.capture2e({ 'GIT_OPTIONAL_LOCKS' => '0' }, *Convert::Migration::GIT, '-C', @root, *arguments)
|
|
85
|
+
rescue SystemCallError
|
|
86
|
+
['', nil]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Why the change cannot be made, or nil.
|
|
90
|
+
def blocked(change)
|
|
91
|
+
_, status = git('--version')
|
|
92
|
+
return 'git was not found, and without it a change could not be seen or undone' unless status&.success?
|
|
93
|
+
|
|
94
|
+
file = change.texts.keys.find { |one| !(committed?(one) || ours?(one)) || !File.writable?(full(one)) }
|
|
95
|
+
return "#{@shown.call(file)} is not a file that git has committed as it is now, so what changes could not be brought back" if file
|
|
96
|
+
|
|
97
|
+
# The lines of a change are written with LF, and a diff would not show the difference.
|
|
98
|
+
crlf = change.texts.keys.find { |one| File.binread(full(one)).include?("\r") }
|
|
99
|
+
crlf && "#{@shown.call(crlf)} ends its lines with CR LF, which this change would not keep"
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A plain file of the project: not a symbolic link, not below one,
|
|
103
|
+
# not outside the project.
|
|
104
|
+
def plain?(file)
|
|
105
|
+
path = full(file)
|
|
106
|
+
return false if File.symlink?(path) || !File.file?(path)
|
|
107
|
+
|
|
108
|
+
File.realpath(path) == File.join(File.realpath(@root), Pathname.new(file).cleanpath.to_s)
|
|
109
|
+
rescue SystemCallError
|
|
110
|
+
false
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Whether git tracks the file as it does any other, and has it
|
|
114
|
+
# committed as it is now.
|
|
115
|
+
def committed?(file)
|
|
116
|
+
listed, status = git('ls-files', '-v', '--error-unmatch', '--', file)
|
|
117
|
+
return false unless plain?(file) && status&.success? && listed.start_with?('H ')
|
|
118
|
+
|
|
119
|
+
Convert::Migration.as_committed?(file, full(file)) { |*arguments| git(*arguments) }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Whether the file is as this command left it, after a change made
|
|
123
|
+
# to it when git had it committed.
|
|
124
|
+
def ours?(file) = plain?(file) && @ours.key?(file) && File.read(full(file)) == @ours[file]
|
|
125
|
+
|
|
126
|
+
# Writes every file of the change. When one cannot be written, every
|
|
127
|
+
# file is put back: also the one that failed, which can be emptied.
|
|
128
|
+
def write(change, before)
|
|
129
|
+
change.texts.each { |file, text| File.write(full(file), text) }
|
|
130
|
+
@ours.merge!(change.texts)
|
|
131
|
+
true
|
|
132
|
+
rescue SystemCallError => e
|
|
133
|
+
lost = change.texts.keys.reject do |file|
|
|
134
|
+
File.write(full(file), before[file])
|
|
135
|
+
true
|
|
136
|
+
rescue SystemCallError
|
|
137
|
+
false
|
|
138
|
+
end
|
|
139
|
+
@screen.line("The change could not be written (#{e.class.name.split('::').last}): " \
|
|
140
|
+
"#{lost.empty? ? 'every file was put back as it was' : "these files could not be put back: #{lost.map { |file| @shown.call(file) }.join(', ')}"}")
|
|
141
|
+
false
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The lines that differ between two texts, with the lines around them.
|
|
146
|
+
module Diff
|
|
147
|
+
# Above this many pairs of lines the middle of the texts is not compared line by line.
|
|
148
|
+
LIMIT = 400_000
|
|
149
|
+
|
|
150
|
+
# [[first line of the old text, [[sign, line], ...]], ...] where sign
|
|
151
|
+
# is '-' for a line taken out, '+' for one added and ' ' for one kept.
|
|
152
|
+
def self.hunks(old, new, context: 2)
|
|
153
|
+
steps = steps(old.lines(chomp: true), new.lines(chomp: true))
|
|
154
|
+
changed = steps.each_index.reject { |index| steps[index].first == ' ' }
|
|
155
|
+
ranges = changed.map { |index| [[index - context, 0].max, [index + context, steps.size - 1].min] }
|
|
156
|
+
ranges = ranges.each_with_object([]) do |(from, to), merged|
|
|
157
|
+
merged.last && from <= merged.last[1] + 1 ? merged.last[1] = [merged.last[1], to].max : merged << [from, to]
|
|
158
|
+
end
|
|
159
|
+
ranges.map { |from, to| [steps[from][2], steps[from..to].map { |sign, line, _| [sign, line] }] }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# [[sign, line, number of the line in the old text], ...]
|
|
163
|
+
def self.steps(old, new)
|
|
164
|
+
head = 0
|
|
165
|
+
head += 1 while head < old.size && head < new.size && old[head] == new[head]
|
|
166
|
+
tail = 0
|
|
167
|
+
tail += 1 while tail < old.size - head && tail < new.size - head && old[-1 - tail] == new[-1 - tail]
|
|
168
|
+
from = old[head...old.size - tail]
|
|
169
|
+
to = new[head...new.size - tail]
|
|
170
|
+
middle = from.size * to.size > LIMIT ? from.map { |line| ['-', line] } + to.map { |line| ['+', line] } : compared(from, to)
|
|
171
|
+
number = 0
|
|
172
|
+
(old.first(head).map { |line| [' ', line] } + middle + old.last(tail).map { |line| [' ', line] }).map do |sign, line|
|
|
173
|
+
number += 1 unless sign == '+'
|
|
174
|
+
[sign, line, sign == '+' ? number + 1 : number]
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# The longest run of lines the two lists share, and what is around it.
|
|
179
|
+
def self.compared(from, to)
|
|
180
|
+
lengths = Array.new(from.size + 1) { Array.new(to.size + 1, 0) }
|
|
181
|
+
(from.size - 1).downto(0) do |i|
|
|
182
|
+
(to.size - 1).downto(0) do |j|
|
|
183
|
+
lengths[i][j] = from[i] == to[j] ? lengths[i + 1][j + 1] + 1 : [lengths[i + 1][j], lengths[i][j + 1]].max
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
steps = []
|
|
187
|
+
i = j = 0
|
|
188
|
+
while i < from.size || j < to.size
|
|
189
|
+
if i < from.size && j < to.size && from[i] == to[j]
|
|
190
|
+
steps << [' ', from[i]]
|
|
191
|
+
i += 1
|
|
192
|
+
j += 1
|
|
193
|
+
elsif i < from.size && (j == to.size || lengths[i + 1][j] >= lengths[i][j + 1])
|
|
194
|
+
steps << ['-', from[i]]
|
|
195
|
+
i += 1
|
|
196
|
+
else
|
|
197
|
+
steps << ['+', to[j]]
|
|
198
|
+
j += 1
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
steps
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|