main
  1#!/usr/bin/env python3
  2"""
  3Skill Packager - Creates a distributable .skill file of a skill folder
  4
  5Usage:
  6    python utils/package_skill.py <path/to/skill-folder> [output-directory]
  7
  8Example:
  9    python utils/package_skill.py skills/public/my-skill
 10    python utils/package_skill.py skills/public/my-skill ./dist
 11"""
 12
 13import sys
 14import zipfile
 15from pathlib import Path
 16from quick_validate import validate_skill
 17
 18
 19def package_skill(skill_path, output_dir=None):
 20    """
 21    Package a skill folder into a .skill file.
 22
 23    Args:
 24        skill_path: Path to the skill folder
 25        output_dir: Optional output directory for the .skill file (defaults to current directory)
 26
 27    Returns:
 28        Path to the created .skill file, or None if error
 29    """
 30    skill_path = Path(skill_path).resolve()
 31
 32    # Validate skill folder exists
 33    if not skill_path.exists():
 34        print(f"❌ Error: Skill folder not found: {skill_path}")
 35        return None
 36
 37    if not skill_path.is_dir():
 38        print(f"❌ Error: Path is not a directory: {skill_path}")
 39        return None
 40
 41    # Validate SKILL.md exists
 42    skill_md = skill_path / "SKILL.md"
 43    if not skill_md.exists():
 44        print(f"❌ Error: SKILL.md not found in {skill_path}")
 45        return None
 46
 47    # Run validation before packaging
 48    print("🔍 Validating skill...")
 49    valid, message = validate_skill(skill_path)
 50    if not valid:
 51        print(f"❌ Validation failed: {message}")
 52        print("   Please fix the validation errors before packaging.")
 53        return None
 54    print(f"{message}\n")
 55
 56    # Determine output location
 57    skill_name = skill_path.name
 58    if output_dir:
 59        output_path = Path(output_dir).resolve()
 60        output_path.mkdir(parents=True, exist_ok=True)
 61    else:
 62        output_path = Path.cwd()
 63
 64    skill_filename = output_path / f"{skill_name}.skill"
 65
 66    # Create the .skill file (zip format)
 67    try:
 68        with zipfile.ZipFile(skill_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
 69            # Walk through the skill directory
 70            for file_path in skill_path.rglob('*'):
 71                if file_path.is_file():
 72                    # Calculate the relative path within the zip
 73                    arcname = file_path.relative_to(skill_path.parent)
 74                    zipf.write(file_path, arcname)
 75                    print(f"  Added: {arcname}")
 76
 77        print(f"\n✅ Successfully packaged skill to: {skill_filename}")
 78        return skill_filename
 79
 80    except Exception as e:
 81        print(f"❌ Error creating .skill file: {e}")
 82        return None
 83
 84
 85def main():
 86    if len(sys.argv) < 2:
 87        print("Usage: python utils/package_skill.py <path/to/skill-folder> [output-directory]")
 88        print("\nExample:")
 89        print("  python utils/package_skill.py skills/public/my-skill")
 90        print("  python utils/package_skill.py skills/public/my-skill ./dist")
 91        sys.exit(1)
 92
 93    skill_path = sys.argv[1]
 94    output_dir = sys.argv[2] if len(sys.argv) > 2 else None
 95
 96    print(f"📦 Packaging skill: {skill_path}")
 97    if output_dir:
 98        print(f"   Output directory: {output_dir}")
 99    print()
100
101    result = package_skill(skill_path, output_dir)
102
103    if result:
104        sys.exit(0)
105    else:
106        sys.exit(1)
107
108
109if __name__ == "__main__":
110    main()