Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame^] | 1 | import os |
| 2 | import sys |
| 3 | |
| 4 | |
| 5 | def main(): |
| 6 | MAX_NUM = 20 |
| 7 | |
| 8 | dirname, _ = os.path.split(os.path.abspath(__file__)) |
| 9 | cmake_binary_dir = sys.argv[1] |
| 10 | |
| 11 | with open(f"{dirname}/src/generate/GenericNumber.java.in", |
| 12 | "r") as templateFile: |
| 13 | template = templateFile.read() |
| 14 | rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/wpiutil/math/numbers" |
| 15 | |
| 16 | if not os.path.exists(rootPath): |
| 17 | os.makedirs(rootPath) |
| 18 | |
| 19 | for i in range(MAX_NUM + 1): |
| 20 | new_contents = template.replace("${num}", str(i)) |
| 21 | |
| 22 | if os.path.exists(f"{rootPath}/N{i}.java"): |
| 23 | with open(f"{rootPath}/N{i}.java", "r") as f: |
| 24 | if f.read() == new_contents: |
| 25 | continue |
| 26 | |
| 27 | # File either doesn't exist or has different contents |
| 28 | with open(f"{rootPath}/N{i}.java", "w") as f: |
| 29 | f.write(new_contents) |
| 30 | |
| 31 | with open(f"{dirname}/src/generate/Nat.java.in", "r") as templateFile: |
| 32 | template = templateFile.read() |
| 33 | outputPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/wpiutil/math/Nat.java" |
| 34 | with open(f"{dirname}/src/generate/NatGetter.java.in", |
| 35 | "r") as getterFile: |
| 36 | getter = getterFile.read() |
| 37 | |
| 38 | importsString = "" |
| 39 | |
| 40 | for i in range(MAX_NUM + 1): |
| 41 | importsString += f"import edu.wpi.first.wpiutil.math.numbers.N{i};\n" |
| 42 | template += getter.replace("${num}", str(i)) |
| 43 | |
| 44 | template += "}\n" |
| 45 | |
| 46 | template = template.replace('{{REPLACEWITHIMPORTS}}', importsString) |
| 47 | |
| 48 | if os.path.exists(outputPath): |
| 49 | with open(outputPath, "r") as f: |
| 50 | if f.read() == template: |
| 51 | return 0 |
| 52 | |
| 53 | # File either doesn't exist or has different contents |
| 54 | with open(outputPath, "w") as f: |
| 55 | f.write(template) |
| 56 | |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | main() |