cybertrain 0.3.0 → 0.4.1
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 +82 -1876
- data/cybertrain/cli/attachment.rb +246 -164
- data/cybertrain/cli/authentication.rb +2405 -898
- data/cybertrain/cli/authentication_tests.rb +3175 -0
- data/cybertrain/cli/build.rb +93 -7
- data/cybertrain/cli/component.rb +118 -0
- data/cybertrain/cli/helper.rb +143 -0
- data/cybertrain/cli/job.rb +159 -0
- data/cybertrain/cli/line_diff.rb +216 -0
- data/cybertrain/cli/new_app.rb +17 -7
- data/cybertrain/cli/scaffold.rb +29 -1
- data/cybertrain/cli/templates.rb +405 -29
- data/cybertrain/cli/test_command.rb +301 -0
- data/cybertrain/cli/toolchain.rb +12 -12
- data/cybertrain/cli.rb +145 -24
- data/cybertrain/crypto.rb +136 -0
- data/cybertrain/ident.rb +9 -1
- data/cybertrain/shell.rb +13 -0
- data/cybertrain/test/args.rb +286 -0
- data/cybertrain/test_files.rb +48 -0
- data/cybertrain/version.rb +2 -2
- metadata +11 -2
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# A unified diff of two texts, line by line, as `diff -u` writes it and
|
|
2
|
+
# `patch -p1` applies it: `--- a/<path>` and `+++ b/<path>`, then hunks
|
|
3
|
+
# with three lines of context (`@@ -l,s +l,s @@`, the `,s` left out when
|
|
4
|
+
# it is 1). `generate authentication` prints one for each file it would
|
|
5
|
+
# write whole that exists with other content (CLI::Authentication,
|
|
6
|
+
# write_or_diff), so the application's owner applies the change by hand.
|
|
7
|
+
#
|
|
8
|
+
# The lines in common are a longest common subsequence (the common head
|
|
9
|
+
# and tail taken off first, then a table over what is left), so a change
|
|
10
|
+
# in the middle of a long file costs only the lines around it.
|
|
11
|
+
module Cybertrain
|
|
12
|
+
module CLI
|
|
13
|
+
module LineDiff
|
|
14
|
+
CONTEXT = 3
|
|
15
|
+
# Kinds of step through the two texts.
|
|
16
|
+
SAME = 0
|
|
17
|
+
REMOVED = 1
|
|
18
|
+
ADDED = 2
|
|
19
|
+
|
|
20
|
+
# The diff that turns old_text into new_text, or "" when they are
|
|
21
|
+
# equal. A last line without a newline is followed by
|
|
22
|
+
# ``, as diff writes it.
|
|
23
|
+
# @param path [String] the file's path from the application's root
|
|
24
|
+
# @param old_text [String]
|
|
25
|
+
# @param new_text [String]
|
|
26
|
+
# @return [String]
|
|
27
|
+
def self.unified(path, old_text, new_text)
|
|
28
|
+
before = as_utf8(old_text.to_s)
|
|
29
|
+
after = as_utf8(new_text.to_s)
|
|
30
|
+
return "" if before == after
|
|
31
|
+
|
|
32
|
+
a = lines_of(before)
|
|
33
|
+
b = lines_of(after)
|
|
34
|
+
kinds = Array.new(0) { 0 }
|
|
35
|
+
froms = Array.new(0) { 0 }
|
|
36
|
+
tos = Array.new(0) { 0 }
|
|
37
|
+
diff_steps(a, b, kinds, froms, tos)
|
|
38
|
+
|
|
39
|
+
out = +""
|
|
40
|
+
out << "--- a/#{path}\n"
|
|
41
|
+
out << "+++ b/#{path}\n"
|
|
42
|
+
n = kinds.size
|
|
43
|
+
k = 0
|
|
44
|
+
while k < n
|
|
45
|
+
if kinds[k] == SAME
|
|
46
|
+
k += 1
|
|
47
|
+
next
|
|
48
|
+
end
|
|
49
|
+
# A hunk: up to CONTEXT steps before this change, then changes
|
|
50
|
+
# whose gap of same lines is at most twice CONTEXT, then up to
|
|
51
|
+
# CONTEXT steps after the last of them.
|
|
52
|
+
first = k - CONTEXT
|
|
53
|
+
first = 0 if first < 0
|
|
54
|
+
last = k
|
|
55
|
+
j = k + 1
|
|
56
|
+
while j < n
|
|
57
|
+
if kinds[j] != SAME
|
|
58
|
+
last = j
|
|
59
|
+
elsif j - last > 2 * CONTEXT
|
|
60
|
+
break
|
|
61
|
+
end
|
|
62
|
+
j += 1
|
|
63
|
+
end
|
|
64
|
+
stop = last + CONTEXT + 1
|
|
65
|
+
stop = n if stop > n
|
|
66
|
+
out << hunk(a, b, kinds, froms, tos, first, stop)
|
|
67
|
+
k = stop
|
|
68
|
+
end
|
|
69
|
+
out
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# text read as UTF-8 (File.read under a C locale answers US-ASCII,
|
|
73
|
+
# which cannot be joined to the generator's UTF-8 text).
|
|
74
|
+
# @param text [String]
|
|
75
|
+
# @return [String]
|
|
76
|
+
def self.as_utf8(text)
|
|
77
|
+
s = text.to_s
|
|
78
|
+
s.encoding.to_s == "UTF-8" ? s : s.dup.force_encoding("UTF-8")
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# text cut into lines, each with its newline (the last one may have
|
|
82
|
+
# none).
|
|
83
|
+
def self.lines_of(text)
|
|
84
|
+
s = text.to_s
|
|
85
|
+
out = Array.new(0) { "" }
|
|
86
|
+
start = 0
|
|
87
|
+
while start < s.size
|
|
88
|
+
eol = s.index("\n", start)
|
|
89
|
+
if eol.nil?
|
|
90
|
+
out << s[start, s.size - start].to_s
|
|
91
|
+
start = s.size
|
|
92
|
+
else
|
|
93
|
+
out << s[start, eol + 1 - start].to_s
|
|
94
|
+
start = eol + 1
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
out
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Cost: the table holds (n + 1) * (m + 1) Integers for the n and m
|
|
101
|
+
# lines left once the common head and tail are taken off, so time and
|
|
102
|
+
# memory grow with their product. It is meant for files of the size
|
|
103
|
+
# the generator writes (hundreds of lines); a file of several thousand
|
|
104
|
+
# changed lines would take tens of millions of cells.
|
|
105
|
+
#
|
|
106
|
+
# Fills kinds, froms and tos with the steps from a to b: each step's
|
|
107
|
+
# kind and the index of its line in a (SAME, REMOVED) or b (ADDED);
|
|
108
|
+
# froms holds a's index, tos b's, for every step (the next line's
|
|
109
|
+
# where the step has none on that side).
|
|
110
|
+
def self.diff_steps(a, b, kinds, froms, tos)
|
|
111
|
+
head = 0
|
|
112
|
+
head += 1 while head < a.size && head < b.size && a[head] == b[head]
|
|
113
|
+
tail = 0
|
|
114
|
+
tail += 1 while tail < a.size - head && tail < b.size - head && a[a.size - 1 - tail] == b[b.size - 1 - tail]
|
|
115
|
+
n = a.size - head - tail
|
|
116
|
+
m = b.size - head - tail
|
|
117
|
+
# table[i * (m + 1) + j]: the longest common subsequence of a's
|
|
118
|
+
# middle from i and b's middle from j.
|
|
119
|
+
width = m + 1
|
|
120
|
+
table = Array.new((n + 1) * width) { 0 }
|
|
121
|
+
i = n - 1
|
|
122
|
+
while i >= 0
|
|
123
|
+
j = m - 1
|
|
124
|
+
while j >= 0
|
|
125
|
+
table[i * width + j] = if a[head + i] == b[head + j]
|
|
126
|
+
table[(i + 1) * width + j + 1] + 1
|
|
127
|
+
else
|
|
128
|
+
down = table[(i + 1) * width + j]
|
|
129
|
+
right = table[i * width + j + 1]
|
|
130
|
+
down >= right ? down : right
|
|
131
|
+
end
|
|
132
|
+
j -= 1
|
|
133
|
+
end
|
|
134
|
+
i -= 1
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
x = 0
|
|
138
|
+
while x < head
|
|
139
|
+
push_step(kinds, froms, tos, SAME, x, x)
|
|
140
|
+
x += 1
|
|
141
|
+
end
|
|
142
|
+
i = 0
|
|
143
|
+
j = 0
|
|
144
|
+
while i < n || j < m
|
|
145
|
+
if i < n && j < m && a[head + i] == b[head + j]
|
|
146
|
+
push_step(kinds, froms, tos, SAME, head + i, head + j)
|
|
147
|
+
i += 1
|
|
148
|
+
j += 1
|
|
149
|
+
elsif i < n && (j >= m || table[(i + 1) * width + j] >= table[i * width + j + 1])
|
|
150
|
+
push_step(kinds, froms, tos, REMOVED, head + i, head + j)
|
|
151
|
+
i += 1
|
|
152
|
+
else
|
|
153
|
+
push_step(kinds, froms, tos, ADDED, head + i, head + j)
|
|
154
|
+
j += 1
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
x = 0
|
|
158
|
+
while x < tail
|
|
159
|
+
push_step(kinds, froms, tos, SAME, head + n + x, head + m + x)
|
|
160
|
+
x += 1
|
|
161
|
+
end
|
|
162
|
+
nil
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def self.push_step(kinds, froms, tos, kind, from, to)
|
|
166
|
+
kinds << kind
|
|
167
|
+
froms << from
|
|
168
|
+
tos << to
|
|
169
|
+
nil
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# The `@@` line and the lines of steps first...stop.
|
|
173
|
+
def self.hunk(a, b, kinds, froms, tos, first, stop)
|
|
174
|
+
old_count = 0
|
|
175
|
+
new_count = 0
|
|
176
|
+
body = +""
|
|
177
|
+
k = first
|
|
178
|
+
while k < stop
|
|
179
|
+
kind = kinds[k]
|
|
180
|
+
if kind == SAME
|
|
181
|
+
old_count += 1
|
|
182
|
+
new_count += 1
|
|
183
|
+
body << hunk_line(" ", a[froms[k]])
|
|
184
|
+
elsif kind == REMOVED
|
|
185
|
+
old_count += 1
|
|
186
|
+
body << hunk_line("-", a[froms[k]])
|
|
187
|
+
else
|
|
188
|
+
new_count += 1
|
|
189
|
+
body << hunk_line("+", b[tos[k]])
|
|
190
|
+
end
|
|
191
|
+
k += 1
|
|
192
|
+
end
|
|
193
|
+
out = +""
|
|
194
|
+
out << "@@ -" << hunk_range(froms[first], old_count) << " +" << hunk_range(tos[first], new_count) << " @@\n"
|
|
195
|
+
out << body
|
|
196
|
+
out
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# `l,s` (1-based), `l` alone when s is 1, and the line before when s
|
|
200
|
+
# is 0, as diff writes it.
|
|
201
|
+
def self.hunk_range(index, count)
|
|
202
|
+
return "#{index},0" if count == 0
|
|
203
|
+
return (index + 1).to_s if count == 1
|
|
204
|
+
|
|
205
|
+
"#{index + 1},#{count}"
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def self.hunk_line(mark, line)
|
|
209
|
+
text = line.to_s
|
|
210
|
+
return "#{mark}#{text}" if text.end_with?("\n")
|
|
211
|
+
|
|
212
|
+
"#{mark}#{text}\n\\n"
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
end
|
data/cybertrain/cli/new_app.rb
CHANGED
|
@@ -24,18 +24,24 @@ module Cybertrain
|
|
|
24
24
|
files = [
|
|
25
25
|
["spin.toml", Templates.spin_toml(package, framework_dep)],
|
|
26
26
|
[".gitignore", Templates.gitignore],
|
|
27
|
+
[".env.example", Templates.env_example],
|
|
27
28
|
["README.md", Templates.readme(title, package)],
|
|
28
29
|
["config/app.rb", Templates.config_app],
|
|
29
30
|
["config/routes.rb", Templates.routes],
|
|
31
|
+
["config/locales/en.yml", Templates.locales_en_yml],
|
|
30
32
|
["db/schema.rb", Templates.schema],
|
|
31
33
|
["db/migrate/.keep", ""],
|
|
32
34
|
["app/controllers/application_controller.rb", Templates.application_controller],
|
|
33
35
|
["app/models/.keep", ""],
|
|
34
36
|
["app/helpers/.keep", ""],
|
|
37
|
+
["app/jobs/.keep", ""],
|
|
38
|
+
["app/assets/stylesheets/application.css", Templates.style_css],
|
|
39
|
+
["app/assets/images/.keep", ""],
|
|
40
|
+
["app/javascript/application.js", Templates.application_js],
|
|
41
|
+
["config/importmap.rb", Templates.importmap],
|
|
35
42
|
["app/views/layouts/application.html.erb", Templates.layout(title)],
|
|
36
43
|
["public/404.html", Templates.error_page("404", "Not Found")],
|
|
37
44
|
["public/500.html", Templates.error_page("500", "Internal Server Error")],
|
|
38
|
-
["public/style.css", Templates.style_css],
|
|
39
45
|
["public/up.html", Templates.up_html],
|
|
40
46
|
["bin/#{package}.rb", Templates.bin_app(package)],
|
|
41
47
|
["bin/gen.rb", Templates.bin_gen],
|
|
@@ -48,7 +54,10 @@ module Cybertrain
|
|
|
48
54
|
["gen/.keep", ""],
|
|
49
55
|
["storage/.keep", ""],
|
|
50
56
|
["tmp/.keep", ""],
|
|
51
|
-
["test
|
|
57
|
+
["test/test_helper.rb", Templates.test_helper],
|
|
58
|
+
["test/integration/health_test.rb", Templates.health_test],
|
|
59
|
+
["test/fixtures/.keep", ""],
|
|
60
|
+
["test/support/.keep", ""]
|
|
52
61
|
]
|
|
53
62
|
created = Array.new(0) { "" }
|
|
54
63
|
files.each do |entry|
|
|
@@ -64,12 +73,12 @@ module Cybertrain
|
|
|
64
73
|
# Returns false when a step failed.
|
|
65
74
|
def self.bootstrap(dir)
|
|
66
75
|
unless Toolchain.ensure!
|
|
67
|
-
puts "skip spin lock /
|
|
76
|
+
puts "skip spin lock / #{Shell::TOOL_SPIN} run gen: Spinel #{Cybertrain::SPINEL_TAG} is not available"
|
|
68
77
|
puts " fix the problem above (`cybertrain doctor` lists the checks), then: cybertrain setup && #{recovery_command(dir)}"
|
|
69
78
|
return false
|
|
70
79
|
end
|
|
71
80
|
|
|
72
|
-
puts "run spin lock &&
|
|
81
|
+
puts "run spin lock && #{Shell::TOOL_SPIN} run gen"
|
|
73
82
|
return true if system(bootstrap_command(dir))
|
|
74
83
|
|
|
75
84
|
puts "error: bootstrapping #{dir} failed; fix the cause, then run: #{recovery_command(dir)}"
|
|
@@ -77,15 +86,16 @@ module Cybertrain
|
|
|
77
86
|
end
|
|
78
87
|
|
|
79
88
|
# Run by bootstrap once Toolchain.ensure! has put spin on this
|
|
80
|
-
# process's PATH.
|
|
89
|
+
# process's PATH. gen in debug mode, as every CLI command builds it
|
|
90
|
+
# (Shell::TOOL_SPIN).
|
|
81
91
|
def self.bootstrap_command(dir)
|
|
82
|
-
"cd #{Shell.quote(dir)} && spin lock &&
|
|
92
|
+
"cd #{Shell.quote(dir)} && spin lock && #{Shell::TOOL_SPIN} run gen"
|
|
83
93
|
end
|
|
84
94
|
|
|
85
95
|
# The same steps for the user to run by hand: through `cybertrain
|
|
86
96
|
# spin`, since the Spinel cybertrain installed is not on their PATH.
|
|
87
97
|
def self.recovery_command(dir)
|
|
88
|
-
"cd #{Shell.quote(dir)} && cybertrain spin lock && cybertrain spin run gen"
|
|
98
|
+
"cd #{Shell.quote(dir)} && cybertrain spin lock && SPIN_DEBUG=1 cybertrain spin run gen"
|
|
89
99
|
end
|
|
90
100
|
end
|
|
91
101
|
end
|
data/cybertrain/cli/scaffold.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# `cybertrain generate scaffold post title:string body:text`: writes the
|
|
2
|
-
# migration, model, controller and views of a resource
|
|
2
|
+
# migration, model, controller and views of a resource, its fixtures
|
|
3
|
+
# (test/fixtures/posts.yml), a model test and a controller test, and adds
|
|
3
4
|
# `resources :posts` to config/routes.rb, the way Rails' scaffold does.
|
|
4
5
|
#
|
|
5
6
|
# A `cover:attachment` field is a stored file rather than a column: the
|
|
@@ -215,6 +216,8 @@ module Cybertrain
|
|
|
215
216
|
parsed << field
|
|
216
217
|
end
|
|
217
218
|
res = Resource.new(name, parsed, any)
|
|
219
|
+
refuse_test_class_clash!(root, "test/models/#{res.singular}_test.rb", "#{res.class_name}Test")
|
|
220
|
+
refuse_test_class_clash!(root, "test/controllers/#{res.plural}_controller_test.rb", "#{res.plural_class}ControllerTest")
|
|
218
221
|
|
|
219
222
|
res.fields.each do |f|
|
|
220
223
|
next unless f.attachment?
|
|
@@ -229,6 +232,8 @@ module Cybertrain
|
|
|
229
232
|
migration = migration_path(root, res)
|
|
230
233
|
record(created, root, migration, Templates.migration(res))
|
|
231
234
|
record(created, root, "app/models/#{res.singular}.rb", Templates.model(res))
|
|
235
|
+
record(created, root, "test/models/#{res.singular}_test.rb", Templates.model_test(res))
|
|
236
|
+
record(created, root, "test/fixtures/#{res.plural}.yml", Templates.fixture_file(res))
|
|
232
237
|
record(created, root, "app/controllers/#{res.plural}_controller.rb", Templates.controller(res))
|
|
233
238
|
views = "app/views/#{res.plural}"
|
|
234
239
|
record(created, root, "#{views}/index.html.erb", Templates.index_view(res))
|
|
@@ -236,6 +241,7 @@ module Cybertrain
|
|
|
236
241
|
record(created, root, "#{views}/new.html.erb", Templates.new_view(res))
|
|
237
242
|
record(created, root, "#{views}/edit.html.erb", Templates.edit_view(res))
|
|
238
243
|
record(created, root, "#{views}/_form.html.erb", Templates.form_partial(res))
|
|
244
|
+
record(created, root, "test/controllers/#{res.plural}_controller_test.rb", Templates.controller_test(res, Templates.signs_in_tests?(root)))
|
|
239
245
|
add_route(routes_path, res.plural)
|
|
240
246
|
attachments = false
|
|
241
247
|
res.fields.each do |f|
|
|
@@ -264,6 +270,28 @@ module Cybertrain
|
|
|
264
270
|
notes
|
|
265
271
|
end
|
|
266
272
|
|
|
273
|
+
# `cybertrain test` compiles every test into one program, where a class
|
|
274
|
+
# opened in two files is one class (gen refuses that, and stops): the
|
|
275
|
+
# scaffold's test class must not be another file's already, as
|
|
276
|
+
# `scaffold health` would reopen `cybertrain new`'s HealthTest.
|
|
277
|
+
# Raises InvalidArgument before anything is written; nil otherwise.
|
|
278
|
+
def self.refuse_test_class_clash!(root, path, class_name)
|
|
279
|
+
Dir.glob(File.join(root, "test/**/*.rb")).sort.each do |file|
|
|
280
|
+
rel = file.delete_prefix("#{root}/")
|
|
281
|
+
next if rel == path
|
|
282
|
+
|
|
283
|
+
File.read(file).each_line do |line|
|
|
284
|
+
text = line.chomp
|
|
285
|
+
next unless text == "class #{class_name}" || text.start_with?("class #{class_name} ") || text.start_with?("class #{class_name};")
|
|
286
|
+
|
|
287
|
+
message = "#{rel} already defines #{class_name}, the class of #{path}: every test runs in one program, " \
|
|
288
|
+
"so pick another name for the scaffold, or rename that test class"
|
|
289
|
+
raise InvalidArgument, message
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
nil
|
|
293
|
+
end
|
|
294
|
+
|
|
267
295
|
def self.record(created, root, path, content)
|
|
268
296
|
created << path if Templates.write(root, path, content) == "create"
|
|
269
297
|
nil
|