Cotton code
53 lines · 1.9 KB · PythonRaw
#!/usr/bin/env python3"""Write the names of the pictures Loom ships into the Rust tree.Kit pictures are data: a kit names a picture (an act's object, a chooser'spicture, a step row's picture, a tool's picture) and Loom's catalogue,`apps/loom/Sources/CottonPaper/Media.xcassets`, holds it. The Rust guardsthat hold the two together (cotton-core `tests/act_pictures.rs`, cotton-ffi`phone/opened_tests.rs`) read nothing outside `crates/`(the rust-crate-local-reads test), so they read this listinstead: every `<name>.imageset` with a `Contents.json`, sorted, one name each.  crates/cotton-core/src/definitions/loom_pictures.json   every picture Loom ships    pnpm gen:loom-pictures                       write it    python3 scripts/gen-loom-pictures.py --check fail when it is staleGuard: `scripts/__tests__/loom-pictures.test.ts` runs `--check`, so animageset added, renamed or removed without regenerating fails there."""import jsonimport pathlibimport sysROOT = pathlib.Path(__file__).resolve().parent.parentMEDIA = ROOT / 'apps/loom/Sources/CottonPaper/Media.xcassets'LIST = ROOT / 'crates/cotton-core/src/definitions/loom_pictures.json'def pictures(media=MEDIA):    names = sorted(path.name.removesuffix('.imageset') for path in media.glob('*.imageset') if (path / 'Contents.json').is_file())    if not names:        raise SystemExit(f'gen-loom-pictures: no imageset under {media}')    return namesdef render(names):    return json.dumps(names, indent=2) + '\n'def main(argv):    text = render(pictures())    current = LIST.read_text() if LIST.exists() else None    if current == text:        return 0    if '--check' in argv:        print(f'gen-loom-pictures: stale — run `pnpm gen:loom-pictures`: {LIST.relative_to(ROOT).as_posix()}', file=sys.stderr)        return 1    LIST.write_text(text)    return 0if __name__ == '__main__':    sys.exit(main(sys.argv[1:]))