Skip to content

Commit

Permalink
More .cproject normalization (#79)
Browse files Browse the repository at this point in the history
* Make `.cproject` normalization split up JSON with newlines

* Run it on the repo

* One more pass
  • Loading branch information
puddly authored Aug 15, 2024
1 parent 0368642 commit 841990b
Show file tree
Hide file tree
Showing 7 changed files with 14,155 additions and 21 deletions.
2,401 changes: 2,398 additions & 3 deletions misc/firmware-eraser/.cproject

Large diffs are not rendered by default.

1,604 changes: 1,602 additions & 2 deletions src/bootloader-uart-xmodem/.cproject

Large diffs are not rendered by default.

2,640 changes: 2,638 additions & 2 deletions src/ncp-uart-hw/.cproject

Large diffs are not rendered by default.

2,575 changes: 2,573 additions & 2 deletions src/ot-rcp/.cproject

Large diffs are not rendered by default.

2,750 changes: 2,748 additions & 2 deletions src/rcp-uart-802154/.cproject

Large diffs are not rendered by default.

2,171 changes: 2,169 additions & 2 deletions src/zwave_ncp_serial_api_controller/.cproject

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions tools/normalize_cproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import xml.etree.ElementTree as ET


def json_dumps_compact(obj: dict | list) -> str:
NEWLINE_SENTINEL = "7200872e315d2518866d8a02e8258034"


def json_dumps(obj: dict | list) -> str:
"""Compactly dump JSON into a string."""
return json.dumps(obj, separators=(",", ":"), indent=None)
return json.dumps(obj, separators=(", ", ": "), indent=4)


cproject_path = pathlib.Path(sys.argv[1])
Expand All @@ -25,9 +28,7 @@ def json_dumps_compact(obj: dict | list) -> str:
copied_files.sort(
key=lambda f: (f["generated"], f["projectPath"], f["version"])
)
storage_module.attrib["projectCommon.copiedFiles"] = json_dumps_compact(
copied_files
)
storage_module.attrib["projectCommon.copiedFiles"] = json_dumps(copied_files)

if "cppBuildConfig.projectBuiltInState" in storage_module.attrib:
project_built_in_state = json.loads(
Expand All @@ -39,16 +40,34 @@ def json_dumps_compact(obj: dict | list) -> str:
resolved_options = json.loads(state["resolvedOptionsStr"])
resolved_options.sort(key=lambda o: o["optionId"])

state["resolvedOptionsStr"] = json_dumps_compact(resolved_options)
state["resolvedOptionsStr"] = json_dumps(resolved_options)

if "builtinIncludesStr" in state:
state["builtinIncludesStr"] = NEWLINE_SENTINEL.join(
state["builtinIncludesStr"].split()
)

storage_module.attrib["cppBuildConfig.projectBuiltInState"] = (
json_dumps_compact(project_built_in_state)
storage_module.attrib["cppBuildConfig.projectBuiltInState"] = json_dumps(
project_built_in_state
)

if "projectCommon.referencedModules" in storage_module.attrib:
referenced_modules = json.loads(
storage_module.attrib["projectCommon.referencedModules"]
)
storage_module.attrib["projectCommon.referencedModules"] = json_dumps(
referenced_modules
)

# Normalize self-closing tag spacing
xml_text = ET.tostring(tree, encoding="unicode", xml_declaration=False)
xml_text = xml_text.replace(" />", "/>")

# Replace newlines with literals
xml_text = xml_text.replace("
", "\n")
xml_text = xml_text.replace(NEWLINE_SENTINEL, "\n")
xml_text = re.sub(r"\s*\\n\s*", "\n\\n", xml_text, flags=re.MULTILINE)

# Only touch the filesystem if we need to
if processing_instructions + xml_text != cproject:
cproject_path.write_text(processing_instructions + xml_text)

0 comments on commit 841990b

Please sign in to comment.