mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[build] Use pathlib in pre-generation scripts (#6745)
This commit is contained in:
@@ -4,50 +4,61 @@
|
||||
# Open Source Software; you can modify and/or share it under the terms of
|
||||
# the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def output(outPath, outfn, contents):
|
||||
if not os.path.exists(outPath):
|
||||
os.makedirs(outPath)
|
||||
|
||||
outpathname = f"{outPath}/{outfn}"
|
||||
|
||||
if os.path.exists(outpathname):
|
||||
with open(outpathname, "r") as f:
|
||||
if f.read() == contents:
|
||||
return
|
||||
|
||||
# File either doesn't exist or has different contents
|
||||
with open(outpathname, "w", newline="\n") as f:
|
||||
f.write(contents)
|
||||
def output(output_dir: Path, outfn: str, contents: str):
|
||||
output_dir.mkdir(parents=True, exist_ok=True)
|
||||
output_file = output_dir / outfn
|
||||
output_file.write_text(contents, encoding="utf-8")
|
||||
|
||||
|
||||
def main():
|
||||
def generate_numbers(output_directory: Path, template_root: Path):
|
||||
MAX_NUM = 20
|
||||
|
||||
dirname, _ = os.path.split(os.path.abspath(__file__))
|
||||
|
||||
env = Environment(
|
||||
loader=FileSystemLoader(f"{dirname}/src/generate/main/java"),
|
||||
loader=FileSystemLoader(template_root / "main/java"),
|
||||
autoescape=False,
|
||||
keep_trailing_newline=True,
|
||||
)
|
||||
|
||||
template = env.get_template("GenericNumber.java.jinja")
|
||||
rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math/numbers"
|
||||
rootPath = output_directory / "main/java/edu/wpi/first/math/numbers"
|
||||
|
||||
for i in range(MAX_NUM + 1):
|
||||
contents = template.render(num=i)
|
||||
output(rootPath, f"N{i}.java", contents)
|
||||
|
||||
template = env.get_template("Nat.java.jinja")
|
||||
rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math"
|
||||
rootPath = output_directory / "main/java/edu/wpi/first/math"
|
||||
contents = template.render(nums=range(MAX_NUM + 1))
|
||||
output(rootPath, "Nat.java", contents)
|
||||
|
||||
|
||||
def main(argv):
|
||||
script_path = Path(__file__).resolve()
|
||||
dirname = script_path.parent
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--output_directory",
|
||||
help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script",
|
||||
default=dirname / "src/generated",
|
||||
type=Path,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--template_root",
|
||||
help="Optional. If set, will use this directory as the root for the jinja templates",
|
||||
default=dirname / "src/generate",
|
||||
type=Path,
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
generate_numbers(args.output_directory, args.template_root)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main(sys.argv[1:])
|
||||
|
||||
@@ -3,31 +3,73 @@
|
||||
# Copyright (c) FIRST and other WPILib contributors.
|
||||
# Open Source Software; you can modify and/or share it under the terms of
|
||||
# the WPILib BSD license file in the root directory of this project.
|
||||
import os.path
|
||||
import subprocess
|
||||
import sys
|
||||
from glob import glob
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
if __name__ == "__main__":
|
||||
proto_files = glob("wpimath/src/main/proto/*.proto")
|
||||
|
||||
def generate_quickbuf(
|
||||
protoc, quickbuf_plugin: Path, output_directory: Path, proto_dir: Path
|
||||
):
|
||||
proto_files = proto_dir.glob("*.proto")
|
||||
for path in proto_files:
|
||||
absolute_filename = os.path.abspath(path)
|
||||
absolute_dir, filename = os.path.split(absolute_filename)
|
||||
absolute_filename = path.absolute()
|
||||
subprocess.run(
|
||||
[
|
||||
sys.argv[1],
|
||||
f"--plugin=protoc-gen-quickbuf={sys.argv[2]}",
|
||||
f"--quickbuf_out=gen_descriptors=true:{os.path.abspath('./wpimath/src/generated/main/java')}",
|
||||
f"-I{absolute_dir}",
|
||||
protoc,
|
||||
f"--plugin=protoc-gen-quickbuf={quickbuf_plugin}",
|
||||
f"--quickbuf_out=gen_descriptors=true:{output_directory.absolute()}",
|
||||
f"-I{absolute_filename.parent}",
|
||||
absolute_filename,
|
||||
]
|
||||
)
|
||||
java_files = glob("wpimath/src/generated/main/java/edu/wpi/first/math/proto/*.java")
|
||||
java_files = (output_directory / "edu/wpi/first/math/proto").glob("*.java")
|
||||
for java_file in java_files:
|
||||
with open(java_file) as file:
|
||||
content = file.read()
|
||||
with open(java_file, "tw") as file:
|
||||
file.write(
|
||||
"// Copyright (c) FIRST and other WPILib contributors.\n// Open Source Software; you can modify and/or share it under the terms of\n// the WPILib BSD license file in the root directory of this project.\n"
|
||||
+ content
|
||||
)
|
||||
with (java_file).open(encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
java_file.write_text(
|
||||
"// Copyright (c) FIRST and other WPILib contributors.\n// Open Source Software; you can modify and/or share it under the terms of\n// the WPILib BSD license file in the root directory of this project.\n"
|
||||
+ content,
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def main(argv):
|
||||
script_path = Path(__file__).resolve()
|
||||
dirname = script_path.parent
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--protoc",
|
||||
help="Protoc executable command",
|
||||
default="protoc",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--quickbuf_plugin",
|
||||
help="Path to the quickbuf protoc plugin",
|
||||
required=True,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output_directory",
|
||||
help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script",
|
||||
default=dirname / "src/generated/main/java",
|
||||
type=Path,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--proto_directory",
|
||||
help="Optional. If set, will use this directory to glob for protobuf files",
|
||||
default=dirname / "src/main/proto",
|
||||
type=Path,
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
generate_quickbuf(
|
||||
args.protoc, args.quickbuf_plugin, args.output_directory, args.proto_directory
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user