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
data/cybertrain/cli/build.rb
CHANGED
|
@@ -82,19 +82,23 @@ module Cybertrain
|
|
|
82
82
|
# command strings stay safe for a caller that skipped that, whatever the
|
|
83
83
|
# spelling. An empty name cannot reach these from the CLI (run_in_app
|
|
84
84
|
# refuses it first); a direct caller passing "" gets `spin build ''`.
|
|
85
|
+
# gen is built in debug mode (Shell::TOOL_SPIN), the application in
|
|
86
|
+
# release mode.
|
|
85
87
|
def self.commands(name)
|
|
86
|
-
["
|
|
88
|
+
["#{Shell::TOOL_SPIN} run gen -- --embed-views", "spin build #{quote_arg(name)}", "#{Shell::TOOL_SPIN} run gen"]
|
|
87
89
|
end
|
|
88
90
|
|
|
89
91
|
# `cybertrain db ARGS`, through bin/db.rb (the app binary cannot compile
|
|
90
92
|
# before the first migration has produced gen/models). bin/db.rb only
|
|
91
93
|
# knows the migrations gen/migrations.rb lists, so gen runs first;
|
|
92
94
|
# migrate and rollback rewrite db/schema.rb, which gen/models is
|
|
93
|
-
# derived from, so gen runs again after them.
|
|
95
|
+
# derived from, so gen runs again after them. Both tools are built in
|
|
96
|
+
# debug mode (Shell::TOOL_SPIN).
|
|
94
97
|
def self.db_commands(args)
|
|
95
98
|
words = args.map { |arg| quote_arg(arg) }.join(" ")
|
|
96
|
-
|
|
97
|
-
commands
|
|
99
|
+
gen = "#{Shell::TOOL_SPIN} run gen"
|
|
100
|
+
commands = [gen, "#{Shell::TOOL_SPIN} run db -- #{words}"]
|
|
101
|
+
commands << gen if args[0] == "migrate" || args[0] == "rollback"
|
|
98
102
|
commands
|
|
99
103
|
end
|
|
100
104
|
|
|
@@ -110,11 +114,92 @@ module Cybertrain
|
|
|
110
114
|
Shell.quote(text)
|
|
111
115
|
end
|
|
112
116
|
|
|
113
|
-
# port is "" (the app's default, 3000) or a port? string.
|
|
117
|
+
# port is "" (the app's default, 3000) or a port? string. The gen step
|
|
118
|
+
# and the run step; run_server puts incremental_command between them
|
|
119
|
+
# when the binary is stale.
|
|
114
120
|
def self.server_commands(name, port)
|
|
115
121
|
target = quote_arg(name)
|
|
116
122
|
run = port == "" ? "spin run #{target}" : "spin run #{target} -- #{quote_arg(port)}"
|
|
117
|
-
["
|
|
123
|
+
["#{Shell::TOOL_SPIN} run gen", run]
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The build the development rebuilder runs after an edit
|
|
127
|
+
# (Dev::Rebuilder#command): Spinel compiles the C units apart and
|
|
128
|
+
# reuses the objects in its cache, which a plain `spin run NAME` (every
|
|
129
|
+
# unit at -O2) neither reads nor fills. spin then reads the binary as
|
|
130
|
+
# up to date, so the `spin run NAME` after it only starts it.
|
|
131
|
+
def self.incremental_command(name)
|
|
132
|
+
"spin build #{quote_arg(name)} -- --incremental"
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Top-level entries of an application binary_stale? does not look in,
|
|
136
|
+
# besides those whose name starts with "." (.git, .bundle, .venv):
|
|
137
|
+
# build output, data, and trees that hold no source of the
|
|
138
|
+
# application and can be very large.
|
|
139
|
+
STALE_SKIPPED = ["build", "dist", "tmp", "storage", "public", "node_modules"]
|
|
140
|
+
|
|
141
|
+
# True when root/build/bin/NAME is missing, or a *.rb file under root
|
|
142
|
+
# (not under STALE_SKIPPED or a top-level dot directory, not through a
|
|
143
|
+
# symlinked directory) is as new as it or newer: spin's own rule for
|
|
144
|
+
# rebuilding an executable, close enough. "As new" counts because some
|
|
145
|
+
# filesystems keep whole seconds, and an edit in the second of the
|
|
146
|
+
# build would otherwise read as built.
|
|
147
|
+
# Wrong either way costs only time: a stale verdict by mistake is one
|
|
148
|
+
# incremental build `spin run` would have skipped; a fresh one by
|
|
149
|
+
# mistake (a path dependency changed) leaves the build to `spin run`.
|
|
150
|
+
# A directory that cannot be listed is passed over (a volume owned by
|
|
151
|
+
# another user would otherwise make every start a build); a binary
|
|
152
|
+
# that cannot be read counts as stale.
|
|
153
|
+
def self.binary_stale?(root, name)
|
|
154
|
+
binary = "#{root}/build/bin/#{name}"
|
|
155
|
+
return true unless File.file?(binary)
|
|
156
|
+
|
|
157
|
+
newer_ruby?(root, mtime_ns(binary), true)
|
|
158
|
+
rescue StandardError
|
|
159
|
+
true
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# True when a *.rb file under dir is no older than since (nanoseconds).
|
|
163
|
+
def self.newer_ruby?(dir, since, top)
|
|
164
|
+
children = listing(dir)
|
|
165
|
+
i = 0
|
|
166
|
+
while i < children.size
|
|
167
|
+
child = children[i]
|
|
168
|
+
i += 1
|
|
169
|
+
next if top && (STALE_SKIPPED.include?(child) || child.start_with?("."))
|
|
170
|
+
|
|
171
|
+
path = "#{dir}/#{child}"
|
|
172
|
+
if File.directory?(path)
|
|
173
|
+
next if File.symlink?(path)
|
|
174
|
+
return true if newer_ruby?(path, since, false)
|
|
175
|
+
elsif child.end_with?(".rb") && File.file?(path)
|
|
176
|
+
return true if mtime_ns(path) >= since
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
false
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# The entries of dir, or none when it cannot be listed.
|
|
183
|
+
def self.listing(dir)
|
|
184
|
+
Dir.children(dir)
|
|
185
|
+
rescue StandardError
|
|
186
|
+
Array.new(0) { "" }
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def self.mtime_ns(path)
|
|
190
|
+
time = File.mtime(path)
|
|
191
|
+
time.to_i * 1_000_000_000 + time.nsec
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# `cybertrain server`: gen, then, when the binary is stale, the
|
|
195
|
+
# incremental build, then the server. Stops at the first step that
|
|
196
|
+
# fails and returns 1; 0 when the server exits 0.
|
|
197
|
+
def self.run_server(root, name, port, runner = Runner.new)
|
|
198
|
+
steps = server_commands(name, port)
|
|
199
|
+
return 1 unless runner.run_step(root, steps[0])
|
|
200
|
+
return 1 if binary_stale?(root, name) && !runner.run_step(root, incremental_command(name))
|
|
201
|
+
|
|
202
|
+
runner.run_step(root, steps[1]) ? 0 : 1
|
|
118
203
|
end
|
|
119
204
|
|
|
120
205
|
# All digits, 1 to 65535 (the app reads 0 as "no port").
|
|
@@ -126,7 +211,8 @@ module Cybertrain
|
|
|
126
211
|
end
|
|
127
212
|
|
|
128
213
|
# Runs one build step (a spin command) in root; true when it succeeds.
|
|
129
|
-
# Build.run
|
|
214
|
+
# Build.run and Build.run_server take a Runner so tests can stand in
|
|
215
|
+
# for spin.
|
|
130
216
|
class Runner
|
|
131
217
|
def run_step(root, command)
|
|
132
218
|
Build.run_in(root, command)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# `cybertrain generate component card title value:integer`: writes
|
|
2
|
+
# app/components/card_component.rb (a Cybertrain::Component with one
|
|
3
|
+
# keyword per attribute), its template app/components/card_component.html.erb
|
|
4
|
+
# (each attribute, then the block's content) and
|
|
5
|
+
# test/components/card_component_test.rb (a ViewComponent::TestCase that
|
|
6
|
+
# renders it with render_inline and checks it with assert_select). A name
|
|
7
|
+
# may carry a namespace ("admin/user_row" -> Admin::UserRowComponent).
|
|
8
|
+
require "cybertrain/generator/inflector"
|
|
9
|
+
require "cybertrain/cli/templates"
|
|
10
|
+
require "cybertrain/cli/scaffold"
|
|
11
|
+
require "cybertrain/cli/helper"
|
|
12
|
+
|
|
13
|
+
module Cybertrain
|
|
14
|
+
module CLI
|
|
15
|
+
module Component
|
|
16
|
+
TYPES = %w[string text integer float boolean]
|
|
17
|
+
|
|
18
|
+
# Names Cybertrain::Component itself answers in a template or defines.
|
|
19
|
+
RESERVED = %w[content view_assigns call_view_method view_method component_name component_template render_with initialize]
|
|
20
|
+
|
|
21
|
+
# Returns the paths written.
|
|
22
|
+
def self.generate(root, name, attrs, force = false)
|
|
23
|
+
GeneratedFiles.require_app!(root)
|
|
24
|
+
segments = GeneratedFiles.name_segments(name.delete_suffix("_component"), "component")
|
|
25
|
+
segments[segments.size - 1] = "#{segments.last}_component"
|
|
26
|
+
modules = segments.map { |s| Inflector.camelize(s) }
|
|
27
|
+
full = modules.join("::")
|
|
28
|
+
rel = segments.join("/")
|
|
29
|
+
names = Array.new(0) { "" }
|
|
30
|
+
types = Array.new(0) { "" }
|
|
31
|
+
attrs.each do |arg|
|
|
32
|
+
parts = arg.split(":")
|
|
33
|
+
attr = parts[0].to_s
|
|
34
|
+
type = parts.size > 1 ? parts[1].to_s : "string"
|
|
35
|
+
raise InvalidArgument, "bad attribute '#{arg}': use name[:type]" if parts.size > 2
|
|
36
|
+
raise InvalidArgument, "bad attribute name '#{attr}'" unless Templates.identifier?(attr)
|
|
37
|
+
raise InvalidArgument, "unknown type '#{type}' for #{attr} (use #{TYPES.join(", ")})" unless TYPES.include?(type)
|
|
38
|
+
raise InvalidArgument, "'#{attr}' is a name Cybertrain::Component already uses; pick another attribute" if RESERVED.include?(attr)
|
|
39
|
+
raise InvalidArgument, "'#{attr}' is a Ruby keyword and cannot name an attribute" if Ident.unusable_reason(attr) == "keyword"
|
|
40
|
+
raise InvalidArgument, "duplicate attribute '#{attr}'" if names.include?(attr)
|
|
41
|
+
|
|
42
|
+
names << attr
|
|
43
|
+
types << type
|
|
44
|
+
end
|
|
45
|
+
files = [
|
|
46
|
+
["app/components/#{rel}.rb", class_source(modules, names)],
|
|
47
|
+
["app/components/#{rel}.html.erb", template_source(segments.last, names)],
|
|
48
|
+
["test/components/#{rel}_test.rb", test_source(full, segments.last, names, types)]
|
|
49
|
+
]
|
|
50
|
+
GeneratedFiles.write_all(root, files, force)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.class_source(modules, names)
|
|
54
|
+
body = +""
|
|
55
|
+
body << "class #{modules.last} < Cybertrain::Component\n"
|
|
56
|
+
if names.empty?
|
|
57
|
+
body << " # Keyword parameters of initialize are what `render component:` passes;\n"
|
|
58
|
+
body << " # each @ivar is a local of the template beside this file.\n"
|
|
59
|
+
else
|
|
60
|
+
body << " def initialize(#{names.map { |n| "#{n}:" }.join(", ")})\n"
|
|
61
|
+
names.each { |n| body << " @#{n} = #{n}\n" }
|
|
62
|
+
body << " end\n"
|
|
63
|
+
end
|
|
64
|
+
body << "end\n"
|
|
65
|
+
GeneratedFiles.nest(modules[0, modules.size - 1], body)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def self.template_source(file_name, names)
|
|
69
|
+
css = file_name.delete_suffix("_component").tr("_", "-")
|
|
70
|
+
out = +""
|
|
71
|
+
out << "<div class=\"#{css}\">\n"
|
|
72
|
+
names.each { |n| out << " <p><%= #{n} %></p>\n" }
|
|
73
|
+
out << " <%= content %>\n"
|
|
74
|
+
out << "</div>\n"
|
|
75
|
+
out
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.sample(type)
|
|
79
|
+
case type
|
|
80
|
+
when "integer" then "1"
|
|
81
|
+
when "float" then "1.5"
|
|
82
|
+
when "boolean" then "true"
|
|
83
|
+
else ""
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The class-form test: render_inline, then assert_select on each
|
|
88
|
+
# attribute's paragraph and on the content.
|
|
89
|
+
def self.test_source(full, file_name, names, types)
|
|
90
|
+
css = file_name.delete_suffix("_component").tr("_", "-")
|
|
91
|
+
args = Array.new(0) { "" }
|
|
92
|
+
expected = Array.new(0) { "" }
|
|
93
|
+
names.each_with_index do |n, i|
|
|
94
|
+
value = sample(types[i])
|
|
95
|
+
if value == ""
|
|
96
|
+
args << "#{n}: \"#{Templates.humanize(n)}\""
|
|
97
|
+
expected << Templates.humanize(n)
|
|
98
|
+
else
|
|
99
|
+
args << "#{n}: #{value}"
|
|
100
|
+
expected << value
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
call = args.empty? ? "#{full}.new" : "#{full}.new(#{args.join(", ")})"
|
|
104
|
+
out = +""
|
|
105
|
+
out << "require \"test_helper\"\n"
|
|
106
|
+
out << "\n"
|
|
107
|
+
out << "class #{full}Test < ViewComponent::TestCase\n"
|
|
108
|
+
out << " test \"renders its attributes and content\" do\n"
|
|
109
|
+
out << " render_inline(#{call}, content: \"<b>inside</b>\")\n"
|
|
110
|
+
expected.each { |e| out << " assert_select \"div.#{css} > p\", text: \"#{e}\"\n" }
|
|
111
|
+
out << " assert_select \"div.#{css} > b\", text: \"inside\"\n"
|
|
112
|
+
out << " end\n"
|
|
113
|
+
out << "end\n"
|
|
114
|
+
out
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# `cybertrain generate helper prices`: writes app/helpers/prices_helper.rb
|
|
2
|
+
# (an empty `module PricesHelper`, which `spin run gen` includes into the
|
|
3
|
+
# app's ApplicationHelpers) and test/helpers/prices_helper_test.rb. A name may carry a
|
|
4
|
+
# namespace ("admin/users" -> Admin::UsersHelper in
|
|
5
|
+
# app/helpers/admin/users_helper.rb).
|
|
6
|
+
#
|
|
7
|
+
# Shared with `generate component` (cli/component.rb): GeneratedFiles, the
|
|
8
|
+
# writer that refuses to overwrite an existing, different file unless
|
|
9
|
+
# --force.
|
|
10
|
+
require "cybertrain/generator/inflector"
|
|
11
|
+
require "cybertrain/cli/templates"
|
|
12
|
+
require "cybertrain/cli/scaffold"
|
|
13
|
+
|
|
14
|
+
module Cybertrain
|
|
15
|
+
module CLI
|
|
16
|
+
module GeneratedFiles
|
|
17
|
+
# files: [path, content] pairs. Checks every path before writing any:
|
|
18
|
+
# one that exists with other content raises InvalidArgument unless
|
|
19
|
+
# force. Returns the paths it created or overwrote.
|
|
20
|
+
def self.write_all(root, files, force)
|
|
21
|
+
files.each do |pair|
|
|
22
|
+
full = File.join(root, pair[0])
|
|
23
|
+
next unless File.exist?(full)
|
|
24
|
+
next if force || File.read(full) == pair[1]
|
|
25
|
+
|
|
26
|
+
raise InvalidArgument, "#{pair[0]} already exists (pass --force to overwrite it)"
|
|
27
|
+
end
|
|
28
|
+
written = Array.new(0) { "" }
|
|
29
|
+
files.each do |pair|
|
|
30
|
+
full = File.join(root, pair[0])
|
|
31
|
+
if File.exist?(full)
|
|
32
|
+
if File.read(full) == pair[1]
|
|
33
|
+
puts "identical #{pair[0]}"
|
|
34
|
+
next
|
|
35
|
+
end
|
|
36
|
+
File.write(full, pair[1])
|
|
37
|
+
puts "force #{pair[0]}"
|
|
38
|
+
else
|
|
39
|
+
Templates.mkdir_p(File.dirname(full))
|
|
40
|
+
File.write(full, pair[1])
|
|
41
|
+
puts "create #{pair[0]}"
|
|
42
|
+
end
|
|
43
|
+
written << pair[0]
|
|
44
|
+
end
|
|
45
|
+
written
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.require_app!(root)
|
|
49
|
+
routes = File.join(root, "config/routes.rb")
|
|
50
|
+
raise InvalidArgument, "#{routes} not found: run this inside a cybertrain app" unless File.exist?(routes)
|
|
51
|
+
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# "admin/user_row" -> ["admin", "user_row"], each segment checked.
|
|
56
|
+
def self.name_segments(name, what)
|
|
57
|
+
segments = Inflector.underscore(name).split("/")
|
|
58
|
+
raise InvalidArgument, "bad #{what} name '#{name}'" if segments.empty?
|
|
59
|
+
|
|
60
|
+
segments.each do |seg|
|
|
61
|
+
raise InvalidArgument, "bad #{what} name '#{name}'" unless Templates.identifier?(seg)
|
|
62
|
+
end
|
|
63
|
+
segments
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# `module Admin` lines around a body, indented.
|
|
67
|
+
def self.nest(modules, body)
|
|
68
|
+
out = +""
|
|
69
|
+
depth = 0
|
|
70
|
+
modules.each do |m|
|
|
71
|
+
out << (" " * depth) << "module #{m}\n"
|
|
72
|
+
depth += 1
|
|
73
|
+
end
|
|
74
|
+
body.each_line do |line|
|
|
75
|
+
out << (" " * depth) unless line == "\n"
|
|
76
|
+
out << line
|
|
77
|
+
end
|
|
78
|
+
while depth > 0
|
|
79
|
+
depth -= 1
|
|
80
|
+
out << (" " * depth) << "end\n"
|
|
81
|
+
end
|
|
82
|
+
out
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
module Helper
|
|
87
|
+
# Returns the paths written.
|
|
88
|
+
def self.generate(root, name, force = false)
|
|
89
|
+
GeneratedFiles.require_app!(root)
|
|
90
|
+
|
|
91
|
+
segments = GeneratedFiles.name_segments(name.delete_suffix("_helper"), "helper")
|
|
92
|
+
segments[segments.size - 1] = "#{segments.last}_helper"
|
|
93
|
+
modules = segments.map { |s| Inflector.camelize(s) }
|
|
94
|
+
full = modules.join("::")
|
|
95
|
+
rel = segments.join("/")
|
|
96
|
+
files = [
|
|
97
|
+
["app/helpers/#{rel}.rb", module_source(modules)],
|
|
98
|
+
["test/helpers/#{rel}_test.rb", test_source(full, rel)]
|
|
99
|
+
]
|
|
100
|
+
GeneratedFiles.write_all(root, files, force)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def self.module_source(modules)
|
|
104
|
+
body = +""
|
|
105
|
+
body << "module #{modules.last}\n"
|
|
106
|
+
body << " # Helpers here are callable from every template once `spin run gen`\n"
|
|
107
|
+
body << " # has run: `def price(value, unit: \"USD\")` is `<%= price 3, unit: \"EUR\" %>`.\n"
|
|
108
|
+
body << " # Parameters are named (no *rest / **opts); values arrive as the\n"
|
|
109
|
+
body << " # template's values, so narrow them with Cybertrain::Cast. Build HTML\n"
|
|
110
|
+
body << " # with Cybertrain::Html.escape / SafeString.of, link_to_path and the\n"
|
|
111
|
+
body << " # *_path helpers.\n"
|
|
112
|
+
body << "end\n"
|
|
113
|
+
GeneratedFiles.nest(modules[0, modules.size - 1], body)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# The class-form test. Its template-rendering helper is
|
|
117
|
+
# render_helper_template: render_inline is the framework's (component
|
|
118
|
+
# and partial tests).
|
|
119
|
+
def self.test_source(full, rel)
|
|
120
|
+
out = +""
|
|
121
|
+
out << "require \"test_helper\"\n"
|
|
122
|
+
out << "\n"
|
|
123
|
+
out << "# app/helpers/#{rel}.rb, through the app's ApplicationHelpers (gen/helpers.rb).\n"
|
|
124
|
+
out << "class #{full}Test < ActiveSupport::TestCase\n"
|
|
125
|
+
out << " test \"#{full} is in the application's helpers\" do\n"
|
|
126
|
+
out << " assert_equal \"ApplicationHelpers\", Cybertrain::Views.helpers_builder.call(nil).class.name\n"
|
|
127
|
+
out << " assert_equal \"<b>\", render_helper_template(\"<%= h '<b>' %>\")\n"
|
|
128
|
+
out << " end\n"
|
|
129
|
+
out << "\n"
|
|
130
|
+
out << " private\n"
|
|
131
|
+
out << "\n"
|
|
132
|
+
out << " # A template rendered with the application's helpers.\n"
|
|
133
|
+
out << " def render_helper_template(source)\n"
|
|
134
|
+
out << " template = Cybertrain::Template::Template.parse(source, \"test/inline\")\n"
|
|
135
|
+
out << " env = { \"__template_dir\" => \"test\" }\n"
|
|
136
|
+
out << " Cybertrain::Template::Interpreter.new(Cybertrain::Views.helpers_builder.call(nil)).render(template, env)\n"
|
|
137
|
+
out << " end\n"
|
|
138
|
+
out << "end\n"
|
|
139
|
+
out
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# `cybertrain generate job send_digest --retry Cybertrain::Mailer::TimeoutError:60:5`:
|
|
2
|
+
# writes app/jobs/send_digest_job.rb (a Cybertrain::Job::Base subclass with
|
|
3
|
+
# an empty `perform(args)` and a `retry_on` line per --retry) and
|
|
4
|
+
# test/jobs/send_digest_job_test.rb, which queues it and performs it with
|
|
5
|
+
# perform_enqueued_jobs. A name may
|
|
6
|
+
# carry a namespace ("admin/report" -> Admin::ReportJob in
|
|
7
|
+
# app/jobs/admin/report_job.rb). `spin run gen` then writes the class's
|
|
8
|
+
# perform_later / perform_at / perform_in / perform_now into gen/jobs.rb.
|
|
9
|
+
require "cybertrain/generator/inflector"
|
|
10
|
+
require "cybertrain/cli/templates"
|
|
11
|
+
require "cybertrain/cli/scaffold"
|
|
12
|
+
require "cybertrain/cli/helper"
|
|
13
|
+
|
|
14
|
+
module Cybertrain
|
|
15
|
+
module CLI
|
|
16
|
+
module Job
|
|
17
|
+
USAGE = "usage: cybertrain generate job NAME [--retry Klass:wait:attempts ...] [--force]"
|
|
18
|
+
|
|
19
|
+
# The value of each `--retry` in argv after `generate job`.
|
|
20
|
+
def self.retry_specs(args)
|
|
21
|
+
specs = Array.new(0) { "" }
|
|
22
|
+
i = 0
|
|
23
|
+
while i < args.size
|
|
24
|
+
if args[i] == "--retry"
|
|
25
|
+
raise InvalidArgument, USAGE if i + 1 >= args.size
|
|
26
|
+
|
|
27
|
+
specs << args[i + 1]
|
|
28
|
+
i += 1
|
|
29
|
+
end
|
|
30
|
+
i += 1
|
|
31
|
+
end
|
|
32
|
+
specs
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# argv after `generate job` without `--force` and the `--retry`
|
|
36
|
+
# options; any other option raises InvalidArgument.
|
|
37
|
+
def self.positional(args)
|
|
38
|
+
out = Array.new(0) { "" }
|
|
39
|
+
i = 0
|
|
40
|
+
while i < args.size
|
|
41
|
+
arg = args[i]
|
|
42
|
+
if arg == "--retry"
|
|
43
|
+
i += 1
|
|
44
|
+
elsif arg.start_with?("-") && arg != "--force"
|
|
45
|
+
raise InvalidArgument, USAGE
|
|
46
|
+
elsif arg != "--force"
|
|
47
|
+
out << arg
|
|
48
|
+
end
|
|
49
|
+
i += 1
|
|
50
|
+
end
|
|
51
|
+
out
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Returns the paths written. retries: "Klass:wait:attempts" each.
|
|
55
|
+
def self.generate(root, name, retries, force = false)
|
|
56
|
+
GeneratedFiles.require_app!(root)
|
|
57
|
+
|
|
58
|
+
# Underscored first, so "SendDigestJob" and "send_digest_job" are
|
|
59
|
+
# both send_digest_job.rb.
|
|
60
|
+
segments = GeneratedFiles.name_segments(name, "job")
|
|
61
|
+
stem = segments.last.delete_suffix("_job")
|
|
62
|
+
raise InvalidArgument, "bad job name '#{name}'" if stem.empty?
|
|
63
|
+
|
|
64
|
+
segments[segments.size - 1] = "#{stem}_job"
|
|
65
|
+
modules = segments.map { |s| Inflector.camelize(s) }
|
|
66
|
+
full = modules.join("::")
|
|
67
|
+
rel = segments.join("/")
|
|
68
|
+
lines = Array.new(0) { "" }
|
|
69
|
+
retries.each { |spec| lines << retry_line(spec) }
|
|
70
|
+
files = [
|
|
71
|
+
["app/jobs/#{rel}.rb", class_source(modules, lines)],
|
|
72
|
+
["test/jobs/#{rel}_test.rb", test_source(full, rel)]
|
|
73
|
+
]
|
|
74
|
+
GeneratedFiles.write_all(root, files, force)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# "Cybertrain::Mailer::TimeoutError:60:5" -> the retry_on line; the
|
|
78
|
+
# wait is seconds or polynomially_longer, the attempts 1 or more.
|
|
79
|
+
def self.retry_line(spec)
|
|
80
|
+
bad = "bad --retry '#{spec}': use Klass:wait:attempts (wait in seconds or polynomially_longer, attempts 1 or more)"
|
|
81
|
+
parts = spec.split(":", -1)
|
|
82
|
+
raise InvalidArgument, bad if parts.size < 3
|
|
83
|
+
|
|
84
|
+
attempts = parts[parts.size - 1].to_s
|
|
85
|
+
wait = parts[parts.size - 2].to_s
|
|
86
|
+
klass = parts[0, parts.size - 2].join(":")
|
|
87
|
+
raise InvalidArgument, bad unless constant_path?(klass)
|
|
88
|
+
raise InvalidArgument, bad unless digits?(attempts) && attempts.to_i >= 1
|
|
89
|
+
|
|
90
|
+
if wait == "polynomially_longer"
|
|
91
|
+
"retry_on #{klass}, wait: :polynomially_longer, attempts: #{attempts.to_i}"
|
|
92
|
+
elsif digits?(wait)
|
|
93
|
+
"retry_on #{klass}, wait: #{wait.to_i}, attempts: #{attempts.to_i}"
|
|
94
|
+
else
|
|
95
|
+
raise InvalidArgument, bad
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def self.digits?(text)
|
|
100
|
+
return false if text.empty?
|
|
101
|
+
|
|
102
|
+
text.each_char { |ch| return false unless ch >= "0" && ch <= "9" }
|
|
103
|
+
true
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# "Net::TimeoutError": segments of a capital letter and word characters.
|
|
107
|
+
def self.constant_path?(text)
|
|
108
|
+
return false if text.empty?
|
|
109
|
+
|
|
110
|
+
text.split("::", -1).each do |seg|
|
|
111
|
+
return false if seg.empty? || !(seg[0] >= "A" && seg[0] <= "Z")
|
|
112
|
+
|
|
113
|
+
seg.each_char do |ch|
|
|
114
|
+
return false unless (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") || (ch >= "0" && ch <= "9") || ch == "_"
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
true
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def self.class_source(modules, retry_lines)
|
|
121
|
+
name = modules.join("::")
|
|
122
|
+
body = +""
|
|
123
|
+
body << "class #{modules.last} < Cybertrain::Job::Base\n"
|
|
124
|
+
retry_lines.each { |line| body << " #{line}\n" }
|
|
125
|
+
body << "\n" unless retry_lines.empty?
|
|
126
|
+
body << " # #{name}.perform_later(\"id\" => record.id) queues it; a worker runs\n"
|
|
127
|
+
body << " # perform with that Hash read back from JSON, so narrow its values with\n"
|
|
128
|
+
body << " # Cybertrain::Cast.int / Cast.str. Declare policies above, one line each\n"
|
|
129
|
+
body << " # with literal values: retry_on Klass, wait: 60, attempts: 5 /\n"
|
|
130
|
+
body << " # discard_on Klass / limits_concurrency to: 1, key: \"id\". `spin run gen`\n"
|
|
131
|
+
body << " # writes perform_later, perform_at, perform_in and perform_now (gen/jobs.rb).\n"
|
|
132
|
+
body << " def perform(args)\n"
|
|
133
|
+
body << " nil\n"
|
|
134
|
+
body << " end\n"
|
|
135
|
+
body << "end\n"
|
|
136
|
+
GeneratedFiles.nest(modules[0, modules.size - 1], body)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The class-form test: the job queued, then performed on this thread
|
|
140
|
+
# by perform_enqueued_jobs.
|
|
141
|
+
def self.test_source(full, rel)
|
|
142
|
+
out = +""
|
|
143
|
+
out << "require \"test_helper\"\n"
|
|
144
|
+
out << "\n"
|
|
145
|
+
out << "# app/jobs/#{rel}.rb: perform_enqueued_jobs performs the jobs its block queues.\n"
|
|
146
|
+
out << "class #{full}Test < ActiveSupport::TestCase\n"
|
|
147
|
+
out << " test \"#{full} runs\" do\n"
|
|
148
|
+
out << " assert_equal \"#{full}\", #{full}.job_kind\n"
|
|
149
|
+
out << " performed = perform_enqueued_jobs do\n"
|
|
150
|
+
out << " #{full}.perform_later({ \"id\" => 1 })\n"
|
|
151
|
+
out << " end\n"
|
|
152
|
+
out << " assert_equal 1, performed\n"
|
|
153
|
+
out << " end\n"
|
|
154
|
+
out << "end\n"
|
|
155
|
+
out
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|