-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_new_project
executable file
·80 lines (56 loc) · 2.47 KB
/
create_new_project
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
#!/usr/bin/env python3
import os
from argparse import ArgumentParser
from pathlib import Path
from typing import Optional
from jinja2 import Template
BACKEND_TOOL_OPTIONS = {
"httpie": [
"--pretty=all",
],
}
def create_project(project_name: str) -> Path:
project_path = Path.joinpath(Path.cwd(), project_name)
if not project_path.exists():
print(f"\nCreating folder for new project: {project_name}\n")
os.mkdir(project_name)
os.mkdir(Path().joinpath(project_name, "routes"))
else:
print(f"\nProject folder has already been created: {project_path}\n")
return project_path
def read_file(file_path: str) -> bytes:
with open(file_path, mode="rb") as fs:
return fs.read()
def write_file(file_path: Path, content: str) -> None:
with open(file_path, "w+") as f:
f.write(content)
def render_template(file_path: str, render_variables: dict) -> Optional[str]:
template_content = read_file(file_path=file_path)
template_object = Template(template_content.decode("utf-8"))
return template_object.render(**render_variables)
def copy_templates(env_variables: dict, project_path: str) -> None:
rendered_local_ain = render_template("templates/local.ain.j2", render_variables=env_variables)
local_ain_path = Path().joinpath(project_path, "local.ain")
write_file(local_ain_path, str(rendered_local_ain))
print(f"\nCreated: {local_ain_path} from template\n")
def create_cli_arguments():
parser = ArgumentParser()
parser.add_argument("--project_name", help="The name of the project you want to create")
parser.add_argument("--dev_port", help="The port used for the dev template", default=8000)
parser.add_argument("--tool", help="You can choose between: curl, wget, httpie", default="httpie")
return parser
def validate_input(arguments):
backend_tool = arguments.tool
if backend_tool not in ["httpie", "curl", "wget"]:
raise RuntimeError("ain supports \"httpie\", \"curl\", and \"wget\"")
if __name__ == "__main__":
arguments = create_cli_arguments().parse_args()
validate_input(arguments=arguments)
project_path = create_project(project_name=arguments.project_name)
backend_tool = arguments.tool
environment_varables = {
"backend_tool": arguments.tool,
"backend_options": BACKEND_TOOL_OPTIONS[backend_tool],
"http_port": arguments.dev_port,
}
copy_templates(env_variables=environment_varables, project_path=project_path)