#!/usr/bin/env python3 """Create directory and symlink for Copilot skills.""" import argparse import subprocess import sys from pathlib import Path def add_skill(markdown_path: str) -> None: """Add a skill by creating symlink in ~/.copilot/skills/. Args: markdown_path: Path to the markdown file (relative or absolute) Raises: FileNotFoundError: If markdown file doesn't exist RuntimeError: If symlink creation fails """ md_file = Path(markdown_path) if not md_file.exists(): raise FileNotFoundError(f"Markdown file not found: {markdown_path}") # Get absolute path abs_md_path = md_file.resolve() # Extract skill name from filename (without .md extension) skill_name = md_file.stem # Create skill directory skills_dir = Path.home() / ".copilot" / "skills" / skill_name skills_dir.mkdir(parents=True, exist_ok=True) # Create symlink symlink_path = skills_dir / "SKILL.md" # Remove existing symlink if it exists if symlink_path.exists() or symlink_path.is_symlink(): symlink_path.unlink() # Create the symlink using ln -s for compatibility result = subprocess.run( ["ln", "-s", str(abs_md_path), str(symlink_path)], capture_output=True, check=False, text=True, ) if result.returncode != 0: raise RuntimeError(f"Failed to create symlink: {result.stderr}") print(f"✓ Created skill link: {symlink_path} -> {abs_md_path}") def main() -> None: """Main entry point.""" parser = argparse.ArgumentParser(description="Create directory and symlink for Copilot skills") subparsers = parser.add_subparsers(dest="command", help="Command to run") # Add subcommand add_parser = subparsers.add_parser("add", help="Add a skill") add_parser.add_argument("markdown", help="Path to markdown skill file") args = parser.parse_args() if not args.command: parser.print_help() sys.exit(1) if args.command == "add": try: add_skill(args.markdown) except (FileNotFoundError, RuntimeError, OSError) as e: print(f"✗ Error: {e}", file=sys.stderr) sys.exit(1) if __name__ == "__main__": main()