Class: Brut::CLI::Apps::Scaffold::Page

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Defined Under Namespace

Classes: Route

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/brut/cli/apps/scaffold.rb', line 284

def execute
  if args.length != 1
    raise "page requires exactly one argument, got #{args.length}"
  end
  route = Route.new(args[0])

  page_class_name    = RichString.from_string(route.handler_class.join("::"))
  page_relative_path = page_class_name.underscorized

  pages_src_dir    = Brut.container.pages_src_dir
  pages_specs_dir  = Brut.container.pages_specs_dir
  i18n_locales_dir = Brut.container.i18n_locales_dir

  page_source_path     = Pathname( (pages_src_dir   / page_relative_path).to_s + ".rb" )
  page_spec_path       = Pathname( (pages_specs_dir / page_relative_path).to_s + ".spec.rb" )
  app_path             = Pathname( Brut.container.app_src_dir / "app.rb" )
  app_translations     = Pathname(  i18n_locales_dir / "en" / "2_app.rb")

  exists = [
    page_source_path,
    page_spec_path,
  ].select(&:exist?)

  if exists.any? && !global_options.overwrite?
    exists.each do |path|
      err.puts "'#{path.relative_path_from(Brut.container.project_root)}' exists already"
    end
    err.puts "Re-run with --overwrite to overwrite these files"
    return 1
  end

  FileUtils.mkdir_p page_source_path.dirname,     noop: global_options.dry_run?
  FileUtils.mkdir_p page_spec_path.dirname,       noop: global_options.dry_run?

  route_code = "page \"#{route.path_template}\""

  initializer_params = route.path_params
  initializer_params_code = if initializer_params.empty?
                              ""
                            else
                              "(" + initializer_params.map { "#{it}:" }.join(", ") + ")"
                            end

  page_class_code = %{class #{page_class_name} < AppPage
  def initialize#{initializer_params_code} # add needed arguments here
  end

  def page_template
h1 { "#{page_class_name} is ready!" }
  end
end}
  page_spec_code = %{require "spec_helper"

RSpec.describe #{page_class_name} do
  it "should have tests" do
expect(true).to eq(false)
  end
end}

  title = RichString.new(page_class_name).underscorized.humanized.to_s.capitalize
  translations_code = "       \"#{page_class_name}\": {\n         title: \"#{title}\",\n       \},"

  if global_options.dry_run?
    out.puts app_path.relative_path_from(Brut.container.project_root)
    out.puts "will contain:\n\n#{route_code}\n\n"
    out.puts page_source_path.relative_path_from(Brut.container.project_root)
    out.puts "will contain:\n\n#{page_class_code}\n\n"
    out.puts page_spec_path.relative_path_from(Brut.container.project_root)
    out.puts "will contain:\n\n#{page_spec_code}\n\n"
    out.puts app_translations.relative_path_from(Brut.container.project_root)
    out.puts "will contain:\n\n#{translations_code}\n\n"
  else

    File.open(page_source_path,"w")     { it.puts page_class_code }
    File.open(page_spec_path,"w")       { it.puts page_spec_code }

    existing_translations = File.read(app_translations).split(/\n/)
    inserted_translation = false
    File.open(app_translations,"w") do |file|
      existing_translations.each do |line|
        if line =~ /^    pages:\s*{/
          file.puts line
          file.puts translations_code
          inserted_translation = true
        else
          file.puts line
        end
      end
    end
    if !inserted_translation
      err.puts "WARNING: Could not find a place to insert the translation for this page's title"
      err.puts "         The page may not render properly the first time you load it"
    end

    routes_editor = RoutesEditor.new(app_path:,out:)
    routes_editor.add_route!(route_code:)

    if !routes_editor.found_routes?
      out.puts "Could not find routes declaration in #{app_path.relative_path_from(Brut.container.project_root)}"
      out.puts "Please add this to wherever you have defined your routes:\n\n#{route_code}\n\n"
    elsif routes_editor.routes_existed?
      out.puts "Routes declaration in #{app_path.relative_path_from(Brut.container.project_root)} contained the route defition already"
      out.puts "Please make sure everything is correct.  Here is the defintion that was not inserted:\n\n#{route_code}"
    end
  end
  out.puts "Page source is in #{page_source_path.relative_path_from(Brut.container.project_root)}"
  out.puts "Page test is in   #{page_spec_path.relative_path_from(Brut.container.project_root)}"
  out.puts "Added title to    #{app_translations.relative_path_from(Brut.container.project_root)}"
  out.puts "Added route to    #{app_path.relative_path_from(Brut.container.project_root)}"
  0
end