Skip to content

Commit 4cc041f

Browse files
authored
Add a file formatting script (#3383)
1 parent fdf8444 commit 4cc041f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

.github/workflows/file_format.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
6+
if len(sys.argv) < 2:
7+
print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.")
8+
sys.exit(1)
9+
10+
BOM = b"\xef\xbb\xbf"
11+
12+
changed = []
13+
invalid = []
14+
15+
for file in sys.argv[1:]:
16+
try:
17+
with open(file, "rt", encoding="utf-8") as f:
18+
original = f.read()
19+
except UnicodeDecodeError:
20+
invalid.append(file)
21+
continue
22+
23+
if original == "":
24+
continue
25+
26+
EOL = "\n"
27+
WANTS_BOM = False
28+
29+
revamp = EOL.join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip(EOL) + EOL
30+
31+
new_raw = revamp.encode(encoding="utf-8")
32+
if not WANTS_BOM and new_raw.startswith(BOM):
33+
new_raw = new_raw[len(BOM) :]
34+
elif WANTS_BOM and not new_raw.startswith(BOM):
35+
new_raw = BOM + new_raw
36+
37+
with open(file, "rb") as f:
38+
old_raw = f.read()
39+
40+
if old_raw != new_raw:
41+
changed.append(file)
42+
with open(file, "wb") as f:
43+
f.write(new_raw)
44+
45+
if changed:
46+
for file in changed:
47+
print(f"FIXED: {file}")
48+
49+
if invalid:
50+
for file in invalid:
51+
print(f"REQUIRES MANUAL CHANGES: {file}")
52+
sys.exit(1)

.pre-commit-config.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
default_language_version:
2+
python: python3
3+
4+
exclude: |
5+
(?x)^(
6+
3rdparty/.*|
7+
bindings/.*|
8+
include/bgfx/c99/bgfx\.h|
9+
.*\.bin\.h|
10+
.*\.ttf\.h$
11+
)
12+
13+
repos:
14+
- repo: local
15+
hooks:
16+
- id: file-format
17+
name: file-format
18+
language: python
19+
entry: python .github/workflows/file_format.py
20+
types_or: [text]

0 commit comments

Comments
 (0)