@@ -33,6 +33,10 @@ public class VersionBumper
33
33
34
34
private string _oldVersion , _newVersion ;
35
35
private bool _isPreview ;
36
+
37
+ // Use static variable to store accounts version to avoid calculating more than once
38
+ private static string _accountsVersion = null ;
39
+
36
40
private IList < string > _changedModules { get ; set ; }
37
41
38
42
public AzurePSVersion MinimalVersion { get ; set ; }
@@ -73,13 +77,77 @@ public void BumpAllVersions()
73
77
UpdateSerializedAssemblyVersion ( ) ;
74
78
UpdateChangeLog ( ) ;
75
79
var releaseNotes = GetReleaseNotes ( ) ;
80
+ _accountsVersion = _accountsVersion ?? GetLatestAccountsVersion ( ) ;
76
81
UpdateOutputModuleManifest ( releaseNotes ) ;
77
82
UpdateDependentModules ( ) ;
78
83
UpdateRollupModuleManifest ( ) ;
79
84
UpdateAssemblyInfo ( ) ;
80
85
Console . WriteLine ( "Finished bumping version " + moduleName + "\n " ) ;
81
86
}
82
87
88
+ private string GetLatestAccountsVersion ( )
89
+ {
90
+ var localVersion = GetLocalAccountsVersion ( ) ;
91
+ var version = GetAccountsVersionFromPSGallery ( ) ;
92
+ if ( ! string . IsNullOrEmpty ( localVersion ) && ! string . IsNullOrEmpty ( version ) )
93
+ {
94
+ return new System . Version ( localVersion ) . CompareTo ( value : new System . Version ( version ) ) > 0 ? localVersion : version ;
95
+ } else if ( string . IsNullOrEmpty ( localVersion ) )
96
+ {
97
+ return version ;
98
+ } else if ( string . IsNullOrEmpty ( version ) )
99
+ {
100
+ return localVersion ;
101
+ }
102
+ else
103
+ {
104
+ throw new Exception ( "Can not find the latest version for Az.Accounts." ) ;
105
+ }
106
+
107
+ }
108
+
109
+ /// <summary>
110
+ /// Get the latest version of Az.Accounts in local
111
+ /// </summary>
112
+ /// <returns></returns>
113
+ private string GetLocalAccountsVersion ( )
114
+ {
115
+
116
+ // Assume in outputModuleDirectory/../Az.Accounts/Az.Accounts.psd1 exists
117
+ var accountsOutputDirectory = Path . Combine ( Directory . GetParent ( _fileHelper . OutputModuleDirectory ) . FullName , "Az.Accounts" ) ;
118
+ var accountsManifest = Directory . GetFiles ( accountsOutputDirectory , "Az.Accounts.psd1" , SearchOption . TopDirectoryOnly )
119
+ . FirstOrDefault ( ) ;
120
+
121
+ string localVersion = null ;
122
+ bool localPreview = false ;
123
+ using ( PowerShell powershell = PowerShell . Create ( ) )
124
+ {
125
+ powershell . AddScript ( "$metadata = Test-ModuleManifest -Path " + accountsManifest + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease" ) ;
126
+ var cmdletResult = powershell . Invoke ( ) ;
127
+ localVersion = cmdletResult [ 0 ] ? . ToString ( ) ;
128
+ localPreview = ! string . IsNullOrEmpty ( cmdletResult [ 1 ] ? . ToString ( ) ) ;
129
+ }
130
+ // Console.WriteLine("The version of Az.Accounts in local is " + localVersion);
131
+ return localPreview ? null : localVersion ;
132
+ }
133
+
134
+ /// <summary>
135
+ /// Get the latest version of Az.Accounts from PSGallery
136
+ /// </summary>
137
+ /// <returns></returns>
138
+ private string GetAccountsVersionFromPSGallery ( )
139
+ {
140
+ string version = null ;
141
+ using ( PowerShell powershell = PowerShell . Create ( ) )
142
+ {
143
+ powershell . AddScript ( "(Find-Module Az.Accounts -Repository PSGallery -AllVersions | Sort-Object {[System.Version]$_.Version} -Descending)[0].Version" ) ;
144
+ var cmdletResult = powershell . Invoke ( ) ;
145
+ version = cmdletResult [ 0 ] ? . ToString ( ) ;
146
+ }
147
+ // Console.WriteLine("The version of Az.Accounts in PSGallery is " + version );
148
+ return version ;
149
+ }
150
+
83
151
/// <summary>
84
152
/// Get the local version of the module.
85
153
/// </summary>
@@ -373,25 +441,47 @@ private void UpdateOutputModuleManifest(List<string> releaseNotes)
373
441
var projectModuleManifestPath = _fileHelper . ProjectModuleManifestPath ;
374
442
var tempModuleManifestPath = Path . Combine ( outputModuleDirectory , moduleName + "-temp.psd1" ) ;
375
443
File . Copy ( outputModuleManifestPath , tempModuleManifestPath , true ) ;
444
+
376
445
var script = "$releaseNotes = @();" ;
377
446
releaseNotes . ForEach ( l => script += "$releaseNotes += \" " + l + "\" ;" ) ;
447
+
448
+ // Get required module list and update Az,Accounts' version
449
+ var getRequiredModulesScript = "Import-LocalizedData -BaseDirectory " + outputModuleDirectory + " -FileName " + Path . GetFileName ( outputModuleManifestPath ) + " -BindingVariable moduleInfo;" ;
450
+ getRequiredModulesScript += "$requiredModules = @();" ;
451
+ getRequiredModulesScript += "$moduleInfo.RequiredModules.ForEach({ " +
452
+ "if ($_.ModuleName -eq \" Az.Accounts\" ){ " +
453
+ " $requiredModules += @{ModuleName = \" Az.Accounts\" ; ModuleVersion = \" " + _accountsVersion + "\" } " +
454
+ "}else " +
455
+ "{ " +
456
+ " $requiredModules += $_ " +
457
+ "} " +
458
+ "});" ;
459
+
460
+ // Update module manifest
461
+ script += getRequiredModulesScript ;
378
462
script += $ "$env:PSModulePath+=\" ;{ _fileHelper . OutputResourceManagerDirectory } \" ;";
379
463
script += "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;" ;
380
- script += "Update-ModuleManifest -Path " + tempModuleManifestPath + " -ModuleVersion " + _newVersion + " -ReleaseNotes $releaseNotes" ;
464
+ script += "Update-ModuleManifest -Path " + tempModuleManifestPath + " -ModuleVersion " + _newVersion + " -ReleaseNotes $releaseNotes" + " -RequiredModules $requiredmodules;" ;
465
+ script += "$?" ;
466
+
381
467
using ( PowerShell powershell = PowerShell . Create ( ) )
382
468
{
383
469
powershell . AddScript ( script ) ;
384
470
var result = powershell . Invoke ( ) ;
385
- if ( powershell . Streams . Error . Any ( ) )
471
+ bool exitcode = false ;
472
+ if ( result . Count > 0 &&
473
+ ( ! bool . TryParse ( result . Last ( ) ? . ToString ( ) , out exitcode ) ) || ! exitcode )
386
474
{
387
- Console . WriteLine ( $ "Found error in updating module { _fileHelper . ModuleName } : { powershell . Streams . Error . First ( ) . ToString ( ) } ") ;
475
+ Console . WriteLine ( $ "Found error in updating module { _fileHelper . ModuleName } : { powershell . Streams . Error . First ( ) ? . ToString ( ) } ") ;
388
476
}
389
477
}
390
478
391
479
var tempModuleContent = File . ReadAllLines ( tempModuleManifestPath ) ;
392
480
tempModuleContent = tempModuleContent . Select ( l => l = l . Replace ( moduleName + "-temp" , moduleName ) ) . ToArray ( ) ;
393
481
var pattern = @"RootModule(\s*)=(\s*)(['\""])" + moduleName + @"(\.)psm1(['\""])" ;
394
482
tempModuleContent = tempModuleContent . Select ( l => Regex . Replace ( l , pattern , @"# RootModule = ''" ) ) . ToArray ( ) ;
483
+
484
+
395
485
File . WriteAllLines ( projectModuleManifestPath , tempModuleContent ) ;
396
486
File . Delete ( tempModuleManifestPath ) ;
397
487
}
0 commit comments