|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Management.Automation; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.PowerShell.EditorServices; |
| 12 | +using OmniSharp.Extensions.Embedded.MediatR; |
| 13 | +using OmniSharp.Extensions.JsonRpc; |
| 14 | + |
| 15 | +namespace PowerShellEditorServices.Engine.Services.Handlers |
| 16 | +{ |
| 17 | + [Serial, Method("powerShell/getCommand")] |
| 18 | + public interface IGetCommandHandler : IJsonRpcRequestHandler<GetCommandParams, List<PSCommandMessage>> { } |
| 19 | + |
| 20 | + public class GetCommandParams : IRequest<List<PSCommandMessage>> { } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Describes the message to get the details for a single PowerShell Command |
| 24 | + /// from the current session |
| 25 | + /// </summary> |
| 26 | + public class PSCommandMessage |
| 27 | + { |
| 28 | + public string Name { get; set; } |
| 29 | + public string ModuleName { get; set; } |
| 30 | + public string DefaultParameterSet { get; set; } |
| 31 | + public Dictionary<string, ParameterMetadata> Parameters { get; set; } |
| 32 | + public System.Collections.ObjectModel.ReadOnlyCollection<CommandParameterSetInfo> ParameterSets { get; set; } |
| 33 | + } |
| 34 | + |
| 35 | + public class GetCommandHandler : IGetCommandHandler |
| 36 | + { |
| 37 | + private readonly ILogger<GetCommandHandler> _logger; |
| 38 | + private readonly PowerShellContextService _powerShellContextService; |
| 39 | + |
| 40 | + public GetCommandHandler(ILoggerFactory factory, PowerShellContextService powerShellContextService) |
| 41 | + { |
| 42 | + _logger = factory.CreateLogger<GetCommandHandler>(); |
| 43 | + _powerShellContextService = powerShellContextService; |
| 44 | + } |
| 45 | + |
| 46 | + public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, CancellationToken cancellationToken) |
| 47 | + { |
| 48 | + PSCommand psCommand = new PSCommand(); |
| 49 | + |
| 50 | + // Executes the following: |
| 51 | + // Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name |
| 52 | + psCommand |
| 53 | + .AddCommand("Microsoft.PowerShell.Core\\Get-Command") |
| 54 | + .AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" }) |
| 55 | + .AddCommand("Microsoft.PowerShell.Utility\\Select-Object") |
| 56 | + .AddParameter("Property", new[] { "Name", "ModuleName" }) |
| 57 | + .AddCommand("Microsoft.PowerShell.Utility\\Sort-Object") |
| 58 | + .AddParameter("Property", "Name"); |
| 59 | + |
| 60 | + IEnumerable<PSObject> result = await _powerShellContextService.ExecuteCommandAsync<PSObject>(psCommand); |
| 61 | + |
| 62 | + var commandList = new List<PSCommandMessage>(); |
| 63 | + if (result != null) |
| 64 | + { |
| 65 | + foreach (dynamic command in result) |
| 66 | + { |
| 67 | + commandList.Add(new PSCommandMessage |
| 68 | + { |
| 69 | + Name = command.Name, |
| 70 | + ModuleName = command.ModuleName, |
| 71 | + Parameters = command.Parameters, |
| 72 | + ParameterSets = command.ParameterSets, |
| 73 | + DefaultParameterSet = command.DefaultParameterSet |
| 74 | + }); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return commandList; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments