and ts is done
This commit is contained in:
+55
-106
@@ -1,13 +1,11 @@
|
|||||||
"thanks chatgpt"
|
"thanks chatgpt"
|
||||||
|
|
||||||
|
import os
|
||||||
|
import textwrap
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
from functools import wraps
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from pprint import pprint
|
from typing import Callable
|
||||||
from typing import Callable, TypedDict, overload
|
|
||||||
|
|
||||||
|
|
||||||
root = Path(__file__).parent.parent
|
root = Path(__file__).parent.parent
|
||||||
static = root / "static"
|
static = root / "static"
|
||||||
@@ -21,13 +19,7 @@ assert 'name = "wakey"' in (root / "Cargo.toml").read_text(encoding="utf-8"), (
|
|||||||
|
|
||||||
def sanitize(name: str) -> str:
|
def sanitize(name: str) -> str:
|
||||||
# Valid Rust identifiers: letters, digits, underscores; no starting digit
|
# Valid Rust identifiers: letters, digits, underscores; no starting digit
|
||||||
out = []
|
s = "".join(c if c.isalnum() else "_" for c in name)
|
||||||
for c in name:
|
|
||||||
if c.isalnum() or c == "_":
|
|
||||||
out.append(c)
|
|
||||||
else:
|
|
||||||
out.append("_")
|
|
||||||
s = "".join(out)
|
|
||||||
if s and s[0].isdigit():
|
if s and s[0].isdigit():
|
||||||
s = "_" + s
|
s = "_" + s
|
||||||
return s
|
return s
|
||||||
@@ -35,74 +27,69 @@ def sanitize(name: str) -> str:
|
|||||||
|
|
||||||
def indent(text: str, n: int) -> str:
|
def indent(text: str, n: int) -> str:
|
||||||
pad = " " * n
|
pad = " " * n
|
||||||
return "\n".join(pad + line if line.strip() else line for line in text.splitlines())
|
return textwrap.indent(text, pad)
|
||||||
|
|
||||||
|
|
||||||
class RsAsset(ABC):
|
class RsAsset(ABC):
|
||||||
path: Path
|
path: Path
|
||||||
|
|
||||||
|
|
||||||
|
class RsAssetWithName(ABC):
|
||||||
|
name: str
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def plain(self) -> str: ...
|
def name_gen(path: Path) -> str:
|
||||||
@abstractmethod
|
"helper"
|
||||||
def macroed(self) -> str: ...
|
|
||||||
|
|
||||||
|
|
||||||
class RsAssetFile(RsAsset):
|
class RsAssetFile(RsAsset, RsAssetWithName):
|
||||||
__match_args__ = ("path", "name")
|
__match_args__ = ("path", "name")
|
||||||
|
|
||||||
def __init__(self, path: Path, name: str | None = None):
|
def __init__(self, path: Path, name: str | None = None):
|
||||||
self.path = path
|
self.path = path
|
||||||
if name is None:
|
if name is None:
|
||||||
self.name: str = sanitize(self.path.name).upper()
|
self.name: str = RsAssetFile.name_gen(path)
|
||||||
else:
|
else:
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def path_relative_to(self, path: Path):
|
def path_relative_to(self, path: Path):
|
||||||
|
"helper"
|
||||||
return self.path.relative_to(path, walk_up=True).as_posix()
|
return self.path.relative_to(path, walk_up=True).as_posix()
|
||||||
|
|
||||||
def _apply_template(self, template):
|
@staticmethod
|
||||||
return template.format(
|
def plain_template(const_name: str, relative_path: str):
|
||||||
const_name=self.name,
|
return f'pub const {const_name}: &str = include_str!("{relative_path}");'
|
||||||
relative_path=self.path_relative_to(src),
|
|
||||||
)
|
|
||||||
|
|
||||||
plain_template = 'pub const {const_name}: &str = include_str!("{relative_path}");'
|
@staticmethod
|
||||||
|
def macroed_template(const_name: str, relative_path: str):
|
||||||
|
return f'file {const_name} "{relative_path}"'
|
||||||
|
|
||||||
macroed_template = 'file {const_name} "{relative_path}"'
|
@staticmethod
|
||||||
|
def name_gen(path: Path) -> str:
|
||||||
def plain(self):
|
return sanitize(path.name).upper()
|
||||||
return self._apply_template(self.plain_template)
|
|
||||||
|
|
||||||
def macroed(self):
|
|
||||||
return self._apply_template(self.macroed_template)
|
|
||||||
|
|
||||||
|
|
||||||
class RsAssetModule(RsAsset):
|
class RsAssetModule(RsAsset, RsAssetWithName):
|
||||||
__match_args__ = ("path", "name")
|
__match_args__ = ("path", "name")
|
||||||
|
|
||||||
def __init__(self, path: Path, name: str | None = None):
|
def __init__(self, path: Path, name: str | None = None):
|
||||||
self.path = path
|
self.path = path
|
||||||
if name is None:
|
if name is None:
|
||||||
self.name: str = sanitize(self.path.name).lower()
|
self.name: str = RsAssetModule.name_gen(path)
|
||||||
else:
|
else:
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
plain_template = "pub mod {sanitized_name} {{\n{indented_body}\n}}"
|
@staticmethod
|
||||||
|
def plain_template(sanitized_name: str, body: str):
|
||||||
|
"braindead"
|
||||||
|
indented_body = indent(body, 4)
|
||||||
|
return f"pub mod {sanitized_name} {{\n{indented_body}\n}}"
|
||||||
|
|
||||||
macroed_template = "folder {sanitized_name} {{\n{indented_body}\n}}"
|
@staticmethod
|
||||||
|
def macroed_template(sanitized_name: str, body: str):
|
||||||
def plain(self):
|
indented_body = indent(body, 4)
|
||||||
return self._apply_template(self.plain_template, lambda it: it.plain())
|
return f"folder {sanitized_name} {{\n{indented_body}\n}}"
|
||||||
|
|
||||||
def macroed(self):
|
|
||||||
return self._apply_template(self.macroed_template, lambda it: it.macroed())
|
|
||||||
|
|
||||||
def _apply_template(self, template: str, renderer: Callable[[RsAsset], str]):
|
|
||||||
body = self.process_body(self.path, renderer)
|
|
||||||
return template.format(
|
|
||||||
sanitized_name=self.name,
|
|
||||||
indented_body=indent(body, 4),
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def process_body(path: Path, renderer: Callable[[RsAsset], str]):
|
def process_body(path: Path, renderer: Callable[[RsAsset], str]):
|
||||||
@@ -120,32 +107,27 @@ class RsAssetModule(RsAsset):
|
|||||||
subs.append(RsAssetModule(f))
|
subs.append(RsAssetModule(f))
|
||||||
yield from subs
|
yield from subs
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def name_gen(path: Path) -> str:
|
||||||
|
return sanitize(path.name).lower()
|
||||||
|
|
||||||
|
|
||||||
# i meant plain
|
# i meant plain
|
||||||
def render_pain(ass: RsAsset) -> str:
|
def render_pain(ass: RsAsset) -> str:
|
||||||
"""
|
"""
|
||||||
fym i have to deal with all RsAsset subclasses
|
fym i have to deal with all RsAsset subclasses.
|
||||||
|
|
||||||
whats the point then. unless there is a `Intermediate Representation` whats the point then.
|
ts like a matrix of ahh.
|
||||||
"""
|
"""
|
||||||
match ass:
|
match ass:
|
||||||
case RsAssetRoot(path):
|
case RsAssetRoot(path):
|
||||||
body = RsAssetModule.process_body(path, render_pain)
|
body = RsAssetModule.process_body(path, render_pain)
|
||||||
return RsAssetRoot.plain_template.format(
|
return RsAssetRoot.plain_template(body)
|
||||||
header=header,
|
|
||||||
body=body,
|
|
||||||
)
|
|
||||||
case RsAssetModule(path, name):
|
case RsAssetModule(path, name):
|
||||||
body = RsAssetModule.process_body(path, render_pain)
|
body = RsAssetModule.process_body(path, render_pain)
|
||||||
return RsAssetModule.plain_template.format(
|
return RsAssetModule.plain_template(name, body)
|
||||||
sanitized_name=name,
|
|
||||||
indented_body=indent(body, 4),
|
|
||||||
)
|
|
||||||
case RsAssetFile(path, name) as file:
|
case RsAssetFile(path, name) as file:
|
||||||
return RsAssetFile.plain_template.format(
|
return RsAssetFile.plain_template(name, file.path_relative_to(src))
|
||||||
const_name=name,
|
|
||||||
relative_path=file.path_relative_to(src),
|
|
||||||
)
|
|
||||||
case _:
|
case _:
|
||||||
raise TypeError("who are you?")
|
raise TypeError("who are you?")
|
||||||
|
|
||||||
@@ -157,23 +139,12 @@ def render_macro(ass: RsAsset) -> str:
|
|||||||
match ass:
|
match ass:
|
||||||
case RsAssetRoot(path):
|
case RsAssetRoot(path):
|
||||||
body = RsAssetModule.process_body(path, render_macro)
|
body = RsAssetModule.process_body(path, render_macro)
|
||||||
return RsAssetRoot.macroed_template.format(
|
return RsAssetRoot.macroed_template(body)
|
||||||
header=header,
|
|
||||||
lda_macro=lda_macro,
|
|
||||||
indented_body=indent(body, 4),
|
|
||||||
)
|
|
||||||
case RsAssetModule(path, name):
|
case RsAssetModule(path, name):
|
||||||
body = RsAssetModule.process_body(path, render_macro)
|
body = RsAssetModule.process_body(path, render_macro)
|
||||||
return RsAssetModule.macroed_template.format(
|
return RsAssetModule.macroed_template(name, body)
|
||||||
sanitized_name=name,
|
|
||||||
indented_body=indent(body, 4),
|
|
||||||
)
|
|
||||||
case RsAssetFile(path, name) as file:
|
case RsAssetFile(path, name) as file:
|
||||||
return RsAssetFile.macroed_template.format(
|
return RsAssetFile.macroed_template(name, file.path_relative_to(src))
|
||||||
const_name=name,
|
|
||||||
relative_path=file.path_relative_to(src),
|
|
||||||
)
|
|
||||||
|
|
||||||
case _:
|
case _:
|
||||||
raise TypeError("who are you?")
|
raise TypeError("who are you?")
|
||||||
|
|
||||||
@@ -203,26 +174,20 @@ macro_rules! hehe {
|
|||||||
header = "// generated with ./scripts/map_static.py"
|
header = "// generated with ./scripts/map_static.py"
|
||||||
|
|
||||||
|
|
||||||
def announce_yourself(*pa, **pkw):
|
|
||||||
def wpr[**P, R](f: Callable[P, R]) -> Callable[P, R]:
|
|
||||||
@wraps(f)
|
|
||||||
def wpd(*a: P.args, **k: P.kwargs) -> R:
|
|
||||||
print(*pa, **pkw)
|
|
||||||
return f(*a, **k)
|
|
||||||
|
|
||||||
return wpd
|
|
||||||
|
|
||||||
return wpr
|
|
||||||
|
|
||||||
|
|
||||||
class RsAssetRoot(RsAsset):
|
class RsAssetRoot(RsAsset):
|
||||||
__match_args__ = ("path",)
|
__match_args__ = ("path",)
|
||||||
|
|
||||||
plain_template = """{header}
|
@staticmethod
|
||||||
|
def plain_template(body: str):
|
||||||
|
return f"""{header}
|
||||||
|
|
||||||
{body}
|
{body}
|
||||||
"""
|
"""
|
||||||
macroed_template = """{header}
|
|
||||||
|
@staticmethod
|
||||||
|
def macroed_template(body: str):
|
||||||
|
indented_body = indent(body, 4)
|
||||||
|
return f"""{header}
|
||||||
{lda_macro}
|
{lda_macro}
|
||||||
hehe! {{
|
hehe! {{
|
||||||
{indented_body}
|
{indented_body}
|
||||||
@@ -232,22 +197,6 @@ hehe! {{
|
|||||||
def __init__(self, path: Path) -> None:
|
def __init__(self, path: Path) -> None:
|
||||||
os.scandir(path) # is you a dir?
|
os.scandir(path) # is you a dir?
|
||||||
self.path = path
|
self.path = path
|
||||||
self._w = RsAssetModule(path)
|
|
||||||
|
|
||||||
def plain(self) -> str:
|
|
||||||
body = RsAssetModule.process_body(self.path, lambda it: it.plain())
|
|
||||||
return self.plain_template.format(
|
|
||||||
header=header,
|
|
||||||
body=body,
|
|
||||||
)
|
|
||||||
|
|
||||||
def macroed(self) -> str:
|
|
||||||
body = RsAssetModule.process_body(self.path, lambda it: it.macroed())
|
|
||||||
return self.macroed_template.format(
|
|
||||||
header=header,
|
|
||||||
lda_macro=lda_macro,
|
|
||||||
indented_body=indent(body, 4),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
asset_root = RsAssetRoot(static)
|
asset_root = RsAssetRoot(static)
|
||||||
|
|||||||
Reference in New Issue
Block a user