Class: Brut::CLI::Apps::Scaffold::Component
Instance Attribute Summary
#parent_command
Instance Method Summary
collapse
#accepts, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #puts, #stderr, #stdin, #stdout, #system!
Instance Method Details
#args_description ⇒ Object
190
|
# File 'lib/brut/cli/apps/scaffold.rb', line 190
def args_description = "ComponentName"
|
#description ⇒ Object
189
|
# File 'lib/brut/cli/apps/scaffold.rb', line 189
def description = "Create a new component and associated test"
|
#detailed_description ⇒ Object
191
|
# File 'lib/brut/cli/apps/scaffold.rb', line 191
def detailed_description = "New components go in the `components/` folder of your app, however using --page will create a 'page private' component. To do that, the component name must be an inner class of an existing page, for example HomePage::Welcome. This component goes in a sub-folder inside the `pages/` area of your app"
|
#opts ⇒ Object
193
194
195
|
# File 'lib/brut/cli/apps/scaffold.rb', line 193
def opts = [
[ "--page","If set, this component is for a specific page and won't go with the other components"],
]
|
#run ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/brut/cli/apps/scaffold.rb', line 197
def run
if argv.length != 1
raise "component requires exactly one argument, got #{argv.length}"
end
class_name = RichString.new(argv[0]).capitalize(:first_only)
if class_name.to_s !~ /Component$/
class_name = RichString.new(class_name.to_s + "Component")
end
relative_path = class_name.underscorized
components_src_dir = Brut.container.components_src_dir
components_specs_dir = Brut.container.components_specs_dir
if options.page?
components_src_dir = Brut.container.pages_src_dir
components_specs_dir = Brut.container.pages_specs_dir
if class_name.to_s !~ /::/
raise "component #{class_name} cannot be a page component - it must be an inner class of an existing page"
else
existing_page = RichString.new(class_name.to_s.split(/::/)[0..-2].join("::")).underscorized.to_s + ".rb"
if !(components_src_dir / existing_page).exist?
raise "#{class_name} was set as a page component, however we cannot find the page it belongs in. File #{existing_page} does not exist and should contain that page"
end
end
end
source_path = Pathname( (components_src_dir / relative_path).to_s + ".rb" )
spec_path = Pathname( (components_specs_dir / relative_path).to_s + ".spec.rb" )
exists = [
source_path,
spec_path,
].select(&:exist?)
if exists.any? && !global_options.overwrite?
exists.each do |path|
stderr.puts "'#{path.relative_path_from(Brut.container.project_root)}' exists already"
end
stderr.puts "Re-run with --overwrite to overwrite these files"
return 1
end
if global_options.dry_run?
stdout.puts "FileUtils.mkdir_p #{source_path.dirname}"
stdout.puts "FileUtils.mkdir_p #{spec_path.dirname}"
else
FileUtils.mkdir_p source_path.dirname
FileUtils.mkdir_p spec_path.dirname
File.open(source_path,"w") do |file|
file.puts %{class #{class_name} < AppComponent
def initialize
end
def view_template
h2 { "Welcome to your new template" }
end
end}
end
File.open(spec_path,"w") do |file|
file.puts %{require "spec_helper"
RSpec.describe #{class_name} do
it "should have tests" do
expect(true).to eq(false)
end
end}
end
end
stdout.puts "Component source is in #{source_path.relative_path_from(Brut.container.project_root)}"
stdout.puts "Component test is in #{spec_path.relative_path_from(Brut.container.project_root)}"
0
end
|