Skip to content

Add new Commands submodule to expose useful commands inside editor #487

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
Jun 3, 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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ lock
registered_data.ini
.vs/
.dotnet/
module/

module/Plaster
module/PSScriptAnalyzer
docs/_site/
docs/_repo/
docs/metadata/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#
# Module manifest for module 'PowerShellEditorServices'
#
# Generated by: daviwil
#
# Generated on: 5/12/2016
#

@{

# Script module or binary module file associated with this manifest.
RootModule = 'PowerShellEditorServices.Commands.psm1'

# Version number of this module.
ModuleVersion = '1.0.0'

# ID used to uniquely identify this module
GUID = '9ca15887-53a2-479a-9cda-48d26bcb6c47'

# Author of this module
Author = 'Microsoft'

# Company or vendor of this module
CompanyName = 'Microsoft'

# Copyright statement for this module
Copyright = '(c) 2016 Microsoft. All rights reserved.'

# Description of the functionality provided by this module
Description = 'Provides internal commands for PowerShell Editor Services that only work in an editor session.'

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ''

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ''

# Minimum version of Microsoft .NET Framework required by this module
# DotNetFrameworkVersion = ''

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ''

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller's environment prior to importing this module.
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Register-EditorCommand', 'Unregister-EditorCommand')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

# Variables to export from this module
VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()

# DSC resources to export from this module
# DscResourcesToExport = @()

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{

PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
# Tags = @()

# A URL to the license for this module.
# LicenseUri = ''

# A URL to the main website for this project.
# ProjectUri = ''

# A URL to an icon representing this module.
# IconUri = ''

# ReleaseNotes of this module
# ReleaseNotes = ''

} # End of PSData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# TODO: Use a better script loading process here
. $PSScriptRoot\Public\CmdletInterface.ps1
6 changes: 6 additions & 0 deletions src/PowerShellEditorServices.Host/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -190,6 +191,11 @@ private async void OnLanguageServiceClientConnect(
this.editorSession,
serverChannel);

await this.editorSession.PowerShellContext.ImportCommandsModule(
Path.Combine(
Path.GetDirectoryName(this.GetType().GetTypeInfo().Assembly.Location),
@"..\..\Commands"));

await this.languageServer.Start();
}

Expand Down
40 changes: 0 additions & 40 deletions src/PowerShellEditorServices/Extensions/ExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,46 +81,6 @@ public async Task Initialize(IEditorOperations editorOperations)
"psEditor",
this.EditorObject);
}

// Load the cmdlet interface
Type thisType = this.GetType();
Stream resourceStream =
thisType.GetTypeInfo().Assembly.GetManifestResourceStream(
thisType.Namespace + ".CmdletInterface.ps1");

using (StreamReader reader = new StreamReader(resourceStream))
{
// Create a temporary folder path
string randomFileNamePart =
Path.GetFileNameWithoutExtension(
Path.GetRandomFileName());

string tempScriptPath =
Path.Combine(
Path.GetTempPath(),
"PSES_ExtensionCmdlets_" + randomFileNamePart + ".ps1");

Logger.Write(
LogLevel.Verbose,
"Executing extension API cmdlet script at path: " + tempScriptPath);

// Read the cmdlet interface script and write it to a temporary
// file so that we don't have to execute the full file contents
// directly. This keeps the script execution from creating a
// lot of noise in the verbose logs.
string cmdletInterfaceScript = reader.ReadToEnd();
File.WriteAllText(
tempScriptPath,
cmdletInterfaceScript);

await this.PowerShellContext.ExecuteScriptString(
". " + tempScriptPath,
writeInputToHost: false,
writeOutputToHost: false);

// Delete the temporary file
File.Delete(tempScriptPath);
}
}

/// <summary>
Expand Down
6 changes: 0 additions & 6 deletions src/PowerShellEditorServices/PowerShellEditorServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Extensions\CmdletInterface.ps1">
<LogicalName>Microsoft.PowerShell.EditorServices.Extensions.CmdletInterface.ps1</LogicalName>
</EmbeddedResource>
</ItemGroup>

<PropertyGroup>
<!-- NOTE: -->
<!-- For PowerShell v5 there are some differences between the APIs released with -->
Expand Down
19 changes: 19 additions & 0 deletions src/PowerShellEditorServices/Session/PowerShellContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,25 @@ public void Initialize(
this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait();
}

/// <summary>
/// Imports the PowerShellEditorServices.Commands module into
/// the runspace. This method will be moved somewhere else soon.
/// </summary>
/// <param name="moduleBasePath"></param>
/// <returns></returns>
public Task ImportCommandsModule(string moduleBasePath)
{
PSCommand importCommand = new PSCommand();
importCommand
.AddCommand("Import-Module")
.AddArgument(
Path.Combine(
moduleBasePath,
"PowerShellEditorServices.Commands.psd1"));

return this.ExecuteCommand<PSObject>(importCommand, false, false);
}

private static bool CheckIfRunspaceNeedsEventHandlers(RunspaceDetails runspaceDetails)
{
// The only types of runspaces that need to be configured are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ private enum EventType
public async Task InitializeAsync()
{
this.powerShellContext = PowerShellContextFactory.Create();
await this.powerShellContext.ImportCommandsModule(@"..\..\..\..\..\module\PowerShellEditorServices\Commands");

this.extensionService = new ExtensionService(this.powerShellContext);
this.editorOperations = new TestEditorOperations();

Expand Down