summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-26 00:40:51 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-26 00:42:44 +0100
commitcf7db48c261ee9c896c813a38ff8c59da5b8fe07 (patch)
tree41d827be5db5f3322d745215fe38a9c395a52fa3 /tools
parent79aaec3d7d12bc1a0483f19eadeda325a2d65920 (diff)
Fix line endings
Diffstat (limited to 'tools')
-rw-r--r--tools/BUILD.bazel8
-rw-r--r--tools/fix_line_endings.py68
2 files changed, 76 insertions, 0 deletions
diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel
new file mode 100644
index 0000000..4a374f3
--- /dev/null
+++ b/tools/BUILD.bazel
@@ -0,0 +1,8 @@
+load("@rules_python//python:py_binary.bzl", "py_binary")
+
+py_binary(
+ name = "fix_line_endings",
+ srcs = [
+ "fix_line_endings.py",
+ ],
+)
diff --git a/tools/fix_line_endings.py b/tools/fix_line_endings.py
new file mode 100644
index 0000000..ce6cf4b
--- /dev/null
+++ b/tools/fix_line_endings.py
@@ -0,0 +1,68 @@
+import os
+import subprocess
+import glob
+import re
+import sys
+import time
+
+TO_FIX = [
+ ".bazelrc",
+ ".clang-tidy",
+ ".gitignore",
+ "**/*.hpp",
+ "**/*.h",
+ "**/*.cpp",
+ "**/*.py",
+ "**/*.bzl",
+ "**/*.bazel",
+ "**/*.txt",
+ "**/*.bat",
+ "**/*.sh",
+]
+
+TO_IGNORE = [
+ "MODULE.bazel.lock",
+]
+
+TO_FIX = [re.compile(glob.translate(p, recursive=True, include_hidden=True)) for p in TO_FIX]
+TO_IGNORE = [re.compile(glob.translate(p, recursive=True, include_hidden=True)) for p in TO_IGNORE]
+
+def get_git_files():
+ return subprocess.check_output(["git", "ls-files"]).decode().splitlines()
+
+def fix_file(file):
+ lines = []
+ with open(file, "rb") as fp:
+ lines = [l.rstrip() + b"\n" for l in fp.readlines()]
+ with open(file, "wb+") as fp:
+ fp.writelines(lines)
+
+if __name__ == "__main__":
+ os.chdir(os.getenv("BUILD_WORKSPACE_DIRECTORY"))
+
+ files = get_git_files()
+ files_to_fix = []
+ unhandled_files = []
+
+ for f in files:
+ for pattern in TO_FIX:
+ if pattern.match(f):
+ files_to_fix.append(f)
+ break
+ else:
+ for pattern in TO_IGNORE:
+ if pattern.match(f):
+ break
+ else:
+ unhandled_files.append(f)
+
+ if len(unhandled_files):
+ print("Some files were not handled:")
+ for f in unhandled_files:
+ print(" ", f)
+ sys.exit(1)
+
+ for i, f in enumerate(files):
+ print(f"\033[K{i}/{len(files_to_fix)} {f}", end="\r", flush=True)
+ fix_file(f)
+ print("")