#!/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 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()