blob: c52ddb4fd390138c9b5bbab9a6e899c1f0851550 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001# 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 Schuh1e69f942020-11-14 15:06:14 -08005import os
6import sys
Austin Schuh812d0d12021-11-04 20:16:48 -07007from jinja2 import Environment, FileSystemLoader
8
9
10def 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 Schuh1e69f942020-11-14 15:06:14 -080024
25
26def main():
27 MAX_NUM = 20
28
29 dirname, _ = os.path.split(os.path.abspath(__file__))
30 cmake_binary_dir = sys.argv[1]
31
Austin Schuh812d0d12021-11-04 20:16:48 -070032 env = Environment(loader=FileSystemLoader(f"{dirname}/src/generate"),
33 autoescape=False,
34 keep_trailing_newline=True)
Austin Schuh1e69f942020-11-14 15:06:14 -080035
Austin Schuh812d0d12021-11-04 20:16:48 -070036 template = env.get_template("GenericNumber.java.jinja")
37 rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/math/numbers"
Austin Schuh1e69f942020-11-14 15:06:14 -080038
Austin Schuh812d0d12021-11-04 20:16:48 -070039 for i in range(MAX_NUM + 1):
40 contents = template.render(num=i)
41 output(rootPath, f"N{i}.java", contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080042
Austin Schuh812d0d12021-11-04 20:16:48 -070043 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 Schuh1e69f942020-11-14 15:06:14 -080047
48
49if __name__ == "__main__":
50 main()