Skip to content

Modify ExecuteScriptAtPath to handle arb script. #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class DebugAdapter : DebugAdapterBase

private bool noDebug;
private bool waitingForAttach;
private string scriptPathToLaunch;
private string scriptToLaunch;
private string arguments;

public DebugAdapter(HostDetails hostDetails, ProfilePaths profilePaths)
Expand Down Expand Up @@ -83,7 +83,7 @@ protected override void Initialize()
protected Task LaunchScript(RequestContext<object> requestContext)
{
return editorSession.PowerShellContext
.ExecuteScriptAtPath(this.scriptPathToLaunch, this.arguments)
.ExecuteScriptWithArgs(this.scriptToLaunch, this.arguments)
.ContinueWith(
async (t) => {
Logger.Write(LogLevel.Verbose, "Execution completed, flushing output then terminating...");
Expand Down Expand Up @@ -120,7 +120,7 @@ protected async Task HandleConfigurationDoneRequest(
object args,
RequestContext<object> requestContext)
{
if (!string.IsNullOrEmpty(this.scriptPathToLaunch))
if (!string.IsNullOrEmpty(this.scriptToLaunch))
{
if (this.editorSession.PowerShellContext.SessionState == PowerShellContextState.Ready)
{
Expand All @@ -144,8 +144,8 @@ protected async Task HandleLaunchRequest(
LaunchRequestArguments launchParams,
RequestContext<object> requestContext)
{
// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
// In case that is null, use the the folder of the script to be executed. If the resulting
// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
// In case that is null, use the the folder of the script to be executed. If the resulting
// working dir path is a file path then extract the directory and use that.
string workingDir =
launchParams.Cwd ??
Expand Down Expand Up @@ -195,15 +195,15 @@ protected async Task HandleLaunchRequest(
// Store the launch parameters so that they can be used later
this.noDebug = launchParams.NoDebug;
#pragma warning disable 618
this.scriptPathToLaunch = launchParams.Script ?? launchParams.Program;
this.scriptToLaunch = launchParams.Script ?? launchParams.Program;
#pragma warning restore 618
this.arguments = arguments;

await requestContext.SendResult(null);

// If no script is being launched, execute an empty script to
// cause the prompt string to be evaluated and displayed
if (string.IsNullOrEmpty(this.scriptPathToLaunch))
if (string.IsNullOrEmpty(this.scriptToLaunch))
{
await this.editorSession.PowerShellContext.ExecuteScriptString(
"", false, true);
Expand All @@ -222,7 +222,7 @@ protected async Task HandleAttachRequest(
{
// If there are no host processes to attach to or the user cancels selection, we get a null for the process id.
// This is not an error, just a request to stop the original "attach to" request.
// Testing against "undefined" is a HACK because I don't know how to make "Cancel" on quick pick loading
// Testing against "undefined" is a HACK because I don't know how to make "Cancel" on quick pick loading
// to cancel on the VSCode side without sending an attachRequest with processId set to "undefined".
if (string.IsNullOrEmpty(attachParams.ProcessId) || (attachParams.ProcessId == "undefined"))
{
Expand Down Expand Up @@ -364,7 +364,7 @@ protected async Task HandleSetBreakpointsRequest(
catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException)
{
Logger.Write(
LogLevel.Warning,
LogLevel.Warning,
$"Attempted to set breakpoints on a non-existing file: {setBreakpointsParams.Source.Path}");

string message = this.noDebug ? string.Empty : "Source does not exist, breakpoint not set.";
Expand All @@ -387,9 +387,9 @@ await requestContext.SendResult(
{
SourceBreakpoint srcBreakpoint = setBreakpointsParams.Breakpoints[i];
breakpointDetails[i] = BreakpointDetails.Create(
scriptFile.FilePath,
srcBreakpoint.Line,
srcBreakpoint.Column,
scriptFile.FilePath,
srcBreakpoint.Line,
srcBreakpoint.Column,
srcBreakpoint.Condition,
srcBreakpoint.HitCondition);
}
Expand Down Expand Up @@ -541,7 +541,7 @@ protected async Task HandleStackTraceRequest(
// be referenced back to the current list of stack frames
newStackFrames.Add(
StackFrame.Create(
stackFrames[i],
stackFrames[i],
i));
}

Expand Down Expand Up @@ -712,9 +712,9 @@ async void DebugService_DebuggerStopped(object sender, DebuggerStoppedEventArgs

// Provide the reason for why the debugger has stopped script execution.
// See https://github.com/Microsoft/vscode/issues/3648
// The reason is displayed in the breakpoints viewlet. Some recommended reasons are:
// The reason is displayed in the breakpoints viewlet. Some recommended reasons are:
// "step", "breakpoint", "function breakpoint", "exception" and "pause".
// We don't support exception breakpoints and for "pause", we can't distinguish
// We don't support exception breakpoints and for "pause", we can't distinguish
// between stepping and the user pressing the pause/break button in the debug toolbar.
string debuggerStoppedReason = "step";
if (e.OriginalEvent.Breakpoints.Count > 0)
Expand Down
35 changes: 28 additions & 7 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -625,26 +626,46 @@ await this.ExecuteCommand<object>(
/// <summary>
/// Executes a script file at the specified path.
/// </summary>
/// <param name="scriptPath">The path to the script file to execute.</param>
/// <param name="script">The script execute.</param>
/// <param name="arguments">Arguments to pass to the script.</param>
/// <returns>A Task that can be awaited for completion.</returns>
public async Task ExecuteScriptAtPath(string scriptPath, string arguments = null)
public async Task ExecuteScriptWithArgs(string script, string arguments = null)
{
PSCommand command = new PSCommand();

if (arguments != null)
{
// If we don't escape wildcard characters in the script path, the script can
// fail to execute if say the script name was foo][.ps1.
// Need to determine If the script string is a path to a script file.
string scriptAbsPath = string.Empty;
try
{
// Assume we can only debug scripts from the FileSystem provider
string workingDir =
this.CurrentRunspace.Runspace.SessionStateProxy.Path.CurrentFileSystemLocation.ProviderPath;
workingDir = workingDir.TrimEnd(Path.DirectorySeparatorChar);
scriptAbsPath = workingDir + Path.DirectorySeparatorChar + script;
}
catch (System.Management.Automation.DriveNotFoundException e)
{
Logger.Write(
LogLevel.Error,
"Could not determine current filesystem location:\r\n\r\n" + e.ToString());
}

// If we don't escape wildcard characters in a path to a script file, the script can
// fail to execute if say the script filename was foo][.ps1.
// Related to issue #123.
string escapedScriptPath = EscapePath(scriptPath, escapeSpaces: true);
string scriptWithArgs = escapedScriptPath + " " + arguments;
if (File.Exists(script) || File.Exists(scriptAbsPath))
{
script = EscapePath(script, escapeSpaces: true);
}

string scriptWithArgs = script + " " + arguments;
command.AddScript(scriptWithArgs);
}
else
{
command.AddCommand(scriptPath);
command.AddCommand(script);
}

await this.ExecuteCommand<object>(command, true);
Expand Down
20 changes: 10 additions & 10 deletions test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ await this.debugService.SetLineBreakpoints(

// Execute the script and wait for the breakpoint to be hit
Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
debugWithParamsFile.FilePath, arguments);

await this.AssertDebuggerStopped(debugWithParamsFile.FilePath);
Expand All @@ -131,7 +131,7 @@ await this.debugService.SetLineBreakpoints(
var = variables.FirstOrDefault(v => v.Name == "$Param2");
Assert.NotNull(var);
Assert.True(var.IsExpandable);

var childVars = debugService.GetVariables(var.Id);
Assert.Equal(9, childVars.Length);
Assert.Equal("\"Bar\"", childVars[0].ValueString);
Expand Down Expand Up @@ -194,7 +194,7 @@ await this.debugService.SetCommandBreakpoints(
await this.AssertStateChange(PowerShellContextState.Ready);

Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
this.debugScriptFile.FilePath);

// Wait for function breakpoint to hit
Expand Down Expand Up @@ -273,7 +273,7 @@ await this.debugService.SetLineBreakpoints(
await this.AssertStateChange(PowerShellContextState.Ready);

Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
this.debugScriptFile.FilePath);

// Wait for a couple breakpoints
Expand Down Expand Up @@ -303,7 +303,7 @@ await this.debugService.SetLineBreakpoints(
await this.AssertStateChange(PowerShellContextState.Ready);

Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
this.debugScriptFile.FilePath);

// Wait for conditional breakpoint to hit
Expand Down Expand Up @@ -353,7 +353,7 @@ await this.debugService.SetLineBreakpoints(
await this.AssertStateChange(PowerShellContextState.Ready);

Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
this.debugScriptFile.FilePath);

// Wait for conditional breakpoint to hit
Expand Down Expand Up @@ -389,7 +389,7 @@ await this.debugService.SetLineBreakpoints(
await this.AssertStateChange(PowerShellContextState.Ready);

Task executeTask =
this.powerShellContext.ExecuteScriptAtPath(
this.powerShellContext.ExecuteScriptWithArgs(
this.debugScriptFile.FilePath);

// Wait for conditional breakpoint to hit
Expand Down Expand Up @@ -466,7 +466,7 @@ public async Task DebuggerBreaksWhenRequested()
this.debugService.Break();

// File path is an empty string when paused while running
await this.AssertDebuggerStopped(string.Empty);
await this.AssertDebuggerStopped(string.Empty);
await this.AssertStateChange(
PowerShellContextState.Ready,
PowerShellExecutionResult.Stopped);
Expand Down Expand Up @@ -545,7 +545,7 @@ await this.debugService.SetLineBreakpoints(

StackFrameDetails[] stackFrames = debugService.GetStackFrames();

VariableDetailsBase[] variables =
VariableDetailsBase[] variables =
debugService.GetVariables(stackFrames[0].LocalVariables.Id);

// TODO: Add checks for correct value strings as well
Expand Down Expand Up @@ -604,7 +604,7 @@ await this.debugService.SetLineBreakpoints(
Assert.Equal(newStrValue, setStrValue);

VariableScope[] scopes = this.debugService.GetVariableScopes(0);

// Test set of script scope int variable (not strongly typed)
VariableScope scriptScope = scopes.FirstOrDefault(s => s.Name == VariableContainerDetails.ScriptScopeName);
string newIntValue = "49";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class PowerShellContextTests : IDisposable

private readonly ProfilePaths TestProfilePaths =
new ProfilePaths(
TestHostDetails.ProfileId,
TestHostDetails.ProfileId,
Path.GetFullPath(
@"..\..\..\..\PowerShellEditorServices.Test.Shared\Profile"),
Path.GetFullPath(
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task CanAbortExecution()
Task.Run(
async () =>
{
var unusedTask = this.powerShellContext.ExecuteScriptAtPath(DebugTestFilePath);
var unusedTask = this.powerShellContext.ExecuteScriptWithArgs(DebugTestFilePath);
await Task.Delay(50);
this.powerShellContext.AbortExecution();
});
Expand Down