Skip to content

Commit c3eb2d0

Browse files
committed
Address review comments
1 parent c7a69ec commit c3eb2d0

File tree

2 files changed

+76
-45
lines changed

2 files changed

+76
-45
lines changed

tools/AzureRM.Example.psm1

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

12-
# Import dependencies using required version, if it is allowed
13-
if ($PSVersionTable.PSVersion.Major -ge 5)
14-
{
15-
%STRICT-DEPENDENCIES%
16-
}
17-
else
18-
{
19-
%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.ExtendedProperties.ContainsKey("Default Resource Group")) {
21+
$context.ExtendedProperties["Default Resource Group"]
22+
}
23+
})
2024
}

tools/UpdateModules.ps1

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,27 @@ function Create-ModulePsm1
6767
$template = $template -replace "%DATE%", [string](Get-Date)
6868
$template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules
6969

70-
if ($AddDefaultParameters)
71-
{
70+
$contructedCommands = Find-DefaultResourceGroupCmdlets -AddDefaultParameters $AddDefaultParameters -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath
71+
$template = $template -replace "%COMMANDS%", $contructedCommands
72+
73+
Write-Host "Writing psm1 manifest to $templateOutputPath"
74+
$template | Out-File -FilePath $templateOutputPath -Force
75+
$file = Get-Item -Path $templateOutputPath
76+
}
77+
}
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+
{
7291
$nestedModules = $ModuleMetadata.NestedModules
7392
$AllCmdlets = @()
7493
$nestedModules | ForEach-Object {
@@ -78,47 +97,55 @@ function Create-ModulePsm1
7897
$AllCmdlets += $dllCmdlets
7998
}
8099

81-
$FilteredCommands = @()
82-
$AllCmdlets | ForEach-Object {
83-
$rgParameter = $_.GetProperties() | Where-Object {$_.Name -eq "ResourceGroupName"}
84-
if ($rgParameter -ne $null) {
85-
$parameterSets = $rgParameter.CustomAttributes | Where-Object {$_.AttributeType.Name -eq "ParameterAttribute"}
86-
$isMandatory = $true
87-
$parameterSets | ForEach-Object {
88-
$hasParameterSet = $_.NamedArguments | Where-Object {$_.MemberName -eq "ParameterSetName"}
89-
$MandatoryParam = $_.NamedArguments | Where-Object {$_.MemberName -eq "Mandatory"}
90-
if (($hasParameterSet -ne $null) -or (!$MandatoryParam.TypedValue.Value)) {
91-
$isMandatory = $false
92-
}
93-
}
94-
if ($isMandatory) {
95-
$FilteredCommands += $_
96-
}
100+
$FilteredCommands = $AllCmdlets | Where-Object {Test-RequiredParameter -Cmdlet $_}
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" + "',"
97109
}
110+
$contructedCommands = $contructedCommands -replace ".$",")"
98111
}
99-
100-
if ($FilteredCommands.Length -eq 0) {
101-
$contructedCommands = "@()"
112+
113+
return $contructedCommands
102114
}
115+
103116
else {
104-
$contructedCommands = "@("
105-
$FilteredCommands | ForEach-Object {
106-
$contructedCommands += "'" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").VerbName + "-" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").NounName + ":ResourceGroupName" + "',"
107-
}
108-
$contructedCommands = $contructedCommands -replace ".$",")"
117+
return "@()"
109118
}
110-
111-
$template = $template -replace "%COMMANDS%", $contructedCommands
112-
}
119+
}
120+
}
113121

114-
else {
115-
$template = $template -replace "%COMMANDS%", "@()"
116-
}
117-
118-
Write-Host "Writing psm1 manifest to $templateOutputPath"
119-
$template | Out-File -FilePath $templateOutputPath -Force
120-
$file = Get-Item -Path $templateOutputPath
121-
}
122+
function Test-RequiredParameter
123+
{
124+
[CmdletBinding()]
125+
param(
126+
[Object]$Cmdlet
127+
)
128+
129+
PROCESS
130+
{
131+
$rgParameter = $Cmdlet.GetProperties() | Where-Object {$_.Name -eq "ResourceGroupName"}
132+
if ($rgParameter -ne $null) {
133+
$parameterSets = $rgParameter.CustomAttributes | Where-Object {$_.AttributeType.Name -eq "ParameterAttribute"}
134+
$isMandatory = $true
135+
$parameterSets | ForEach-Object {
136+
$hasParameterSet = $_.NamedArguments | Where-Object {$_.MemberName -eq "ParameterSetName"}
137+
$MandatoryParam = $_.NamedArguments | Where-Object {$_.MemberName -eq "Mandatory"}
138+
if (($hasParameterSet -ne $null) -or (!$MandatoryParam.TypedValue.Value)) {
139+
$isMandatory = $false
140+
}
141+
}
142+
if ($isMandatory) {
143+
return $true
144+
}
145+
}
146+
147+
return $false
148+
}
122149
}
123150

124151
function Create-MinimumVersionEntry

0 commit comments

Comments
 (0)