|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +from .Check import Check |
| 6 | +from ci_tools.logging import logger |
| 7 | + |
| 8 | +_README_TEMPLATE = """\ |
| 9 | +# {package_name} Samples |
| 10 | +
|
| 11 | +This directory contains samples for the `{package_name}` package. |
| 12 | +
|
| 13 | +## Getting started |
| 14 | +
|
| 15 | +Install the package and its dependencies: |
| 16 | +
|
| 17 | +```bash |
| 18 | +pip install {package_name} |
| 19 | +``` |
| 20 | +
|
| 21 | +## Running the samples |
| 22 | +
|
| 23 | +```bash |
| 24 | +python sample_hello_world.py |
| 25 | +``` |
| 26 | +
|
| 27 | +## Advanced code generation |
| 28 | +
|
| 29 | +To generate a fuller set of samples and SDK code from a TypeSpec or Swagger |
| 30 | +spec, use the `azpysdk generate` command: |
| 31 | +
|
| 32 | +```bash |
| 33 | +azpysdk generate <target> |
| 34 | +``` |
| 35 | +
|
| 36 | +See `azpysdk generate --help` for more information. |
| 37 | +""" |
| 38 | + |
| 39 | +_HELLO_WORLD_TEMPLATE = """\ |
| 40 | +# ------------------------------------ |
| 41 | +# Copyright (c) Microsoft Corporation. |
| 42 | +# Licensed under the MIT License. |
| 43 | +# ------------------------------------ |
| 44 | +\"\"\" |
| 45 | +FILE: sample_hello_world.py |
| 46 | +
|
| 47 | +DESCRIPTION: |
| 48 | + A minimal sample for the {package_name} package. |
| 49 | +
|
| 50 | + To learn how to generate a fuller set of samples from a TypeSpec or |
| 51 | + Swagger spec, run: |
| 52 | +
|
| 53 | + azpysdk generate <target> |
| 54 | +\"\"\" |
| 55 | +
|
| 56 | +
|
| 57 | +def main(): |
| 58 | + # TODO: replace with real usage of {package_name} |
| 59 | + print("Hello from {package_name}!") |
| 60 | +
|
| 61 | +
|
| 62 | +if __name__ == "__main__": |
| 63 | + main() |
| 64 | +""" |
| 65 | + |
| 66 | + |
| 67 | +class create_samples(Check): |
| 68 | + """Generate a starter samples scaffold for a targeted package. |
| 69 | +
|
| 70 | + For each targeted package this check: |
| 71 | +
|
| 72 | + * Creates a ``samples/`` directory if one does not already exist. |
| 73 | + * Generates a starter ``README.md`` inside ``samples/`` (if absent). |
| 74 | + * Generates a starter ``sample_hello_world.py`` inside ``samples/`` (if absent). |
| 75 | +
|
| 76 | + For full SDK code generation (including samples) from a TypeSpec or Swagger |
| 77 | + spec, use the existing ``azpysdk generate`` command:: |
| 78 | +
|
| 79 | + azpysdk generate <target> |
| 80 | + """ |
| 81 | + |
| 82 | + def __init__(self) -> None: |
| 83 | + super().__init__() |
| 84 | + |
| 85 | + def register( |
| 86 | + self, subparsers: "argparse._SubParsersAction", parent_parsers: Optional[List[argparse.ArgumentParser]] = None |
| 87 | + ) -> None: |
| 88 | + """Register the ``create-samples`` subcommand. |
| 89 | +
|
| 90 | + Creates a starter ``samples/`` scaffold (README + hello-world sample) |
| 91 | + for each targeted package. For full SDK code generation from a |
| 92 | + TypeSpec/Swagger spec use ``azpysdk generate <target>``. |
| 93 | + """ |
| 94 | + parents = parent_parsers or [] |
| 95 | + p = subparsers.add_parser( |
| 96 | + "create-samples", |
| 97 | + parents=parents, |
| 98 | + help=( |
| 99 | + "Generate a starter samples scaffold (README.md, sample_hello_world.py) " |
| 100 | + "for each targeted package. For full code generation from a TypeSpec or " |
| 101 | + "Swagger spec, use: azpysdk generate <target>" |
| 102 | + ), |
| 103 | + ) |
| 104 | + p.set_defaults(func=self.run) |
| 105 | + p.add_argument( |
| 106 | + "--output-dir", |
| 107 | + default=None, |
| 108 | + help=( |
| 109 | + "Directory to write the samples scaffold into. " |
| 110 | + "Defaults to <package_dir>/samples." |
| 111 | + ), |
| 112 | + ) |
| 113 | + |
| 114 | + def run(self, args: argparse.Namespace) -> int: |
| 115 | + """Create a samples scaffold for each targeted package.""" |
| 116 | + logger.info("Running create-samples check...") |
| 117 | + |
| 118 | + targeted = self.get_targeted_directories(args) |
| 119 | + if not targeted: |
| 120 | + logger.warning("No target packages discovered for create-samples check.") |
| 121 | + return 0 |
| 122 | + |
| 123 | + results: List[int] = [] |
| 124 | + |
| 125 | + for parsed in targeted: |
| 126 | + package_dir = parsed.folder |
| 127 | + package_name = parsed.name |
| 128 | + |
| 129 | + output_dir = getattr(args, "output_dir", None) or os.path.join(package_dir, "samples") |
| 130 | + |
| 131 | + try: |
| 132 | + self._create_scaffold(package_name, output_dir) |
| 133 | + logger.info( |
| 134 | + f"create-samples SUCCEEDED for {package_name}. " |
| 135 | + f"Samples scaffold written to: {output_dir}" |
| 136 | + ) |
| 137 | + logger.info( |
| 138 | + "Tip: for full SDK code generation from a TypeSpec or Swagger spec, run: " |
| 139 | + f"azpysdk generate {package_dir}" |
| 140 | + ) |
| 141 | + except Exception as exc: |
| 142 | + logger.error(f"create-samples FAILED for {package_name}: {exc}") |
| 143 | + results.append(1) |
| 144 | + |
| 145 | + return max(results) if results else 0 |
| 146 | + |
| 147 | + def _create_scaffold(self, package_name: str, output_dir: str) -> None: |
| 148 | + """Create the samples directory and starter files.""" |
| 149 | + os.makedirs(output_dir, exist_ok=True) |
| 150 | + |
| 151 | + readme_path = os.path.join(output_dir, "README.md") |
| 152 | + if not os.path.exists(readme_path): |
| 153 | + with open(readme_path, "w", encoding="utf-8") as f: |
| 154 | + f.write(_README_TEMPLATE.format(package_name=package_name)) |
| 155 | + logger.info(f"Created {readme_path}") |
| 156 | + else: |
| 157 | + logger.info(f"Skipping {readme_path} (already exists)") |
| 158 | + |
| 159 | + hello_world_path = os.path.join(output_dir, "sample_hello_world.py") |
| 160 | + if not os.path.exists(hello_world_path): |
| 161 | + with open(hello_world_path, "w", encoding="utf-8") as f: |
| 162 | + f.write(_HELLO_WORLD_TEMPLATE.format(package_name=package_name)) |
| 163 | + logger.info(f"Created {hello_world_path}") |
| 164 | + else: |
| 165 | + logger.info(f"Skipping {hello_world_path} (already exists)") |
0 commit comments