|
| 1 | +# Copyright (c) 2025 |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import argparse |
| 6 | +import textwrap |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from copier import Worker |
| 10 | +from west.commands import WestCommand |
| 11 | + |
| 12 | + |
| 13 | +class Template(WestCommand): |
| 14 | + """Use built-in templates to create new samples, drivers, tests, etc. |
| 15 | +
|
| 16 | + Initial implementation supports creating a new sample using the Copier |
| 17 | + template engine. Additional template types can be added over time. |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__(self): |
| 21 | + super().__init__( |
| 22 | + "template", |
| 23 | + "use built-in templates to create new samples, drivers, tests, etc.", |
| 24 | + "Create Zephyr content from templates (initially: samples)", |
| 25 | + accepts_unknown_args=False, |
| 26 | + ) |
| 27 | + |
| 28 | + def do_add_parser(self, parser_adder): |
| 29 | + parser = parser_adder.add_parser( |
| 30 | + self.name, |
| 31 | + help=self.help, |
| 32 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 33 | + description=self.description, |
| 34 | + epilog=textwrap.dedent(""" |
| 35 | + Examples: |
| 36 | +
|
| 37 | + - Create a new sample and answer prompts interactively: |
| 38 | + west template --type sample |
| 39 | + """), |
| 40 | + ) |
| 41 | + |
| 42 | + parser.add_argument( |
| 43 | + "-t", |
| 44 | + "--type", |
| 45 | + choices=["sample"], |
| 46 | + default="sample", |
| 47 | + help="Type of template to instantiate (default: sample)", |
| 48 | + ) |
| 49 | + |
| 50 | + # parser.add_argument( |
| 51 | + # "-o", |
| 52 | + # "--output", |
| 53 | + # dest="output", |
| 54 | + # metavar="DIR", |
| 55 | + # type=Path, |
| 56 | + # help=( |
| 57 | + # "Destination root directory. For samples, this should typically be the " |
| 58 | + # "'samples' directory. Defaults to <workspace>/zephyr/samples." |
| 59 | + # ), |
| 60 | + # ) |
| 61 | + |
| 62 | + return parser |
| 63 | + |
| 64 | + def do_run(self, args, _): |
| 65 | + # Determine template kind (only 'sample' for now) |
| 66 | + template_kind = args.type |
| 67 | + |
| 68 | + # Resolve workspace and zephyr repository paths |
| 69 | + workspace_topdir = Path(self.topdir) |
| 70 | + zephyr_repo_dir = workspace_topdir / "zephyr" |
| 71 | + |
| 72 | + if not zephyr_repo_dir.is_dir(): |
| 73 | + self.die(f"Could not locate Zephyr repository at {zephyr_repo_dir}") |
| 74 | + |
| 75 | + templates_root = Path(__file__).parent / "templates" |
| 76 | + if template_kind == "sample": |
| 77 | + copier_template_dir = templates_root / "sample" |
| 78 | + default_output_dir = zephyr_repo_dir / "samples" |
| 79 | + else: |
| 80 | + self.die(f"Unsupported template type: {template_kind}") |
| 81 | + return |
| 82 | + |
| 83 | + if not copier_template_dir.is_dir(): |
| 84 | + self.die(f"Built-in template not found. Expected at: {copier_template_dir}") |
| 85 | + |
| 86 | + output_dir = default_output_dir |
| 87 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 88 | + |
| 89 | + with Worker( |
| 90 | + src_path=str(copier_template_dir), |
| 91 | + dst_path=str(output_dir), |
| 92 | + unsafe=True, |
| 93 | + quiet=True, |
| 94 | + ) as worker: |
| 95 | + worker.run_copy() |
0 commit comments