cybertrain 0.2.0 → 0.3.0
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 +1320 -36
- data/cybertrain/app_name.rb +36 -0
- data/cybertrain/cli/attachment.rb +1323 -0
- data/cybertrain/cli/authentication.rb +1528 -0
- data/cybertrain/cli/build.rb +75 -19
- data/cybertrain/cli/new_app.rb +14 -2
- data/cybertrain/cli/scaffold.rb +137 -21
- data/cybertrain/cli/templates.rb +401 -20
- data/cybertrain/cli/toolchain.rb +100 -41
- data/cybertrain/cli.rb +80 -6
- data/cybertrain/generator/inflector.rb +31 -9
- data/cybertrain/ident.rb +171 -0
- data/cybertrain/shell.rb +15 -0
- data/cybertrain/version.rb +8 -4
- metadata +6 -1
data/cybertrain/cli/build.rb
CHANGED
|
@@ -1,10 +1,43 @@
|
|
|
1
1
|
# `cybertrain build`: dist/ = the app binary with app/views embedded, plus
|
|
2
2
|
# public/. Also the command lists `cybertrain db` and `server` run. Plain Ruby:
|
|
3
3
|
# runs under CRuby (the gem) and compiles under Spinel (spin install).
|
|
4
|
+
require "cybertrain/app_name"
|
|
5
|
+
require "cybertrain/shell"
|
|
6
|
+
|
|
4
7
|
module Cybertrain
|
|
5
8
|
module CLI
|
|
6
9
|
module Build
|
|
7
|
-
# The
|
|
10
|
+
# The entries assemble() keeps in dist/ besides the binary: the copied
|
|
11
|
+
# public/ and the storage/ and tmp/ directories. A package named like
|
|
12
|
+
# one of them would collide with it (the binary renamed to dist/public
|
|
13
|
+
# gets moved aside as dist/.public.old and deleted; a dist/storage
|
|
14
|
+
# directory cannot be replaced by the binary), so `cybertrain build`
|
|
15
|
+
# (require_dist_name!) and `cybertrain new` refuse these names;
|
|
16
|
+
# app_name, which every command reads, does not: `cybertrain db` and
|
|
17
|
+
# `server` work for an existing app named like one. assemble builds its
|
|
18
|
+
# paths from these constants so the two cannot drift.
|
|
19
|
+
DIST_PUBLIC = "public"
|
|
20
|
+
DIST_STORAGE = "storage"
|
|
21
|
+
DIST_TMP = "tmp"
|
|
22
|
+
DIST_ENTRIES = [DIST_PUBLIC, DIST_STORAGE, DIST_TMP]
|
|
23
|
+
|
|
24
|
+
# "" when name cannot collide with what assemble() puts in dist/, else
|
|
25
|
+
# the reason. A leading "." covers assemble's scratch entries
|
|
26
|
+
# (dist/.<name>.tmp, dist/.public.tmp, dist/.public.old) and "."/"..".
|
|
27
|
+
# The entry names match case-insensitively: on a case-insensitive
|
|
28
|
+
# filesystem (macOS's default APFS, one of the CI OSes; Windows) "Public"
|
|
29
|
+
# and "TMP" are the same directory entry as public and tmp. Only a
|
|
30
|
+
# different spelling of the whole name ("tmp2", "my_public") is another
|
|
31
|
+
# entry. downcase is ASCII-safe here: the entries are ASCII.
|
|
32
|
+
def self.dist_name_problem(name)
|
|
33
|
+
return "" unless DIST_ENTRIES.include?(name.downcase) || name.start_with?(".")
|
|
34
|
+
|
|
35
|
+
"collides with what `cybertrain build` keeps in dist/ (#{DIST_ENTRIES.join(", ")}) or its scratch entries: it cannot be one of those names (in any letter case) or start with '.'"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The [package] name in root/spin.toml, or "" when there is none. The
|
|
39
|
+
# name ends up in shell command strings and file paths, so it must be
|
|
40
|
+
# a name `cybertrain new` could have produced.
|
|
8
41
|
def self.app_name(root)
|
|
9
42
|
path = "#{root}/spin.toml"
|
|
10
43
|
return "" unless File.exist?(path)
|
|
@@ -22,11 +55,35 @@ module Cybertrain
|
|
|
22
55
|
break
|
|
23
56
|
end
|
|
24
57
|
end
|
|
58
|
+
# An empty name is run_in_app's "no spin.toml with a [package] name".
|
|
59
|
+
# Any other name must be usable as a `spin build` target and a
|
|
60
|
+
# build/bin/<name> path: the same predicate Application.new applies to
|
|
61
|
+
# the dev rebuilder's target. The name reaches the shell through
|
|
62
|
+
# quote_arg, so only what breaks that target or path is refused.
|
|
63
|
+
return "" if name.empty?
|
|
64
|
+
|
|
65
|
+
problem = AppName.problem(name)
|
|
66
|
+
raise InvalidArgument, "spin.toml [package] name '#{name}' #{problem}" unless problem.empty?
|
|
67
|
+
|
|
25
68
|
name
|
|
26
69
|
end
|
|
27
70
|
|
|
71
|
+
# The build path's check, made once in CLI.build_app before the
|
|
72
|
+
# toolchain is touched (Build.run does not repeat it): a name that
|
|
73
|
+
# collides with a dist/ entry only matters to assemble. Returns nil.
|
|
74
|
+
def self.require_dist_name!(name)
|
|
75
|
+
problem = dist_name_problem(name)
|
|
76
|
+
raise InvalidArgument, "spin.toml [package] name '#{name}' #{problem}" unless problem.empty?
|
|
77
|
+
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# name goes through quote_arg too: app_name validates it, but the
|
|
82
|
+
# command strings stay safe for a caller that skipped that, whatever the
|
|
83
|
+
# spelling. An empty name cannot reach these from the CLI (run_in_app
|
|
84
|
+
# refuses it first); a direct caller passing "" gets `spin build ''`.
|
|
28
85
|
def self.commands(name)
|
|
29
|
-
["spin run gen -- --embed-views", "spin build #{name}", "spin run gen"]
|
|
86
|
+
["spin run gen -- --embed-views", "spin build #{quote_arg(name)}", "spin run gen"]
|
|
30
87
|
end
|
|
31
88
|
|
|
32
89
|
# `cybertrain db ARGS`, through bin/db.rb (the app binary cannot compile
|
|
@@ -50,12 +107,13 @@ module Cybertrain
|
|
|
50
107
|
def self.quote_arg(text)
|
|
51
108
|
return text if !text.empty? && text.match(/\A[A-Za-z0-9_:.\/-]+\z/)
|
|
52
109
|
|
|
53
|
-
|
|
110
|
+
Shell.quote(text)
|
|
54
111
|
end
|
|
55
112
|
|
|
56
113
|
# port is "" (the app's default, 3000) or a port? string.
|
|
57
114
|
def self.server_commands(name, port)
|
|
58
|
-
|
|
115
|
+
target = quote_arg(name)
|
|
116
|
+
run = port == "" ? "spin run #{target}" : "spin run #{target} -- #{quote_arg(port)}"
|
|
59
117
|
["spin run gen", run]
|
|
60
118
|
end
|
|
61
119
|
|
|
@@ -84,6 +142,8 @@ module Cybertrain
|
|
|
84
142
|
end
|
|
85
143
|
|
|
86
144
|
# Runs the build in root and assembles dist/. Returns the exit code.
|
|
145
|
+
# name must already have passed require_dist_name!: CLI.build_app checks
|
|
146
|
+
# it once, before the toolchain is fetched.
|
|
87
147
|
def self.run(root, name, runner = Runner.new)
|
|
88
148
|
steps = commands(name)
|
|
89
149
|
ok = false
|
|
@@ -120,7 +180,7 @@ module Cybertrain
|
|
|
120
180
|
|
|
121
181
|
def self.run_in(root, command)
|
|
122
182
|
puts "run #{command}"
|
|
123
|
-
system("cd #{
|
|
183
|
+
system("cd #{Shell.quote(root)} && #{command}")
|
|
124
184
|
end
|
|
125
185
|
|
|
126
186
|
# Copies build/bin/<name> and public/ into dist/; storage/ and tmp/ are
|
|
@@ -138,32 +198,32 @@ module Cybertrain
|
|
|
138
198
|
dist = "#{root}/dist"
|
|
139
199
|
Templates.mkdir_p(dist)
|
|
140
200
|
binary_tmp = "#{dist}/.#{name}.tmp"
|
|
141
|
-
public_tmp = "#{dist}
|
|
142
|
-
public_old = "#{dist}
|
|
201
|
+
public_tmp = "#{dist}/.#{DIST_PUBLIC}.tmp"
|
|
202
|
+
public_old = "#{dist}/.#{DIST_PUBLIC}.old"
|
|
143
203
|
# Leftovers of an interrupted earlier run.
|
|
144
204
|
rm_tree(binary_tmp)
|
|
145
205
|
rm_tree(public_tmp)
|
|
146
206
|
rm_tree(public_old)
|
|
147
207
|
|
|
148
|
-
copy_command = "cp #{
|
|
208
|
+
copy_command = "cp #{Shell.quote(binary)} #{Shell.quote(binary_tmp)}"
|
|
149
209
|
raise InvalidArgument, "could not copy #{binary} to dist/" unless system(copy_command)
|
|
150
210
|
|
|
151
211
|
File.rename(binary_tmp, "#{dist}/#{name}")
|
|
152
212
|
|
|
153
|
-
if File.directory?("#{root}
|
|
154
|
-
public_command = "cp -R #{
|
|
155
|
-
raise InvalidArgument, "could not copy
|
|
213
|
+
if File.directory?("#{root}/#{DIST_PUBLIC}")
|
|
214
|
+
public_command = "cp -R #{Shell.quote("#{root}/#{DIST_PUBLIC}")} #{Shell.quote(public_tmp)}"
|
|
215
|
+
raise InvalidArgument, "could not copy #{DIST_PUBLIC}/ to dist/" unless system(public_command)
|
|
156
216
|
else
|
|
157
217
|
Dir.mkdir(public_tmp)
|
|
158
218
|
end
|
|
159
|
-
public_dir = "#{dist}
|
|
219
|
+
public_dir = "#{dist}/#{DIST_PUBLIC}"
|
|
160
220
|
File.rename(public_dir, public_old) if File.exist?(public_dir) || File.symlink?(public_dir)
|
|
161
221
|
File.rename(public_tmp, public_dir)
|
|
162
222
|
rm_tree(public_old)
|
|
163
223
|
|
|
164
|
-
Templates.mkdir_p("#{dist}
|
|
165
|
-
Templates.mkdir_p("#{dist}
|
|
166
|
-
["dist/#{name}", "dist/
|
|
224
|
+
Templates.mkdir_p("#{dist}/#{DIST_STORAGE}")
|
|
225
|
+
Templates.mkdir_p("#{dist}/#{DIST_TMP}")
|
|
226
|
+
["dist/#{name}", "dist/#{DIST_PUBLIC}/", "dist/#{DIST_STORAGE}/", "dist/#{DIST_TMP}/"]
|
|
167
227
|
end
|
|
168
228
|
|
|
169
229
|
# A symlink is removed itself, never followed: its target may be
|
|
@@ -183,10 +243,6 @@ module Cybertrain
|
|
|
183
243
|
end
|
|
184
244
|
nil
|
|
185
245
|
end
|
|
186
|
-
|
|
187
|
-
def self.shell_quote(text)
|
|
188
|
-
"'#{text.gsub("'", "'\\\\''")}'"
|
|
189
|
-
end
|
|
190
246
|
end
|
|
191
247
|
end
|
|
192
248
|
end
|
data/cybertrain/cli/new_app.rb
CHANGED
|
@@ -3,6 +3,7 @@ require "cybertrain/generator/inflector"
|
|
|
3
3
|
require "cybertrain/cli/templates"
|
|
4
4
|
require "cybertrain/cli/build"
|
|
5
5
|
require "cybertrain/cli/toolchain"
|
|
6
|
+
require "cybertrain/shell"
|
|
6
7
|
|
|
7
8
|
module Cybertrain
|
|
8
9
|
module CLI
|
|
@@ -14,6 +15,11 @@ module Cybertrain
|
|
|
14
15
|
# relative to dir.
|
|
15
16
|
def self.create(dir, framework_dep)
|
|
16
17
|
package = File.basename(dir)
|
|
18
|
+
# Before anything is written: `public`, `storage` and `tmp` are valid
|
|
19
|
+
# identifiers but collide with dist/ entries (Build.dist_name_problem).
|
|
20
|
+
problem = Build.dist_name_problem(package)
|
|
21
|
+
raise InvalidArgument, "'#{package}' is not a valid app name: it #{problem}" unless problem.empty?
|
|
22
|
+
|
|
17
23
|
title = Inflector.camelize(package)
|
|
18
24
|
files = [
|
|
19
25
|
["spin.toml", Templates.spin_toml(package, framework_dep)],
|
|
@@ -30,9 +36,15 @@ module Cybertrain
|
|
|
30
36
|
["public/404.html", Templates.error_page("404", "Not Found")],
|
|
31
37
|
["public/500.html", Templates.error_page("500", "Internal Server Error")],
|
|
32
38
|
["public/style.css", Templates.style_css],
|
|
39
|
+
["public/up.html", Templates.up_html],
|
|
33
40
|
["bin/#{package}.rb", Templates.bin_app(package)],
|
|
34
41
|
["bin/gen.rb", Templates.bin_gen],
|
|
35
42
|
["bin/db.rb", Templates.bin_db],
|
|
43
|
+
["Dockerfile", Templates.dockerfile(package)],
|
|
44
|
+
[".dockerignore", Templates.dockerignore],
|
|
45
|
+
["config/deploy.yml", Templates.deploy_yml(package)],
|
|
46
|
+
[".kamal/secrets", Templates.kamal_secrets],
|
|
47
|
+
[".github/workflows/ci.yml", Templates.ci_workflow],
|
|
36
48
|
["gen/.keep", ""],
|
|
37
49
|
["storage/.keep", ""],
|
|
38
50
|
["tmp/.keep", ""],
|
|
@@ -67,13 +79,13 @@ module Cybertrain
|
|
|
67
79
|
# Run by bootstrap once Toolchain.ensure! has put spin on this
|
|
68
80
|
# process's PATH.
|
|
69
81
|
def self.bootstrap_command(dir)
|
|
70
|
-
"cd #{
|
|
82
|
+
"cd #{Shell.quote(dir)} && spin lock && spin run gen"
|
|
71
83
|
end
|
|
72
84
|
|
|
73
85
|
# The same steps for the user to run by hand: through `cybertrain
|
|
74
86
|
# spin`, since the Spinel cybertrain installed is not on their PATH.
|
|
75
87
|
def self.recovery_command(dir)
|
|
76
|
-
"cd #{
|
|
88
|
+
"cd #{Shell.quote(dir)} && cybertrain spin lock && cybertrain spin run gen"
|
|
77
89
|
end
|
|
78
90
|
end
|
|
79
91
|
end
|
data/cybertrain/cli/scaffold.rb
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
# `cybertrain generate scaffold post title:string body:text`: writes the
|
|
2
2
|
# migration, model, controller and views of a resource and adds
|
|
3
3
|
# `resources :posts` to config/routes.rb, the way Rails' scaffold does.
|
|
4
|
+
#
|
|
5
|
+
# A `cover:attachment` field is a stored file rather than a column: the
|
|
6
|
+
# scaffold writes its five metadata columns and its form and show markup,
|
|
7
|
+
# then hands the model, controller, route and test edits to
|
|
8
|
+
# CLI::Attachment (cli/attachment.rb, loaded by cybertrain/cli.rb).
|
|
4
9
|
require "cybertrain/generator/inflector"
|
|
10
|
+
require "cybertrain/ident"
|
|
5
11
|
require "cybertrain/cli/templates"
|
|
6
12
|
|
|
7
13
|
module Cybertrain
|
|
@@ -12,20 +18,15 @@ module Cybertrain
|
|
|
12
18
|
# One "name:type" argument. A references field ("post:references")
|
|
13
19
|
# becomes the post_id column.
|
|
14
20
|
class Field
|
|
15
|
-
TYPES = %w[string text integer float boolean date datetime references]
|
|
21
|
+
TYPES = %w[string text integer float boolean date datetime references attachment]
|
|
16
22
|
|
|
17
23
|
# Columns every table already has (the primary key and t.timestamps).
|
|
24
|
+
# Everything else a column may not be called -- Ruby keywords, names
|
|
25
|
+
# that collide with the generated model -- comes from Cybertrain::Ident,
|
|
26
|
+
# the same rules the generator applies, so the scaffold refuses the
|
|
27
|
+
# name instead of writing files that `spin run gen` then rejects.
|
|
18
28
|
RESERVED_COLUMNS = %w[id created_at updated_at]
|
|
19
29
|
|
|
20
|
-
# Ruby keywords: a column (or resource) named after one would generate
|
|
21
|
-
# `def class` / `|end|` and break the generated code.
|
|
22
|
-
RUBY_KEYWORDS = %w[
|
|
23
|
-
__ENCODING__ __LINE__ __FILE__ BEGIN END alias and begin break case
|
|
24
|
-
class def defined do else elsif end ensure false for if in module
|
|
25
|
-
next nil not or redo rescue retry return self super then true undef
|
|
26
|
-
unless until when while yield
|
|
27
|
-
]
|
|
28
|
-
|
|
29
30
|
attr_reader :field_name, :field_type
|
|
30
31
|
|
|
31
32
|
def self.parse(arg)
|
|
@@ -38,11 +39,23 @@ module Cybertrain
|
|
|
38
39
|
type = parts.size > 1 ? parts[1].to_s : "string"
|
|
39
40
|
type = "references" if type == "belongs_to"
|
|
40
41
|
raise InvalidArgument, "bad field name '#{name}'" unless Templates.identifier?(name)
|
|
41
|
-
raise InvalidArgument, "'#{name}' is a Ruby keyword and cannot name a field" if RUBY_KEYWORDS.include?(name)
|
|
42
42
|
raise InvalidArgument, "unknown type '#{type}' for #{name} (use #{TYPES.join(", ")})" unless TYPES.include?(type)
|
|
43
43
|
|
|
44
44
|
field = Field.new(name, type)
|
|
45
45
|
raise InvalidArgument, "'#{field.column_name}' is a column every table already has" if RESERVED_COLUMNS.include?(field.column_name)
|
|
46
|
+
# Ident.unusable_reason is the one definition of a name the scaffold
|
|
47
|
+
# must not invent; Scaffold.refuse_unusable! turns its answer into the
|
|
48
|
+
# message. A name that only shadows an Object method is accepted, as the
|
|
49
|
+
# generator accepts it, and Scaffold.generate prints a note.
|
|
50
|
+
Scaffold.refuse_unusable!(field.column_name, "field")
|
|
51
|
+
# An attachment is not a column; the five columns it expands to
|
|
52
|
+
# (cover_key ...) answer to the same rules, and so does the name
|
|
53
|
+
# the model methods are built from. The download action `def <name>`
|
|
54
|
+
# is checked by Scaffold.generate, which knows the resource.
|
|
55
|
+
field.expanded_columns.each { |col| Scaffold.refuse_unusable!(col, "field") } if field.attachment?
|
|
56
|
+
# A references field also defines the reader `def <name>` (post:references
|
|
57
|
+
# -> def post), which answers to the same rules as a column.
|
|
58
|
+
Scaffold.refuse_unusable!(name, "reference") if field.reference?
|
|
46
59
|
|
|
47
60
|
field
|
|
48
61
|
end
|
|
@@ -56,6 +69,16 @@ module Cybertrain
|
|
|
56
69
|
@field_type == "references"
|
|
57
70
|
end
|
|
58
71
|
|
|
72
|
+
# A stored file (cover:attachment): five columns, not one.
|
|
73
|
+
def attachment?
|
|
74
|
+
@field_type == "attachment"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The column names this field adds to its table.
|
|
78
|
+
def expanded_columns
|
|
79
|
+
attachment? ? Templates.attachment_column_names(@field_name) : [column_name]
|
|
80
|
+
end
|
|
81
|
+
|
|
59
82
|
def column_name
|
|
60
83
|
reference? ? "#{@field_name}_id" : @field_name
|
|
61
84
|
end
|
|
@@ -64,8 +87,11 @@ module Cybertrain
|
|
|
64
87
|
Templates.humanize(@field_name)
|
|
65
88
|
end
|
|
66
89
|
|
|
67
|
-
|
|
68
|
-
|
|
90
|
+
# The create_table lines of this field's columns.
|
|
91
|
+
def migration_lines
|
|
92
|
+
return Templates.attachment_table_lines(@field_name) if attachment?
|
|
93
|
+
|
|
94
|
+
[reference? ? "t.references :#{@field_name}" : "t.#{@field_type} \"#{@field_name}\""]
|
|
69
95
|
end
|
|
70
96
|
|
|
71
97
|
# The FormBuilder method that edits this column.
|
|
@@ -74,6 +100,7 @@ module Cybertrain
|
|
|
74
100
|
when "text" then "text_area"
|
|
75
101
|
when "integer", "float" then "number_field"
|
|
76
102
|
when "boolean" then "check_box"
|
|
103
|
+
when "attachment" then "file_field"
|
|
77
104
|
else "text_field"
|
|
78
105
|
end
|
|
79
106
|
end
|
|
@@ -82,14 +109,17 @@ module Cybertrain
|
|
|
82
109
|
# The names a scaffold derives from its NAME argument ("post", "Post"
|
|
83
110
|
# or "posts" all give post / posts / Post / Posts).
|
|
84
111
|
class Resource
|
|
85
|
-
attr_reader :singular, :plural, :class_name, :plural_class, :fields
|
|
112
|
+
attr_reader :singular, :plural, :class_name, :plural_class, :fields, :any
|
|
86
113
|
|
|
87
|
-
|
|
114
|
+
# `any` is the --any option: attachments accept any detected file type,
|
|
115
|
+
# not only the four raster image types.
|
|
116
|
+
def initialize(name, fields, any = false)
|
|
88
117
|
@singular = Inflector.singularize(Inflector.underscore(name))
|
|
89
118
|
@plural = Inflector.pluralize(@singular)
|
|
90
119
|
@class_name = Inflector.camelize(@singular)
|
|
91
120
|
@plural_class = Inflector.camelize(@plural)
|
|
92
121
|
@fields = fields
|
|
122
|
+
@any = any
|
|
93
123
|
end
|
|
94
124
|
|
|
95
125
|
# The index route helper's name without _path, as Rails' scaffold
|
|
@@ -99,6 +129,13 @@ module Cybertrain
|
|
|
99
129
|
@plural == @singular ? "#{@plural}_index" : @plural
|
|
100
130
|
end
|
|
101
131
|
|
|
132
|
+
# Whether any field is a stored file (the form then needs multipart).
|
|
133
|
+
def attachments?
|
|
134
|
+
found = false
|
|
135
|
+
@fields.each { |f| found = true if f.attachment? }
|
|
136
|
+
found
|
|
137
|
+
end
|
|
138
|
+
|
|
102
139
|
# The column the model validates for presence ("" when there is none).
|
|
103
140
|
def first_string_field
|
|
104
141
|
@fields.each { |f| return f.field_name if f.field_type == "string" }
|
|
@@ -109,29 +146,84 @@ module Cybertrain
|
|
|
109
146
|
module Scaffold
|
|
110
147
|
DRAW_LINE = "Cybertrain::Routes.draw do"
|
|
111
148
|
|
|
149
|
+
# Policy: the scaffold refuses what the generator would RENAME (Ruby
|
|
150
|
+
# keywords, Ident::RESERVED_COLUMN_NAMES): its views and controller call
|
|
151
|
+
# the reader by the field's name, so a renamed reader would break them.
|
|
152
|
+
# It accepts what the generator only annotates (a name that shadows an
|
|
153
|
+
# Object method, `method`, `display`, `send`) and says so on stdout
|
|
154
|
+
# (shadow_notes), as the generator does in the model file.
|
|
155
|
+
#
|
|
156
|
+
# Raises InvalidArgument when `name` is a name the scaffold must not
|
|
157
|
+
# invent, with the message for the reason Ident.unusable_reason gives;
|
|
158
|
+
# `what` says what the name would name ("field", "reference",
|
|
159
|
+
# "resource"). The one place that maps a reason to a message, shared by the
|
|
160
|
+
# field column, the references reader and the resource singular: a
|
|
161
|
+
# reason Ident adds must be added here, and the `else` raises for one
|
|
162
|
+
# that is not (a `case` without it would return nil and let the name
|
|
163
|
+
# through silently). Returns nil, explicitly, for a usable name (NOTES
|
|
164
|
+
# rules 10/34: one return type).
|
|
165
|
+
def self.refuse_unusable!(name, what)
|
|
166
|
+
case Ident.unusable_reason(name)
|
|
167
|
+
when ""
|
|
168
|
+
nil
|
|
169
|
+
when "keyword"
|
|
170
|
+
raise InvalidArgument, "'#{name}' is a Ruby keyword and cannot name a #{what}"
|
|
171
|
+
when "reserved"
|
|
172
|
+
raise InvalidArgument, "'#{name}' would shadow a method of the generated model (Cybertrain::Model); pick another #{what}"
|
|
173
|
+
else
|
|
174
|
+
raise RuntimeError, "Ident.unusable_reason('#{name}') answered a reason Scaffold.refuse_unusable! has no message for"
|
|
175
|
+
end
|
|
176
|
+
nil
|
|
177
|
+
end
|
|
178
|
+
|
|
112
179
|
# Returns the paths it created (files that already exist are reported
|
|
113
180
|
# as "identical" or "exist" and left alone). Raises InvalidArgument on a
|
|
114
181
|
# bad name, a bad or duplicate field, or a routes file without a
|
|
115
182
|
# `Cybertrain::Routes.draw do` line, before anything is written.
|
|
116
|
-
def self.generate(root, name, fields)
|
|
183
|
+
def self.generate(root, name, fields, any = false)
|
|
117
184
|
routes_path = File.join(root, "config/routes.rb")
|
|
118
185
|
raise InvalidArgument, "#{routes_path} not found: run this inside a cybertrain app" unless File.exist?(routes_path)
|
|
119
186
|
raise InvalidArgument, "#{routes_path} has no `#{DRAW_LINE}` line" if File.read(routes_path).index(DRAW_LINE).nil?
|
|
120
187
|
|
|
121
188
|
underscored = Inflector.underscore(name)
|
|
122
189
|
raise InvalidArgument, "bad resource name '#{name}'" unless Templates.identifier?(underscored)
|
|
123
|
-
|
|
190
|
+
singular = Inflector.singularize(underscored)
|
|
191
|
+
# Policy (refuse_unusable!): the scaffold refuses a singular the
|
|
192
|
+
# generator would RENAME. A reserved one (update, save) would give
|
|
193
|
+
# every referencing model a `<stem>_as_<col>` reader, and the singular
|
|
194
|
+
# is also the model class name. A shadowing one (display, tap) is
|
|
195
|
+
# accepted with a note (shadow_notes). The generator itself still
|
|
196
|
+
# generates a schema with a reserved table (a hand-written table named
|
|
197
|
+
# `updates` works), so this is the scaffold's choice, not the
|
|
198
|
+
# generator's refusal. The plural (the has_many side) is left to the
|
|
199
|
+
# generator, which renames such a reader (`errors_as_post`) -- and
|
|
200
|
+
# nothing may reference the table.
|
|
201
|
+
Scaffold.refuse_unusable!(singular, "resource")
|
|
124
202
|
|
|
125
203
|
parsed = Array.new(0) { Field.new("", "") }
|
|
126
204
|
columns = Array.new(0) { "" }
|
|
127
205
|
fields.each do |arg|
|
|
128
206
|
field = Field.parse(arg)
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
207
|
+
if field.attachment? && Templates.attachment_action_clash?(field.field_name, singular)
|
|
208
|
+
raise InvalidArgument, "'#{field.field_name}' would redefine a method of #{Inflector.camelize(Inflector.pluralize(singular))}Controller (the download action is `def #{field.field_name}`); pick another field name"
|
|
209
|
+
end
|
|
210
|
+
field.expanded_columns.each do |col|
|
|
211
|
+
raise InvalidArgument, "duplicate column '#{col}'" if columns.include?(col)
|
|
212
|
+
|
|
213
|
+
columns << col
|
|
214
|
+
end
|
|
132
215
|
parsed << field
|
|
133
216
|
end
|
|
134
|
-
res = Resource.new(name, parsed)
|
|
217
|
+
res = Resource.new(name, parsed, any)
|
|
218
|
+
|
|
219
|
+
res.fields.each do |f|
|
|
220
|
+
next unless f.attachment?
|
|
221
|
+
|
|
222
|
+
spec = AttachmentSpec.new(res.singular, f.field_name, any)
|
|
223
|
+
controller_path = File.join(root, Attachment.controller_file(spec))
|
|
224
|
+
source = File.file?(controller_path) ? File.read(controller_path) : Templates.controller(res)
|
|
225
|
+
Attachment.check_download_policy!(root, spec, source)
|
|
226
|
+
end
|
|
135
227
|
|
|
136
228
|
created = Array.new(0) { "" }
|
|
137
229
|
migration = migration_path(root, res)
|
|
@@ -145,9 +237,33 @@ module Cybertrain
|
|
|
145
237
|
record(created, root, "#{views}/edit.html.erb", Templates.edit_view(res))
|
|
146
238
|
record(created, root, "#{views}/_form.html.erb", Templates.form_partial(res))
|
|
147
239
|
add_route(routes_path, res.plural)
|
|
240
|
+
attachments = false
|
|
241
|
+
res.fields.each do |f|
|
|
242
|
+
next unless f.attachment?
|
|
243
|
+
|
|
244
|
+
attachments = true
|
|
245
|
+
Attachment.install_code(root, AttachmentSpec.new(res.singular, f.field_name, any), created)
|
|
246
|
+
end
|
|
247
|
+
puts Attachment::BACKUP_NOTE if attachments
|
|
248
|
+
shadow_notes(res).each { |note| puts note }
|
|
148
249
|
created
|
|
149
250
|
end
|
|
150
251
|
|
|
252
|
+
# One line per name the scaffold accepted that shadows an Object method
|
|
253
|
+
# (Ident::SHADOWING_COLUMN_NAMES), the generator's own note told on
|
|
254
|
+
# stdout: a field (or references reader) on the model, the resource
|
|
255
|
+
# singular on every model that references it.
|
|
256
|
+
def self.shadow_notes(res)
|
|
257
|
+
notes = Array.new(0) { "" }
|
|
258
|
+
res.fields.each do |f|
|
|
259
|
+
notes << "note: '#{f.field_name}' shadows Object##{f.field_name} on the generated model; call it as #{res.singular}.#{f.field_name}" if Ident.shadowing_column?(f.field_name)
|
|
260
|
+
end
|
|
261
|
+
if Ident.shadowing_column?(res.singular)
|
|
262
|
+
notes << "note: '#{res.singular}' shadows Object##{res.singular} on a model that references #{res.plural} (belongs_to reader); call it as record.#{res.singular}"
|
|
263
|
+
end
|
|
264
|
+
notes
|
|
265
|
+
end
|
|
266
|
+
|
|
151
267
|
def self.record(created, root, path, content)
|
|
152
268
|
created << path if Templates.write(root, path, content) == "create"
|
|
153
269
|
nil
|