From 92822c64123728fce828d3ca2ed926811fa7de58 Mon Sep 17 00:00:00 2001 From: Denis Kopyrin Date: Tue, 2 Jul 2024 09:44:54 +0800 Subject: [PATCH] fixlights: added support for ripgrep (#802) --- tools/fixlights.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tools/fixlights.py b/tools/fixlights.py index e504a882..a5449238 100755 --- a/tools/fixlights.py +++ b/tools/fixlights.py @@ -1,21 +1,37 @@ #!/usr/bin/python3 -import sys, subprocess, re +import sys, subprocess, re, shutil if len(sys.argv) != 2: print(f"Usage: {sys.argv[0]} [folder to search]") sys.exit() lightFiles = set() +has_rg = shutil.which("rg") is not None + +def make_search_cmd(regex, include = None): + cmd = None + if has_rg: + cmd = f"rg -lF {regex}" + else: + cmd = f"grep -Rl {regex}" + + if include: + if has_rg: + cmd += f" -g {include}" + else: + cmd += f" --include {include}" + + return cmd # The original command that was run across the entire repo to convert all assets # cmds = ("grep -Rl \"gsSPSetLight\" levels actors bin","grep -Rl \"gsSPLight\" levels actors bin","grep -Rl \"Lights1\" levels actors bin") # Operate on the folder passed as an argument to this program cmds = [ - f"grep -Rl \"gsSPSetLight\" {sys.argv[1]}", - f"grep -Rl \"gsSPLight\" {sys.argv[1]}", - f"grep -Rl \"Lights1\" {sys.argv[1]}" + make_search_cmd(f"\"gsSPSetLight\" {sys.argv[1]}"), + make_search_cmd(f"\"gsSPLight\" {sys.argv[1]}"), + make_search_cmd(f"\"Lights1\" {sys.argv[1]}") ] for cmd in cmds: @@ -135,8 +151,7 @@ for file in lightFiles: def get_texscroll_files(): mat_grep_commands = [] for mat in material_deltas.keys(): - print("grep -Rl \"segmented_to_virtual(" + re.escape(mat) + f"\" {sys.argv[1]}**/texscroll.inc.c") - mat_grep_commands.append("grep -Rl \"segmented_to_virtual(" + re.escape(mat) + rf"\" {sys.argv[1]} --include \*texscroll*") + mat_grep_commands.append(make_search_cmd("\"segmented_to_virtual(" + re.escape(mat) + rf"\" {sys.argv[1]}", "\"*texscroll*\"")) texscroll_files = set() for cmd in mat_grep_commands: