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

Inherits:
Commands::BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Defined Under Namespace

Classes: Route

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#args_descriptionObject



289
# File 'lib/brut/cli/apps/scaffold.rb', line 289

def args_description = "page_route"

#descriptionObject



288
# File 'lib/brut/cli/apps/scaffold.rb', line 288

def description = "Create a new page and associated test"

#runObject



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
395
396
397
398
399
400
# File 'lib/brut/cli/apps/scaffold.rb', line 290

def run
  if argv.length != 1
    raise "page requires exactly one argument, got #{argv.length}"
  end
  route = Route.new(argv[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|
      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

  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?
    stdout.puts app_path.relative_path_from(Brut.container.project_root)
    stdout.puts "will contain:\n\n#{route_code}\n\n"
    stdout.puts page_source_path.relative_path_from(Brut.container.project_root)
    stdout.puts "will contain:\n\n#{page_class_code}\n\n"
    stdout.puts page_spec_path.relative_path_from(Brut.container.project_root)
    stdout.puts "will contain:\n\n#{page_spec_code}\n\n"
    stdout.puts app_translations.relative_path_from(Brut.container.project_root)
    stdout.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
      stderr.puts "WARNING: Could not find a place to insert the translation for this page's title"
      stderr.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?
      stdout.puts "Could not find routes declaration in #{app_path.relative_path_from(Brut.container.project_root)}"
      stdout.puts "Please add this to wherever you have defined your routes:\n\n#{route_code}\n\n"
    elsif routes_editor.routes_existed?
      stdout.puts "Routes declaration in #{app_path.relative_path_from(Brut.container.project_root)} contained the route defition already"
      stdout.puts "Please make sure everything is correct.  Here is the defintion that was not inserted:\n\n#{route_code}"
    end
  end
  stdout.puts "Page source is in #{page_source_path.relative_path_from(Brut.container.project_root)}"
  stdout.puts "Page test is in   #{page_spec_path.relative_path_from(Brut.container.project_root)}"
  stdout.puts "Added title to    #{app_translations.relative_path_from(Brut.container.project_root)}"
  stdout.puts "Added route to    #{app_path.relative_path_from(Brut.container.project_root)}"
  0
end