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