cybertrain 0.2.1 → 0.3.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.
@@ -2,6 +2,7 @@
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
4
  require "cybertrain/app_name"
5
+ require "cybertrain/shell"
5
6
 
6
7
  module Cybertrain
7
8
  module CLI
@@ -106,7 +107,7 @@ module Cybertrain
106
107
  def self.quote_arg(text)
107
108
  return text if !text.empty? && text.match(/\A[A-Za-z0-9_:.\/-]+\z/)
108
109
 
109
- shell_quote(text)
110
+ Shell.quote(text)
110
111
  end
111
112
 
112
113
  # port is "" (the app's default, 3000) or a port? string.
@@ -179,7 +180,7 @@ module Cybertrain
179
180
 
180
181
  def self.run_in(root, command)
181
182
  puts "run #{command}"
182
- system("cd #{shell_quote(root)} && #{command}")
183
+ system("cd #{Shell.quote(root)} && #{command}")
183
184
  end
184
185
 
185
186
  # Copies build/bin/<name> and public/ into dist/; storage/ and tmp/ are
@@ -204,13 +205,13 @@ module Cybertrain
204
205
  rm_tree(public_tmp)
205
206
  rm_tree(public_old)
206
207
 
207
- copy_command = "cp #{shell_quote(binary)} #{shell_quote(binary_tmp)}"
208
+ copy_command = "cp #{Shell.quote(binary)} #{Shell.quote(binary_tmp)}"
208
209
  raise InvalidArgument, "could not copy #{binary} to dist/" unless system(copy_command)
209
210
 
210
211
  File.rename(binary_tmp, "#{dist}/#{name}")
211
212
 
212
213
  if File.directory?("#{root}/#{DIST_PUBLIC}")
213
- public_command = "cp -R #{shell_quote("#{root}/#{DIST_PUBLIC}")} #{shell_quote(public_tmp)}"
214
+ public_command = "cp -R #{Shell.quote("#{root}/#{DIST_PUBLIC}")} #{Shell.quote(public_tmp)}"
214
215
  raise InvalidArgument, "could not copy #{DIST_PUBLIC}/ to dist/" unless system(public_command)
215
216
  else
216
217
  Dir.mkdir(public_tmp)
@@ -242,10 +243,6 @@ module Cybertrain
242
243
  end
243
244
  nil
244
245
  end
245
-
246
- def self.shell_quote(text)
247
- "'#{text.gsub("'", "'\\\\''")}'"
248
- end
249
246
  end
250
247
  end
251
248
  end
@@ -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
@@ -35,9 +36,15 @@ module Cybertrain
35
36
  ["public/404.html", Templates.error_page("404", "Not Found")],
36
37
  ["public/500.html", Templates.error_page("500", "Internal Server Error")],
37
38
  ["public/style.css", Templates.style_css],
39
+ ["public/up.html", Templates.up_html],
38
40
  ["bin/#{package}.rb", Templates.bin_app(package)],
39
41
  ["bin/gen.rb", Templates.bin_gen],
40
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],
41
48
  ["gen/.keep", ""],
42
49
  ["storage/.keep", ""],
43
50
  ["tmp/.keep", ""],
@@ -72,13 +79,13 @@ module Cybertrain
72
79
  # Run by bootstrap once Toolchain.ensure! has put spin on this
73
80
  # process's PATH.
74
81
  def self.bootstrap_command(dir)
75
- "cd #{Build.shell_quote(dir)} && spin lock && spin run gen"
82
+ "cd #{Shell.quote(dir)} && spin lock && spin run gen"
76
83
  end
77
84
 
78
85
  # The same steps for the user to run by hand: through `cybertrain
79
86
  # spin`, since the Spinel cybertrain installed is not on their PATH.
80
87
  def self.recovery_command(dir)
81
- "cd #{Build.shell_quote(dir)} && cybertrain spin lock && cybertrain spin run gen"
88
+ "cd #{Shell.quote(dir)} && cybertrain spin lock && cybertrain spin run gen"
82
89
  end
83
90
  end
84
91
  end
@@ -1,6 +1,11 @@
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"
5
10
  require "cybertrain/ident"
6
11
  require "cybertrain/cli/templates"
@@ -13,7 +18,7 @@ module Cybertrain
13
18
  # One "name:type" argument. A references field ("post:references")
14
19
  # becomes the post_id column.
15
20
  class Field
16
- TYPES = %w[string text integer float boolean date datetime references]
21
+ TYPES = %w[string text integer float boolean date datetime references attachment]
17
22
 
18
23
  # Columns every table already has (the primary key and t.timestamps).
19
24
  # Everything else a column may not be called -- Ruby keywords, names
@@ -43,6 +48,11 @@ module Cybertrain
43
48
  # message. A name that only shadows an Object method is accepted, as the
44
49
  # generator accepts it, and Scaffold.generate prints a note.
45
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?
46
56
  # A references field also defines the reader `def <name>` (post:references
47
57
  # -> def post), which answers to the same rules as a column.
48
58
  Scaffold.refuse_unusable!(name, "reference") if field.reference?
@@ -59,6 +69,16 @@ module Cybertrain
59
69
  @field_type == "references"
60
70
  end
61
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
+
62
82
  def column_name
63
83
  reference? ? "#{@field_name}_id" : @field_name
64
84
  end
@@ -67,8 +87,11 @@ module Cybertrain
67
87
  Templates.humanize(@field_name)
68
88
  end
69
89
 
70
- def migration_line
71
- reference? ? "t.references :#{@field_name}" : "t.#{@field_type} \"#{@field_name}\""
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}\""]
72
95
  end
73
96
 
74
97
  # The FormBuilder method that edits this column.
@@ -77,6 +100,7 @@ module Cybertrain
77
100
  when "text" then "text_area"
78
101
  when "integer", "float" then "number_field"
79
102
  when "boolean" then "check_box"
103
+ when "attachment" then "file_field"
80
104
  else "text_field"
81
105
  end
82
106
  end
@@ -85,14 +109,17 @@ module Cybertrain
85
109
  # The names a scaffold derives from its NAME argument ("post", "Post"
86
110
  # or "posts" all give post / posts / Post / Posts).
87
111
  class Resource
88
- attr_reader :singular, :plural, :class_name, :plural_class, :fields
112
+ attr_reader :singular, :plural, :class_name, :plural_class, :fields, :any
89
113
 
90
- def initialize(name, fields)
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)
91
117
  @singular = Inflector.singularize(Inflector.underscore(name))
92
118
  @plural = Inflector.pluralize(@singular)
93
119
  @class_name = Inflector.camelize(@singular)
94
120
  @plural_class = Inflector.camelize(@plural)
95
121
  @fields = fields
122
+ @any = any
96
123
  end
97
124
 
98
125
  # The index route helper's name without _path, as Rails' scaffold
@@ -102,6 +129,13 @@ module Cybertrain
102
129
  @plural == @singular ? "#{@plural}_index" : @plural
103
130
  end
104
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
+
105
139
  # The column the model validates for presence ("" when there is none).
106
140
  def first_string_field
107
141
  @fields.each { |f| return f.field_name if f.field_type == "string" }
@@ -146,7 +180,7 @@ module Cybertrain
146
180
  # as "identical" or "exist" and left alone). Raises InvalidArgument on a
147
181
  # bad name, a bad or duplicate field, or a routes file without a
148
182
  # `Cybertrain::Routes.draw do` line, before anything is written.
149
- def self.generate(root, name, fields)
183
+ def self.generate(root, name, fields, any = false)
150
184
  routes_path = File.join(root, "config/routes.rb")
151
185
  raise InvalidArgument, "#{routes_path} not found: run this inside a cybertrain app" unless File.exist?(routes_path)
152
186
  raise InvalidArgument, "#{routes_path} has no `#{DRAW_LINE}` line" if File.read(routes_path).index(DRAW_LINE).nil?
@@ -170,12 +204,26 @@ module Cybertrain
170
204
  columns = Array.new(0) { "" }
171
205
  fields.each do |arg|
172
206
  field = Field.parse(arg)
173
- raise InvalidArgument, "duplicate column '#{field.column_name}'" if columns.include?(field.column_name)
174
-
175
- columns << field.column_name
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
176
215
  parsed << field
177
216
  end
178
- 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
179
227
 
180
228
  created = Array.new(0) { "" }
181
229
  migration = migration_path(root, res)
@@ -189,6 +237,14 @@ module Cybertrain
189
237
  record(created, root, "#{views}/edit.html.erb", Templates.edit_view(res))
190
238
  record(created, root, "#{views}/_form.html.erb", Templates.form_partial(res))
191
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
192
248
  shadow_notes(res).each { |note| puts note }
193
249
  created
194
250
  end