Class: Brut::CLI::Apps::Scaffold::Page
- Inherits:
-
BaseCommand
- Object
- Commands::BaseCommand
- BaseCommand
- Brut::CLI::Apps::Scaffold::Page
- Defined in:
- lib/brut/cli/apps/scaffold.rb
Defined Under Namespace
Classes: Route
Instance Attribute Summary
Attributes inherited from Commands::BaseCommand
Instance Method Summary collapse
Methods inherited from BaseCommand
#bootstrap?, #default_rack_env, #opts
Methods inherited from Commands::BaseCommand
#accepts, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #detailed_description, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme
Instance Method Details
#args_description ⇒ Object
296 |
# File 'lib/brut/cli/apps/scaffold.rb', line 296 def args_description = "page_route" |
#description ⇒ Object
295 |
# File 'lib/brut/cli/apps/scaffold.rb', line 295 def description = "Create a new page and associated test" |
#run ⇒ Object
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 401 402 403 404 405 406 407 |
# File 'lib/brut/cli/apps/scaffold.rb', line 297 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? && !.overwrite? exists.each do |path| error "'#{path.relative_path_from(Brut.container.project_root)}' exists already" end error "Re-run with --overwrite to overwrite these files" return 1 end FileUtils.mkdir_p page_source_path.dirname, noop: .dry_run? FileUtils.mkdir_p page_spec_path.dirname, noop: .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 .dry_run? puts app_path.relative_path_from(Brut.container.project_root) puts "will contain:\n\n#{route_code}\n\n" puts page_source_path.relative_path_from(Brut.container.project_root) puts "will contain:\n\n#{page_class_code}\n\n" puts page_spec_path.relative_path_from(Brut.container.project_root) puts "will contain:\n\n#{page_spec_code}\n\n" puts app_translations.relative_path_from(Brut.container.project_root) 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 error "WARNING: Could not find a place to insert the translation for this page's title" error " The page may not render properly the first time you load it" end routes_editor = RoutesEditor.new(app_path:,stdout: execution_context.stdout) routes_editor.add_route!(route_code:) if !routes_editor.found_routes? puts "Could not find routes declaration in #{app_path.relative_path_from(Brut.container.project_root)}" puts "Please add this to wherever you have defined your routes:\n\n#{route_code}\n\n" elsif routes_editor.routes_existed? puts "Routes declaration in #{app_path.relative_path_from(Brut.container.project_root)} contained the route defition already" puts "Please make sure everything is correct. Here is the defintion that was not inserted:\n\n#{route_code}" end end puts "Page source is in #{page_source_path.relative_path_from(Brut.container.project_root)}" puts "Page test is in #{page_spec_path.relative_path_from(Brut.container.project_root)}" puts "Added title to #{app_translations.relative_path_from(Brut.container.project_root)}" puts "Added route to #{app_path.relative_path_from(Brut.container.project_root)}" 0 end |