diff options
author | Thaddeus Crews <repiteo@outlook.com> | 2024-05-01 16:02:35 -0500 |
---|---|---|
committer | Thaddeus Crews <repiteo@outlook.com> | 2024-05-08 10:30:24 -0500 |
commit | df969ff7426d61c8d32f3a89ec461b47b34e83b2 (patch) | |
tree | 61d1981b8e6ba123df4600035d48677b27aa46ab /misc/scripts/file_format.py | |
parent | 55b8724bd5188932b69a6ee484ab092cd95d4498 (diff) | |
download | redot-engine-df969ff7426d61c8d32f3a89ec461b47b34e83b2.tar.gz |
CI: Overhaul static checks to use `pre-commit`
Diffstat (limited to 'misc/scripts/file_format.py')
-rw-r--r-- | misc/scripts/file_format.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/misc/scripts/file_format.py b/misc/scripts/file_format.py new file mode 100644 index 0000000000..a4ea544a45 --- /dev/null +++ b/misc/scripts/file_format.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys + +if len(sys.argv) < 2: + print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.") + sys.exit(1) + +BOM = b"\xef\xbb\xbf" + +changed = [] +invalid = [] + +for file in sys.argv[1:]: + try: + with open(file, "rt", encoding="utf-8") as f: + original = f.read() + except UnicodeDecodeError: + invalid.append(file) + continue + + if original == "": + continue + + EOL = "\r\n" if file.endswith((".csproj", ".sln", ".bat")) or file.startswith("misc/msvs") else "\n" + WANTS_BOM = file.endswith((".csproj", ".sln")) + + revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL + + new_raw = revamp.encode(encoding="utf-8") + if not WANTS_BOM and new_raw.startswith(BOM): + new_raw = new_raw[len(BOM) :] + elif WANTS_BOM and not new_raw.startswith(BOM): + new_raw = BOM + new_raw + + with open(file, "rb") as f: + old_raw = f.read() + + if old_raw != new_raw: + changed.append(file) + with open(file, "wb") as f: + f.write(new_raw) + +if changed: + for file in changed: + print(f"FIXED: {file}") +if invalid: + for file in invalid: + print(f"REQUIRES MANUAL CHANGES: {file}") + sys.exit(1) |