-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild.py
More file actions
360 lines (297 loc) · 12.1 KB
/
build.py
File metadata and controls
360 lines (297 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
"""
Packages skill folders into .skill files for Claude.ai upload, or product ZIPs
for Gumroad distribution.
Usage:
python build.py <skill-name> # Package a single skill
python build.py --all # Package all skills
python build.py --list # List available skills
python build.py --product <name> # Package one product
python build.py --all-products # Package all products
python build.py --list-products # List available products
"""
import argparse
import shutil
import sys
import tempfile
import zipfile
from pathlib import Path
import yaml
def get_repo_root() -> Path:
"""Get the repository root (where this script lives)."""
return Path(__file__).parent.resolve()
def get_skill_dirs(repo_root: Path) -> list[Path]:
"""Find all valid skill directories (folders containing SKILL.md)."""
skills = []
skills_dir = repo_root / "skills"
if not skills_dir.exists():
return skills
for item in skills_dir.iterdir():
if item.is_dir() and (item / "SKILL.md").exists():
skills.append(item)
return sorted(skills)
def validate_skill(skill_dir: Path) -> tuple[bool, list[str]]:
"""
Validate a skill directory.
Returns (is_valid, list_of_errors).
"""
errors = []
skill_md = skill_dir / "SKILL.md"
if not skill_md.exists():
errors.append("Missing SKILL.md")
return False, errors
# Parse and validate frontmatter
content = skill_md.read_text(encoding="utf-8")
if not content.startswith("---"):
errors.append("SKILL.md must start with YAML frontmatter (---)")
return False, errors
try:
# Extract frontmatter
parts = content.split("---", 2)
if len(parts) < 3:
errors.append("Invalid frontmatter format")
return False, errors
frontmatter = yaml.safe_load(parts[1])
if not frontmatter:
errors.append("Empty frontmatter")
return False, errors
if "name" not in frontmatter:
errors.append("Missing 'name' in frontmatter")
if "description" not in frontmatter:
errors.append("Missing 'description' in frontmatter")
elif len(frontmatter.get("description", "")) < 20:
errors.append("Description too short (min 20 chars)")
except yaml.YAMLError as e:
errors.append(f"Invalid YAML frontmatter: {e}")
return len(errors) == 0, errors
def package_skill(skill_dir: Path, output_dir: Path) -> Path:
"""
Package a skill directory into a .skill file.
Returns the path to the created .skill file.
"""
skill_name = skill_dir.name
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / f"{skill_name}.skill"
with zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED) as zf:
for file_path in skill_dir.rglob("*"):
if file_path.is_file():
# Skip hidden files and common unwanted files
if any(part.startswith(".") for part in file_path.parts):
continue
if file_path.name in ("__pycache__", ".DS_Store", "Thumbs.db"):
continue
# Archive path includes the skill folder name
archive_path = Path(skill_name) / file_path.relative_to(skill_dir)
zf.write(file_path, archive_path)
print(f" Added: {archive_path}")
return output_file
def get_product_configs(repo_root: Path) -> list[Path]:
"""Find all product YAML config files in the products/ directory."""
products_dir = repo_root / "products"
if not products_dir.exists():
return []
return sorted(products_dir.glob("*.yaml"))
def load_product_config(config_path: Path) -> dict:
"""Load and return a product config from a YAML file."""
return yaml.safe_load(config_path.read_text(encoding="utf-8"))
def validate_product(config_path: Path, repo_root: Path) -> tuple[bool, list[str]]:
"""
Validate a product config and all its referenced skills.
Returns (is_valid, list_of_errors).
"""
errors = []
try:
config = load_product_config(config_path)
except yaml.YAMLError as e:
return False, [f"Invalid YAML: {e}"]
if "name" not in config:
errors.append("Missing 'name' field")
if "skills" not in config or not config["skills"]:
errors.append("Missing or empty 'skills' list")
return False, errors
for skill_path_str in config["skills"]:
skill_dir = repo_root / skill_path_str
if not skill_dir.exists():
errors.append(f"Skill directory not found: {skill_path_str}")
continue
is_valid, skill_errors = validate_skill(skill_dir)
if not is_valid:
for err in skill_errors:
errors.append(f"{skill_path_str}: {err}")
return len(errors) == 0, errors
def package_product(config_path: Path, repo_root: Path, output_dir: Path) -> Path:
"""
Package a product into a ZIP file.
Returns the path to the created .zip file.
"""
config = load_product_config(config_path)
product_name = config["name"]
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / f"{product_name}.zip"
readme_path = config_path.parent / f"{product_name}-README.md"
with tempfile.TemporaryDirectory() as tmp_dir:
staging = Path(tmp_dir) / product_name
# Copy each skill directory into staging root
for skill_path_str in config["skills"]:
skill_dir = repo_root / skill_path_str
dest = staging / skill_dir.name
shutil.copytree(skill_dir, dest)
# Copy product README to staging root
if readme_path.exists():
shutil.copy2(readme_path, staging / "README.md")
# Zip staging directory
with zipfile.ZipFile(output_file, "w", zipfile.ZIP_DEFLATED) as zf:
for file_path in staging.rglob("*"):
if not file_path.is_file():
continue
if any(part.startswith(".") for part in file_path.parts):
continue
if file_path.name in ("__pycache__", ".DS_Store", "Thumbs.db"):
continue
archive_path = file_path.relative_to(staging)
zf.write(file_path, archive_path)
print(f" Added: {archive_path}")
return output_file
def main():
parser = argparse.ArgumentParser(
description="Package skills for Claude.ai or products for Gumroad",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python build.py brainstorm # Package the brainstorm skill
python build.py --all # Package all skills
python build.py --list # List available skills
python build.py --product brainstorm # Package the brainstorm product
python build.py --all-products # Package all products
python build.py --list-products # List available products
""",
)
parser.add_argument("skill", nargs="?", help="Name of skill to package")
parser.add_argument("--all", action="store_true", help="Package all skills")
parser.add_argument("--list", action="store_true", help="List available skills")
parser.add_argument("--product", metavar="NAME", help="Package a single product")
parser.add_argument(
"--all-products", action="store_true", help="Package all products"
)
parser.add_argument(
"--list-products", action="store_true", help="List available products"
)
parser.add_argument(
"--output",
"-o",
type=Path,
default=None,
help="Output directory (default: ./dist)",
)
args = parser.parse_args()
repo_root = get_repo_root()
output_dir = args.output or (repo_root / "dist")
skill_dirs = get_skill_dirs(repo_root)
product_configs = get_product_configs(repo_root)
# List products mode
if args.list_products:
print("Available products:")
for config_path in product_configs:
is_valid, errors = validate_product(config_path, repo_root)
status = "✓" if is_valid else "✗"
config = load_product_config(config_path)
skill_count = len(config.get("skills", []))
print(
f" {status} {config.get('name', config_path.stem)} ({skill_count} skill{'s' if skill_count != 1 else ''})"
)
if errors:
for error in errors:
print(f" └─ {error}")
return 0
# List skills mode
if args.list:
print("Available skills:")
for skill_dir in skill_dirs:
is_valid, errors = validate_skill(skill_dir)
status = "✓" if is_valid else "✗"
print(f" {status} {skill_dir.name}")
if errors:
for error in errors:
print(f" └─ {error}")
return 0
# Package products
if args.all_products or args.product:
if args.all_products:
to_package_products = product_configs
else:
matches = [
p
for p in product_configs
if load_product_config(p).get("name") == args.product
]
if not matches:
print(f"Error: Product '{args.product}' not found")
available = [
load_product_config(p).get("name", p.stem) for p in product_configs
]
print(f"Available products: {', '.join(available)}")
return 1
to_package_products = matches
success_count = 0
for config_path in to_package_products:
config = load_product_config(config_path)
product_name = config.get("name", config_path.stem)
print(f"\n📦 Packaging product: {product_name}")
is_valid, errors = validate_product(config_path, repo_root)
if not is_valid:
print("❌ Validation failed:")
for error in errors:
print(f" └─ {error}")
continue
print("✓ Validation passed")
try:
output_file = package_product(config_path, repo_root, output_dir)
print(f"✅ Created: {output_file}")
success_count += 1
except Exception as e:
print(f"❌ Packaging failed: {e}")
print(f"\n{'─' * 40}")
print(f"Packaged {success_count}/{len(to_package_products)} products")
if success_count > 0:
print(f"Output directory: {output_dir}")
return 0 if success_count == len(to_package_products) else 1
# Determine which skills to package
if args.all:
to_package = skill_dirs
elif args.skill:
skill_path = repo_root / "skills" / args.skill
if not skill_path.exists():
print(f"Error: Skill '{args.skill}' not found")
print(f"Available skills: {', '.join(s.name for s in skill_dirs)}")
return 1
to_package = [skill_path]
else:
parser.print_help()
return 1
# Package skills
success_count = 0
for skill_dir in to_package:
print(f"\n📦 Packaging: {skill_dir.name}")
# Validate first
is_valid, errors = validate_skill(skill_dir)
if not is_valid:
print("❌ Validation failed:")
for error in errors:
print(f" └─ {error}")
continue
print("✓ Validation passed")
# Package
try:
output_file = package_skill(skill_dir, output_dir)
print(f"✅ Created: {output_file}")
success_count += 1
except Exception as e:
print(f"❌ Packaging failed: {e}")
# Summary
print(f"\n{'─' * 40}")
print(f"Packaged {success_count}/{len(to_package)} skills")
if success_count > 0:
print(f"Output directory: {output_dir}")
return 0 if success_count == len(to_package) else 1
if __name__ == "__main__":
sys.exit(main())