forked from XertroV/tm-editor-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_codegen.py
71 lines (60 loc) · 3.07 KB
/
run_codegen.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
import os
import subprocess
from pathlib import Path
import sys
import click
from termcolor import colored
def path_hca(p1: Path, p2: Path) -> Path:
common = []
for a, b in zip(p1.parts, p2.parts):
if a == b:
common.append(a)
else:
break
return Path(*common)
def find_and_process_toml_files(source_dir, dest_dir, executable):
source_dir_path = Path(source_dir).resolve()
dest_dir_path = Path(dest_dir).resolve()
common_root = path_hca(source_dir_path, dest_dir_path)
for root, dirs, files in os.walk(source_dir):
for file in files:
if file.endswith('.xtoml'):
file_path: Path = (Path(root) / file).resolve()
relative_path = file_path.relative_to(source_dir_path)
output_path = (dest_dir_path / relative_path).with_suffix('.as')
# Ensure the destination directory exists
output_path.parent.mkdir(parents=True, exist_ok=True)
# Run the executable with the current file as an argument
try:
r = subprocess.run([executable, str(file_path)], check=True, stdout=subprocess.PIPE)
except FileNotFoundError as e:
print(colored(f"\nERROR!! Could not find executable {executable}\nException: {e}", 'light_red'))
sys.exit(1)
r.check_returncode()
up_dirs = '../' * (len(output_path.parents) - len(common_root.parents) - 1)
# capture stdout and write to the output file.
output_path.write_text(
# f"/// ! This file is generated from {common_root.relative_to(file_path).relative_to(output_path)} !\n"
# f"/// ! This file is generated from {file_path.relative_to(common_root).resolve().relative_to(output_path)} !\n"
f"/// ! This file is generated from {up_dirs}{file_path.relative_to(common_root)} !\n"
f"/// ! Do not edit this file manually !\n"
f"\n{r.stdout.decode('utf-8')}"
)
print(colored(f"Parsed {file_path} \nGenerated {output_path}", 'light_cyan'))
def main():
source_dir = "path/to/source"
dest_dir = "path/to/destination"
executable = "path/to/executable"
find_and_process_toml_files(source_dir, dest_dir, executable)
@click.command()
@click.argument('source_dir', type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True))
@click.argument('dest_dir', type=click.Path(exists=False, file_okay=False, dir_okay=True))
@click.option('--executable', '-e', help='Path to the executable program', required=True, type=click.Path(exists=False, dir_okay=False, executable=True))
def cli(source_dir, dest_dir, executable):
"""
Processes XTOML files found in SOURCE_DIR using an executable and stores the results in DEST_DIR.
"""
click.echo(f"Processing XTOML files from {source_dir} to {dest_dir} using {executable}")
find_and_process_toml_files(source_dir, dest_dir, executable)
if __name__ == "__main__":
cli()