|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +Licensed under the MIT License. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import shutil |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | +from typing import Optional |
| 14 | + |
| 15 | + |
| 16 | +def run_command(cmd: list[str], cwd: Optional[str] = None) -> None: |
| 17 | + """Run a command and raise an exception if it fails.""" |
| 18 | + result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True) |
| 19 | + if result.returncode != 0: |
| 20 | + print(f"Error running command: {' '.join(cmd)}") |
| 21 | + print(f"Error output: {result.stderr}") |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + |
| 25 | +def create_package(package_name: str) -> None: |
| 26 | + """Create a new package with the specified name.""" |
| 27 | + # Convert package name to lowercase for directory name |
| 28 | + package_dir = package_name.lower() |
| 29 | + package_path = Path("packages") / package_dir |
| 30 | + |
| 31 | + if package_path.exists(): |
| 32 | + print(f"Error: Package directory {package_path} already exists") |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + # Step 1: Run uv init |
| 36 | + print(f"Creating new package {package_name}...") |
| 37 | + run_command(["uv", "init", "--lib", str(package_path)]) |
| 38 | + |
| 39 | + # Step 2: Move and restructure the source directory |
| 40 | + src_dir = package_path / "src" |
| 41 | + original_package_dir = src_dir / package_name |
| 42 | + target_dir = src_dir / "microsoft" / "teams" / package_name.lower() |
| 43 | + |
| 44 | + # Create the new directory structure |
| 45 | + target_dir.parent.mkdir(parents=True, exist_ok=True) |
| 46 | + |
| 47 | + # Move the package directory |
| 48 | + if original_package_dir.exists(): |
| 49 | + shutil.move(str(original_package_dir), str(target_dir)) |
| 50 | + |
| 51 | + # Step 3 & 4: Update pyproject.toml |
| 52 | + pyproject_path = package_path / "pyproject.toml" |
| 53 | + if pyproject_path.exists(): |
| 54 | + with open(pyproject_path, "r") as f: |
| 55 | + content = f.read() |
| 56 | + |
| 57 | + # Update package name |
| 58 | + content = content.replace(f'name = "{package_name}"', f'name = "microsoft.teams.{package_name.lower()}"') |
| 59 | + |
| 60 | + # Add wheel build configuration |
| 61 | + if "[tool.hatch.build.targets.wheel]" not in content: |
| 62 | + content += '\n[tool.hatch.build.targets.wheel]\npackages = ["src/microsoft"]\n' |
| 63 | + |
| 64 | + with open(pyproject_path, "w") as f: |
| 65 | + f.write(content) |
| 66 | + |
| 67 | + # Step 5: Update root pyproject.toml |
| 68 | + root_pyproject_path = Path("pyproject.toml") |
| 69 | + if root_pyproject_path.exists(): |
| 70 | + with open(root_pyproject_path, "r") as f: |
| 71 | + content = f.read() |
| 72 | + |
| 73 | + # Add the new package to uv.sources if not already present |
| 74 | + source_entry = f'"microsoft.teams.{package_name.lower()}" = {{ workspace = true }}' |
| 75 | + if source_entry not in content: |
| 76 | + # Find the [tool.uv.sources] section |
| 77 | + if "[tool.uv.sources]" in content: |
| 78 | + # Add the new source after the section header |
| 79 | + content = content.replace("[tool.uv.sources]", f"[tool.uv.sources]\n{source_entry}") |
| 80 | + else: |
| 81 | + # Add the entire section if it doesn't exist |
| 82 | + content += f"\n[tool.uv.sources]\n{source_entry}\n" |
| 83 | + |
| 84 | + with open(root_pyproject_path, "w") as f: |
| 85 | + f.write(content) |
| 86 | + |
| 87 | + print(f"\nPackage {package_name} created successfully!") |
| 88 | + print(f"Location: {package_path}") |
| 89 | + print("\nNext steps:") |
| 90 | + print("1. Add your package code in src/microsoft/teams/{package_name.lower()}") |
| 91 | + print("2. Update the package description in pyproject.toml") |
| 92 | + print("3. Add any required dependencies to pyproject.toml") |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + parser = argparse.ArgumentParser(description="Create a new package in the teams.py project") |
| 97 | + parser.add_argument("package_name", help="Name of the package to create") |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + create_package(args.package_name) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments