package_skill.py 3.2 KB

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