blob: 61cc02a7edc467d619c009f38140ebf1f2b793e9 [file] [log] [blame]
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08001import os
2import shutil
3import sys
4
5MAX_NUM = 20
6
7dirname, _ = os.path.split(os.path.abspath(__file__))
8cmake_binary_dir = sys.argv[1]
9
10with open(f"{dirname}/src/generate/GenericNumber.java.in", "r") as templateFile:
11 template = templateFile.read()
12 rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/wpiutil/math/numbers"
13
14 if os.path.exists(rootPath):
15 shutil.rmtree(rootPath)
16 os.makedirs(rootPath)
17
18 for i in range(MAX_NUM + 1):
19 with open(f"{rootPath}/N{i}.java", "w") as f:
20 f.write(template.replace("${num}", str(i)))
21
22with open(f"{dirname}/src/generate/Nat.java.in", "r") as templateFile:
23 template = templateFile.read()
24 outputPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/wpiutil/math/Nat.java"
25 with open(f"{dirname}/src/generate/NatGetter.java.in", "r") as getterFile:
26 getter = getterFile.read()
27
28 if os.path.exists(outputPath):
29 os.remove(outputPath)
30
31 importsString = ""
32
33 for i in range(MAX_NUM + 1):
34 importsString += f"import edu.wpi.first.wpiutil.math.numbers.N{i};\n"
35 template += getter.replace("${num}", str(i))
36
37 template += "}\n"
38
39 template = template.replace('{{REPLACEWITHIMPORTS}}', importsString)
40
41 with open(outputPath, "w") as f:
42 f.write(template)