James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 1 | import os |
| 2 | import shutil |
| 3 | import sys |
| 4 | |
| 5 | MAX_NUM = 20 |
| 6 | |
| 7 | dirname, _ = os.path.split(os.path.abspath(__file__)) |
| 8 | cmake_binary_dir = sys.argv[1] |
| 9 | |
| 10 | with 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 | |
| 22 | with 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) |