Skip to content

Commit

Permalink
Add prettyfy for java output ... makes the input/output files more ob…
Browse files Browse the repository at this point in the history
…viously identical
  • Loading branch information
andy31415 committed Mar 21, 2023
1 parent a9e2f2b commit ad4aa7c
Show file tree
Hide file tree
Showing 2 changed files with 2,752 additions and 3,530 deletions.
29 changes: 29 additions & 0 deletions scripts/tools/zap_regen_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import sys
import tempfile
import time
import traceback
import urllib.request
from dataclasses import dataclass
from enum import Flag, auto
from pathlib import Path
Expand Down Expand Up @@ -211,9 +213,36 @@ def __init__(self, generator: str, output_directory: str, idl_path: str):
self.command = ["./scripts/codegen.py", "--output-dir", output_directory,
"--generator", generator, idl_path]

def runJavaPrettifier(self):
try:
java_outputs = subprocess.check_output(["./scripts/codegen.py", "--name-only", "--generator",
self.generator, "--log-level", "fatal", self.idl_path]).decode("utf8").split("\n")
java_outputs = [os.path.join(self.output_directory, name) for name in java_outputs if name]

logging.info("Prettifying %d java files:", len(java_outputs))
for name in java_outputs:
logging.info(" %s" % name)

# Keep this version in sync with what restyler uses (https://github.com/project-chip/connectedhomeip/blob/master/.restyled.yaml).
FORMAT_VERSION = "1.6"
URL_PREFIX = 'https://github.com/google/google-java-format/releases/download/google-java-format'
JAR_NAME = f"google-java-format-{FORMAT_VERSION}-all-deps.jar"
jar_url = f"{URL_PREFIX}-{FORMAT_VERSION}/{JAR_NAME}"

home = str(Path.home())
path, http_message = urllib.request.urlretrieve(jar_url, Path.home().joinpath(JAR_NAME).as_posix())

subprocess.check_call(['java', '-jar', path, '--replace'] + java_outputs)
except Exception as err:
traceback.print_exception(err)
print('google-java-format error:', err)

def generate(self) -> TargetRunStats:
generate_start = time.time()

subprocess.check_call(self.command)
self.runJavaPrettifier()

generate_end = time.time()

return TargetRunStats(
Expand Down
Loading

0 comments on commit ad4aa7c

Please sign in to comment.