From aa68ee6f2092b691ea3e6c8c6fcf6cae3c2ab745 Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Mon, 28 Dec 2020 17:09:48 -0800 Subject: [PATCH 1/2] Don't assume install directory Use CallerFilePath to fetch the install directory for the ignore file. --- FindReferencesInProject2.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/FindReferencesInProject2.cs b/FindReferencesInProject2.cs index df5e5a3..ae4aeb2 100644 --- a/FindReferencesInProject2.cs +++ b/FindReferencesInProject2.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Runtime.CompilerServices; using System.Threading; using UnityEditor; using UnityEngine; @@ -11,9 +12,18 @@ public static class FindReferencesInProject2 private const string MenuItemName = "Assets/Find References In Project %#&f"; private const string MetaExtension = ".meta"; + private static string InstallDirectory = "Assets/Editor/FindReferencesInProject2"; + + private static void UpdateInstallDirectory([CallerFilePath] string executingFilePath = "") + { + InstallDirectory = Path.GetDirectoryName(executingFilePath); + } + [MenuItem(MenuItemName, false, 25)] public static void Find() { + UpdateInstallDirectory(); + bool isMacOS = Application.platform == RuntimePlatform.OSXEditor; int totalWaitMilliseconds = isMacOS ? 2 * 1000 : 300 * 1000; int cpuCount = Environment.ProcessorCount; @@ -40,11 +50,12 @@ public static void Find() } else { + var ignore_file = Path.Combine(InstallDirectory, "ignore.txt"); psi.FileName = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2\rg.exe"); psi.Arguments = string.Format("--case-sensitive --follow --files-with-matches --no-text --fixed-strings " + - "--ignore-file Assets/Editor/FindReferencesInProject2/ignore.txt " + + "--ignore-file {3} " + "--threads {0} --regexp {1} -- {2}", - cpuCount, selectedAssetGUID, appDataPath); + cpuCount, selectedAssetGUID, appDataPath, ignore_file); } psi.UseShellExecute = false; From 329d0a64e0b7d81e21f5c71577257f3453739c4f Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Mon, 28 Dec 2020 17:11:52 -0800 Subject: [PATCH 2/2] Allow rg to be in the PATH If rg isn't in the assumed location, assume it's in the PATH. Everything should continue working as before, but people who have it in their PATH don't need to install it in each of their Unity projects. Add instructions when ripgrep cannot be found. --- FindReferencesInProject2.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/FindReferencesInProject2.cs b/FindReferencesInProject2.cs index ae4aeb2..61e0cda 100644 --- a/FindReferencesInProject2.cs +++ b/FindReferencesInProject2.cs @@ -51,7 +51,13 @@ public static void Find() else { var ignore_file = Path.Combine(InstallDirectory, "ignore.txt"); - psi.FileName = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2\rg.exe"); + var filepath = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2\rg.exe"); + if (!File.Exists(filepath)) + { + // Assume it's in our path. + filepath = "rg.exe"; + } + psi.FileName = filepath; psi.Arguments = string.Format("--case-sensitive --follow --files-with-matches --no-text --fixed-strings " + "--ignore-file {3} " + "--threads {0} --regexp {1} -- {2}", @@ -87,7 +93,19 @@ public static void Find() output.AppendLine("Error: " + e.Data); }; - process.Start(); + try + { + process.Start(); + } + catch (SystemException) + { + if (!isMacOS) + { + var destination = Path.Combine(Environment.CurrentDirectory, @"Tools\FindReferencesInProject2"); + UnityEngine.Debug.LogError($"Couldn't find ripgrep. Download ripgrep from https://github.com/BurntSushi/ripgrep/releases/latest and extract rg.exe to {destination} or add it to your PATH."); + } + throw; + } process.BeginOutputReadLine(); process.BeginErrorReadLine();