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