diff --git a/.gitignore b/.gitignore index 6094175c5..b6c409934 100644 --- a/.gitignore +++ b/.gitignore @@ -27,8 +27,8 @@ lock registered_data.ini .vs/ .dotnet/ -module/ - +module/Plaster +module/PSScriptAnalyzer docs/_site/ docs/_repo/ docs/metadata/ diff --git a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 new file mode 100644 index 000000000..ba6bd62a9 --- /dev/null +++ b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 @@ -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 = '' + +} + diff --git a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psm1 b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psm1 new file mode 100644 index 000000000..62ac9af39 --- /dev/null +++ b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psm1 @@ -0,0 +1,3 @@ + +# TODO: Use a better script loading process here +. $PSScriptRoot\Public\CmdletInterface.ps1 \ No newline at end of file diff --git a/src/PowerShellEditorServices/Extensions/BuiltInCommands.ps1 b/module/PowerShellEditorServices/Commands/Private/BuiltInCommands.ps1 similarity index 100% rename from src/PowerShellEditorServices/Extensions/BuiltInCommands.ps1 rename to module/PowerShellEditorServices/Commands/Private/BuiltInCommands.ps1 diff --git a/src/PowerShellEditorServices/Extensions/CmdletInterface.ps1 b/module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 similarity index 100% rename from src/PowerShellEditorServices/Extensions/CmdletInterface.ps1 rename to module/PowerShellEditorServices/Commands/Public/CmdletInterface.ps1 diff --git a/src/PowerShellEditorServices.Host/EditorServicesHost.cs b/src/PowerShellEditorServices.Host/EditorServicesHost.cs index dde7a2abe..fd1ebb761 100644 --- a/src/PowerShellEditorServices.Host/EditorServicesHost.cs +++ b/src/PowerShellEditorServices.Host/EditorServicesHost.cs @@ -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; @@ -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(); } diff --git a/src/PowerShellEditorServices/Extensions/ExtensionService.cs b/src/PowerShellEditorServices/Extensions/ExtensionService.cs index 5a451ceb9..ac6b6383c 100644 --- a/src/PowerShellEditorServices/Extensions/ExtensionService.cs +++ b/src/PowerShellEditorServices/Extensions/ExtensionService.cs @@ -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); - } } /// diff --git a/src/PowerShellEditorServices/PowerShellEditorServices.csproj b/src/PowerShellEditorServices/PowerShellEditorServices.csproj index f8be04c10..f836409b8 100644 --- a/src/PowerShellEditorServices/PowerShellEditorServices.csproj +++ b/src/PowerShellEditorServices/PowerShellEditorServices.csproj @@ -8,12 +8,6 @@ $(PackageTargetFallback);dnxcore50;portable-net45+win8 - - - Microsoft.PowerShell.EditorServices.Extensions.CmdletInterface.ps1 - - - diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 179bb06fd..7a87c2363 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -252,6 +252,25 @@ public void Initialize( this.runspaceWaitQueue.EnqueueAsync(runspaceHandle).Wait(); } + /// + /// Imports the PowerShellEditorServices.Commands module into + /// the runspace. This method will be moved somewhere else soon. + /// + /// + /// + public Task ImportCommandsModule(string moduleBasePath) + { + PSCommand importCommand = new PSCommand(); + importCommand + .AddCommand("Import-Module") + .AddArgument( + Path.Combine( + moduleBasePath, + "PowerShellEditorServices.Commands.psd1")); + + return this.ExecuteCommand(importCommand, false, false); + } + private static bool CheckIfRunspaceNeedsEventHandlers(RunspaceDetails runspaceDetails) { // The only types of runspaces that need to be configured are: diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs index 6c085d508..ecfe7f7be 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionServiceTests.cs @@ -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();