Skip to content

Commit a2ddd01

Browse files
authored
Add a new function GetOldVesion to VersionBumper to avoid duplicated version (#12084)
* Add a new function GetOldVersion and change the way of getting oldversion. * Add return value to function GetOldVersion and change some logic to make it more readable. * Merge the logic of judging preview version into GetOldVersion and return both version and ispreview.
1 parent 00d47c8 commit a2ddd01

File tree

1 file changed

+84
-13
lines changed

1 file changed

+84
-13
lines changed

tools/VersionController/Models/VersionBumper.cs

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Management.Automation;
6+
using System.Management.Automation.Language;
67
using System.Reflection;
78
using System.Text.RegularExpressions;
89
using Tools.Common.Models;
@@ -33,26 +34,16 @@ public void BumpAllVersions()
3334
{
3435
var moduleName = _fileHelper.ModuleName;
3536
Console.WriteLine("Bumping version for " + moduleName + "...");
36-
using (PowerShell powershell = PowerShell.Create())
37-
{
38-
powershell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;");
39-
powershell.AddScript("$metadata = Test-ModuleManifest -Path " + _fileHelper.OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease");
40-
var cmdletResult = powershell.Invoke();
41-
_oldVersion = cmdletResult[0]?.ToString();
42-
_isPreview = !string.IsNullOrEmpty(cmdletResult[1]?.ToString());
43-
}
4437

45-
if (_oldVersion == null)
46-
{
47-
throw new Exception("Unable to obtain old version of " + moduleName + " using the built module manifest.");
48-
}
38+
(_oldVersion, _isPreview) = GetOldVersion();
4939

5040
_newVersion = IsNewModule() ? _oldVersion : GetBumpedVersion();
51-
if(MinimalVersion != null && MinimalVersion > new AzurePSVersion(_newVersion))
41+
if (MinimalVersion != null && MinimalVersion > new AzurePSVersion(_newVersion))
5242
{
5343
Console.WriteLine($"Adjust version from {_newVersion} to {MinimalVersion} due to MinimalVersion.csv");
5444
_newVersion = MinimalVersion.ToString();
5545
}
46+
5647
if (_oldVersion == _newVersion)
5748
{
5849
Console.WriteLine(_fileHelper.ModuleName + " is a new module. Keeping the version at " + _oldVersion);
@@ -78,6 +69,86 @@ public void BumpAllVersions()
7869
Console.WriteLine("Finished bumping version " + moduleName + "\n");
7970
}
8071

72+
/// <summary>
73+
/// Get the old version of the module.
74+
/// Compare the version from PSGallery and TestGallery to the version obtain from manifest.
75+
/// Choose the maximum one as the old version.
76+
/// </summary>
77+
/// <returns>The old version and is or not preview before the bump.</returns>
78+
public Tuple<string, bool> GetOldVersion()
79+
{
80+
string version;
81+
string localVersion = null, psVersion = null, testVersion = null;
82+
bool isPreview;
83+
bool localPreview = false, psPreview = false, testPreview = false;
84+
var moduleName = _fileHelper.ModuleName;
85+
86+
using (PowerShell powershell = PowerShell.Create())
87+
{
88+
powershell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;");
89+
powershell.AddScript("$metadata = Test-ModuleManifest -Path " + _fileHelper.OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease");
90+
var cmdletResult = powershell.Invoke();
91+
localVersion = cmdletResult[0]?.ToString();
92+
localPreview = !string.IsNullOrEmpty(cmdletResult[1]?.ToString());
93+
}
94+
if (localVersion == null)
95+
{
96+
throw new Exception("Unable to obtain old version of " + moduleName + " using the built module manifest.");
97+
}
98+
version = localVersion;
99+
isPreview = localPreview;
100+
101+
using (PowerShell powetshell = PowerShell.Create())
102+
{
103+
powetshell.AddScript("Register-PackageSource -Name PSGallery -Location https://www.powershellgallery.com/api/v2 -ProviderName PowerShellGet");
104+
powetshell.AddScript("Find-Module -Name " + moduleName + " -AllowPrerelease -Repository PSGallery -AllVersions");
105+
var cmdletResult = powetshell.Invoke();
106+
if (cmdletResult.Count != 0)
107+
{
108+
var psVersionImformation = cmdletResult[0].ToString();
109+
Regex reg = new Regex("Version=(.*?)(-|;)");
110+
Match match = reg.Match(psVersionImformation);
111+
psVersion = match.Groups[1].Value;
112+
psPreview = Regex.IsMatch(psVersionImformation, "preview");
113+
}
114+
}
115+
if (psVersion == null)
116+
{
117+
psVersion = "0.1.0";
118+
}
119+
if (new AzurePSVersion(psVersion) > new AzurePSVersion(version))
120+
{
121+
version = psVersion;
122+
isPreview = psPreview;
123+
}
124+
125+
using (PowerShell powetshell = PowerShell.Create())
126+
{
127+
powetshell.AddScript("Register-PackageSource -Name TestGallery -Location https://www.poshtestgallery.com/api/v2 -ProviderName PowerShellGet");
128+
powetshell.AddScript("Find-Module -Name " + moduleName + " -AllowPrerelease -Repository TestGallery -AllVersions");
129+
var cmdletResult = powetshell.Invoke();
130+
if (cmdletResult.Count != 0)
131+
{
132+
var testVersionImformation = cmdletResult[0].ToString();
133+
Regex reg = new Regex("Version=(.*?)(-|;)");
134+
Match match = reg.Match(testVersionImformation);
135+
testVersion = match.Groups[1].Value;
136+
testPreview = Regex.IsMatch(testVersionImformation, "preview");
137+
}
138+
}
139+
if (testVersion == null)
140+
{
141+
testVersion = "0.1.0";
142+
}
143+
if (new AzurePSVersion(testVersion) > new AzurePSVersion(version))
144+
{
145+
version = testVersion;
146+
isPreview = testPreview;
147+
}
148+
149+
return Tuple.Create(version, isPreview);
150+
}
151+
81152
/// <summary>
82153
/// Apply a version bump to a given version.
83154
/// </summary>

0 commit comments

Comments
 (0)