-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Normalize
.cproject
files with pre-commit (#60)
* Create a pre-commit hook to normalize .cproject files * Normalize existing projects * Normalize deeper * Clean up normalizer script * Add ruff * Run ruff * Run pre-commit in CI
- Loading branch information
Showing
7 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
repos: | ||
- repo: local | ||
hooks: | ||
- id: normalize-cproject | ||
name: Normalize Eclipse .cproject files | ||
entry: python tools/normalize_cproject.py | ||
language: system | ||
files: \.cproject$ | ||
pass_filenames: true | ||
|
||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
rev: v0.4.3 | ||
hooks: | ||
- id: ruff | ||
args: [--fix] | ||
- id: ruff-format |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import re | ||
import sys | ||
import json | ||
import pathlib | ||
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() | ||
|
||
# Capture all preprocessing directives verbatim | ||
match = re.search(r"(<\?.*\?>)", cproject[:200], flags=re.DOTALL) | ||
processing_instructions = match.group(0) | ||
|
||
tree = ET.fromstring(cproject) | ||
|
||
for storage_module in tree.findall(".//storageModule"): | ||
if "projectCommon.copiedFiles" in storage_module.attrib: | ||
copied_files = json.loads(storage_module.attrib["projectCommon.copiedFiles"]) | ||
copied_files.sort( | ||
key=lambda f: (f["generated"], f["projectPath"], f["version"]) | ||
) | ||
storage_module.attrib["projectCommon.copiedFiles"] = json_dumps_compact( | ||
copied_files | ||
) | ||
|
||
if "cppBuildConfig.projectBuiltInState" in storage_module.attrib: | ||
project_built_in_state = json.loads( | ||
storage_module.attrib["cppBuildConfig.projectBuiltInState"] | ||
) | ||
|
||
for state in project_built_in_state: | ||
if "resolvedOptionsStr" in state: | ||
resolved_options = json.loads(state["resolvedOptionsStr"]) | ||
resolved_options.sort(key=lambda o: o["optionId"]) | ||
|
||
state["resolvedOptionsStr"] = json_dumps_compact(resolved_options) | ||
|
||
storage_module.attrib["cppBuildConfig.projectBuiltInState"] = ( | ||
json_dumps_compact(project_built_in_state) | ||
) | ||
|
||
# Normalize self-closing tag spacing | ||
xml_text = ET.tostring(tree, encoding="unicode", xml_declaration=False) | ||
xml_text = xml_text.replace(" />", "/>") | ||
|
||
# Only touch the filesystem if we need to | ||
if processing_instructions + xml_text != cproject: | ||
cproject_path.write_text(processing_instructions + xml_text) |