Files
breeze/scripts/embed-app-assets.py
T
waoziandClaude Opus 5.5 ac944513cd Build KSS and the style packs from the KSS package
KSS moved out of Kryon into kryonlabs/kss. Inbe depends on it, compiles
it from build/packages/kss/src with --module-path kryon=... for its
Kryon imports, and embeds the style packs from build/packages/kss/styles
as styles/NAME.kss. The Android build, the source check, and the tests
use the same paths, and the lock moves Kryon and Ziran to the commits
that carry the move.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-29 21:09:07 -03:00

87 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Embed the app's files in a deterministic, borrowed asset table."""
import argparse
import os
from pathlib import Path
import json
MIME_TYPES = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".ogg": "audio/ogg",
".wav": "audio/wav",
".mp3": "audio/mpeg",
".ttf": "font/ttf",
".otf": "font/otf",
".kss": "text/plain",
".txt": "text/plain",
".dat": "application/octet-stream",
}
def emit(output: Path, files: list[Path], root: Path) -> None:
entries = {}
for file in files:
resolved = file.resolve(strict=True)
# Keep build/packages/<name> links unresolved so package assets get
# stable embedded names regardless of the package cache location.
path = Path(os.path.abspath(file)).relative_to(root).as_posix()
if path.startswith("build/packages/kss/styles/"):
path = path.removeprefix("build/packages/kss/")
if path.startswith("build/packages/kryon/icons/"):
path = path.removeprefix("build/packages/kryon/")
if path in entries:
raise ValueError(f"duplicate asset path: {path}")
mime = MIME_TYPES.get(resolved.suffix.lower(), "application/octet-stream")
entries[path] = (resolved.read_bytes(), mime)
lines = [
"/* Generated by scripts/embed-app-assets.py. */",
'#include "assets_host.h"',
"",
]
for index, (_, (data, _)) in enumerate(sorted(entries.items())):
lines.append(f"static const unsigned char asset_{index}[] = {{")
for offset in range(0, len(data), 16):
lines.append(" " + ", ".join(f"0x{byte:02x}" for byte in data[offset:offset + 16]) + ",")
if not data:
lines.append(" 0x00,")
lines.append("};")
lines.extend(["", "const AssetEntry asset_entries[] = {"])
for index, (path, (data, mime)) in enumerate(sorted(entries.items())):
lines.append(
f" {{(uint8_t *){json.dumps(path)}, (uint8_t *){json.dumps(mime)}, "
f"(uint8_t *)asset_{index}, {len(data)}u}},"
)
if not entries:
lines.append(' {(uint8_t *)"", (uint8_t *)"", NULL, 0u},')
lines.extend([
"};",
f"const size_t asset_entry_count = {len(entries)}u;",
"const AssetEntry *asset_entry_at(size_t index) {",
" return index < asset_entry_count ? &asset_entries[index] : NULL;",
"}",
"size_t asset_entry_total(void) { return asset_entry_count; }",
"",
])
output.parent.mkdir(parents=True, exist_ok=True)
new_content = "\n".join(lines)
if not output.exists() or output.read_text() != new_content:
output.write_text(new_content)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("output", type=Path)
parser.add_argument("files", nargs="*", type=Path)
args = parser.parse_args()
root = Path(__file__).resolve().parent.parent
emit(args.output, args.files, root)
if __name__ == "__main__":
main()