-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkproject
executable file
·128 lines (105 loc) · 3.77 KB
/
mkproject
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
#!/usr/bin/env python3
"""
supported languages:
{languages}
current template:
{templates}
"""
import sys
import os
import json
import shutil
import pathlib
import argparse
import importlib
ROOT = pathlib.Path(__file__).resolve().parent.resolve()
TEMPLATE_DIR = os.path.join(ROOT, "templates")
languages = []
def import_module(rawPath: str):
path = pathlib.Path(rawPath)
moduleName = path.stem
modulePath = path.parent
if modulePath.parent not in sys.path:
sys.path.append(str(modulePath))
return importlib.import_module(moduleName)
def scandir(path: str, *paths: [str]) -> [str]:
path = os.path.join(path, *paths)
result = []
with os.scandir(path) as it:
for entry in it:
if entry.is_dir() and not entry.name.startswith("_"):
result.append(entry.name)
return result
def helptemplate():
""""""
result = ""
for lang in languages:
templates = scandir(TEMPLATE_DIR, lang)
result += " " + lang + ':' + os.linesep
for template in templates:
result += " " + template + os.linesep
if len(templates) == 0:
result += " No template found"
result += os.linesep
return result
def main(args) -> None:
""""""
global languages
# find paths
if args.remote:
projectPath = args.name
# projectPath = args.remote.split('/')[-1]
# if projectPath.endswith(".git"):
# projectPath = projectPath[:-4]
else:
projectPath = args.name
templates = args.template or args.multi
# import build module from template
languageBuilderPath = os.path.join(TEMPLATE_DIR, args.language, args.language + '.py')
builder = import_module(languageBuilderPath)
# init git
if not args.git and not args.remote:
if os.system("git init " + projectPath):
print("error: git init failled")
sys.exit(1)
elif not args.remote:
os.mkdir(projectPath)
elif args.remote:
if os.system("git clone " + args.remote + " " + args.name):
print("error: git clone failled")
sys.exit(1)
# build
builder.build(
name=args.name,
path=projectPath,
template=templates
)
if __name__ == "__main__":
# os.chdir(pathlib.Path(__file__).parent.resolve())
languages = scandir(TEMPLATE_DIR)
parser = argparse.ArgumentParser(
prog="mkproject",
description="Create and init project",
formatter_class=argparse.RawTextHelpFormatter,
epilog=__doc__.format(languages=', '.join(languages), templates=helptemplate())
)
parser.add_argument("name", type=str, help="Project name")
parser.add_argument("language", type=str, help="Language of the project")
parser.add_argument("template", type=str, nargs='?', help="Template")
parser.add_argument("-m", dest="multi", type=str, nargs=2, metavar=('name', 'template'), action="append", help="for nested project")
parser.add_argument("--remote", type=str, help="git url of the project")
parser.add_argument("--no-git", dest="git", action="store_true", help="don't initialize git")
args = parser.parse_args()
if args.multi == None and args.template == None:
parser.error("You must specify either a template or a multi target (see -m)")
if args.language not in languages:
parser.error("Language '" + args.language + "' not found")
templates = scandir(os.path.join(TEMPLATE_DIR, args.language))
if args.template:
if args.template not in templates:
parser.error("Template '" + args.template + "' not found")
if args.multi:
for _, template in args.multi:
if template not in templates:
parser.error("Template '" + template + "' not found")
main(args)