hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/init_c
blob: 9e570b5b8fc3f1c9a5afca3122c3cea768296952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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()