@@ -26,7 +26,8 @@ function Create-ModulePsm1
26
26
[CmdletBinding()]
27
27
param(
28
28
[string]$ModulePath,
29
- [string]$TemplatePath
29
+ [string]$TemplatePath,
30
+ [bool]$AddDefaultParameters
30
31
)
31
32
32
33
PROCESS
@@ -65,12 +66,89 @@ function Create-ModulePsm1
65
66
$template = $template -replace "%MODULE-NAME%", $file.BaseName
66
67
$template = $template -replace "%DATE%", [string](Get-Date)
67
68
$template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules
69
+
70
+ $contructedCommands = Find-DefaultResourceGroupCmdlets -AddDefaultParameters $AddDefaultParameters -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath
71
+ $template = $template -replace "%COMMANDS%", $contructedCommands
72
+
68
73
Write-Host "Writing psm1 manifest to $templateOutputPath"
69
74
$template | Out-File -FilePath $templateOutputPath -Force
70
75
$file = Get-Item -Path $templateOutputPath
71
76
}
72
77
}
73
78
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
+
74
152
function Create-MinimumVersionEntry
75
153
{
76
154
[CmdletBinding()]
@@ -122,22 +200,22 @@ $templateLocation = "$PSScriptRoot\AzureRM.Example.psm1"
122
200
if (($scope -eq 'All') -or $publishToLocal ) {
123
201
# If we publish 'All' or to local folder, publish AzureRM.Profile first, becasue it is the common dependency
124
202
Write-Host "Updating profile module"
125
- Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation
203
+ Create-ModulePsm1 -ModulePath "$resourceManagerRootFolder\AzureRM.Profile" -TemplatePath $templateLocation $true
126
204
Write-Host "Updated profile module"
127
205
}
128
206
129
207
if (($scope -eq 'All') -or ($scope -eq 'AzureStorage')) {
130
208
$modulePath = "$packageFolder\$buildConfig\Storage\Azure.Storage"
131
209
# Publish AzureStorage module
132
210
Write-Host "Updating AzureStorage module from $modulePath"
133
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
211
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
134
212
}
135
213
136
214
if (($scope -eq 'All') -or ($scope -eq 'ServiceManagement')) {
137
215
$modulePath = "$packageFolder\$buildConfig\ServiceManagement\Azure"
138
216
# Publish Azure module
139
217
Write-Host "Updating ServiceManagement(aka Azure) module from $modulePath"
140
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
218
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
141
219
}
142
220
143
221
$resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory
@@ -148,15 +226,15 @@ if ($scope -eq 'All') {
148
226
if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) {
149
227
$modulePath = $module.FullName
150
228
Write-Host "Updating $module module from $modulePath"
151
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
229
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $true
152
230
Write-Host "Updated $module module"
153
231
}
154
232
}
155
233
} elseif ($scope -ne 'AzureRM') {
156
234
$modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope"
157
235
if (Test-Path $modulePath) {
158
236
Write-Host "Updating $scope module from $modulePath"
159
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
237
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
160
238
Write-Host "Updated $scope module"
161
239
} else {
162
240
Write-Error "Can not find module with name $scope to publish"
@@ -169,17 +247,17 @@ if (($scope -eq 'All') -or ($scope -eq 'AzureRM')) {
169
247
{
170
248
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureRM"
171
249
Write-Host "Updating AzureRM module from $modulePath"
172
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
250
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
173
251
Write-Host "Updated AzureRM module"
174
252
$modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureStack"
175
253
Write-Host "Updating AzureRM module from $modulePath"
176
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
254
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
177
255
Write-Host "Updated AzureStack module"
178
256
}
179
257
else {
180
258
$modulePath = "$PSScriptRoot\AzureRM"
181
259
Write-Host "Updating AzureRM module from $modulePath"
182
- Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation
260
+ Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false
183
261
Write-Host "Updated Azure module"
184
262
}
185
263
}
0 commit comments