blob: c9da1a4cd82dc4aa18bd860ef50ebc9d02bab82e [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
James Kuszmaulcf324122023-01-14 14:07:17 -080032 env = Environment(
33 loader=FileSystemLoader(f"{dirname}/src/generate"),
34 autoescape=False,
35 keep_trailing_newline=True,
36 )
Austin Schuh1e69f942020-11-14 15:06:14 -080037
Austin Schuh812d0d12021-11-04 20:16:48 -070038 template = env.get_template("GenericNumber.java.jinja")
39 rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/math/numbers"
Austin Schuh1e69f942020-11-14 15:06:14 -080040
Austin Schuh812d0d12021-11-04 20:16:48 -070041 for i in range(MAX_NUM + 1):
42 contents = template.render(num=i)
43 output(rootPath, f"N{i}.java", contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080044
Austin Schuh812d0d12021-11-04 20:16:48 -070045 template = env.get_template("Nat.java.jinja")
46 rootPath = f"{cmake_binary_dir}/generated/main/java/edu/wpi/first/math"
47 contents = template.render(nums=range(MAX_NUM + 1))
48 output(rootPath, "Nat.java", contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080049
50
51if __name__ == "__main__":
52 main()