Skip to content

Commit a22ca0b

Browse files
author
msJinLei
committed
Address to PM requirements
1. Remove prefix Az. when input module name 2. update calculate from most recently installed respositories of different modules 3. reorder the sequence of module udpate and uninstall 4. other bug fix
1 parent c7df94d commit a22ca0b

File tree

4 files changed

+155
-136
lines changed

4 files changed

+155
-136
lines changed

tools/Az.Tools.Installer/Az.Tools.Installer.psm1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ elseif ($null -eq $module) {
2323
$exportedFunctions = @( Get-ChildItem -Path $PSScriptRoot/exports/*.ps1 -Recurse -ErrorAction SilentlyContinue )
2424
$internalFunctions = @( Get-ChildItem -Path $PSScriptRoot/internal/*.ps1 -ErrorAction SilentlyContinue )
2525

26-
2726
$allFunctions = $exportedFunctions + $internalFunctions
2827
foreach($function in $allFunctions) {
2928
try {
@@ -36,8 +35,7 @@ foreach($function in $allFunctions) {
3635
Export-ModuleMember -Function $exportedFunctions.Basename
3736

3837
$commandsWithRepositoryParameter = @(
39-
"Update-AzModule"
38+
"Install-AzModule"
4039
)
4140

42-
Add-RepositoryArgumentCompleter -Cmdlets $commandsWithRepositoryParameter -ParameterName "Repository"
43-
Add-RepositoryDefaultValue -Cmdlets $commandsWithRepositoryParameter -ParameterName "Repository"
41+
Add-RepositoryArgumentCompleter -Cmdlets $commandsWithRepositoryParameter -ParameterName "Repository"
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
function Get-AzModuleUpdateList {
16+
[OutputType([System.Collections.ArrayList])]
17+
[CmdletBinding(DefaultParameterSetName = 'Default', PositionalBinding = $false)]
18+
param(
19+
[Parameter(HelpMessage = 'The module name.')]
20+
[ValidateNotNullOrEmpty()]
21+
[string[]]
22+
${Name},
23+
24+
[Parameter(HelpMessage = 'The Registered Repostory.')]
25+
[ValidateNotNullOrEmpty()]
26+
[string]
27+
${Repository}
28+
)
29+
30+
process {
31+
32+
$modules = New-Object System.Collections.ArrayList
33+
34+
Write-Debug "Retrieving installed Az modules"
35+
$installModules = @{}
36+
try {
37+
(PowerShellGet\Get-InstalledModule -Name "Az*" -ErrorAction Stop).Where({
38+
($_.Author -eq 'Microsoft Corporation' -or $_.CompanyName -eq 'Microsoft Corporation') -and ($_.Name -match "Az(\.[a-zA-Z0-9]+)?$")
39+
}) | ForEach-Object {
40+
$installModules[$_.Name] = @()
41+
}
42+
}
43+
catch {
44+
Write-Warning $_
45+
}
46+
47+
Write-Debug "The Az modules currently installed: $($installModules.Keys)"
48+
49+
if ($installModules.Keys) {
50+
foreach ($key in $installModules.Keys.Clone()) {
51+
$installedModules = (PowerShellGet\Get-InstalledModule -Name $key -AllVersions).Where( { -not $_.AdditionalMetadata.IsPrerelease })
52+
foreach ($installed in $installedModules) {
53+
$installModules[$key] += [System.Tuple]::Create($installed.Version, $installed.Repository)
54+
}
55+
if($installModules[$key].Count -gt 1) {
56+
$installModules[$key] = ($installModules[$key] | Sort-Object -Property @{Expression={[Version]$_.Item1}} -Descending)
57+
}
58+
}
59+
}
60+
61+
$modulesToCheck = @()
62+
$modulesToCheck += if ($Name) {
63+
#Fixme:Damien
64+
$Name.Foreach({
65+
if ($installModules.ContainsKey($_) -and $installModules[$_]) {
66+
[System.Tuple]::Create($_, $installModules[$_][0].Item2)
67+
}
68+
})
69+
} else {
70+
$installModules.Keys.ForEach({[System.Tuple]::Create($_, $installModules[$_][0].Item2)})
71+
}
72+
73+
$moduleSet = [System.Collections.Generic.HashSet[string]]::new()
74+
$null = $modulesToCheck | ForEach-Object {$moduleSet.Add($_.Item1)}
75+
76+
$index = 0
77+
while($index -lt $modulesToCheck.Count) {
78+
$moduleName = $modulesToCheck[$index].Item1
79+
$repo = if ($Repository) {$Repository} else {$modulesToCheck[$index].Item2}
80+
if($repo) {
81+
$module = PowerShellGet\Find-Module -Name $moduleName -Repository $repo
82+
if ($module) {
83+
Write-Progress -Activity "Find Module" -CurrentOperation "$($module.Name) with the latest version $($module.Version)" -PercentComplete ($index / $modulesToCheck.Count * 100)
84+
$null = $modules.Add($module)
85+
foreach ($dep in $module.Dependencies.Name) {
86+
if (!$moduleSet.Contains($dep)) {
87+
$modulesToCheck += [System.Tuple]::Create($dep, $repo)
88+
$null = $moduleSet.Add($dep)
89+
}
90+
}
91+
}
92+
}
93+
else {
94+
Write-Warning "Failed to find Repository of $moduleName. The module cannot be updated."
95+
}
96+
$index += 1
97+
}
98+
Write-Progress -Activity "Find Module" -Completed
99+
$modules = ($modules | Sort-Object -Property Name -Unique)
100+
101+
Write-Debug "The modules to check for update: $($modules.Name)"
102+
103+
$modulesToUpdate = $modules.Where({ !$installModules.ContainsKey($_.Name) -or !$installModules[$_.Name] -or [Version]($_.Version) -gt [Version]$installModules[$_.Name][0].Item1 })
104+
if ("Az" -eq ($modulesToUpdate | Select-Object -First 1).Name) {
105+
$first, $rest = $modulesToUpdate
106+
$modulesToUpdate = (@() + $rest + $first)
107+
}
108+
109+
If (-not $modulesToUpdate) {
110+
Write-Warning "None of your specifed module needs upgrading."
111+
}
112+
else {
113+
Write-Debug "The modules contain update: $($modulesToUpdate.Name)"
114+
}
115+
Write-Output ($modulesToUpdate | ForEach-Object {
116+
New-Object -Type PSObject -Property @{
117+
'Name' = $_.Name
118+
'VersionToUpgrade' = $_.Version
119+
'InstalledVersion' = if ($installModules.ContainsKey($_.Name)) {$installModules[$_.Name].Item1} else {@()}
120+
'Repository' = $_.Repository
121+
}
122+
})
123+
}
124+
}

tools/Az.Tools.Installer/exports/Get-AzModuleWithUpdate.ps1

Lines changed: 0 additions & 100 deletions
This file was deleted.

tools/Az.Tools.Installer/exports/Update-AzModule.ps1

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ function Update-AzModule {
2121
[string[]]
2222
${Name},
2323

24-
[Parameter(HelpMessage = 'The Registered Repostory.')]
25-
[ValidateNotNullOrEmpty()]
26-
[string]
27-
${Repository},
28-
2924
[Parameter(HelpMessage = 'Present to decide whether to remove the previous versions of the modules.')]
3025
[switch]
3126
${RemovePrevious},
@@ -37,49 +32,51 @@ function Update-AzModule {
3732
)
3833

3934
process {
40-
Write-Host "Powershell $($PSVersionTable.PSEdition) Version $($PSVersionTable.PSVersion)"
41-
$parameters = @{}
42-
if ($Name) {
43-
$parameters['Name'] = ($Name | Sort-Object -Unique)
35+
$ppsedition = $PSVersionTable.PSEdition
36+
Write-Host "Powershell $ppsedition Version $($PSVersionTable.PSVersion)"
37+
38+
if ($ppsedition -eq "Core") {
39+
$allPahts = (Microsoft.PowerShell.Core\Get-Module -Name "Az*" -ListAvailable -ErrorAction Stop).Where({$_.Author -eq "Microsoft Corporation" -and $_.Name -match "Az(\.[a-zA-Z0-9]+)?$"}).Path
40+
$allPahts = ($allPahts | Select-String -Pattern "WindowsPowerShell")
41+
if ($allPahts) {
42+
Write-Warning "Az modules are also installed in WindowsPowerShell. Please update them using WindowsPowerShell."
43+
}
4444
}
4545

46-
if ($Repository) {
47-
$parameters['Repository'] = $Repository
46+
$parameters = @{}
47+
if ($Name) {
48+
$Name = $Name.Foreach({"Az." + $_}) | Sort-Object -Unique
49+
$parameters['Name'] = $Name
4850
}
4951

50-
Write-Debug "Time Elapsed: $((Measure-Command { $allToUpdate = Get-AzModuleWithUpdate @parameters }).TotalSeconds) s"
52+
Write-Debug "Time Elapsed: $((Measure-Command { $allToUpdate = Get-AzModuleUpdateList @parameters }).TotalSeconds)s"
5153

5254
Write-Host -ForegroundColor DarkGreen "The modules to Upddate:$($allToUpdate | Out-String)"
5355

54-
if($allToUpdate) {
55-
$azModule = $allToUpdate.Where({$_.Name -eq "Az"})
56-
$allToUpdate | Foreach-Object {
56+
if($allToUpdate.Count) {
57+
foreach ($module in $allToUpdate) {
58+
if ($Force -or $PSCmdlet.ShouldProcess($module.Name, "Upgrade to the latest version $($module.VersionToUpgrade) from $($module.Repository)")) {
59+
if ($Force) {
60+
Write-Debug "Upgrade $($module.Name) to the latest version $($module.VersionToUpgrade) from $($module.Repository)."
61+
}
62+
PowerShellGet\Update-Module -Name $module.Name -Force:$Force
63+
}
5764
if ($RemovePrevious) {
58-
if ($_.InstalledVersion -and ($Force -or $PSCmdlet.ShouldProcess($_.Name, "Uninstall the versions: $($_.InstalledVersion)"))) {
65+
if ($module.InstalledVersion -and ($Force -or $PSCmdlet.ShouldProcess($module.Name, "Uninstall the versions: $($module.InstalledVersion)"))) {
5966
if ($Force) {
60-
Write-Debug "Uninstall $($_.Name) of versions $($_.InstalledVersion)."
67+
Write-Debug "Uninstall $($module.Name) of versions $($module.InstalledVersion)."
68+
}
69+
foreach($version in $module.InstalledVersion) {
70+
PowerShellGet\Uninstall-Module -Name $module.Name -RequiredVersion $version
6171
}
62-
PowerShellGet\Uninstall-Module -Name $_.Name -AllVersions
63-
}
64-
}
65-
if (-not $azModule -and ($Force -or $PSCmdlet.ShouldProcess($_.Name, "Upgrade to the latest version $($_.VersionToUpgrade) from $($_.Repository)"))) {
66-
if ($Force) {
67-
Write-Debug "Upgrade $($_.Name) to the latest version $($_.VersionToUpgrade) from $($_.Repository)."
6872
}
69-
PowerShellGet\Install-Module -Name $_.Name -Repository $_.Repository -Force
70-
}
71-
}
72-
if($azModule -and ($Force -or $PSCmdlet.ShouldProcess($azModule.Name, "Upgrade to the latest version $($azModule.VersionToUpgrade) from $($azModule.Repository)"))) {
73-
if ($Force) {
74-
Write-Debug "Upgrade $($azModule.Name) to the latest version $($azModule.VersionToUpgrade) from $($azModule.Repository)."
7573
}
76-
PowerShellGet\Install-Module -Name $azModule.Name -Repository $azModule.Repository -Force
7774
}
7875

7976
$output = @()
80-
$allToUpdate.Name | Foreach-Object {
77+
foreach($name in $allToUpdate.Name) {
8178
try {
82-
$output += (Get-InstalledModule -Name $_ -ErrorAction Stop)
79+
$output += (Get-InstalledModule -Name $name -ErrorAction Stop)
8380
}
8481
catch {
8582
Write-Warning $_

0 commit comments

Comments
 (0)