Skip to content

Commit a635b42

Browse files
committed
Reintroduce Keith's script execution changes
1 parent a273d3e commit a635b42

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ protected override void Initialize()
9797

9898
protected Task LaunchScript(RequestContext<object> requestContext)
9999
{
100+
// Ensure the read loop is stopped
101+
this.editorSession.ConsoleService.CancelReadLoop();
102+
100103
return editorSession.PowerShellContext
101104
.ExecuteScriptWithArgs(this.scriptToLaunch, this.arguments)
102105
.ContinueWith(this.OnExecutionCompleted);
@@ -138,10 +141,11 @@ private async Task OnExecutionCompleted(Task executeTask)
138141
}
139142
}
140143

141-
if (!this.ownsEditorSession)
142-
{
143-
this.editorSession.ConsoleService.StartReadLoop();
144-
}
144+
}
145+
146+
if (!this.ownsEditorSession)
147+
{
148+
this.editorSession.ConsoleService.StartReadLoop();
145149
}
146150

147151
if (this.disconnectRequestContext != null)

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Microsoft.PowerShell.EditorServices
2222
using System.Management.Automation.Host;
2323
using System.Management.Automation.Runspaces;
2424
using Microsoft.PowerShell.EditorServices.Session.Capabilities;
25+
using System.IO;
2526

2627
/// <summary>
2728
/// Manages the lifetime and usage of a PowerShell session.
@@ -665,21 +666,48 @@ await this.ExecuteCommand<object>(
665666
/// <summary>
666667
/// Executes a script file at the specified path.
667668
/// </summary>
668-
/// <param name="scriptPath">The path to the script file to execute.</param>
669+
/// <param name="script">The script execute.</param>
669670
/// <param name="arguments">Arguments to pass to the script.</param>
670671
/// <returns>A Task that can be awaited for completion.</returns>
671-
public async Task ExecuteScriptAtPath(string scriptPath, string arguments = null)
672-
{
673-
// If we don't escape wildcard characters in the script path, the script can
674-
// fail to execute if say the script name was foo][.ps1.
675-
// Related to issue #123.
676-
string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true);
677-
678-
await this.ExecuteScriptString(
679-
$"{escapedScriptPath} {arguments}",
680-
true,
681-
true,
682-
false);
672+
public async Task ExecuteScriptWithArgs(string script, string arguments = null)
673+
{
674+
PSCommand command = new PSCommand();
675+
676+
if (arguments != null)
677+
{
678+
// Need to determine If the script string is a path to a script file.
679+
string scriptAbsPath = string.Empty;
680+
try
681+
{
682+
// Assume we can only debug scripts from the FileSystem provider
683+
string workingDir =
684+
this.CurrentRunspace.Runspace.SessionStateProxy.Path.CurrentFileSystemLocation.ProviderPath;
685+
workingDir = workingDir.TrimEnd(Path.DirectorySeparatorChar);
686+
scriptAbsPath = workingDir + Path.DirectorySeparatorChar + script;
687+
}
688+
catch (System.Management.Automation.DriveNotFoundException e)
689+
{
690+
Logger.Write(
691+
LogLevel.Error,
692+
"Could not determine current filesystem location:\r\n\r\n" + e.ToString());
693+
}
694+
695+
// If we don't escape wildcard characters in a path to a script file, the script can
696+
// fail to execute if say the script filename was foo][.ps1.
697+
// Related to issue #123.
698+
if (File.Exists(script) || File.Exists(scriptAbsPath))
699+
{
700+
script = EscapePath(script, escapeSpaces: true);
701+
}
702+
string scriptWithArgs = script + " " + arguments;
703+
command.AddScript(scriptWithArgs);
704+
}
705+
else
706+
{
707+
command.AddCommand(script);
708+
}
709+
710+
await this.ExecuteCommand<object>(command, true);
683711
}
684712

685713
internal static TResult ExecuteScriptAndGetItem<TResult>(string scriptToExecute, Runspace runspace, TResult defaultValue = default(TResult))

0 commit comments

Comments
 (0)