Skip to content

Fix Argument Completers #4945

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

Merged
merged 24 commits into from
Nov 21, 2017
Merged

Fix Argument Completers #4945

merged 24 commits into from
Nov 21, 2017

Conversation

maddieclayton
Copy link
Contributor

@maddieclayton maddieclayton commented Nov 8, 2017

Description

#4910
Add script to psm1 file that will register Location/ResourceGroup argument completers.


This checklist is used to make sure that common guidelines for a pull request are followed. You can find a more complete discussion of PowerShell cmdlet best practices here.

General Guidelines

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.
  • The pull request does not introduce breaking changes (unless a major version change occurs in the assembly and module).

Testing Guidelines

  • Pull request includes test coverage for the included changes.
  • PowerShell scripts used in tests should do any necessary setup as part of the test or suite setup, and should not use hard-coded values for locations or existing resources.

Cmdlet Signature Guidelines

  • New cmdlets that make changes or have side effects should implement ShouldProcess and have SupportShouldProcess=true specified in the cmdlet attribute. You can find more information on ShouldProcess here.
  • Cmdlet specifies OutputType attribute if any output is produced - if the cmdlet produces no output, it should implement a PassThru parameter.

Cmdlet Parameter Guidelines

  • Parameter types should not expose types from the management library - complex parameter types should be defined in the module.
  • Complex parameter types are discouraged - a parameter type should be simple types as often as possible. If complex types are used, they should be shallow and easily creatable from a constructor or another cmdlet.
  • Cmdlet parameter sets should be mutually exclusive - each parameter set must have at least one mandatory parameter not in other parameter sets.

$ResourceGroupCommands = %RGCCOMMANDS%

$CommonAssembly = [Reflection.Assembly]::LoadFrom('.\Microsoft.Azure.Commands.ResourceManager.Common.dll')
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to define $CommonAssembly? This assembly should be loaded in the ImportedDependencies above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

$resourceGroupCompleterCommands = Find-CompleterAttribute -CompleterName "ResourceGroupCompleterAttribute" -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath -isRMModule $isRMModule
$template = $template -replace "%RGCCOMMANDS%", $resourceGroupCompleterCommands

$locationCompleterCommands = Find-CompleterAttribute -CompleterName "LocationCompleterAttribute" -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath -isRMModule $isRMModule
Copy link
Member

Choose a reason for hiding this comment

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

what is the result if the list is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if the list is empty, an empty list will be inserted into the template and no completers will be registered in the foreach loop,

@@ -122,22 +191,22 @@ $templateLocation = "$PSScriptRoot\AzureRM.Example.psm1"
if (($scope -eq 'All') -or $publishToLocal ) {
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
Write-Host "Updating profile module"
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation $true
Copy link
Member

Choose a reason for hiding this comment

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

this last parameter should be a named parameter, so there is some indication of what this is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

if ($completerAttribute -ne $null) {
$constructedCommands += "@('" + $currentCmdlet.GetCustomAttributes("System.Management.Automation.CmdletAttribute").VerbName + "-" + $currentCmdlet.GetCustomAttributes("System.Management.Automation.CmdletAttribute").NounName + "', "
$constructedCommands += "'" + $_.Name + "', "
if ($completerAttribute.ConstructorArguments.Count -eq 0)
Copy link
Member

Choose a reason for hiding this comment

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

this would be simplified if you store the cmdletattribute in a varaible:

$AtrributeTypeName="System.Management.Automation.CmdletAttribute"
$cmdletAttribute = $currentCmdlet.GetCustomAttributes($attributeTypeName)

It also seems like the result should be a list of hashtables @{CmdletName="<name>"; CmdletType="<typeName>"} this will make any code that uses this list a bit easier to read.

Finally, I think we should add some methods to the attribute itself to generate the list of constructor argument values, i.e. $completerAttribute.GetConstructorArgumentList() .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with the hashtable is that we actual need three values: the cmdlet name, the parameter name, and the attribute arguments (for the location completer). I could make a hashtable of hashtables, but I think that the list of lists might be easier to read in this case. If you would like me to change it let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrote the code adding a method for generating the list of constructor arguments, but it actually ended up being more convoluted because I had to create a new LocationCompleterAttribute object and convert the list of strings from c# to powershell. I'll leave this how it is for now, but let me know if you would rather creating a new method.

@@ -26,7 +26,8 @@ function Create-ModulePsm1
[CmdletBinding()]
param(
[string]$ModulePath,
[string]$TemplatePath
[string]$TemplatePath,
[bool]$isRMModule
Copy link
Member

Choose a reason for hiding this comment

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

Follow the conventiosn with capital i

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this is still lower case 'i'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yikes. Fixed.

{
}

public virtual string[] GetCompleterValues()
Copy link
Member

Choose a reason for hiding this comment

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

You can make this abstract, so that no impementation is necessary (If the class is abstract, it also cannot be created).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -26,7 +26,8 @@ function Create-ModulePsm1
[CmdletBinding()]
param(
[string]$ModulePath,
[string]$TemplatePath
[string]$TemplatePath,
[bool]$isRMModule
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this is still lower case 'i'

@maddieclayton
Copy link
Contributor Author

@azuresdkci test this please

@maddieclayton
Copy link
Contributor Author

@maddieclayton
Copy link
Contributor Author

maddieclayton commented Nov 21, 2017

@markcowl markcowl removed their assignment Nov 21, 2017
@markcowl
Copy link
Member

@maddieclayton I've added the posh-pack and release builds below as well - as long as the packaging job produces usable debug build, and the release job passes (does not need to produce a working build), then we are good to merge this. Excellent work!

packaging run: https://azuresdkci.westus2.cloudapp.azure.com/view/PowerShell/job/posh-pack/23/

@markcowl
Copy link
Member

@maddieclayton maddieclayton merged commit 082c27e into Azure:preview Nov 21, 2017
@maddieclayton maddieclayton deleted the FixCompleters branch November 21, 2017 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants