Skip to content

Commit 19e3c24

Browse files
authored
[Tools] Use latest Az.Accounts when bumping module version. (#19312)
* get latest accounts when bumping version * hide info of Az.Accounts
1 parent 5e5dd5f commit 19e3c24

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

tools/VersionController/Models/VersionBumper.cs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class VersionBumper
3333

3434
private string _oldVersion, _newVersion;
3535
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+
3640
private IList<string> _changedModules { get; set; }
3741

3842
public AzurePSVersion MinimalVersion { get; set; }
@@ -73,13 +77,77 @@ public void BumpAllVersions()
7377
UpdateSerializedAssemblyVersion();
7478
UpdateChangeLog();
7579
var releaseNotes = GetReleaseNotes();
80+
_accountsVersion = _accountsVersion ?? GetLatestAccountsVersion();
7681
UpdateOutputModuleManifest(releaseNotes);
7782
UpdateDependentModules();
7883
UpdateRollupModuleManifest();
7984
UpdateAssemblyInfo();
8085
Console.WriteLine("Finished bumping version " + moduleName + "\n");
8186
}
8287

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+
83151
/// <summary>
84152
/// Get the local version of the module.
85153
/// </summary>
@@ -373,25 +441,47 @@ private void UpdateOutputModuleManifest(List<string> releaseNotes)
373441
var projectModuleManifestPath = _fileHelper.ProjectModuleManifestPath;
374442
var tempModuleManifestPath = Path.Combine(outputModuleDirectory, moduleName + "-temp.psd1");
375443
File.Copy(outputModuleManifestPath, tempModuleManifestPath, true);
444+
376445
var script = "$releaseNotes = @();";
377446
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;
378462
script += $"$env:PSModulePath+=\";{_fileHelper.OutputResourceManagerDirectory}\";";
379463
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+
381467
using (PowerShell powershell = PowerShell.Create())
382468
{
383469
powershell.AddScript(script);
384470
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)
386474
{
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()}");
388476
}
389477
}
390478

391479
var tempModuleContent = File.ReadAllLines(tempModuleManifestPath);
392480
tempModuleContent = tempModuleContent.Select(l => l = l.Replace(moduleName + "-temp", moduleName)).ToArray();
393481
var pattern = @"RootModule(\s*)=(\s*)(['\""])" + moduleName + @"(\.)psm1(['\""])";
394482
tempModuleContent = tempModuleContent.Select(l => Regex.Replace(l, pattern, @"# RootModule = ''")).ToArray();
483+
484+
395485
File.WriteAllLines(projectModuleManifestPath, tempModuleContent);
396486
File.Delete(tempModuleManifestPath);
397487
}

0 commit comments

Comments
 (0)