[build] Use pathlib in pre-generation scripts (#6745)

This commit is contained in:
PJ Reiniger
2024-06-18 10:40:37 -04:00
committed by GitHub
parent e884221a8d
commit 66c0abb732
8 changed files with 374 additions and 230 deletions

View File

@@ -3,22 +3,23 @@
# 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 pathlib
from pathlib import Path
import sys
import argparse
def main():
def generate_usage_reporting(output_directory: Path, template_directory: Path):
# Gets the folder this script is in (the hal/ directory)
HAL_ROOT = pathlib.Path(__file__).parent
java_package = "edu/wpi/first/hal"
# fmt: off
(HAL_ROOT / "src/generated/main/native/include/hal").mkdir(parents=True, exist_ok=True)
(HAL_ROOT / f"src/generated/main/java/{java_package}").mkdir(parents=True, exist_ok=True)
(output_directory / "main/native/include/hal").mkdir(parents=True, exist_ok=True)
(output_directory / f"main/java/{java_package}").mkdir(parents=True, exist_ok=True)
# fmt: on
usage_reporting_types_cpp = []
usage_reporting_instances_cpp = []
usage_reporting_types = []
usage_reporting_instances = []
with open(HAL_ROOT / "src/generate/Instances.txt") as instances:
with (template_directory / "Instances.txt").open(encoding="utf-8") as instances:
for instance in instances:
usage_reporting_instances_cpp.append(f" {instance.strip()},")
usage_reporting_instances.append(
@@ -26,7 +27,9 @@ def main():
f" public static final int {instance.strip()};"
)
with open(HAL_ROOT / "src/generate/ResourceType.txt") as resource_types:
with (template_directory / "ResourceType.txt").open(
encoding="utf-8"
) as resource_types:
for resource_type in resource_types:
usage_reporting_types_cpp.append(f" {resource_type.strip()},")
usage_reporting_types.append(
@@ -34,7 +37,9 @@ def main():
f" public static final int {resource_type.strip()};"
)
with open(HAL_ROOT / "src/generate/FRCNetComm.java.in") as java_usage_reporting:
with (template_directory / "FRCNetComm.java.in").open(
encoding="utf-8"
) as java_usage_reporting:
contents = (
# fmt: off
java_usage_reporting.read()
@@ -43,12 +48,12 @@ def main():
# fmt: on
)
with open(
HAL_ROOT / f"src/generated/main/java/{java_package}/FRCNetComm.java", "w"
) as java_out:
java_out.write(contents)
frc_net_comm = output_directory / f"main/java/{java_package}/FRCNetComm.java"
frc_net_comm.write_text(contents, encoding="utf-8")
with open(HAL_ROOT / "src/generate/FRCUsageReporting.h.in") as cpp_usage_reporting:
with (template_directory / "FRCUsageReporting.h.in").open(
encoding="utf-8"
) as cpp_usage_reporting:
contents = (
# fmt: off
cpp_usage_reporting.read()
@@ -57,11 +62,33 @@ def main():
# fmt: on
)
with open(
HAL_ROOT / "src/generated/main/native/include/hal/FRCUsageReporting.h", "w"
) as cpp_out:
cpp_out.write(contents)
usage_reporting_hdr = (
output_directory / "main/native/include/hal/FRCUsageReporting.h"
)
usage_reporting_hdr.write_text(contents, encoding="utf-8")
def main(argv):
dirname = Path(__file__).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_usage_reporting(args.output_directory, args.template_root)
if __name__ == "__main__":
main()
main(sys.argv[1:])