hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/init_c
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-08-25 10:59:06 +0200
committerhachem <im@hachem.wtf>2026-08-25 10:59:06 +0200
commited6fb2af238776df4d2b707d0ce99121f0dcc2ca (patch)
treefcf9be1181dddd87cb109fa0e582e05c589edc9f /.local/bin/init_c
parent423b2c2d62064057593e504d79b3f749733c611c (diff)
[chore]: update
Diffstat (limited to '.local/bin/init_c')
-rwxr-xr-x.local/bin/init_c168
1 files changed, 168 insertions, 0 deletions
diff --git a/.local/bin/init_c b/.local/bin/init_c
new file mode 100755
index 0000000..9e570b5
--- /dev/null
+++ b/.local/bin/init_c
@@ -0,0 +1,168 @@
+#!/usr/bin/env python3
+
+"""
+init_c - small makefile project generator.
+
+a small dumb script cause i dont want to keep
+writing makefiles for small dumb c projects.
+
+Author: hachem
+Version: 0.1.0
+"""
+
+from pathlib import Path
+import subprocess
+import questionary
+
+
+def ask_project_name():
+ return questionary.text(
+ "Project name:",
+ validate=lambda value: (
+ True
+ if value and not Path(value).exists()
+ else "Project already exists or name is empty."
+ ),
+ ).ask()
+
+
+def generate_makefile(project, use_intermediate):
+ target_dir = "bin"
+
+ if use_intermediate:
+ obj_dir = "bin-int"
+ else:
+ obj_dir = "bin"
+
+ target = f"{target_dir}/{project}"
+
+ return f"""CC := clang
+
+CFLAGS := -Wall -Wextra -Werror -pedantic -std=c2y -Isrc
+LDFLAGS :=
+
+TARGET := {target}
+SRC := $(wildcard src/*.c)
+OBJ := $(SRC:src/%.c={obj_dir}/%.o)
+
+all: $(TARGET)
+
+$(TARGET): $(OBJ)
+\t@mkdir -p {target_dir}
+\t$(CC) $^ -o $@ $(LDFLAGS)
+
+{obj_dir}/%.o: src/%.c
+\t@mkdir -p {obj_dir}
+\t$(CC) $(CFLAGS) -c $< -o $@
+
+clean:
+\trm -rf bin bin-int
+
+.PHONY: all clean
+"""
+
+
+def main():
+ project = ask_project_name()
+
+ if project is None:
+ return
+
+ options = questionary.checkbox(
+ "Options:",
+ choices=[
+ questionary.Choice(
+ "Initialize git",
+ value="git",
+ checked=True,
+ ),
+ questionary.Choice(
+ "Use separate intermediate directory",
+ value="intermediate",
+ ),
+ questionary.Choice(
+ "Ignore clangd files",
+ value="clangd_ignore",
+ checked=True,
+ ),
+ ],
+ ).ask()
+
+ if options is None:
+ return
+
+ use_git = "git" in options
+ use_intermediate = "intermediate" in options
+ use_clangd_ignore = "clangd_ignore" in options
+
+ root = Path(project)
+ root.mkdir()
+ (root / "src").mkdir()
+
+ makefile = generate_makefile(
+ project,
+ use_intermediate,
+ )
+
+ (root / "Makefile").write_text(makefile)
+
+ (root / "src" / "main.c").write_text(
+ """#include <stdio.h>
+
+int main(void)
+{
+ puts("type shi");
+}
+"""
+ )
+
+ ignored = []
+
+ if use_intermediate:
+ ignored.extend([
+ "bin/",
+ "bin-int/",
+ ])
+ else:
+ ignored.append("bin/")
+
+ if use_clangd_ignore:
+ ignored.extend([
+ "",
+ ".clangd",
+ "compile_flags.txt",
+ "compile_commands.json",
+ ])
+
+ if ignored:
+ (root / ".gitignore").write_text(
+ "\n".join(ignored) + "\n"
+ )
+
+ if use_git:
+ try:
+ subprocess.run(
+ ["git", "init"],
+ cwd=root,
+ check=True,
+ stdout=subprocess.DEVNULL,
+ )
+ except FileNotFoundError:
+ # legit how bro
+ print("Warning: git is not installed.")
+
+ print()
+ print(f"✔ Created {project}")
+ print()
+
+ try:
+ subprocess.run(
+ ["tree", "-a", "--dirsfirst", "-I", ".git", project],
+ check=False,
+ )
+ except FileNotFoundError:
+ print("(tree is not installed)")
+
+
+if __name__ == "__main__":
+ main() \ No newline at end of file