Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | # Copyright (c) FIRST and other WPILib contributors. |
| 2 | # Open Source Software; you can modify and/or share it under the terms of |
| 3 | # the WPILib BSD license file in the root directory of this project. |
| 4 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 5 | import os |
| 6 | import sys |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | from jinja2 import Environment, FileSystemLoader |
| 8 | |
| 9 | |
| 10 | def output(outPath, outfn, contents): |
| 11 | if not os.path.exists(outPath): |
| 12 | os.makedirs(outPath) |
| 13 | |
| 14 | outpathname = f"{outPath}/{outfn}" |
| 15 | |
| 16 | if os.path.exists(outpathname): |
| 17 | with open(outpathname, "r") as f: |
| 18 | if f.read() == contents: |
| 19 | return |
| 20 | |
| 21 | # File either doesn't exist or has different contents |
| 22 | with open(outpathname, "w") as f: |
| 23 | f.write(contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def main(): |
| 27 | MAX_NUM = 20 |
| 28 | |
| 29 | dirname, _ = os.path.split(os.path.abspath(__file__)) |
| 30 | cmake_binary_dir = sys.argv[1] |
| 31 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 32 | env = Environment(loader=FileSystemLoader(f"{dirname}/src/generate"), |
| 33 | autoescape=False, |
| 34 | keep_trailing_newline=True) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 36 | template = env.get_template("GenericNumber.java.jinja") |
| 37 | rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/math/numbers" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 38 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 39 | for i in range(MAX_NUM + 1): |
| 40 | contents = template.render(num=i) |
| 41 | output(rootPath, f"N{i}.java", contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | template = env.get_template("Nat.java.jinja") |
| 44 | rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/math" |
| 45 | contents = template.render(nums=range(MAX_NUM + 1)) |
| 46 | output(rootPath, "Nat.java", contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | main() |