Generate from template.
(module_instance, config: GenerationConfig)
| 476 | |
| 477 | |
| 478 | def generate_template(module_instance, config: GenerationConfig) -> None: # noqa: PLR0912, PLR0915 |
| 479 | """Generate from template.""" |
| 480 | logger.info(f"Starting generation for template '{config.id}' from module '{module_instance.name}'") |
| 481 | |
| 482 | display = DisplayManager(quiet=config.quiet) if config.quiet else module_instance.display |
| 483 | template = _prepare_template(module_instance, config.id, config.var_file, config.var, display) |
| 484 | slug = getattr(template, "slug", template.id) |
| 485 | used_implicit_dry_run_destination = False |
| 486 | |
| 487 | try: |
| 488 | destination = resolve_cli_destination(config.output, config.remote, config.remote_path, slug) |
| 489 | except ValueError as e: |
| 490 | display.error(str(e), context="template generation") |
| 491 | raise Exit(code=1) from None |
| 492 | |
| 493 | if not config.quiet: |
| 494 | # Display template header |
| 495 | module_instance.display.templates.render_template_header(template, config.id) |
| 496 | # Display file tree |
| 497 | module_instance.display.templates.render_file_tree(template) |
| 498 | # Display variables table |
| 499 | module_instance.display.variables.render_variables_table(template) |
| 500 | module_instance.display.text("") |
| 501 | |
| 502 | try: |
| 503 | rendered_files, _variable_values = _render_template(template, config.id, display, config.interactive) |
| 504 | rendered_files = apply_output_name(rendered_files, config.name) |
| 505 | |
| 506 | if destination is None: |
| 507 | if config.dry_run: |
| 508 | destination = GenerationDestination(mode="local", local_output_dir=Path.cwd() / slug) |
| 509 | used_implicit_dry_run_destination = True |
| 510 | elif config.interactive: |
| 511 | destination = prompt_generation_destination(slug) |
| 512 | else: |
| 513 | destination = GenerationDestination(mode="local", local_output_dir=Path.cwd() / slug) |
| 514 | |
| 515 | if not destination.is_remote: |
| 516 | output_dir = destination.local_output_dir or (Path.cwd() / slug) |
| 517 | if ( |
| 518 | not config.dry_run |
| 519 | and not config.quiet |
| 520 | and check_output_directory(output_dir, rendered_files, config.interactive, display) is None |
| 521 | ): |
| 522 | return |
| 523 | |
| 524 | # Execute generation (dry run or actual) |
| 525 | dry_run_stats = None |
| 526 | if destination.is_remote: |
| 527 | remote_host = destination.remote_host or "" |
| 528 | remote_path = destination.remote_path or f"~/{slug}" |
| 529 | if config.dry_run: |
| 530 | if not config.quiet: |
| 531 | dry_run_stats = execute_remote_dry_run( |
| 532 | remote_host, |
| 533 | remote_path, |
| 534 | rendered_files, |
| 535 | config.show_files, |