|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Management.Automation; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using MediatR; |
| 9 | +using OmniSharp.Extensions.JsonRpc; |
| 10 | +using Microsoft.PowerShell.EditorServices.Services.PowerShell; |
| 11 | +using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution; |
| 12 | + |
| 13 | +namespace Microsoft.PowerShell.EditorServices.Handlers |
| 14 | +{ |
| 15 | + [Serial, Method("powerShell/getModule", Direction.ClientToServer)] |
| 16 | + internal interface IGetModuleHandler : IJsonRpcRequestHandler<GetModuleParams, PSModuleMessage> { } |
| 17 | + |
| 18 | + internal class GetModuleParams : IRequest<PSModuleMessage> |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// The name of the module to retrieve metadata for. |
| 22 | + /// </summary> |
| 23 | + public string Name { get; set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// An optional specific version of the module. When omitted, the newest |
| 27 | + /// available version is returned. |
| 28 | + /// </summary> |
| 29 | + public string Version { get; set; } |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Describes the metadata for a single PowerShell module, used to populate |
| 34 | + /// the Command Explorer's module tooltips. |
| 35 | + /// </summary> |
| 36 | + internal class PSModuleMessage |
| 37 | + { |
| 38 | + public string Name { get; set; } |
| 39 | + public string Version { get; set; } |
| 40 | + public string Description { get; set; } |
| 41 | + public string Path { get; set; } |
| 42 | + public string Author { get; set; } |
| 43 | + public string CompanyName { get; set; } |
| 44 | + public string ProjectUri { get; set; } |
| 45 | + public string PowerShellVersion { get; set; } |
| 46 | + } |
| 47 | + |
| 48 | + internal class GetModuleHandler : IGetModuleHandler |
| 49 | + { |
| 50 | + private readonly IInternalPowerShellExecutionService _executionService; |
| 51 | + |
| 52 | + public GetModuleHandler(IInternalPowerShellExecutionService executionService) => _executionService = executionService; |
| 53 | + |
| 54 | + public async Task<PSModuleMessage> Handle(GetModuleParams request, CancellationToken cancellationToken) |
| 55 | + { |
| 56 | + if (string.IsNullOrEmpty(request.Name)) |
| 57 | + { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + // Resolve a module's metadata from the available modules, pinning to a |
| 62 | + // specific version when requested and otherwise taking the newest. |
| 63 | + const string GetModuleScript = @" |
| 64 | + [System.Diagnostics.DebuggerHidden()] |
| 65 | + [CmdletBinding()] |
| 66 | + param ( |
| 67 | + [String]$Name, |
| 68 | + [String]$Version |
| 69 | + ) |
| 70 | + $modules = Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name $Name -ErrorAction Ignore |
| 71 | + if ($Version) { |
| 72 | + $modules = $modules | Microsoft.PowerShell.Core\Where-Object { $_.Version.ToString() -eq $Version } |
| 73 | + } |
| 74 | + $module = $modules | Microsoft.PowerShell.Utility\Sort-Object Version -Descending | Microsoft.PowerShell.Utility\Select-Object -First 1 |
| 75 | + if ($null -eq $module) { |
| 76 | + return |
| 77 | + } |
| 78 | + [PSCustomObject]@{ |
| 79 | + Name = $module.Name |
| 80 | + Version = $module.Version.ToString() |
| 81 | + Description = $module.Description |
| 82 | + Path = $module.Path |
| 83 | + Author = $module.Author |
| 84 | + CompanyName = $module.CompanyName |
| 85 | + ProjectUri = if ($module.ProjectUri) { $module.ProjectUri.ToString() } else { '' } |
| 86 | + PowerShellVersion = if ($module.PowerShellVersion) { $module.PowerShellVersion.ToString() } else { '' } |
| 87 | + } |
| 88 | + "; |
| 89 | + |
| 90 | + PSCommand getModuleCommand = new PSCommand() |
| 91 | + .AddScript(GetModuleScript, useLocalScope: true) |
| 92 | + .AddParameter("Name", request.Name) |
| 93 | + .AddParameter("Version", request.Version); |
| 94 | + |
| 95 | + IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>( |
| 96 | + getModuleCommand, |
| 97 | + cancellationToken, |
| 98 | + new PowerShellExecutionOptions |
| 99 | + { |
| 100 | + ThrowOnError = false |
| 101 | + }).ConfigureAwait(false); |
| 102 | + |
| 103 | + PSObject result = results is { Count: > 0 } ? results[0] : null; |
| 104 | + if (result is null) |
| 105 | + { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return new PSModuleMessage |
| 110 | + { |
| 111 | + Name = GetPropertyString(result, "Name"), |
| 112 | + Version = GetPropertyString(result, "Version"), |
| 113 | + Description = GetPropertyString(result, "Description"), |
| 114 | + Path = GetPropertyString(result, "Path"), |
| 115 | + Author = GetPropertyString(result, "Author"), |
| 116 | + CompanyName = GetPropertyString(result, "CompanyName"), |
| 117 | + ProjectUri = GetPropertyString(result, "ProjectUri"), |
| 118 | + PowerShellVersion = GetPropertyString(result, "PowerShellVersion") |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + private static string GetPropertyString(PSObject psObject, string propertyName) |
| 123 | + => psObject.Properties[propertyName]?.Value as string ?? string.Empty; |
| 124 | + } |
| 125 | +} |
0 commit comments