27 lines
1.3 KiB
Python
27 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""tokens.json -> Tokens.kt (Compose). Re-run after every token change."""
|
|
import json, re, sys, pathlib
|
|
src = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else "tokens.json")
|
|
out = pathlib.Path(sys.argv[2] if len(sys.argv) > 2 else src.with_name("Tokens.kt"))
|
|
t = json.loads(src.read_text())
|
|
camel = lambda s: re.sub(r"-(\w)", lambda m: m.group(1).upper(), s)
|
|
L = ["// Generated from tokens.json by gen_theme.py. Do not edit by hand.",
|
|
"package app.telefon.ui.theme", "",
|
|
"import androidx.compose.ui.graphics.Color",
|
|
"import androidx.compose.ui.text.TextStyle",
|
|
"import androidx.compose.ui.text.font.FontWeight",
|
|
"import androidx.compose.ui.unit.dp",
|
|
"import androidx.compose.ui.unit.sp", "",
|
|
"object WColor {"]
|
|
L += [f" val {camel(k)} = Color(0xFF{v.lstrip('#').upper()})" for k, v in t["color"].items()]
|
|
L += ["}", "", "object WType {"]
|
|
for k, v in t["type"].items():
|
|
L.append(f" val {camel(k)} = TextStyle(fontSize = {v['size']}.sp, lineHeight = {v['line']}.sp, fontWeight = FontWeight.W{v['weight']})")
|
|
L += ["}", "", "object WSpace {"]
|
|
L += [f" val s{v} = {v}.dp" for v in t["space"]]
|
|
L += ["}", "", "object WRadius {"]
|
|
L += [f" val {camel(k)} = {v}.dp" for k, v in t["radius"].items()]
|
|
L += ["}", ""]
|
|
out.write_text("\n".join(L))
|
|
print("wrote", out)
|