Skip to content

Commit

Permalink
Make .cproject normalization split up JSON with newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Aug 14, 2024
1 parent 0368642 commit 293a8f6
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions tools/normalize_cproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import xml.etree.ElementTree as ET


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


cproject_path = pathlib.Path(sys.argv[1])
cproject = cproject_path.read_text()

Expand All @@ -25,9 +20,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 +32,28 @@ 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)

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("\\n", "\n\\n")

# 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 293a8f6

Please sign in to comment.