Skip to content

Commit

Permalink
Refactor GetCommandHandler to not use dynamic (#1268)
Browse files Browse the repository at this point in the history
* Refactor GetCommandHandler to not use dynamic

* streamline a couple CommandType's

Co-authored-by: Tyler Leonhardt (POWERSHELL) <tyleonha@microsoft.com>
  • Loading branch information
TylerLeonhardt and TylerLeonhardt authored Apr 23, 2020
1 parent 6567d5c commit fc1a95a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,53 @@ public async Task<List<PSCommandMessage>> Handle(GetCommandParams request, Cance
PSCommand psCommand = new PSCommand();

// Executes the following:
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Select-Object -Property Name,ModuleName | Sort-Object -Property Name
// Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name
psCommand
.AddCommand("Microsoft.PowerShell.Core\\Get-Command")
.AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" })
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object")
.AddParameter("Property", new[] { "Name", "ModuleName" })
.AddCommand("Microsoft.PowerShell.Utility\\Sort-Object")
.AddParameter("Property", "Name");

IEnumerable<PSObject> result = await _powerShellContextService.ExecuteCommandAsync<PSObject>(psCommand).ConfigureAwait(false);
IEnumerable<CommandInfo> result = await _powerShellContextService.ExecuteCommandAsync<CommandInfo>(psCommand).ConfigureAwait(false);

var commandList = new List<PSCommandMessage>();
if (result != null)
{
foreach (dynamic command in result)
foreach (CommandInfo command in result)
{
// Some info objects have a quicker way to get the DefaultParameterSet. These
// are also the most likely to show up so win-win.
string defaultParameterSet = null;
switch (command)
{
case CmdletInfo info:
defaultParameterSet = info.DefaultParameterSet;
break;
case FunctionInfo info:
defaultParameterSet = info.DefaultParameterSet;
break;
}

if (defaultParameterSet == null)
{
// Try to get the default ParameterSet if it isn't streamlined (ExternalScriptInfo for example)
foreach (CommandParameterSetInfo parameterSetInfo in command.ParameterSets)
{
if (parameterSetInfo.IsDefault)
{
defaultParameterSet = parameterSetInfo.Name;
break;
}
}
}

commandList.Add(new PSCommandMessage
{
Name = command.Name,
ModuleName = command.ModuleName,
Parameters = command.Parameters,
ParameterSets = command.ParameterSets,
DefaultParameterSet = command.DefaultParameterSet
DefaultParameterSet = defaultParameterSet
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1004,8 +1004,8 @@ await LanguageClient.SendRequest<EvaluateResponseBody>(
[Fact]
public async Task CanSendGetCommandRequest()
{
List<PSCommandMessage> pSCommandMessages =
await LanguageClient.SendRequest<List<PSCommandMessage>>("powerShell/getCommand", new GetCommandParams());
List<object> pSCommandMessages =
await LanguageClient.SendRequest<List<object>>("powerShell/getCommand", new GetCommandParams());

Assert.NotEmpty(pSCommandMessages);
// There should be at least 20 commands or so.
Expand Down

0 comments on commit fc1a95a

Please sign in to comment.