Skip to content

Commit

Permalink
Fix: On Powershell >= 7.3, when receiving a string literal command, r…
Browse files Browse the repository at this point in the history
…eplacing " with \" is no longer needed.
  • Loading branch information
gerardog committed Nov 18, 2022
1 parent d2d91f4 commit 74c4a46
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
9 changes: 6 additions & 3 deletions src/gsudo/Helpers/CommandToRunAdapter.cs
Expand Up @@ -163,9 +163,12 @@ private IList<string> ApplyShell(IList<string> args)
}
// ----

string pscommand = string.Join(" ", args)
.ReplaceOrdinal("\"", "\\\"")
.Quote();
string pscommand = string.Join(" ", args);

if (ShellHelper.GetInvokingShellVersion() < new Version(7, 3, 0))
pscommand = pscommand.ReplaceOrdinal("\"", "\\\"");

pscommand = pscommand.Quote();

newArgs.Add(pscommand);
}
Expand Down
70 changes: 36 additions & 34 deletions src/gsudo/Helpers/ShellHelper.cs
Expand Up @@ -28,12 +28,7 @@ public static string InvokingShellFullPath
{
get
{
if (!IsIntialized)
{
_invokingShell = Initialize(out _invokingShellFullPath);
IsIntialized = true;
}

if (!IsIntialized) Initialize();
return _invokingShellFullPath;
}
}
Expand All @@ -42,19 +37,20 @@ public static Shell InvokingShell
{
get
{
if (!IsIntialized)
{
_invokingShell = Initialize(out _invokingShellFullPath);
IsIntialized = true;
}

if (!IsIntialized) Initialize();
return _invokingShell.Value;
}
}

private static bool IsIntialized { get; set; }

private static Shell Initialize(out string invokingShellFullPath)
private static void Initialize()
{
_invokingShell = InitializeInternal(out _invokingShellFullPath);
IsIntialized = true;
}

private static Shell InitializeInternal(out string invokingShellFullPath)
{
var parentProcess = Process.GetCurrentProcess().GetParentProcessExcludingShim();

Expand All @@ -63,37 +59,34 @@ private static Shell Initialize(out string invokingShellFullPath)
invokingShellFullPath = parentProcess.GetExeName();

var shell = DetectShellByFileName(invokingShellFullPath);

if (shell.HasValue)
return shell.Value;
else
// Depending on how pwsh was installed, Pwsh.exe -calls-> dotnet -calls-> gsudo.
var grandParentProcess = parentProcess.GetParentProcessExcludingShim();
if (grandParentProcess != null)
{
// Depending on how pwsh was installed, Pwsh.exe -calls-> dotnet -calls-> gsudo.
var grandParentProcess = parentProcess.GetParentProcessExcludingShim();
if (grandParentProcess != null)
var grandParentExeName = Path.GetFileName(grandParentProcess.GetExeName()).ToUpperInvariant();
if (grandParentExeName == "PWSH.EXE" || grandParentExeName == "PWSH")
{
var grandParentExeName = Path.GetFileName(grandParentProcess.GetExeName()).ToUpperInvariant();
if (grandParentExeName == "PWSH.EXE" || grandParentExeName == "PWSH")
{
invokingShellFullPath = grandParentProcess.GetExeName();
invokingShellFullPath = grandParentProcess.GetExeName();

FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(invokingShellFullPath);
Version fileVersion = GetInvokingShellVersion();

if (Version.Parse(versionInfo.FileVersion) <= Version.Parse("6.2.3.0") && invokingShellFullPath.EndsWith(".dotnet\\tools\\pwsh.exe", StringComparison.OrdinalIgnoreCase))
{
return Helpers.Shell.PowerShellCore623BuggedGlobalInstall;
}

return Helpers.Shell.PowerShellCore;
if (fileVersion <= new Version(6, 2, 3) && invokingShellFullPath.EndsWith(".dotnet\\tools\\pwsh.exe", StringComparison.OrdinalIgnoreCase))
{
return Helpers.Shell.PowerShellCore623BuggedGlobalInstall;
}
}

if (ProcessFactory.IsWindowsApp(invokingShellFullPath))
{
invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC");
return Helpers.Shell.WindowsApp; // Called from explorer.exe, task mgr, etc.
return Helpers.Shell.PowerShellCore;
}
}

if (ProcessFactory.IsWindowsApp(invokingShellFullPath))
{
invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC");
return Helpers.Shell.WindowsApp; // Called from explorer.exe, task mgr, etc.
}
}

// Unknown Shell.
Expand All @@ -102,6 +95,15 @@ private static Shell Initialize(out string invokingShellFullPath)
invokingShellFullPath = Environment.GetEnvironmentVariable("COMSPEC");
return Helpers.Shell.Cmd;
}


public static Version GetInvokingShellVersion()
{
if (!IsIntialized) Initialize();
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(_invokingShellFullPath);
var fileVersion = new Version(versionInfo.FileMajorPart, versionInfo.FileMinorPart, versionInfo.FileBuildPart);
return fileVersion;
}

public static Shell? DetectShellByFileName(string filename)
{
Expand Down

0 comments on commit 74c4a46

Please sign in to comment.