diff --git a/tools/AzureRM.Example.psm1 b/tools/AzureRM.Example.psm1 index a0823eb08331..5bd1f9c33eb1 100644 --- a/tools/AzureRM.Example.psm1 +++ b/tools/AzureRM.Example.psm1 @@ -9,4 +9,16 @@ $PSDefaultParameterValues.Clear() Set-StrictMode -Version Latest -%IMPORTED-DEPENDENCIES% \ No newline at end of file +%IMPORTED-DEPENDENCIES% + +$FilteredCommands = %COMMANDS% + +$FilteredCommands | ForEach-Object { + $global:PSDefaultParameterValues.Add($_, + { + $context = Get-AzureRmContext + if (($context -ne $null) -and $context.ExtendedProperties.ContainsKey("Default Resource Group")) { + $context.ExtendedProperties["Default Resource Group"] + } + }) +} diff --git a/tools/UpdateModules.ps1 b/tools/UpdateModules.ps1 index 84bdb15647b9..73190cb68c46 100644 --- a/tools/UpdateModules.ps1 +++ b/tools/UpdateModules.ps1 @@ -26,7 +26,8 @@ function Create-ModulePsm1 [CmdletBinding()] param( [string]$ModulePath, - [string]$TemplatePath + [string]$TemplatePath, + [bool]$AddDefaultParameters ) PROCESS @@ -65,12 +66,89 @@ function Create-ModulePsm1 $template = $template -replace "%MODULE-NAME%", $file.BaseName $template = $template -replace "%DATE%", [string](Get-Date) $template = $template -replace "%IMPORTED-DEPENDENCIES%", $importedModules + + $contructedCommands = Find-DefaultResourceGroupCmdlets -AddDefaultParameters $AddDefaultParameters -ModuleMetadata $ModuleMetadata -ModulePath $ModulePath + $template = $template -replace "%COMMANDS%", $contructedCommands + Write-Host "Writing psm1 manifest to $templateOutputPath" $template | Out-File -FilePath $templateOutputPath -Force $file = Get-Item -Path $templateOutputPath } } +function Find-DefaultResourceGroupCmdlets +{ + [CmdletBinding()] + param( + [bool]$AddDefaultParameters, + [Hashtable]$ModuleMetadata, + [string]$ModulePath + ) + PROCESS + { + if ($AddDefaultParameters) + { + $nestedModules = $ModuleMetadata.NestedModules + $AllCmdlets = @() + $nestedModules | ForEach-Object { + $dllPath = Join-Path -Path $ModulePath -ChildPath $_ + $Assembly = [Reflection.Assembly]::LoadFrom($dllPath) + $dllCmdlets = $Assembly.GetTypes() | Where-Object {$_.CustomAttributes.AttributeType.Name -contains "CmdletAttribute"} + $AllCmdlets += $dllCmdlets + } + + $FilteredCommands = $AllCmdlets | Where-Object {Test-CmdletRequiredParameter -Cmdlet $_ -Parameter "ResourceGroupName"} + + if ($FilteredCommands.Length -eq 0) { + $contructedCommands = "@()" + } + else { + $contructedCommands = "@(" + $FilteredCommands | ForEach-Object { + $contructedCommands += "'" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").VerbName + "-" + $_.GetCustomAttributes("System.Management.Automation.CmdletAttribute").NounName + ":ResourceGroupName" + "'," + } + $contructedCommands = $contructedCommands -replace ".$",")" + } + + return $contructedCommands + } + + else { + return "@()" + } + } +} + +function Test-CmdletRequiredParameter +{ + [CmdletBinding()] + param( + [Object]$Cmdlet, + [string]$Parameter + ) + + PROCESS + { + $rgParameter = $Cmdlet.GetProperties() | Where-Object {$_.Name -eq $Parameter} + if ($rgParameter -ne $null) { + $parameterAttributes = $rgParameter.CustomAttributes | Where-Object {$_.AttributeType.Name -eq "ParameterAttribute"} + $isMandatory = $true + $parameterAttributes | ForEach-Object { + $hasParameterSet = $_.NamedArguments | Where-Object {$_.MemberName -eq "ParameterSetName"} + $MandatoryParam = $_.NamedArguments | Where-Object {$_.MemberName -eq "Mandatory"} + if (($hasParameterSet -ne $null) -or (!$MandatoryParam.TypedValue.Value)) { + $isMandatory = $false + } + } + if ($isMandatory) { + return $true + } + } + + return $false + } +} + function Create-MinimumVersionEntry { [CmdletBinding()] @@ -122,7 +200,7 @@ $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 Write-Host "Updated profile module" } @@ -130,14 +208,14 @@ if (($scope -eq 'All') -or ($scope -eq 'AzureStorage')) { $modulePath = "$packageFolder\$buildConfig\Storage\Azure.Storage" # Publish AzureStorage module Write-Host "Updating AzureStorage module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false } if (($scope -eq 'All') -or ($scope -eq 'ServiceManagement')) { $modulePath = "$packageFolder\$buildConfig\ServiceManagement\Azure" # Publish Azure module Write-Host "Updating ServiceManagement(aka Azure) module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false } $resourceManagerModules = Get-ChildItem -Path $resourceManagerRootFolder -Directory @@ -148,7 +226,7 @@ if ($scope -eq 'All') { if (($module.Name -ne "AzureRM.Profile") -and ($module.Name -ne "Azure.Storage")) { $modulePath = $module.FullName Write-Host "Updating $module module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $true Write-Host "Updated $module module" } } @@ -156,7 +234,7 @@ if ($scope -eq 'All') { $modulePath = Join-Path $resourceManagerRootFolder "AzureRM.$scope" if (Test-Path $modulePath) { Write-Host "Updating $scope module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false Write-Host "Updated $scope module" } else { Write-Error "Can not find module with name $scope to publish" @@ -169,17 +247,17 @@ if (($scope -eq 'All') -or ($scope -eq 'AzureRM')) { { $modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureRM" Write-Host "Updating AzureRM module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false Write-Host "Updated AzureRM module" $modulePath = "$PSScriptRoot\..\src\StackAdmin\AzureStack" Write-Host "Updating AzureRM module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false Write-Host "Updated AzureStack module" } else { $modulePath = "$PSScriptRoot\AzureRM" Write-Host "Updating AzureRM module from $modulePath" - Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation + Create-ModulePsm1 -ModulePath $modulePath -TemplatePath $templateLocation $false Write-Host "Updated Azure module" } }