From 74c4a46ffa20d025bd56fd85eac69bc30f313599 Mon Sep 17 00:00:00 2001 From: Gerardo Grignoli Date: Fri, 18 Nov 2022 14:55:19 -0300 Subject: [PATCH] Fix: On Powershell >= 7.3, when receiving a string literal command, replacing " with \" is no longer needed. --- src/gsudo/Helpers/CommandToRunAdapter.cs | 9 ++- src/gsudo/Helpers/ShellHelper.cs | 70 ++++++++++++------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/gsudo/Helpers/CommandToRunAdapter.cs b/src/gsudo/Helpers/CommandToRunAdapter.cs index 1109cc72..6cb1811e 100644 --- a/src/gsudo/Helpers/CommandToRunAdapter.cs +++ b/src/gsudo/Helpers/CommandToRunAdapter.cs @@ -163,9 +163,12 @@ Running ./gsudo {command} should elevate the powershell command. } // ---- - 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); } diff --git a/src/gsudo/Helpers/ShellHelper.cs b/src/gsudo/Helpers/ShellHelper.cs index bdb21e80..c7a91296 100644 --- a/src/gsudo/Helpers/ShellHelper.cs +++ b/src/gsudo/Helpers/ShellHelper.cs @@ -28,12 +28,7 @@ public static string InvokingShellFullPath { get { - if (!IsIntialized) - { - _invokingShell = Initialize(out _invokingShellFullPath); - IsIntialized = true; - } - + if (!IsIntialized) Initialize(); return _invokingShellFullPath; } } @@ -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(); @@ -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. @@ -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) {