-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_gen_project.py
50 lines (42 loc) · 1.27 KB
/
post_gen_project.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
import subprocess
import sys
# Ansi color codes
RED = '\033[0;31m'
GRAY = '\033[0;37m'
RESET = '\033[0m'
def run_and_output(args) -> None:
"""
Run a command and print its standard error.
:param args: The arguments used to launch the process.
This may be a list or a string.
:return: nothing
"""
result: subprocess.CompletedProcess = subprocess.run(
args, capture_output=True, text=True
)
print(f"{GRAY}{result.stderr}{RESET}", file=sys.stderr)
result.check_returncode()
def main():
commands = [
["bundler", "install"],
["git", "init"],
["git", "add", "-A"],
["git", "commit", "-m", "Initial commit"]
]
n = len(commands)
print()
for index, command in enumerate(commands):
print(f"Post-generate hook {index + 1}/{n}: {' '.join(command)}")
try:
run_and_output(command)
except FileNotFoundError:
raise SystemExit(
f"{RED}Requirement {command[0]} not met. Stopping.{RESET}"
)
except subprocess.CalledProcessError:
raise SystemExit(
f"{RED}Post-generate hook failed. Stopping.{RESET}"
)
print("Successfully ran post-generate hooks.")
if __name__ == "__main__":
main()