blob: 2aeb45a56d81ca852a7100cfb9d0f55e3ca3d2dc [file] [log] [blame]
Maxwell Henderson80bec322024-01-09 15:48:44 -08001#!/usr/bin/env python3
2
Austin Schuh812d0d12021-11-04 20:16:48 -07003# Copyright (c) FIRST and other WPILib contributors.
4# Open Source Software; you can modify and/or share it under the terms of
5# the WPILib BSD license file in the root directory of this project.
6
Austin Schuh1e69f942020-11-14 15:06:14 -08007import os
8import sys
Austin Schuh812d0d12021-11-04 20:16:48 -07009from jinja2 import Environment, FileSystemLoader
10
11
12def output(outPath, outfn, contents):
13 if not os.path.exists(outPath):
14 os.makedirs(outPath)
15
16 outpathname = f"{outPath}/{outfn}"
17
18 if os.path.exists(outpathname):
19 with open(outpathname, "r") as f:
20 if f.read() == contents:
21 return
22
23 # File either doesn't exist or has different contents
Maxwell Henderson80bec322024-01-09 15:48:44 -080024 with open(outpathname, "w", newline="\n") as f:
Austin Schuh812d0d12021-11-04 20:16:48 -070025 f.write(contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080026
27
28def main():
29 MAX_NUM = 20
30
31 dirname, _ = os.path.split(os.path.abspath(__file__))
Austin Schuh1e69f942020-11-14 15:06:14 -080032
James Kuszmaulcf324122023-01-14 14:07:17 -080033 env = Environment(
Maxwell Henderson80bec322024-01-09 15:48:44 -080034 loader=FileSystemLoader(f"{dirname}/src/generate/main/java"),
James Kuszmaulcf324122023-01-14 14:07:17 -080035 autoescape=False,
36 keep_trailing_newline=True,
37 )
Austin Schuh1e69f942020-11-14 15:06:14 -080038
Austin Schuh812d0d12021-11-04 20:16:48 -070039 template = env.get_template("GenericNumber.java.jinja")
Maxwell Henderson80bec322024-01-09 15:48:44 -080040 rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math/numbers"
Austin Schuh1e69f942020-11-14 15:06:14 -080041
Austin Schuh812d0d12021-11-04 20:16:48 -070042 for i in range(MAX_NUM + 1):
43 contents = template.render(num=i)
44 output(rootPath, f"N{i}.java", contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080045
Austin Schuh812d0d12021-11-04 20:16:48 -070046 template = env.get_template("Nat.java.jinja")
Maxwell Henderson80bec322024-01-09 15:48:44 -080047 rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math"
Austin Schuh812d0d12021-11-04 20:16:48 -070048 contents = template.render(nums=range(MAX_NUM + 1))
49 output(rootPath, "Nat.java", contents)
Austin Schuh1e69f942020-11-14 15:06:14 -080050
51
52if __name__ == "__main__":
53 main()