Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor GetCommandHandler to not use dynamic #1268

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,38 @@ 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)
{
// Get the default ParameterSet
string defaultParameterSet = null;
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the story on needing the for-loop instead of this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultParameterSet's might only exist on CmdletInfo and FunctionInfo.. so I could special case those types... but there are a lot of *Info's

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we're probably mostly going to deal with those types, I think that would make sense as an optimisation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a switch but still kept the loop for everything else (which atm is just ExternalScript).

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