feat: add windows path ext resolution
All checks were successful
default / dotnet-default-workflow (push) Successful in 2m21s
All checks were successful
default / dotnet-default-workflow (push) Successful in 2m21s
This resolves a problem where the file name of a script file was not correctly executed when passed without extension.
This commit is contained in:
parent
2e94c513fc
commit
38b6958412
1 changed files with 20 additions and 4 deletions
|
|
@ -11,7 +11,6 @@ public sealed partial class Command
|
||||||
return process.MainModule?.FileName;
|
return process.MainModule?.FileName;
|
||||||
});
|
});
|
||||||
|
|
||||||
private static readonly string[] WindowsExecutableExtensions = ["exe", "cmd", "bat"];
|
|
||||||
private static readonly TimeSpan CancelWaitTimeout = TimeSpan.FromSeconds(5);
|
private static readonly TimeSpan CancelWaitTimeout = TimeSpan.FromSeconds(5);
|
||||||
|
|
||||||
private static string? ProcessPath => ProcessPathLazy.Value;
|
private static string? ProcessPath => ProcessPathLazy.Value;
|
||||||
|
|
@ -71,8 +70,7 @@ public sealed partial class Command
|
||||||
|
|
||||||
// Don't do anything for fully qualified paths or paths that already have an extension specified.
|
// Don't do anything for fully qualified paths or paths that already have an extension specified.
|
||||||
// System.Diagnostics.Process knows how to handle those without our help.
|
// System.Diagnostics.Process knows how to handle those without our help.
|
||||||
if (Path.IsPathFullyQualified(TargetFilePath) ||
|
if (Path.IsPathFullyQualified(TargetFilePath) || !string.IsNullOrWhiteSpace(Path.GetExtension(TargetFilePath)))
|
||||||
!string.IsNullOrWhiteSpace(Path.GetExtension(TargetFilePath)))
|
|
||||||
{
|
{
|
||||||
return TargetFilePath;
|
return TargetFilePath;
|
||||||
}
|
}
|
||||||
|
|
@ -82,7 +80,7 @@ public sealed partial class Command
|
||||||
where Directory.Exists(probeDirPath)
|
where Directory.Exists(probeDirPath)
|
||||||
select Path.Combine(probeDirPath, TargetFilePath)
|
select Path.Combine(probeDirPath, TargetFilePath)
|
||||||
into baseFilePath
|
into baseFilePath
|
||||||
from extension in WindowsExecutableExtensions
|
from extension in GetPathExtensions()
|
||||||
select Path.ChangeExtension(baseFilePath, extension)
|
select Path.ChangeExtension(baseFilePath, extension)
|
||||||
).FirstOrDefault(File.Exists) ??
|
).FirstOrDefault(File.Exists) ??
|
||||||
TargetFilePath;
|
TargetFilePath;
|
||||||
|
|
@ -112,6 +110,24 @@ public sealed partial class Command
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static IEnumerable<string> GetPathExtensions()
|
||||||
|
{
|
||||||
|
if (System.Environment.GetEnvironmentVariable("PATHEXT")?.Split(Path.PathSeparator) is { } extensions)
|
||||||
|
{
|
||||||
|
foreach (var extension in extensions)
|
||||||
|
{
|
||||||
|
yield return extension;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
yield return ".COM";
|
||||||
|
yield return ".EXE";
|
||||||
|
yield return ".BAT";
|
||||||
|
yield return ".CMD";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue