Skip to content

Commit a63fe80

Browse files
committed
Add the ability to use the Call operator instead of the DotSource operator
Add support to switch the ExecuteMode from DotSource to Call
1 parent a99180d commit a63fe80

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/DebugStateService.cs

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ internal class DebugStateService
3434

3535
internal bool IsUsingTempIntegratedConsole { get; set; }
3636

37+
internal string ExecuteMode { get; set; }
38+
3739
// This gets set at the end of the Launch/Attach handler which set debug state.
3840
internal TaskCompletionSource<bool> ServerStarted { get; set; }
3941

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/ConfigurationDoneHandler.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,17 @@ internal async Task LaunchScriptAsync(string scriptToLaunch)
110110
PSCommand command;
111111
if (System.IO.File.Exists(scriptToLaunch))
112112
{
113-
// For a saved file we just execute its path (after escaping it).
114-
command = PSCommandHelpers.BuildDotSourceCommandWithArguments(
115-
PSCommandHelpers.EscapeScriptFilePath(scriptToLaunch), _debugStateService?.Arguments);
113+
if(_debugStateService.ExecuteMode == "Call"){
114+
// For a saved file we just execute its path (after escaping it), with the Call operator.
115+
command = PSCommandHelpers.BuildCallScriptCommandWithArguments(
116+
PSCommandHelpers.EscapeScriptFilePath(scriptToLaunch), _debugStateService?.Arguments);
117+
118+
} else {
119+
// For a saved file we just execute its path (after escaping it), with the DotSource operator.
120+
command = PSCommandHelpers.BuildDotSourceCommandWithArguments(
121+
PSCommandHelpers.EscapeScriptFilePath(scriptToLaunch), _debugStateService?.Arguments);
122+
}
123+
116124
}
117125
else // It's a URI to an untitled script, or a raw script.
118126
{

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ internal record PsesLaunchRequestArguments : LaunchRequestArguments
6565
/// </summary>
6666
public string[] RuntimeArgs { get; set; }
6767

68+
/// <summary>
69+
/// Gets or sets the script execution mode, typically "DotSource" or "Call".
70+
/// </summary>
71+
public string ExecuteMode { get; set; }
72+
6873
/// <summary>
6974
/// Gets or sets optional environment variables to pass to the debuggee. The string valued
7075
/// properties of the 'environmentVariables' are used as key/value pairs.
@@ -186,6 +191,7 @@ public async Task<LaunchResponse> Handle(PsesLaunchRequestArguments request, Can
186191
_debugStateService.ScriptToLaunch = request.Script;
187192
_debugStateService.Arguments = request.Args;
188193
_debugStateService.IsUsingTempIntegratedConsole = request.CreateTemporaryIntegratedConsole;
194+
_debugStateService.ExecuteMode = request.ExecuteMode;
189195

190196
if (request.CreateTemporaryIntegratedConsole
191197
&& !string.IsNullOrEmpty(request.Script)

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,13 @@ public static PSCommand BuildDotSourceCommandWithArguments(string command, IEnum
101101
// HACK: We use AddScript instead of AddArgument/AddParameter to reuse Powershell parameter binding logic.
102102
return new PSCommand().AddScript(script);
103103
}
104+
105+
public static PSCommand BuildCallScriptCommandWithArguments(string command, IEnumerable<string> arguments)
106+
{
107+
string args = string.Join(" ", arguments ?? Array.Empty<string>());
108+
string script = string.Concat("& ", command, string.IsNullOrEmpty(args) ? "" : " ", args);
109+
// HACK: We use AddScript instead of AddArgument/AddParameter to reuse Powershell parameter binding logic.
110+
return new PSCommand().AddScript(script);
111+
}
104112
}
105113
}

0 commit comments

Comments
 (0)