|
| 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 Microsoft.PowerShell.EditorServices.Utility; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Management.Automation; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Microsoft.PowerShell.EditorServices |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Provides utility methods for working with PowerShell commands. |
| 16 | + /// </summary> |
| 17 | + public static class CommandHelpers |
| 18 | + { |
| 19 | + private static readonly HashSet<string> NounBlackList = |
| 20 | + new HashSet<string> |
| 21 | + { |
| 22 | + "Module", |
| 23 | + "Script", |
| 24 | + "Package", |
| 25 | + "PackageProvider", |
| 26 | + "PackageSource", |
| 27 | + "InstalledModule", |
| 28 | + "InstalledScript", |
| 29 | + "ScriptFileInfo", |
| 30 | + "PSRepository" |
| 31 | + }; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets the CommandInfo instance for a command with a particular name. |
| 35 | + /// </summary> |
| 36 | + /// <param name="commandName">The name of the command.</param> |
| 37 | + /// <param name="powerShellContext">The PowerShellContext to use for running Get-Command.</param> |
| 38 | + /// <returns>A CommandInfo object with details about the specified command.</returns> |
| 39 | + public static async Task<CommandInfo> GetCommandInfoAsync( |
| 40 | + string commandName, |
| 41 | + PowerShellContextService powerShellContext) |
| 42 | + { |
| 43 | + Validate.IsNotNull(nameof(commandName), commandName); |
| 44 | + |
| 45 | + // Make sure the command's noun isn't blacklisted. This is |
| 46 | + // currently necessary to make sure that Get-Command doesn't |
| 47 | + // load PackageManagement or PowerShellGet because they cause |
| 48 | + // a major slowdown in IntelliSense. |
| 49 | + var commandParts = commandName.Split('-'); |
| 50 | + if (commandParts.Length == 2 && NounBlackList.Contains(commandParts[1])) |
| 51 | + { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + PSCommand command = new PSCommand(); |
| 56 | + command.AddCommand(@"Microsoft.PowerShell.Core\Get-Command"); |
| 57 | + command.AddArgument(commandName); |
| 58 | + command.AddParameter("ErrorAction", "Ignore"); |
| 59 | + |
| 60 | + return |
| 61 | + (await powerShellContext |
| 62 | + .ExecuteCommandAsync<PSObject>(command, false, false)) |
| 63 | + .Select(o => o.BaseObject) |
| 64 | + .OfType<CommandInfo>() |
| 65 | + .FirstOrDefault(); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Gets the command's "Synopsis" documentation section. |
| 70 | + /// </summary> |
| 71 | + /// <param name="commandInfo">The CommandInfo instance for the command.</param> |
| 72 | + /// <param name="powerShellContext">The PowerShellContext to use for getting command documentation.</param> |
| 73 | + /// <returns></returns> |
| 74 | + public static async Task<string> GetCommandSynopsisAsync( |
| 75 | + CommandInfo commandInfo, |
| 76 | + PowerShellContextService powerShellContext) |
| 77 | + { |
| 78 | + string synopsisString = string.Empty; |
| 79 | + |
| 80 | + if (commandInfo != null && |
| 81 | + (commandInfo.CommandType == CommandTypes.Cmdlet || |
| 82 | + commandInfo.CommandType == CommandTypes.Function || |
| 83 | + commandInfo.CommandType == CommandTypes.Filter)) |
| 84 | + { |
| 85 | + PSCommand command = new PSCommand(); |
| 86 | + command.AddCommand(@"Microsoft.PowerShell.Core\Get-Help"); |
| 87 | + command.AddArgument(commandInfo); |
| 88 | + command.AddParameter("ErrorAction", "Ignore"); |
| 89 | + |
| 90 | + var results = await powerShellContext.ExecuteCommandAsync<PSObject>(command, false, false); |
| 91 | + PSObject helpObject = results.FirstOrDefault(); |
| 92 | + |
| 93 | + if (helpObject != null) |
| 94 | + { |
| 95 | + // Extract the synopsis string from the object |
| 96 | + synopsisString = |
| 97 | + (string)helpObject.Properties["synopsis"].Value ?? |
| 98 | + string.Empty; |
| 99 | + |
| 100 | + // Ignore the placeholder value for this field |
| 101 | + if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.CurrentCultureIgnoreCase)) |
| 102 | + { |
| 103 | + synopsisString = string.Empty; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return synopsisString; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments