/
create.py
executable file
·71 lines (48 loc) · 1.42 KB
/
create.py
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
#!/bin/env python3
"""create boilerplate for your c++ project
Usage:
./create.py [-h] [--parent=<parent>] [--namespace=<ns>] <path>
./create.py (-h | --help)
Options:
-n , --namespace <ns> namespace to use [default: ft]
-p , --parent <parent> root of directory path [default: containers]
"""
from pathlib import Path
from sys import argv
from docopt import docopt
template = """\
#ifndef {name}{ext}
#define {name}{ext}
{include}
namespace {namespace} {{
}} // namespace {namespace}
#endif // {name}{ext}
"""
def base_category_name(stem: str) -> str:
return stem.rsplit("_", maxsplit=1)[0]
def get_include(path: Path) -> str:
if path.suffix == ".tpp":
return f'#include "{base_category_name(path.stem)}.hpp"'
return ""
def main():
assert __doc__ is not None
args: dict[str, str] = docopt(__doc__)
# print(args)
path = Path(args["<path>"])
name = f"{path.parent.stem}_{path.stem}"
ext = path.suffix.replace(".", "_")
include = get_include(path)
text = template.format(
name=name.upper(),
ext=ext.upper(),
namespace=args["--namespace"],
include=include,
)
fullpath = Path(__file__).parent / args["--parent"] / args["<path>"]
fullpath.parent.mkdir(parents=True, exist_ok=True)
if not fullpath.exists():
fullpath.write_text(text)
else:
print("file already exists")
if __name__ == "__main__":
main()