Skip to content

Commit 9b78152

Browse files
author
maddieclayton
authored
Merge pull request Azure#4911 from maddieclayton/DefaultScript
Add ResourceGroup to DefaultParameterValues
2 parents 5193770 + 0fa53f9 commit 9b78152

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

tools/AzureRM.Example.psm1

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,16 @@
99
$PSDefaultParameterValues.Clear()
1010
Set-StrictMode -Version Latest
1111

12-
%IMPORTED-DEPENDENCIES%
12+
%IMPORTED-DEPENDENCIES%
13+
14+
$FilteredCommands = %COMMANDS%
15+
16+
$FilteredCommands | ForEach-Object {
17+
$global:PSDefaultParameterValues.Add($_,
18+
{
19+
$context = Get-AzureRmContext
20+
if (($context -ne $null) -and $context.ExtendedProperties.ContainsKey("Default Resource Group")) {
21+
$context.ExtendedProperties["Default Resource Group"]
22+
}
23+
})
24+
}

tools/UpdateModules.ps1

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ function Create-ModulePsm1
2626
[CmdletBinding()]
2727
param(
2828
[string]$ModulePath,
29-
[string]$TemplatePath
29+
[string]$TemplatePath,
30+
[bool]$AddDefaultParameters
3031
)
3132

3233
PROCESS
@@ -65,12 +66,89 @@ function Create-ModulePsm1
6566
$template = $template -replace "%MODULE-NAME%", $file.BaseName
6667
$template = $template -replace "%DATE%", [string](Get-Date)
6768
$template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules
69+
70+
$contructedCommands = Find-DefaultResourceGroupCmdlets -AddDefaultParameters $AddDefaultParameters -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath
71+
$template = $template -replace "%COMMANDS%", $contructedCommands
72+
6873
Write-Host "Writing psm1 manifest to $templateOutputPath"
6974
$template | Out-File -FilePath $templateOutputPath -Force
7075
$file = Get-Item -Path $templateOutputPath
7176
}
7277
}
7378

79+
function Find-DefaultResourceGroupCmdlets
80+
{
81+
[CmdletBinding()]
82+
param(
83+
[bool]$AddDefaultParameters,
84+
[Hashtable]$ModuleMetadata,
85+
[string]$ModulePath
86+
)
87+
PROCESS
88+
{
89+
if ($AddDefaultParameters)
90+
{
91+
$nestedModules = $ModuleMetadata.NestedModules
92+
$AllCmdlets = @()
93+
$nestedModules | ForEach-Object {
94+
$dllPath = Join-Path -Path $ModulePath -ChildPath $_
95+
$Assembly = [Reflection.Assembly]::LoadFrom($dllPath)
96+
$dllCmdlets = $Assembly.GetTypes() | Where-Object {$_.CustomAttributes.AttributeType.Name -contains "CmdletAttribute"}
97+
$AllCmdlets += $dllCmdlets
98+
}
99+
100+
$FilteredCommands = $AllCmdlets | Where-Object {Test-CmdletRequiredParameter -Cmdlet $_ -Parameter "ResourceGroupName"}
101+
102+
if ($FilteredCommands.Length -eq 0) {
103+
$contructedCommands = "@()"
104+
}
105+
else {
106+
$contructedCommands = "@("
107+
$FilteredCommands | ForEach-Object {
108+
$contructedCommands += "'" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").VerbName + "-" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").NounName + ":ResourceGroupName" + "',"
109+
}
110+
$contructedCommands = $contructedCommands -replace ".$",")"
111+
}
112+
113+
return $contructedCommands
114+
}
115+
116+
else {
117+
return "@()"
118+
}
119+
}
120+
}
121+
122+
function Test-CmdletRequiredParameter
123+
{
124+
[CmdletBinding()]
125+
param(
126+
[Object]$Cmdlet,
127+
[string]$Parameter
128+
)
129+
130+
PROCESS
131+
{
132+
$rgParameter = $Cmdlet.GetProperties() | Where-Object {$_.Name -eq $Parameter}
133+
if ($rgParameter -ne $null) {
134+
$parameterAttributes = $rgParameter.CustomAttributes | Where-Object {$_.AttributeType.Name -eq "ParameterAttribute"}
135+
$isMandatory = $true
136+
$parameterAttributes | ForEach-Object {
137+
$hasParameterSet = $_.NamedArguments | Where-Object {$_.MemberName -eq "ParameterSetName"}
138+
$MandatoryParam = $_.NamedArguments | Where-Object {$_.MemberName -eq "Mandatory"}
139+
if (($hasParameterSet -ne $null) -or (!$MandatoryParam.TypedValue.Value)) {
140+
$isMandatory = $false
141+
}
142+
}
143+
if ($isMandatory) {
144+
return $true
145+
}
146+
}
147+
148+
return $false
149+
}
150+
}
151+
74152
function Create-MinimumVersionEntry
75153
{
76154
[CmdletBinding()]
@@ -122,22 +200,22 @@ $templateLocation = "$PSScriptRoot\AzureRM.Example.psm1"
122200
if (($scope -eq 'All') -or $publishToLocal ) {
123201
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
124202
Write-Host "Updating profile module"
125-
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation
203+
Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation $true
126204
Write-Host "Updated profile module"
127205
}
128206

129207
if (($scope -eq 'All') -or ($scope -eq 'AzureStorage')) {
130208
$modulePath = "$packageFolder\$buildConfig\Storage\Azure.Storage"
131209
# Publish AzureStorage module
132210
Write-Host "Updating AzureStorage module from $modulePath"
133-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
211+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
134212
}
135213

136214
if (($scope -eq 'All') -or ($scope -eq 'ServiceManagement')) {
137215
$modulePath = "$packageFolder\$buildConfig\ServiceManagement\Azure"
138216
# Publish Azure module
139217
Write-Host "Updating ServiceManagement(aka Azure) module from $modulePath"
140-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
218+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
141219
}
142220

143221
$resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory
@@ -148,15 +226,15 @@ if ($scope -eq 'All') {
148226
if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) {
149227
$modulePath = $module.FullName
150228
Write-Host "Updating $module module from $modulePath"
151-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
229+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $true
152230
Write-Host "Updated $module module"
153231
}
154232
}
155233
} elseif ($scope -ne 'AzureRM') {
156234
$modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope"
157235
if (Test-Path $modulePath) {
158236
Write-Host "Updating $scope module from $modulePath"
159-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
237+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
160238
Write-Host "Updated $scope module"
161239
} else {
162240
Write-Error "Can not find module with name $scope to publish"
@@ -169,17 +247,17 @@ if (($scope -eq 'All') -or ($scope -eq 'AzureRM')) {
169247
{
170248
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureRM"
171249
Write-Host "Updating AzureRM module from $modulePath"
172-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
250+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
173251
Write-Host "Updated AzureRM module"
174252
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureStack"
175253
Write-Host "Updating AzureRM module from $modulePath"
176-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
254+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
177255
Write-Host "Updated AzureStack module"
178256
}
179257
else {
180258
$modulePath = "$PSScriptRoot\AzureRM"
181259
Write-Host "Updating AzureRM module from $modulePath"
182-
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
260+
Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
183261
Write-Host "Updated Azure module"
184262
}
185263
}

0 commit comments

Comments
 (0)