Skip to content

Commit 10ce7ae

Browse files
committed
Allow for a which search to return all the findings, not just the first
1 parent c652877 commit 10ce7ae

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

src/UniGetUI.Core.Tools/Tools.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,16 @@ public static void RelaunchProcess()
103103
/// Finds an executable in path and returns its location
104104
/// </summary>
105105
/// <param name="command">The executable alias to find</param>
106-
/// <returns>A tuple containing: a boolean hat represents whether the path was found or not; the path to the file if found.</returns>
106+
/// <returns>A tuple containing: a boolean that represents whether the path was found or not; the path to the file if found.</returns>
107107
public static async Task<Tuple<bool, string>> WhichAsync(string command)
108108
{
109109
return await Task.Run(() => Which(command));
110110
}
111111

112-
public static Tuple<bool, string> Which(string command, bool updateEnv = true)
112+
public static Tuple<bool, List<string>> WhichMultiple(string command, bool updateEnv = true)
113113
{
114+
List<string> paths = [];
115+
114116
command = command.Replace(";", "").Replace("&", "").Trim();
115117
Logger.Debug($"Begin \"which\" search for command {command}");
116118

@@ -142,28 +144,36 @@ public static Tuple<bool, string> Which(string command, bool updateEnv = true)
142144
try
143145
{
144146
process.Start();
145-
string? line = process.StandardOutput.ReadLine();
146-
string output;
147-
148-
if (line is null) output = "";
149-
else output = line.Trim();
147+
string[] lines = process.StandardOutput.ReadToEnd()
148+
.Split(["\r\n", "\n"], StringSplitOptions.RemoveEmptyEntries);
150149

151150
process.WaitForExit();
152151

153-
if (process.ExitCode != 0 || output == "")
152+
if ((process.ExitCode != 0 || lines.Length == 0) && paths.Count == 0)
154153
{
155154
Logger.ImportantInfo($"Command {command} was not found on the system");
156-
return new Tuple<bool, string>(false, "");
155+
return new Tuple<bool, List<string>>(false, []);
157156
}
158157

159-
Logger.Debug($"Command {command} was found on {output}");
160-
return new Tuple<bool, string>(File.Exists(output), output);
158+
foreach (var line in lines)
159+
{
160+
Logger.Debug($"Command {command} was found on {line}{(line.Length > 1 ? $" (#{paths.Count + 1})" : "")}");
161+
paths.Add(line);
162+
}
161163
}
162164
catch
163165
{
164-
if (updateEnv) return Which(command, false);
166+
if (updateEnv) return WhichMultiple(command, false);
165167
throw;
166168
}
169+
return new Tuple<bool, List<string>>(true, paths);
170+
}
171+
172+
public static Tuple<bool, string> Which(string command, bool updateEnv = true)
173+
{
174+
var (found, paths) = WhichMultiple(command, updateEnv);
175+
if (found && paths.Count > 0) return new Tuple<bool, string>(true, paths[0]);
176+
else return new Tuple<bool, string>(false, "");
167177
}
168178

169179
/// <summary>

0 commit comments

Comments
 (0)