From 84fdb908ef5146927f8897c526a611854c40c122 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 29 May 2024 12:25:42 +0300 Subject: [PATCH] Added project generator and some templates --- .gitignore | 1 + _templates/none/template.toml | 1 + _templates/raylib/empty/Makefile | 12 ++++ _templates/raylib/empty/main.c | 12 ++++ _templates/raylib/empty/template.toml | 13 ++++ new.py | 92 +++++++++++++++++++++++++++ requirements.txt | 3 + 7 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 _templates/none/template.toml create mode 100644 _templates/raylib/empty/Makefile create mode 100644 _templates/raylib/empty/main.c create mode 100644 _templates/raylib/empty/template.toml create mode 100644 new.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7275bb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv/ diff --git a/_templates/none/template.toml b/_templates/none/template.toml new file mode 100644 index 0000000..5012556 --- /dev/null +++ b/_templates/none/template.toml @@ -0,0 +1 @@ +name = "No template (empty folder)" diff --git a/_templates/raylib/empty/Makefile b/_templates/raylib/empty/Makefile new file mode 100644 index 0000000..add9fe4 --- /dev/null +++ b/_templates/raylib/empty/Makefile @@ -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 diff --git a/_templates/raylib/empty/main.c b/_templates/raylib/empty/main.c new file mode 100644 index 0000000..2e6f06d --- /dev/null +++ b/_templates/raylib/empty/main.c @@ -0,0 +1,12 @@ +#include + +int main(int argc, char **argv) { + InitWindow(__WINDOW_WIDTH, __WINDOW_HEIGHT, "__PROGNAME"); + + while (!WindowShouldClose()) { + BeginDrawing(); + ClearBackground(BLACK); + DrawText("Hi", 20, 20, 20, WHITE); + EndDrawing(); + } +} diff --git a/_templates/raylib/empty/template.toml b/_templates/raylib/empty/template.toml new file mode 100644 index 0000000..decab55 --- /dev/null +++ b/_templates/raylib/empty/template.toml @@ -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" diff --git a/new.py b/new.py new file mode 100644 index 0000000..faeb4ad --- /dev/null +++ b/new.py @@ -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) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4293088 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +prompt-toolkit==3.0.36 +questionary==2.0.1 +wcwidth==0.2.13