Skip to content

Commit

Permalink
Enhance command execution by resolving full path from PATH environme…
Browse files Browse the repository at this point in the history
…nt variable
  • Loading branch information
thiagolunardi authored and tjementum committed Jan 4, 2025
1 parent c9bb16b commit cfdfdcf
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions developer-cli/Utilities/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ private static ProcessStartInfo CreateProcessStartInfo(
bool createNoWindow = false
)
{
var fileName = command.Split(' ')[0];
var arguments = command.Length > fileName.Length ? command.Substring(fileName.Length + 1) : string.Empty;
var originalFileName = command.Split(' ')[0];
var fileName = FindFullPathFromPath(originalFileName) ?? throw new FileNotFoundException($"Command '{command}' not found");
var arguments = command.Contains(' ') ? command[(originalFileName.Length + 1)..] : string.Empty;
var processStartInfo = new ProcessStartInfo
{
FileName = fileName,
Expand Down Expand Up @@ -123,6 +124,29 @@ public static bool IsProcessRunning(string process)
{
return Process.GetProcessesByName(process).Length > 0;
}

private static string? FindFullPathFromPath(string command)
{
Debug.Assert(!string.IsNullOrWhiteSpace(command));

string[] commandFormats = OperatingSystem.IsWindows() ? ["{0}.exe", "{0}.cmd"] : ["{0}"];

var pathVariable = Environment.GetEnvironmentVariable("PATH");
foreach (var directory in pathVariable?.Split(';') ?? [])
{
foreach (var format in commandFormats)
{
var fullPath = Path.Combine(directory, string.Format(format, command));

if (File.Exists(fullPath))
{
return fullPath;
}
}
}

return null;
}
}

public class ProcessExecutionException(int exitCode, string message)
Expand Down

0 comments on commit cfdfdcf

Please sign in to comment.