Skip to content

Finish PowerShell v3 and v4 support #184

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 2 commits into from
Mar 9, 2016
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
9 changes: 8 additions & 1 deletion src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,14 @@ protected Task HandlePauseRequest(
object pauseParams,
RequestContext<object> requestContext)
{
editorSession.DebugService.Break();
try
{
editorSession.DebugService.Break();
}
catch (NotSupportedException e)
{
return requestContext.SendError(e.Message);
}

// This request is responded to by sending the "stopped" event
return Task.FromResult(true);
Expand Down
4 changes: 4 additions & 0 deletions src/PowerShellEditorServices/PowerShellEditorServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@
<Compile Include="Console\ChoiceDetails.cs" />
<Compile Include="Session\EditorSession.cs" />
<Compile Include="Console\IConsoleHost.cs" />
<Compile Include="Session\IVersionSpecificOperations.cs" />
<Compile Include="Session\OutputType.cs" />
<Compile Include="Session\OutputWrittenEventArgs.cs" />
<Compile Include="Session\PowerShell3Operations.cs" />
<Compile Include="Session\PowerShell4Operations.cs" />
<Compile Include="Session\PowerShell5Operations.cs" />
<Compile Include="Session\PowerShellExecutionResult.cs" />
<Compile Include="Session\PowerShellContext.cs" />
<Compile Include="Session\PowerShellContextState.cs" />
Expand Down
33 changes: 26 additions & 7 deletions src/PowerShellEditorServices/Session/EditorSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

using Microsoft.PowerShell.EditorServices.Console;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Threading;

namespace Microsoft.PowerShell.EditorServices
Expand Down Expand Up @@ -66,17 +68,34 @@ public void StartSession()
this.DebugService = new DebugService(this.PowerShellContext);
this.ConsoleService = new ConsoleService(this.PowerShellContext);

// AnalysisService will throw FileNotFoundException if
// Script Analyzer binaries are not included.
try
// Only enable the AnalysisService if the machine has PowerShell
// v5 installed. Script Analyzer works on earlier PowerShell
// versions but our hard dependency on their binaries complicates
// the deployment and assembly loading since we would have to
// conditionally load the binaries for v3/v4 support. This problem
// will be solved in the future by using Script Analyzer as a
// module rather than an assembly dependency.
if (this.PowerShellContext.PowerShellVersion.Major >= 5)
{
this.AnalysisService = new AnalysisService();
// AnalysisService will throw FileNotFoundException if
// Script Analyzer binaries are not included.
try
{
this.AnalysisService = new AnalysisService();
}
catch (FileNotFoundException)
{
Logger.Write(
LogLevel.Warning,
"Script Analyzer binaries not found, AnalysisService will be disabled.");
}
}
catch (FileNotFoundException)
else
{
Logger.Write(
LogLevel.Warning,
"Script Analyzer binaries not found, AnalysisService will be disabled.");
LogLevel.Normal,
"Script Analyzer cannot be loaded due to unsupported PowerShell version " +
this.PowerShellContext.PowerShellVersion.ToString());
}

// Create a workspace to contain open files
Expand Down
25 changes: 25 additions & 0 deletions src/PowerShellEditorServices/Session/IVersionSpecificOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Microsoft.PowerShell.EditorServices.Session
{
internal interface IVersionSpecificOperations
{
void ConfigureDebugger(Runspace runspace);

void PauseDebugger(Runspace runspace);

IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
PowerShellContext powerShellContext,
Runspace currentRunspace,
PSCommand psCommand,
bool sendOutputToHost);
}
}

62 changes: 62 additions & 0 deletions src/PowerShellEditorServices/Session/PowerShell3Operations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Microsoft.PowerShell.EditorServices.Session
{
internal class PowerShell3Operations : IVersionSpecificOperations
{
public void ConfigureDebugger(Runspace runspace)
{
// The debugger has no SetDebugMode in PowerShell v3.
}

public void PauseDebugger(Runspace runspace)
{
// The debugger cannot be paused in PowerShell v3.
throw new NotSupportedException("Debugger cannot be paused in PowerShell v3");
}

public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
PowerShellContext powerShellContext,
Runspace currentRunspace,
PSCommand psCommand,
bool sendOutputToHost)
{
IEnumerable<TResult> executionResult = null;

using (var nestedPipeline = currentRunspace.CreateNestedPipeline())
{
foreach (var command in psCommand.Commands)
{
nestedPipeline.Commands.Add(command);
}

executionResult =
nestedPipeline
.Invoke()
.Select(pso => pso.BaseObject)
.Cast<TResult>();
}

// Write the output to the host if necessary
if (sendOutputToHost)
{
foreach (var line in executionResult)
{
powerShellContext.WriteOutput(line.ToString(), true);
}
}

return executionResult;
}
}
}

65 changes: 65 additions & 0 deletions src/PowerShellEditorServices/Session/PowerShell4Operations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace Microsoft.PowerShell.EditorServices.Session
{
internal class PowerShell4Operations : IVersionSpecificOperations
{
public void ConfigureDebugger(Runspace runspace)
{
#if !PowerShellv3
runspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
#endif
}

public virtual void PauseDebugger(Runspace runspace)
{
// The debugger cannot be paused in PowerShell v4.
throw new NotSupportedException("Debugger cannot be paused in PowerShell v4");
}

public IEnumerable<TResult> ExecuteCommandInDebugger<TResult>(
PowerShellContext powerShellContext,
Runspace currentRunspace,
PSCommand psCommand,
bool sendOutputToHost)
{
PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();

#if !PowerShellv3
if (sendOutputToHost)
{
outputCollection.DataAdded +=
(obj, e) =>
{
for (int i = e.Index; i < outputCollection.Count; i++)
{
powerShellContext.WriteOutput(
outputCollection[i].ToString(),
true);
}
};
}

DebuggerCommandResults commandResults =
currentRunspace.Debugger.ProcessCommand(
psCommand,
outputCollection);
#endif

return
outputCollection
.Select(pso => pso.BaseObject)
.Cast<TResult>();
}
}
}

20 changes: 20 additions & 0 deletions src/PowerShellEditorServices/Session/PowerShell5Operations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Management.Automation.Runspaces;

namespace Microsoft.PowerShell.EditorServices.Session
{
internal class PowerShell5Operations : PowerShell4Operations
{
public override void PauseDebugger(Runspace runspace)
{
#if !PowerShellv3 && !PowerShellv4
runspace.Debugger.SetDebuggerStepMode(true);
#endif
}
}
}

Loading