Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 3 | import glob |
| 4 | import os |
| 5 | import sys |
| 6 | from jinja2 import Environment, FileSystemLoader |
| 7 | import json |
| 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 |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 22 | with open(outpathname, "w", newline="\n") as f: |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 23 | f.write(contents) |
| 24 | |
| 25 | |
| 26 | def main(): |
| 27 | dirname, _ = os.path.split(os.path.abspath(__file__)) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 28 | |
| 29 | with open(f"{dirname}/src/generate/types.json") as f: |
| 30 | types = json.load(f) |
| 31 | |
| 32 | # Java files |
| 33 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 34 | loader=FileSystemLoader(f"{dirname}/src/generate/main/java"), autoescape=False |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 35 | ) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 36 | rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/networktables" |
| 37 | for fn in glob.glob(f"{dirname}/src/generate/main/java/*.jinja"): |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 38 | template = env.get_template(os.path.basename(fn)) |
| 39 | outfn = os.path.basename(fn)[:-6] # drop ".jinja" |
| 40 | if os.path.basename(fn).startswith("NetworkTable") or os.path.basename( |
| 41 | fn |
| 42 | ).startswith("Generic"): |
| 43 | output = template.render(types=types) |
| 44 | Output(rootPath, outfn, output) |
| 45 | else: |
| 46 | for replacements in types: |
| 47 | output = template.render(replacements) |
| 48 | if outfn == "Timestamped.java": |
| 49 | outfn2 = f"Timestamped{replacements['TypeName']}.java" |
| 50 | else: |
| 51 | outfn2 = f"{replacements['TypeName']}{outfn}" |
| 52 | Output(rootPath, outfn2, output) |
| 53 | |
| 54 | # C++ classes |
| 55 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 56 | loader=FileSystemLoader( |
| 57 | f"{dirname}/src/generate/main/native/include/networktables" |
| 58 | ), |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 59 | autoescape=False, |
| 60 | ) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 61 | rootPath = f"{dirname}/src/generated/main/native/include/networktables" |
| 62 | for fn in glob.glob( |
| 63 | f"{dirname}/src/generate/main/native/include/networktables/*.jinja" |
| 64 | ): |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 65 | template = env.get_template(os.path.basename(fn)) |
| 66 | outfn = os.path.basename(fn)[:-6] # drop ".jinja" |
| 67 | for replacements in types: |
| 68 | output = template.render(replacements) |
| 69 | outfn2 = f"{replacements['TypeName']}{outfn}" |
| 70 | Output(rootPath, outfn2, output) |
| 71 | |
| 72 | # C++ handle API (header) |
| 73 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 74 | loader=FileSystemLoader(f"{dirname}/src/generate/main/native/include"), |
| 75 | autoescape=False, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 76 | ) |
| 77 | template = env.get_template("ntcore_cpp_types.h.jinja") |
| 78 | output = template.render(types=types) |
| 79 | Output( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 80 | f"{dirname}/src/generated/main/native/include", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 81 | "ntcore_cpp_types.h", |
| 82 | output, |
| 83 | ) |
| 84 | |
| 85 | # C++ handle API (source) |
| 86 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 87 | loader=FileSystemLoader(f"{dirname}/src/generate/main/native/cpp"), |
| 88 | autoescape=False, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 89 | ) |
| 90 | template = env.get_template("ntcore_cpp_types.cpp.jinja") |
| 91 | output = template.render(types=types) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 92 | Output(f"{dirname}/src/generated/main/native/cpp", "ntcore_cpp_types.cpp", output) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 93 | |
| 94 | # C handle API (header) |
| 95 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 96 | loader=FileSystemLoader(f"{dirname}/src/generate/main/native/include"), |
| 97 | autoescape=False, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 98 | ) |
| 99 | template = env.get_template("ntcore_c_types.h.jinja") |
| 100 | output = template.render(types=types) |
| 101 | Output( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 102 | f"{dirname}/src/generated/main/native/include", |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 103 | "ntcore_c_types.h", |
| 104 | output, |
| 105 | ) |
| 106 | |
| 107 | # C handle API (source) |
| 108 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 109 | loader=FileSystemLoader(f"{dirname}/src/generate/main/native/cpp"), |
| 110 | autoescape=False, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 111 | ) |
| 112 | template = env.get_template("ntcore_c_types.cpp.jinja") |
| 113 | output = template.render(types=types) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 114 | Output(f"{dirname}/src/generated/main/native/cpp", "ntcore_c_types.cpp", output) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 115 | |
| 116 | # JNI |
| 117 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 118 | loader=FileSystemLoader(f"{dirname}/src/generate/main/native/cpp/jni"), |
| 119 | autoescape=False, |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 120 | ) |
| 121 | template = env.get_template("types_jni.cpp.jinja") |
| 122 | output = template.render(types=types) |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame^] | 123 | Output(f"{dirname}/src/generated/main/native/cpp/jni", "types_jni.cpp", output) |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 124 | |
| 125 | |
| 126 | if __name__ == "__main__": |
| 127 | main() |