Skip to content

Commit 082c27e

Browse files
author
maddieclayton
authored
Merge pull request Azure#4945 from maddieclayton/FixCompleters
Fix Argument Completers
2 parents 9fd5030 + 875a08e commit 082c27e

File tree

7 files changed

+181
-52
lines changed

7 files changed

+181
-52
lines changed

build.proj

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,22 +210,22 @@
210210
<CallTarget Targets="BuildSetupTest"/>
211211
<!-- moved copy tasks -->
212212

213-
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Latest &quot; " Condition="'$(Latest)' == 'true' "/>
214-
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Stack &quot; " Condition="'$(Stack)' == 'true' "/>
213+
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Latest &quot; " Condition="'$(Latest)' == 'true' and '$(CodeSign)' == 'false'"/>
214+
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Stack &quot; " Condition="'$(Stack)' == 'true' and '$(CodeSign)' == 'false'"/>
215215
<!-- Copying shortcut to be signed -->
216216
<Copy SourceFiles="$(LibraryRoot)tools\AzureRM\AzureRM.psd1"
217217
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Latest)' == 'true'"/>
218218
<Copy SourceFiles="$(LibraryRoot)tools\AzureRM\AzureRM.psm1"
219-
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Latest)' == 'true'"/>
219+
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Latest)' == 'true' and '$(CodeSign)' == 'false'"/>
220220

221221
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureRM\AzureRM.psd1"
222222
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
223223
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureRM\AzureRM.psm1"
224-
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
224+
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' and '$(CodeSign)' == 'false'"/>
225225
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureStack\AzureStack.psd1"
226226
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
227227
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureStack\AzureStack.psm1"
228-
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
228+
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' and '$(CodeSign)' == 'false'"/>
229229

230230
<Copy SourceFiles="$(LibraryRoot)setup\Setup\RemoveGalleryModules.ps1"
231231
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)"
@@ -381,18 +381,6 @@
381381
<DelaySignedAssembliesToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\Microsoft*Azure*Commands*.dll" Condition="'$(Stack)' == 'true' " />
382382
<DelaySignedAssembliesToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\Microsoft.Azure.Common.Extensions.dll" Condition="'$(Stack)' == 'true' " />
383383
<DelaySignedAssembliesToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\Microsoft.Azure.Management.Sql.Legacy.dll" Condition="'$(Stack)' == 'true' " />
384-
385-
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.ps1" Condition="'$(Latest)' == 'true' " />
386-
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.psm1" Condition="'$(Latest)' == 'true' " />
387-
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.ps1xml" Condition="'$(Latest)' == 'true' " />
388-
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.js" Condition="'$(Latest)' == 'true' " />
389-
<ScriptsToSign Include="$(LibraryRoot)setup\Setup\*.ps1" Condition="'$(Latest)' == 'true' "/>
390-
391-
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.ps1" Condition="'$(Stack)' == 'true' "/>
392-
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.psm1" Condition="'$(Stack)' == 'true' " />
393-
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.ps1xml" Condition="'$(Stack)' == 'true' " />
394-
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.js" Condition="'$(Stack)' == 'true' " />
395-
<!-- <ScriptsToSign Include="$(LibraryRoot)stacksetup\Setup\*.ps1" Condition="'$(Stack)' == 'true' " /> -->
396384
</ItemGroup>
397385

398386
<Message Importance="high" Text="$(LibrarySourceFolder)\Package\$(Configuration) does not contains any files to sign. Code sign will skip."
@@ -417,6 +405,39 @@
417405
ToolsPath="$(CIToolsPath)"
418406
Condition="!$(DelaySign) and '@(DelaySignedAssembliesToSign)' != ''"/>
419407

408+
<ValidateStrongNameSignatureTask
409+
WindowsSdkPath="$(WindowsSdkPath)"
410+
Assembly="%(DelaySignedAssembliesToSign.Identity)"
411+
ExpectedTokenSignature="$(StrongNameToken)"
412+
ExpectedDelaySigned="false"
413+
ContinueOnError="false"
414+
Condition="!$(DelaySign) and '@(DelaySignedAssembliesToSign)' != ''"/>
415+
416+
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Latest &quot; " Condition="'$(Latest)' == 'true' "/>
417+
<Exec Command="&quot;$(PowerShellCommand)&quot; -NonInteractive -NoLogo -NoProfile -Command &quot;. $(LibraryToolsFolder)\UpdateModules.ps1 $(Configuration) $(Scope) -Profile Stack &quot; " Condition="'$(Stack)' == 'true' "/>
418+
419+
<!-- Copying shortcut to be signed -->
420+
<Copy SourceFiles="$(LibraryRoot)tools\AzureRM\AzureRM.psm1"
421+
DestinationFolder="$(LibrarySourceFolder)\Package\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Latest)' == 'true' "/>
422+
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureRM\AzureRM.psm1"
423+
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
424+
<Copy SourceFiles="$(LibrarySourceFolder)\StackAdmin\AzureStack\AzureStack.psm1"
425+
DestinationFolder="$(LibrarySourceFolder)\Stack\$(Configuration)" Condition= " '$(NetCore)' == 'false' and '$(Stack)' == 'true' "/>
426+
427+
<ItemGroup>
428+
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.ps1" Condition="'$(Latest)' == 'true' " />
429+
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.psm1" Condition="'$(Latest)' == 'true' " />
430+
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.ps1xml" Condition="'$(Latest)' == 'true' " />
431+
<ScriptsToSign Include="$(LibrarySourceFolder)\Package\$(Configuration)\**\*.js" Condition="'$(Latest)' == 'true' " />
432+
<ScriptsToSign Include="$(LibraryRoot)setup\Setup\*.ps1" Condition="'$(Latest)' == 'true' "/>
433+
434+
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.ps1" Condition="'$(Stack)' == 'true' "/>
435+
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.psm1" Condition="'$(Stack)' == 'true' " />
436+
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.ps1xml" Condition="'$(Stack)' == 'true' " />
437+
<ScriptsToSign Include="$(LibrarySourceFolder)\Stack\$(Configuration)\**\*.js" Condition="'$(Stack)' == 'true' " />
438+
<!-- <ScriptsToSign Include="$(LibraryRoot)stacksetup\Setup\*.ps1" Condition="'$(Stack)' == 'true' " /> -->
439+
</ItemGroup>
440+
420441
<CodeSigningTask
421442
Description="Microsoft Azure PowerShell"
422443
Keywords="Microsoft Azure PowerShell"
@@ -428,14 +449,6 @@
428449
ToolsPath="$(CIToolsPath)"
429450
Condition="!$(DelaySign) and '@(ScriptsToSign)' != ''"/>
430451

431-
<ValidateStrongNameSignatureTask
432-
WindowsSdkPath="$(WindowsSdkPath)"
433-
Assembly="%(DelaySignedAssembliesToSign.Identity)"
434-
ExpectedTokenSignature="$(StrongNameToken)"
435-
ExpectedDelaySigned="false"
436-
ContinueOnError="false"
437-
Condition="!$(DelaySign) and '@(DelaySignedAssembliesToSign)' != ''"/>
438-
439452
<!-- We cannot verify .js files using Get-AuthenticodeSignature -->
440453
<VerifyAuthenticodeSignatureTask ProbingDirectory="$(PackageDirectory)\$(Configuration)"
441454
FileFilterPattern="microsoft.*.dll;system.*.dll;*.ps1;*.psm1;*.ps1xml;PowerShellSetup.Test.dll" Condition="'$(Latest)' == 'true'">

src/ResourceManager/Common/Commands.ResourceManager.Common/ArgumentCompleters/LocationCompleter.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters
3030
/// <summary>
3131
/// This attribute will allow the user to autocomplete the -Location parameter of a cmdlet with valid locations (as determined by the list of ResourceTypes given)
3232
/// </summary>
33-
public class LocationCompleterAttribute : ArgumentCompleterAttribute
33+
public class LocationCompleterAttribute : PSCompleterBaseAttribute
3434
{
3535
private static IDictionary<int, IDictionary<string, ICollection<string>>> _resourceTypeLocationDictionary = new ConcurrentDictionary<int, IDictionary<string, ICollection<string>>>();
3636
private static readonly object _lock = new object();
37+
private static string[] _resourceTypes;
3738

3839
protected static IDictionary<string, ICollection<string>> ResourceTypeLocationDictionary
3940
{
@@ -43,6 +44,7 @@ protected static IDictionary<string, ICollection<string>> ResourceTypeLocationDi
4344
{
4445
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
4546
var contextHash = HashContext(context);
47+
IDictionary<string, ICollection<string>> output;
4648
if (!_resourceTypeLocationDictionary.ContainsKey(contextHash))
4749
{
4850
try
@@ -59,34 +61,40 @@ protected static IDictionary<string, ICollection<string>> ResourceTypeLocationDi
5961
if (allProviders.Result != null)
6062
{
6163
_resourceTypeLocationDictionary[contextHash] = CreateLocationDictionary(allProviders.Result.ToList());
64+
output = _resourceTypeLocationDictionary[contextHash];
6265
}
6366
else
6467
{
65-
_resourceTypeLocationDictionary[contextHash] = CreateLocationDictionary(new List<Provider>());
68+
output = CreateLocationDictionary(new List<Provider>());
6669
#if DEBUG
6770
throw new Exception("Result from client.Providers is null");
6871
#endif
6972
}
7073
}
7174
else
7275
{
73-
_resourceTypeLocationDictionary[contextHash] = new ConcurrentDictionary<string, ICollection<string>>(StringComparer.OrdinalIgnoreCase);
76+
output = new ConcurrentDictionary<string, ICollection<string>>(StringComparer.OrdinalIgnoreCase);
7477
#if DEBUG
7578
throw new Exception(Resources.TimeOutForProviderList);
7679
#endif
7780
}
7881
}
7982
catch (Exception ex)
8083
{
81-
_resourceTypeLocationDictionary[contextHash] = new ConcurrentDictionary<string, ICollection<string>>(StringComparer.OrdinalIgnoreCase);
84+
output = new ConcurrentDictionary<string, ICollection<string>>(StringComparer.OrdinalIgnoreCase);
8285
if (ex == null) { }
8386
#if DEBUG
8487
throw ex;
8588
#endif
8689
}
8790
}
8891

89-
return _resourceTypeLocationDictionary[contextHash];
92+
else
93+
{
94+
output = _resourceTypeLocationDictionary[contextHash];
95+
}
96+
97+
return output;
9098
}
9199
}
92100
}
@@ -96,8 +104,14 @@ protected static IDictionary<string, ICollection<string>> ResourceTypeLocationDi
96104
/// Example: [Parameter(ParameterSetName = ListByNameInTenantParameterSet, ValueFromPipelineByPropertyName = true, Mandatory = false), LocationCompleter(new string[] { "Microsoft.Batch/operationss" })]
97105
/// </summary>
98106
/// <param name="resourceTypes"></param>
99-
public LocationCompleterAttribute(params string[] resourceTypes) : base(CreateScriptBlock(resourceTypes))
107+
public LocationCompleterAttribute(params string[] resourceTypes)
108+
{
109+
_resourceTypes = resourceTypes;
110+
}
111+
112+
public override string[] GetCompleterValues()
100113
{
114+
return FindLocations(_resourceTypes);
101115
}
102116

103117
public static string[] FindLocations(string[] resourceTypes)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters
4+
{
5+
/// <summary>
6+
/// This attribute will allow the user to autocomplete the -Location parameter of a cmdlet with valid locations (as determined by the list of ResourceTypes given)
7+
/// </summary>
8+
abstract public class PSCompleterBaseAttribute : Attribute
9+
{
10+
abstract public string[] GetCompleterValues();
11+
}
12+
}

src/ResourceManager/Common/Commands.ResourceManager.Common/ArgumentCompleters/ResourceGroupCompleter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters
2929
/// <summary>
3030
/// This attribute will allow the user to autocomplete the -ResourceGroup parameter of a cmdlet with valid resource groups
3131
/// </summary>
32-
public class ResourceGroupCompleterAttribute : ArgumentCompleterAttribute
32+
public class ResourceGroupCompleterAttribute : PSCompleterBaseAttribute
3333
{
3434
private static IDictionary<int, IList<String>> _resourceGroupNamesDictionary = new ConcurrentDictionary<int, IList<string>>();
3535
private static readonly object _lock = new object();
@@ -95,10 +95,15 @@ protected static IList<String> ResourceGroupNames
9595
/// Example: [Parameter(ParameterSetName = ListByNameInTenantParameterSet, ValueFromPipelineByPropertyName = true, Mandatory = false), ResourceGroupCompleter()]
9696
/// </summary>
9797
/// <param name="resourceTypes"></param>
98-
public ResourceGroupCompleterAttribute() : base(CreateScriptBlock())
98+
public ResourceGroupCompleterAttribute()
9999
{
100100
}
101101

102+
public override string[] GetCompleterValues()
103+
{
104+
return GetResourceGroups();
105+
}
106+
102107
public static string[] GetResourceGroups()
103108
{
104109
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;

src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
</ItemGroup>
6262
<ItemGroup>
6363
<Compile Include="AccessTokenExtensions.cs" />
64+
<Compile Include="ArgumentCompleters\PSCompleterBase.cs" />
6465
<Compile Include="AzureRmCmdlet.cs" />
6566
<Compile Include="Generated\DeploymentOperations.cs" />
6667
<Compile Include="Generated\DeploymentOperationsExtensions.cs" />

tools/AzureRM.Example.psm1

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,22 @@ Set-StrictMode -Version Latest
1111

1212
%IMPORTED-DEPENDENCIES%
1313

14-
$FilteredCommands = %COMMANDS%
14+
if ($PSVersionTable.PSVersion.Major -ge 5)
15+
{
16+
$completerCommands = %COMPLETERCOMMANDS%
17+
18+
$completerCommands | ForEach-Object {
19+
$completerObject = New-Object $_.AttributeType -ArgumentList $_.ArgumentList
20+
Register-ArgumentCompleter -CommandName $_.Command -ParameterName $_.Parameter -ScriptBlock {
21+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
22+
23+
$locations = $completerObject.GetCompleterValues()
24+
$locations | Where-Object { $_ -Like "$wordToComplete*" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }
25+
}
26+
}
27+
}
28+
29+
$FilteredCommands = %DEFAULTRGCOMMANDS%
1530

1631
$FilteredCommands | ForEach-Object {
1732
$global:PSDefaultParameterValues.Add($_,

0 commit comments

Comments
 (0)