-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmlpx
315 lines (240 loc) · 8.99 KB
/
mlpx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/python
import os
import sys
import optparse
from typing import List
quiet = False
msg_enabled = True
color_enabled = True
ml_path = '.'
ml_flags = '-C'
c_flags = '-g -O2'
c_compiler_path = 'gcc'
ml_compiler_path = f'python3 {os.path.join(ml_path, "src/Main.py")}'
c_include_path = os.path.join(ml_path, 'skel/include')
ml_include_path = os.path.join(ml_path, 'include')
proj_name = 'main'
build_dir = 'bin'
source_dir = '.'
build_opt = 'default'
source = ''
class Color:
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
WARNING = '\033[35m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def c_suff(s): return add_suffix(s, 'c')
def ml_suff(s): return add_suffix(s, 'ml')
def build_clean(args: List[str]):
def clean(arg: str):
print_ok(f'Cleaning {arg}')
if os.path.exists(arg):
execute(f'rm -vrf {arg}')
if len(args) == 0:
clean(build_dir)
return
for arg in args:
clean(arg)
def find_err_args(args: List[str]) -> List[str]:
err_args = []
for arg in args:
if not os.path.exists(arg):
err_args.append(arg)
return err_args
def build_default(args: List[str]):
print_ok(f'Running default option on {source_dir}')
ml_cmd = f'{ml_compiler_path} {ml_flags} -I {source_dir} -I {ml_include_path} -o {os.path.join(build_dir, c_suff(proj_name))} {" ".join(args)}'
c_cmd = f'{c_compiler_path} {c_flags} {os.path.join(c_include_path, "*.c")} -I {c_include_path} {os.path.join(build_dir, c_suff(proj_name))} -o {os.path.join(build_dir, proj_name)}'
if not os.path.exists(build_dir):
os.makedirs(build_dir)
execute(ml_cmd)
execute(c_cmd)
def build_debug(args: List[str]):
print_ok(f'Running debug option on {source_dir}')
cmd = f'{ml_compiler_path} -d -I {source_dir} -I {ml_include_path} {" ".join(args)}'
execute(cmd)
def build_cdebug(args: List[str]):
print_ok(f'Running cdebug option on {source_dir}')
cmd = f'{ml_compiler_path} -I {source_dir} -I {ml_include_path} {" ".join(args)}'
execute(cmd)
OPTION_MAP = {
'clean': build_clean,
'default': build_default,
'debug': build_debug,
'cdebug': build_cdebug,
'def': build_default,
'dbg': build_debug,
'cdbg': build_cdebug,
}
def color_str(color: Color, msg: str):
if not color_enabled:
return msg
return f'{color}{msg}{Color.ENDC}'
def print_ok(msg: str):
if not msg_enabled:
return
print(color_str(Color.GREEN, msg),
file=sys.stderr)
def print_warning(msg: str):
print(f'{color_str(Color.WARNING, f"WARNING: {msg}")}',
file=sys.stderr)
exit(1)
def print_error(msg: str):
print(f'{color_str(Color.FAIL, f"ERROR: {msg}")}',
file=sys.stderr)
exit(1)
def execute(cmd: str):
if not quiet:
print(cmd)
os.system(cmd)
def add_suffix(source: str, suff: str) -> str:
return f'{source}.{suff}'
def handle_init(args: List[str]):
def init(arg: str):
print_ok(f'Initializing {arg}')
if os.path.exists(arg):
print_warning(f'Directory {arg} already exists, skipping...')
else:
ml_dir = os.path.relpath(ml_path, arg)
src_dir = os.path.join(
os.path.join(ml_path, "skel"), "src")
execute(f'mkdir -p {arg}')
execute(f'cp -vr {src_dir} {arg}')
execute('echo -e \"' + '\\n'.join([
'#!/bin/bash',
f'{os.path.join(ml_dir, "mlpx")} -p {ml_dir} \$@']) + f'\" > {os.path.join(arg, "mlpx")}')
execute(f'chmod +x {os.path.join(arg, "mlpx")}')
if len(args) == 0:
args = [proj_name]
for arg in args:
init(arg)
def handle_init_makefile(args: List[str]):
def init(arg: str):
print_ok(f'Initializing {arg}')
if os.path.exists(arg):
print_warning(f'Directory {arg} already exists, skipping...')
else:
execute(f'mkdir {arg}')
execute(f'cp -vr {os.path.join(ml_path, "skel")}/* {arg}')
if len(args) == 0:
args = [proj_name]
for arg in args:
init(arg)
def handle_build(args: List[str]):
if build_opt not in OPTION_MAP:
print_error(f'Invalid option {build_opt}.')
build_args = args if len(args) > 0 else [build_opt]
args = [source]
err_args = find_err_args(args)
if len(err_args):
print_error(
f'Source files {", ".join(map(repr,err_args))} do not exist.')
for arg in build_args:
if arg not in OPTION_MAP:
print_error(f'Invalid option {arg}.')
OPTION_MAP.get(arg)(args)
def handle_run(args: List[str]):
if len(args) > 0:
print_error('Command run doesn not accept any arguments.')
target = os.path.join(build_dir, proj_name)
if not os.path.exists(target):
print_error(f'Build failed, {target} does not exist.')
print_ok(f'Running {target}')
execute(target)
def handle_clean(args: List[str]):
build_clean(args)
def handle_rclean(args: List[str]):
# Iterate over all items in the directory
if len(args) == 0:
print_warning('No directory specified')
return
for arg in args:
if not os.path.isdir(arg):
print_warning(f'Directory {arg} does not exist, skipping...')
continue
for dir in os.listdir(arg):
item_path = os.path.join(arg,
os.path.join(dir, build_dir))
# If the item is a directory, recursively call the function
if os.path.isdir(item_path):
handle_rclean([item_path])
handle_clean([item_path])
def handle_cmd(parts: List[str]):
cmd = parts[0]
args = parts[1:]
if cmd == 'run':
handle_run(args)
elif cmd == 'build':
handle_build(args)
elif cmd == 'rclean':
handle_rclean(args)
elif cmd == 'clean':
handle_clean(args)
elif cmd == 'init':
handle_init(args)
elif cmd == 'init-makefile':
handle_init_makefile(args)
else:
print_error(f'Invalid request: {cmd}')
def handle_cmd_list(args: List[str]):
parts = []
for arg in args:
if arg == 'and':
handle_cmd(parts)
parts.clear()
else:
parts.append(arg)
if len(parts) > 0:
handle_cmd(parts)
else:
# Defaults to build and run for convenience
handle_build([])
handle_run([])
if __name__ == '__main__':
desc = ', '.join(['The mini language project manager extension',
'Version: 1.0.0',
'Source: https://github.com/NICUP14/MiniLang.git'])
parser = optparse.OptionParser(description=desc)
parser.add_option('-C', '--proj-path', default=source_dir,
help='Specify the project path (default=".").')
parser.add_option('-p', '--path', default=ml_path,
help='Specify the MiniLang project path (default=".").')
parser.add_option('-b', '--build-dir', default=build_dir,
help='Specify the build directory (default="bin").')
parser.add_option('-o', '--build-opt', default=build_opt,
help=f'Specify the build option; Choose between: {", ".join(OPTION_MAP.keys())} (default="default").')
parser.add_option('-n', '--name', default=proj_name,
help='Specify the project\'s name (default="main").')
parser.add_option('-i', '--include', default=ml_include_path,
help='Speficy the MiniLang compiler include path (default="include").')
parser.add_option('-I', '--c-include', default=c_include_path,
help='Specify the C compiler include path (default="skel/include").')
parser.add_option('-q', '--quiet', action='store_true',
help='Run in quiet mode; Does not show compile commands. Overrides "-m".')
parser.add_option('-m', '--no-msg', action='store_true',
help='Run in script mode; Does not show mlpx logs.')
parser.add_option('-c', '--no-color', action='store_true',
help='Run in text mode; Does not use colors.')
values, args = parser.parse_args()
values_dict = vars(values)
quiet = values_dict.get('quiet')
msg_enabled = not quiet and not values_dict.get('no_msg')
color_enabled = not values_dict.get('no_color')
ml_path = values_dict.get('path')
c_include_path = values_dict.get('c_include')
ml_include_path = values_dict.get('include')
proj_name = values_dict.get('name')
build_dir = values_dict.get('build_dir')
build_opt = values_dict.get('build_opt')
source_dir = values_dict.get('proj_path')
build_dir = os.path.join(source_dir, build_dir)
source = os.path.join(source_dir, 'src/main.ml')
ml_compiler_path = f'python3 {os.path.join(ml_path, "src/Main.py")}'
c_include_path = os.path.join(ml_path, 'skel/include')
ml_include_path = os.path.join(ml_path, 'include')
handle_cmd_list(args)
print_ok('Done!')