Added project generator and some templates

This commit is contained in:
Casey 2024-05-29 12:25:42 +03:00
commit 84fdb908ef
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
7 changed files with 134 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
venv/

View File

@ -0,0 +1 @@
name = "No template (empty folder)"

View File

@ -0,0 +1,12 @@
CFLAGS += -Wall -Wextra `exec pkg-config --cflags raylib`
LDFLAGS := -lm `pkg-config --libs raylib`
__PROGNAME: main.c
$(CC) $(CFLAGS) main.c $(LDFLAGS) -o __PROGNAME
clean:
$(RM) __PROGNAME
run: __PROGNAME
./__PROGNAME

View File

@ -0,0 +1,12 @@
#include <raylib.h>
int main(int argc, char **argv) {
InitWindow(__WINDOW_WIDTH, __WINDOW_HEIGHT, "__PROGNAME");
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(BLACK);
DrawText("Hi", 20, 20, 20, WHITE);
EndDrawing();
}
}

View File

@ -0,0 +1,13 @@
name = "Empty Raylib project"
templates = [
"Makefile",
"main.c"
]
[params.WINDOW_WIDTH]
prompt = "Window width"
default = "800"
[params.WINDOW_HEIGHT]
prompt = "Window height"
default = "600"

92
new.py Normal file
View File

@ -0,0 +1,92 @@
#!/usr/bin/env python3
from typing import Any, Literal, Optional
from tomllib import load
from pathlib import Path
from dataclasses import dataclass, field
import questionary
TEMPLATES_DIR = Path("./_templates")
@dataclass(frozen=True)
class TemplateParameter:
prompt: str
default: Optional[Any] = None
@dataclass
class ProjectTemplate:
name: str
path: Path
templates: list[str] = field(default_factory=list)
parameters: dict[str, TemplateParameter] = field(default_factory=dict)
templates: dict[str, ProjectTemplate] = {}
for v in TEMPLATES_DIR.rglob("template.toml"):
with v.open("rb") as fp:
data = load(fp)
tpl = ProjectTemplate(
data.get('name', str(v.parent.relative_to(TEMPLATES_DIR))),
v.parent,
data.get('templates', []),
{
k: TemplateParameter(**v)
for k, v in data.get('params', {}).items()
}
)
templates[tpl.name] = tpl
name: Optional[str] = questionary.text("Project name").ask()
if not name:
exit(1)
template: Optional[ProjectTemplate] = questionary.select("Template", [
questionary.Choice(tpl.name, tpl) for tpl in templates.values()
]).ask()
if not template:
exit(1)
parameters = questionary.prompt([
{
"type": "text",
"name": k,
"message": param.prompt,
"default": param.default
}
for k, param in template.parameters.items()
])
if not parameters:
exit(1)
project_dir = Path(name)
project_dir.mkdir(parents=True, exist_ok=True)
for base, dirs, files in template.path.walk():
for dirname in dirs:
(base / dirname).mkdir(exist_ok=True, parents=True)
for filename in files:
in_path = base / filename
name = str(in_path.relative_to(template.path))
out_path = project_dir / name
if name == "template.toml":
continue
elif name in template.templates:
print("TMPL", out_path)
with in_path.open("r") as fp_in:
with out_path.open("w") as fp_out:
for line in fp_in:
line = line.replace("__PROGNAME", name)
for param in template.parameters:
line = line.replace(f"__{param}", parameters[param])
fp_out.write(line)
else:
print("COPY", out_path)
with in_path.open("rb") as fp_in:
with out_path.open("wb") as fp_out:
while (chunk := fp_in.read(32768)):
fp_out.write(chunk)

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
prompt-toolkit==3.0.36
questionary==2.0.1
wcwidth==0.2.13