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.
@@ -0,0 +1,301 @@
1
+ # `cybertrain test [FILE|DIR ...] [-n NAME|-n /TEXT/]` (spec section 8): every
2
+ # test of the application in one program. The arguments are checked first
3
+ # (Test::Args, the same parser the compiled bundle filters with), then
4
+ # `SPIN_DEBUG=1 spin run gen` writes gen/tests.rb and gen/test/**, gen/tests.rb is
5
+ # compiled into build/test/tests only when an input changed since that
6
+ # binary, and the binary runs with the arguments: a filter never
7
+ # recompiles. gen's and the compiler's output go to stderr, so stdout is
8
+ # the test run's alone. Plain Ruby: runs under CRuby (the gem) and compiles
9
+ # under Spinel (bin/cybertrain.rb).
10
+ require "cybertrain/shell"
11
+ require "cybertrain/test_files"
12
+ require "cybertrain/test/args"
13
+ require "cybertrain/cli/templates"
14
+ require "cybertrain/cli/toolchain"
15
+
16
+ module Cybertrain
17
+ module CLI
18
+ module TestCommand
19
+ BINARY = "build/test/tests".freeze
20
+ # gen in spin's debug mode, as every other command builds it
21
+ # (Shell::TOOL_SPIN): one gen binary, never rebuilt for a mode switch.
22
+ GEN = "#{Shell::TOOL_SPIN} run gen".freeze
23
+ # The application's top-level directories whose files are no input of
24
+ # the bundle: build output, the release, scratch files, data.
25
+ SKIPPED = ["build", "dist", "tmp", "storage"].freeze
26
+ # The extensions of the files the bundle is built from: Ruby, YAML
27
+ # (fixtures, locales) and templates. A file of another kind that gen
28
+ # or the program reads at build time is not watched: none exists
29
+ # today; add its extension here when one does.
30
+ INPUTS = [".rb", ".yml", ".erb"].freeze
31
+ # Beside the binary: what it was built from (TestCommand.compile_bundle).
32
+ RECORD = ".inputs".freeze
33
+
34
+ # The world outside the command: the toolchain, the shell and stdout.
35
+ # TestCommand.run takes one so tests can stand in for spin.
36
+ class Runner
37
+ # Whether a Spinel of the pinned release is on PATH (installed the
38
+ # first time).
39
+ def ready?
40
+ Toolchain.ensure!
41
+ end
42
+
43
+ # Runs a spin or spinel command in root, its output on stderr; true
44
+ # when it succeeds.
45
+ def run_step(root, command)
46
+ system("cd #{Shell.quote(root)} && #{command} 1>&2")
47
+ end
48
+
49
+ def stale?(root)
50
+ TestCommand.stale?(root, "#{root}/#{BINARY}")
51
+ end
52
+
53
+ # Runs build/test/tests in root with argv; its exit status.
54
+ def run_tests(root, argv)
55
+ words = argv.map { |arg| Shell.quote(arg) }.join(" ")
56
+ command = "cd #{Shell.quote(root)} && ./#{BINARY} #{words}"
57
+ pid = Process.spawn("/bin/sh", "-c", command)
58
+ Process.wait(pid)
59
+ TestCommand.exit_code($?)
60
+ end
61
+
62
+ # Prints a line of the command's own (not the test run's) on stdout.
63
+ def say(text)
64
+ puts text
65
+ nil
66
+ end
67
+ end
68
+
69
+ # The command; its exit status: the test run's (0 or 1), 2 for bad
70
+ # arguments, 1 when gen, the compile or the bundle (gen's refusal of
71
+ # duplicate helpers) fails.
72
+ # @param root [String]
73
+ # @param argv [Array<String>]
74
+ # @return [Integer]
75
+ def self.run(root, argv, runner = Runner.new)
76
+ files = TestFiles.list(root)
77
+ # Before any step: a compile takes minutes. Parsed against the
78
+ # absolute root, as the bundle parses against Dir.pwd, so an
79
+ # absolute file argument means the same to both.
80
+ args = Cybertrain::Test::Args.parse(argv, files, File.expand_path(root))
81
+ unless args.arg_error.empty?
82
+ runner.say("#{Cybertrain::Test::Args::USAGE}\n#{args.arg_error}")
83
+ return 2
84
+ end
85
+ if files.empty?
86
+ runner.say("0 tests, 0 assertions, 0 failures")
87
+ return 0
88
+ end
89
+ return 1 unless runner.ready?
90
+ return 1 unless runner.run_step(root, GEN)
91
+
92
+ problem = refusal(root)
93
+ unless problem.empty?
94
+ runner.say(problem)
95
+ return 1
96
+ end
97
+ if runner.stale?(root)
98
+ return 1 unless compile_bundle(root, runner)
99
+ end
100
+ runner.run_tests(root, argv)
101
+ end
102
+
103
+ # Compiles build/test/tests and records what it was built from in
104
+ # build/test/tests.inputs ({inputs_record}). The record is written
105
+ # before the compile starts and kept only when it succeeds, so its
106
+ # mtime is the start: a source edited during the compile is newer
107
+ # than it. The old binary and record go first, so an interrupted or
108
+ # failed compile leaves nothing that looks fresh.
109
+ def self.compile_bundle(root, runner)
110
+ binary = "#{root}/#{BINARY}"
111
+ record = "#{binary}#{RECORD}"
112
+ pending = "#{record}.tmp"
113
+ Templates.mkdir_p("#{root}/build/test")
114
+ File.delete(binary) if File.exist?(binary)
115
+ File.delete(record) if File.exist?(record)
116
+ File.write(pending, inputs_record(root, spin_flags_in(root)))
117
+ unless runner.run_step(root, compile_command(root))
118
+ File.delete(pending) if File.exist?(pending)
119
+ return false
120
+ end
121
+ File.rename(pending, record)
122
+ true
123
+ end
124
+
125
+ # `spin flags` in root.
126
+ def self.spin_flags_in(root)
127
+ `cd #{Shell.quote(root)} && spin flags`
128
+ end
129
+
130
+ # What a bundle is built from besides its sources' contents: the
131
+ # compile command, the `spin flags` output (which framework checkout
132
+ # and which load path), each framework root's cybertrain/version.rb,
133
+ # spin.toml and spin.lock as they are, and the paths of test/**/*.rb.
134
+ # A different record (another framework version or checkout, a
135
+ # repointed dependency, a file under test/ added or deleted, which
136
+ # no mtime shows: a test's `require_relative` of a deleted file must
137
+ # fail the compile, not reuse the binary) means a compile, whatever
138
+ # the mtimes say.
139
+ # @param root [String]
140
+ # @param flags [String] the `spin flags` output
141
+ # @return [String]
142
+ def self.inputs_record(root, flags)
143
+ out = +"compile: #{compile_command(root)}\nflags: #{flags.strip}\n"
144
+ framework_roots(root, flags).each do |dir|
145
+ out << "framework: #{dir}\n" << file_text("#{dir}/cybertrain/version.rb")
146
+ end
147
+ out << "spin.toml:\n" << file_text("#{root}/spin.toml")
148
+ out << "spin.lock:\n" << file_text("#{root}/spin.lock")
149
+ out << "test files:\n"
150
+ paths = Array.new(0) { "" }
151
+ TestFiles.walk(root, "test", paths)
152
+ paths.sort.each { |path| out << path << "\n" }
153
+ out
154
+ end
155
+
156
+ # path's contents, or "(none)\n".
157
+ def self.file_text(path)
158
+ File.file?(path) ? File.read(path) : "(none)\n"
159
+ end
160
+
161
+ # The bundle's compile, run in the application's directory (plan
162
+ # decision 7, as task 1's spike recorded it): gen/test before test,
163
+ # so a top-level-form file's `require "test_helper"` finds the copy
164
+ # the bundle already loaded (ruling R9). `$(spin flags)` is split by
165
+ # the shell, as spin prints it: a path with a space in it cannot be
166
+ # passed through it.
167
+ # @param root [String]
168
+ # @return [String]
169
+ def self.compile_command(root)
170
+ "spinel gen/tests.rb $(spin flags) -I gen/test -I test -o #{BINARY}"
171
+ end
172
+
173
+ # Whether binary must be compiled again: it or its record
174
+ # (binary + ".inputs", written by {compile_bundle}) is missing, the record
175
+ # differs from {inputs_record} now, or an input is newer than the
176
+ # record (the compile's start): a *.rb, *.yml or *.erb of the
177
+ # application (outside build/, dist/, tmp/, storage/ and dot
178
+ # directories), the framework's cybertrain.rb and cybertrain/**/*.rb
179
+ # (the framework root is the `-I` of `spin flags` that is not the
180
+ # application), or a toolchain file of `spin flags --deps`.
181
+ # @param root [String]
182
+ # @param binary [String]
183
+ # @return [Boolean]
184
+ def self.stale?(root, binary)
185
+ record = "#{binary}#{RECORD}"
186
+ return true unless File.file?(binary) && File.file?(record)
187
+
188
+ flags = spin_flags_in(root)
189
+ return true unless File.read(record) == inputs_record(root, flags)
190
+
191
+ built = mtime_ns(record)
192
+ return true if newer_under?(root, "", built, true)
193
+
194
+ framework_roots(root, flags).each do |dir|
195
+ return true if mtime_ns("#{dir}/cybertrain.rb") > built
196
+ return true if newer_under?(dir, "cybertrain", built, false)
197
+ end
198
+ `cd #{Shell.quote(root)} && spin flags --deps`.split.each do |path|
199
+ return true if mtime_ns(path) > built
200
+ end
201
+ false
202
+ end
203
+
204
+ # The `-I` directories of `spin flags` output other than root's own,
205
+ # each in its real spelling (spin prints real paths; root may be
206
+ # spelled through a symlink).
207
+ def self.framework_roots(root, flags)
208
+ own = Cybertrain::Test::Args.real_path(File.expand_path(root))
209
+ out = Array.new(0) { "" }
210
+ words = flags.split
211
+ i = 0
212
+ while i < words.size
213
+ if words[i] == "-I" && i + 1 < words.size
214
+ dir = Cybertrain::Test::Args.real_path(File.expand_path(words[i + 1], own))
215
+ out << dir unless dir == own || out.include?(dir)
216
+ i += 2
217
+ else
218
+ i += 1
219
+ end
220
+ end
221
+ out
222
+ end
223
+
224
+ # Whether a bundle input under base/rel is newer than built; at the
225
+ # application's top level (top) SKIPPED is left out. Dot entries
226
+ # (.git, .github) never are inputs.
227
+ def self.newer_under?(base, rel, built, top)
228
+ dir = rel.empty? ? base : "#{base}/#{rel}"
229
+ return false unless File.directory?(dir)
230
+
231
+ Dir.children(dir).each do |entry|
232
+ next if entry.start_with?(".")
233
+ next if top && SKIPPED.include?(entry)
234
+
235
+ child = rel.empty? ? entry : "#{rel}/#{entry}"
236
+ path = "#{base}/#{child}"
237
+ if File.directory?(path)
238
+ return true if newer_under?(base, child, built, false)
239
+ elsif INPUTS.any? { |ext| entry.end_with?(ext) }
240
+ return true if mtime_ns(path) > built
241
+ end
242
+ end
243
+ false
244
+ end
245
+
246
+ # path's mtime in nanoseconds, -1 when it is no file.
247
+ def self.mtime_ns(path)
248
+ return -1 unless File.file?(path)
249
+
250
+ t = File.mtime(path)
251
+ t.to_i * 1_000_000_000 + t.nsec
252
+ end
253
+
254
+ # gen's refusal of the bundle (a gen/tests.rb that only aborts, with
255
+ # the reason: duplicate top-level helpers, an unsupported block form),
256
+ # or "" when gen/tests.rb is a program to compile.
257
+ # @param root [String]
258
+ # @return [String]
259
+ def self.refusal(root)
260
+ path = "#{root}/gen/tests.rb"
261
+ return "" unless File.file?(path)
262
+
263
+ File.read(path).each_line do |line|
264
+ text = line.chomp
265
+ next if text.start_with?("#") || text.empty?
266
+ return "" unless text.start_with?("abort(\"") && text.end_with?("\")")
267
+
268
+ return unescape(text[7, text.size - 9].to_s)
269
+ end
270
+ ""
271
+ end
272
+
273
+ # The text of a Ruby double-quoted literal's body as gen writes it
274
+ # (\\, \", \n and \#).
275
+ def self.unescape(body)
276
+ out = +""
277
+ i = 0
278
+ while i < body.size
279
+ ch = body[i]
280
+ if ch == "\\" && i + 1 < body.size
281
+ nxt = body[i + 1]
282
+ out << (nxt == "n" ? "\n" : nxt)
283
+ i += 2
284
+ else
285
+ out << ch
286
+ i += 1
287
+ end
288
+ end
289
+ out
290
+ end
291
+
292
+ # A child's exit status, 1 when it has none (a signal ended it).
293
+ def self.exit_code(status)
294
+ return 1 if status.nil?
295
+
296
+ code = status.exitstatus
297
+ code.nil? ? 1 : code
298
+ end
299
+ end
300
+ end
301
+ end
@@ -629,8 +629,10 @@ module Cybertrain
629
629
  compiles?("zlib.h", "zlibVersion() != 0 ? 0 : 1", "-lz")
630
630
  end
631
631
 
632
- # Spinel's own Makefile finds a Homebrew OpenSSL by itself; this probe
633
- # only decides whether to warn.
632
+ # OpenSSL's header and library: every application links Spinel's
633
+ # openssl package (the session cookie is encrypted), which Spinel
634
+ # builds only when it finds them. Spinel's own Makefile finds a
635
+ # Homebrew OpenSSL by itself; this probe also tries that prefix.
634
636
  def self.openssl_ok?
635
637
  return true if compiles?("openssl/ssl.h", "TLS_client_method() != 0 ? 0 : 1", "-lssl -lcrypto")
636
638
 
@@ -641,9 +643,10 @@ module Cybertrain
641
643
  "-I#{Shell.quote(File.join(brew, "include"))} -L#{Shell.quote(File.join(brew, "lib"))} -lssl -lcrypto")
642
644
  end
643
645
 
644
- # What a Spinel build cannot do without, by name. OpenSSL is not in
645
- # this list: without its headers Spinel builds without its openssl
646
- # package, which cybertrain does not use.
646
+ # What a Spinel build cannot do without, by name. OpenSSL comes with
647
+ # the application requirements below: without its headers Spinel
648
+ # still builds, only without its openssl package, which every
649
+ # application links (the session cookie is encrypted).
647
650
  def self.missing_requirements
648
651
  missing = Array.new(0) { "" }
649
652
  if platform == "darwin" && !system("xcode-select -p > /dev/null 2>&1")
@@ -677,6 +680,7 @@ module Cybertrain
677
680
  end
678
681
  missing << "the SQLite 3 headers and library" unless sqlite_ok?
679
682
  missing << "the zlib headers and library" unless zlib_ok?
683
+ missing << "the OpenSSL headers and library" unless openssl_ok?
680
684
  missing
681
685
  end
682
686
 
@@ -730,9 +734,9 @@ module Cybertrain
730
734
  ["sudo zypper install -y gcc make git curl sqlite3-devel zlib-devel libopenssl-devel"]
731
735
  when "darwin"
732
736
  ["xcode-select --install # cc, make, git, curl, the SQLite and zlib headers",
733
- "brew install openssl@3 # optional: Spinel's openssl package"]
737
+ "brew install openssl@3 # the OpenSSL headers every app links"]
734
738
  else
735
- ["install git, make, curl, a C compiler and the SQLite 3 and zlib development headers with your package manager"]
739
+ ["install git, make, curl, a C compiler and the SQLite 3, zlib and OpenSSL development headers with your package manager"]
736
740
  end
737
741
  end
738
742
 
@@ -753,11 +757,7 @@ module Cybertrain
753
757
  if have_command?(cc_program)
754
758
  report("sqlite3", sqlite_ok?, sqlite_ok? ? "headers and library found" : "sqlite3.h or -lsqlite3 missing")
755
759
  report("zlib", zlib_ok?, zlib_ok? ? "headers and library found" : "zlib.h or -lz missing")
756
- if openssl_ok?
757
- report("openssl", true, "headers and library found")
758
- else
759
- info("openssl", "not found: Spinel builds without its openssl package, which cybertrain does not use")
760
- end
760
+ report("openssl", openssl_ok?, openssl_ok? ? "headers and library found" : "openssl/ssl.h or -lssl -lcrypto missing")
761
761
  end
762
762
  ready = doctor_spinel
763
763
  problems = doctor_problems(ready)
data/cybertrain/cli.rb CHANGED
@@ -4,20 +4,28 @@
4
4
  # cybertrain new NAME [--path DIR | --version V | --git URL [--ref R]] [--skip-spin]
5
5
  # cybertrain generate scaffold NAME field:type ... [parent:references] [--any]
6
6
  # cybertrain generate attachment MODEL FIELD [--any]
7
- # cybertrain generate authentication [--invite-only]
7
+ # cybertrain generate authentication [--invite-only] [--confirmable] [--force]
8
+ # cybertrain generate helper NAME [--force] | generate component NAME [attr[:type]...] [--force]
9
+ # cybertrain generate job NAME [--retry Klass:wait:attempts ...] [--force]
8
10
  # cybertrain db migrate | db COMMAND... | server [PORT] | build | spin ARGS...
9
- # cybertrain setup [--force] | doctor | version | help
11
+ # cybertrain test [FILE|DIR ...] [-n NAME|-n /TEXT/]
12
+ # cybertrain secret [--hex] | setup [--force] | doctor | version | help
10
13
  #
11
14
  # Every command that runs spin first puts a Spinel of the pinned release on
12
15
  # PATH (Toolchain.ensure!), installing it under ~/.cybertrain the first time.
13
16
  require "cybertrain/version"
17
+ require "cybertrain/crypto"
14
18
  require "cybertrain/cli/templates"
15
19
  require "cybertrain/cli/new_app"
16
20
  require "cybertrain/cli/scaffold"
17
21
  require "cybertrain/cli/authentication"
18
22
  require "cybertrain/cli/attachment"
23
+ require "cybertrain/cli/helper"
24
+ require "cybertrain/cli/component"
25
+ require "cybertrain/cli/job"
19
26
  require "cybertrain/cli/build"
20
27
  require "cybertrain/cli/toolchain"
28
+ require "cybertrain/cli/test_command"
21
29
 
22
30
  module Cybertrain
23
31
  module CLI
@@ -25,7 +33,8 @@ module Cybertrain
25
33
  Usage:
26
34
  cybertrain new NAME [--path DIR | --version V | --git URL [--ref R]] [--skip-spin]
27
35
  Create an application in NAME, then run `spin lock` and
28
- `spin run gen` in it (skipped with --skip-spin). Its spin.toml
36
+ `SPIN_DEBUG=1 spin run gen` in it (skipped with --skip-spin;
37
+ gen and db are built in spin's debug mode). Its spin.toml
29
38
  depends on this cybertrain's release by default:
30
39
  git tag v#{Cybertrain::VERSION} of #{Cybertrain::REPOSITORY}
31
40
  --path DIR: the framework checkout at DIR (relative to the
@@ -46,32 +55,80 @@ module Cybertrain
46
55
  up with the SQLite file). Only PNG, JPEG, GIF and WebP images are
47
56
  accepted, unless --any allows any file; the stored type is always
48
57
  the one detected from the bytes.
49
- cybertrain generate authentication [--invite-only]
58
+ cybertrain generate authentication [--invite-only] [--confirmable] [--force]
50
59
  Add email-and-password sign-in to the application in the current
51
60
  directory: users and sessions tables, User and Session models,
52
- SessionsController and RegistrationsController with their views,
53
- the routes, a before_action in ApplicationController and a test.
54
- --invite-only: no sign-up page. Users are created with
55
- `./NAME user EMAIL` (password from stdin), and the first one
56
- also at /setup, which answers only to this machine, only with the
57
- one-time token the server's boot warning prints, and only while
58
- no user exists (NAME_SETUP_OPEN=1 lifts the address check).
61
+ the Authentication concern (included into ApplicationController),
62
+ SessionsController and RegistrationsController with their views
63
+ (their texts in the bundled en and ja dictionaries), the routes,
64
+ the jobs page behind require_admin, and tests. `./NAME user
65
+ EMAIL` creates a user (password from stdin), `./NAME user admin
66
+ EMAIL` makes one an admin, `user unadmin EMAIL` takes it back.
67
+ --invite-only: no sign-up page. The first user is created with
68
+ the user command, or at /setup, which answers only to this
69
+ machine, only with the one-time token the server's boot warning
70
+ prints, and only while no user exists (NAME_SETUP_OPEN=1 lifts
71
+ the address check).
72
+ --confirmable: a new account confirms its address by mail
73
+ before it can sign in (a form sends the link again), and a new
74
+ address on the account page takes effect once its own link is
75
+ opened. Users the user command or /setup creates are confirmed.
76
+ Run again over an application generated before (by 0.4, or
77
+ without --confirmable), it writes the missing files and a
78
+ migration with the missing columns, and prints a diff for each
79
+ file it would change, to apply by hand (patch -p1 applies them).
80
+ --force: overwrite those files instead (and replace 0.4's
81
+ authentication block in ApplicationController, unless edited).
82
+ cybertrain generate helper NAME [--force]
83
+ Add app/helpers/NAME_helper.rb (an empty module NameHelper, which
84
+ `spin run gen` makes callable from every template) and a test.
85
+ cybertrain generate component NAME [attr[:type] ...] [--force]
86
+ Add a view component: app/components/NAME_component.rb (one
87
+ keyword per attribute), its template NAME_component.html.erb and
88
+ a test rendering it. Types: #{Component::TYPES.join(", ")}
89
+ (default string). Templates render it with
90
+ <%= render component: "NAME", attr: value %>.
91
+ --force overwrites files that already exist.
92
+ cybertrain generate job NAME [--retry Klass:wait:attempts ...] [--force]
93
+ Add a background job: app/jobs/NAME_job.rb (a Cybertrain::Job::Base
94
+ with an empty perform(args) and a retry_on line per --retry: wait
95
+ in seconds or polynomially_longer, then the attempts) and a test
96
+ running it. Queue it with NameJob.perform_later("id" => 1) once
97
+ `spin run gen` has written gen/jobs.rb.
59
98
  cybertrain db migrate
60
99
  Generate, apply pending migrations, generate again
61
- (spin run gen; spin run db -- migrate; spin run gen).
100
+ (SPIN_DEBUG=1 spin run gen; SPIN_DEBUG=1 spin run db -- migrate;
101
+ SPIN_DEBUG=1 spin run gen).
62
102
  cybertrain db COMMAND...
63
- Any other database command, after `spin run gen`: status,
64
- rollback [N] (generates again afterwards), schema:dump, create.
103
+ Any other database command, after `SPIN_DEBUG=1 spin run gen`:
104
+ status, rollback [N] (generates again afterwards), schema,
105
+ schema:dump, create.
65
106
  `cybertrain migration` is the same as `cybertrain db migrate`.
66
107
  cybertrain server [PORT]
67
108
  Generate, then start the development server
68
- (spin run gen; spin run NAME [-- PORT]; default port 3000).
109
+ (SPIN_DEBUG=1 spin run gen; spin build NAME -- --incremental
110
+ when a Ruby file is newer than build/bin/NAME; spin run NAME
111
+ [-- PORT]; default port 3000).
69
112
  cybertrain build
70
113
  Build NAME with app/views embedded and assemble dist/
71
114
  (the binary, public/, storage/, tmp/).
115
+ cybertrain test [FILE|DIR ...] [-n NAME|-n /TEXT/]
116
+ Run the application's tests (test/**/*_test.rb and test/*.rb)
117
+ as one program: SPIN_DEBUG=1 spin run gen, then gen/tests.rb compiled into
118
+ build/test/tests when a source is newer than it, then run.
119
+ FILE or DIR keeps the tests of those files; -n NAME the test
120
+ named NAME (or its method name, test_...); -n /TEXT/ the tests
121
+ whose name contains TEXT. A filter never recompiles. Exits 1
122
+ when a test failed, 2 for bad arguments.
72
123
  cybertrain spin ARGS...
73
124
  Run spin with the pinned Spinel, from any directory
74
125
  (cybertrain spin test; cybertrain spin run gen -- --check).
126
+ cybertrain secret [--hex]
127
+ Print a new random secret for CYBERTRAIN_SECRET_KEY_BASE (signs
128
+ the session cookie); with --hex, a new key for
129
+ CYBERTRAIN_ENCRYPTION_KEY (encrypts the columns of `encrypts`).
130
+ Both are 64 hex digits (32 random bytes). Keep the encryption
131
+ key: data encrypted with it cannot be read without it.
75
132
  cybertrain setup [--force]
76
133
  Install Spinel #{Cybertrain::SPINEL_TAG} into ~/.cybertrain (CYBERTRAIN_HOME)
77
134
  unless a spinel of that release is already on PATH, and print
@@ -93,7 +150,11 @@ module Cybertrain
93
150
  when "db" then run_in_app { |_name| run_db(argv) }
94
151
  when "server" then run_in_app { |name| run_server(name, argv) }
95
152
  when "build" then run_in_app { |name| build_app(name) }
153
+ when "test" then run_in_app { |_name| TestCommand.run(".", argv[1, argv.size - 1]) }
96
154
  when "spin" then run_spin_passthrough(argv)
155
+ when "secret"
156
+ puts secret_text(argv[1, argv.size - 1])
157
+ 0
97
158
  when "setup" then Toolchain.setup(argv[1, argv.size - 1])
98
159
  when "doctor" then run_doctor(argv)
99
160
  when "version", "--version", "-v"
@@ -112,6 +173,15 @@ module Cybertrain
112
173
  1
113
174
  end
114
175
 
176
+ # `cybertrain secret`: a session secret; `secret --hex`: an encryption
177
+ # key. Both 64 hex digits; kept apart so that each says what it is for.
178
+ def self.secret_text(args)
179
+ return Crypto.random_token(32) if args.empty?
180
+ return Crypto.encryption_key_hex if args == ["--hex"]
181
+
182
+ raise InvalidArgument, "usage: cybertrain secret [--hex]"
183
+ end
184
+
115
185
  def self.run_new(argv)
116
186
  raise InvalidArgument, "usage: cybertrain new NAME [--path DIR | --version V | --git URL [--ref R]] [--skip-spin]" if argv.size < 2
117
187
 
@@ -241,13 +311,13 @@ module Cybertrain
241
311
  def self.run_db(argv)
242
312
  args = argv[1, argv.size - 1]
243
313
  if args.empty?
244
- puts "usage: cybertrain db (migrate|rollback [N]|status|schema:dump|create)"
314
+ puts "usage: cybertrain db (migrate|rollback [N]|status|schema|schema:dump|create|normalize)"
245
315
  return 1
246
316
  end
247
317
  run_spin(Build.db_commands(args))
248
318
  end
249
319
 
250
- # `cybertrain server [PORT]`.
320
+ # `cybertrain server [PORT]`: Build.run_server's steps.
251
321
  def self.run_server(name, argv)
252
322
  port = argv.size > 1 ? argv[1] : ""
253
323
  unless port == "" || Build.port?(port)
@@ -258,7 +328,9 @@ module Cybertrain
258
328
  puts "error: unexpected argument '#{argv[2]}'"
259
329
  return 1
260
330
  end
261
- run_spin(Build.server_commands(name, port))
331
+ return 1 unless Toolchain.ensure!
332
+
333
+ Build.run_server(".", name, port)
262
334
  end
263
335
 
264
336
  # `args` without the (every) `flag` option.
@@ -288,13 +360,15 @@ module Cybertrain
288
360
  puts "next:"
289
361
  puts " cybertrain db migrate"
290
362
  puts " cybertrain server"
291
- puts " cybertrain spin test # test/attachment_*.rb; snapshot it from the binary (README, \"Testing an app\")"
363
+ puts " cybertrain test # test/integration/*_attachment_test.rb among the others"
292
364
  when "authentication"
293
- args = without_flag(argv[2, argv.size - 2], "--invite-only")
294
- raise InvalidArgument, "usage: cybertrain generate authentication [--invite-only]" unless args.empty?
365
+ args = without_flag(without_flag(without_flag(argv[2, argv.size - 2], "--invite-only"), "--confirmable"), "--force")
366
+ raise InvalidArgument, "usage: cybertrain generate authentication [--invite-only] [--confirmable] [--force]" unless args.empty?
295
367
 
296
368
  invite_only = argv.include?("--invite-only")
297
- Authentication.generate(".", invite_only)
369
+ confirmable = argv.include?("--confirmable")
370
+ result = Authentication.generate_with_result(".", invite_only, confirmable, argv.include?("--force"))
371
+ result.manual.each { |diff| puts diff }
298
372
  puts ""
299
373
  puts "next:"
300
374
  puts " cybertrain db migrate"
@@ -304,9 +378,56 @@ module Cybertrain
304
378
  else
305
379
  puts " cybertrain server # then open /sign_up"
306
380
  end
307
- puts " cybertrain spin test # test/authentication.rb; snapshot it from the binary (README, \"Testing an app\")"
381
+ puts " cybertrain test # test/controllers/sessions_controller_test.rb and the other authentication tests among the others"
382
+ base_url_env = Authentication.base_url_env(Build.app_name("."))
383
+ mails = confirmable ? "password reset and confirmation mails" : "password reset mails"
384
+ puts " # #{mails}: set #{base_url_env} and CYBERTRAIN_SMTP_* (config/app.rb, Cybertrain::Config)"
385
+ unless result.manual.empty?
386
+ # The warnings above a diff (with --confirmable, what the sessions
387
+ # and registrations controllers leave out until theirs is
388
+ # applied), once more at the end.
389
+ result.manual.each do |diff|
390
+ lines = diff.lines
391
+ next if lines.size < 2 || !lines[1].start_with?("IMPORTANT: ")
392
+
393
+ path = lines[0].delete_prefix("manual ")
394
+ path = path[0, path.index(":").to_i].to_s
395
+ puts "IMPORTANT: #{path}: #{lines[1].strip.delete_prefix("IMPORTANT: ")}"
396
+ end
397
+ puts ""
398
+ puts "next: apply the diffs above, or re-run with --force for files you never edited"
399
+ puts " # the app may not build until these diffs are applied: the files written now need them"
400
+ end
401
+ when "helper"
402
+ args = without_flag(argv[2, argv.size - 2], "--force")
403
+ raise InvalidArgument, "usage: cybertrain generate helper NAME [--force]" unless args.size == 1
404
+
405
+ Helper.generate(".", args[0], argv.include?("--force"))
406
+ puts ""
407
+ puts "next:"
408
+ puts " spin run gen # includes the module into the app's helpers"
409
+ puts " cybertrain test # test/helpers/#{Inflector.underscore(args[0]).delete_suffix("_helper")}_helper_test.rb among the others"
410
+ when "component"
411
+ args = without_flag(argv[2, argv.size - 2], "--force")
412
+ raise InvalidArgument, "usage: cybertrain generate component NAME [attr[:type] ...] [--force]" if args.empty?
413
+
414
+ Component.generate(".", args[0], args[1, args.size - 1], argv.include?("--force"))
415
+ puts ""
416
+ puts "next:"
417
+ puts " spin run gen # writes gen/components.rb"
418
+ puts " cybertrain test # test/components/#{Inflector.underscore(args[0]).delete_suffix("_component")}_component_test.rb among the others"
419
+ when "job"
420
+ rest = argv[2, argv.size - 2]
421
+ args = Job.positional(rest)
422
+ raise InvalidArgument, Job::USAGE unless args.size == 1
423
+
424
+ Job.generate(".", args[0], Job.retry_specs(rest), argv.include?("--force"))
425
+ puts ""
426
+ puts "next:"
427
+ puts " spin run gen # writes gen/jobs.rb (perform_later and the retry arms)"
428
+ puts " cybertrain test # test/jobs/#{Inflector.underscore(args[0]).delete_suffix("_job")}_job_test.rb among the others"
308
429
  else
309
- raise InvalidArgument, "only `generate scaffold`, `generate attachment` and `generate authentication` are supported"
430
+ raise InvalidArgument, "only `generate scaffold`, `generate attachment`, `generate authentication`, `generate helper`, `generate component` and `generate job` are supported"
310
431
  end
311
432
  0
312
433
  end