Skip to content

Add a new function GetOldVesion to VersionBumper to avoid duplicated version #12084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 84 additions & 13 deletions tools/VersionController/Models/VersionBumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Reflection;
using System.Text.RegularExpressions;
using Tools.Common.Models;
Expand Down Expand Up @@ -33,26 +34,16 @@ public void BumpAllVersions()
{
var moduleName = _fileHelper.ModuleName;
Console.WriteLine("Bumping version for " + moduleName + "...");
using (PowerShell powershell = PowerShell.Create())
{
powershell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;");
powershell.AddScript("$metadata = Test-ModuleManifest -Path " + _fileHelper.OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease");
var cmdletResult = powershell.Invoke();
_oldVersion = cmdletResult[0]?.ToString();
_isPreview = !string.IsNullOrEmpty(cmdletResult[1]?.ToString());
}

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

_newVersion = IsNewModule() ? _oldVersion : GetBumpedVersion();
if(MinimalVersion != null && MinimalVersion > new AzurePSVersion(_newVersion))
if (MinimalVersion != null && MinimalVersion > new AzurePSVersion(_newVersion))
{
Console.WriteLine($"Adjust version from {_newVersion} to {MinimalVersion} due to MinimalVersion.csv");
_newVersion = MinimalVersion.ToString();
}

if (_oldVersion == _newVersion)
{
Console.WriteLine(_fileHelper.ModuleName + " is a new module. Keeping the version at " + _oldVersion);
Expand All @@ -78,6 +69,86 @@ public void BumpAllVersions()
Console.WriteLine("Finished bumping version " + moduleName + "\n");
}

/// <summary>
/// Get the old version of the module.
/// Compare the version from PSGallery and TestGallery to the version obtain from manifest.
/// Choose the maximum one as the old version.
/// </summary>
/// <returns>The old version and is or not preview before the bump.</returns>
public Tuple<string, bool> GetOldVersion()
{
string version;
string localVersion = null, psVersion = null, testVersion = null;
bool isPreview;
bool localPreview = false, psPreview = false, testPreview = false;
var moduleName = _fileHelper.ModuleName;

using (PowerShell powershell = PowerShell.Create())
{
powershell.AddScript("Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process;");
powershell.AddScript("$metadata = Test-ModuleManifest -Path " + _fileHelper.OutputModuleManifestPath + ";$metadata.Version;$metadata.PrivateData.PSData.Prerelease");
var cmdletResult = powershell.Invoke();
localVersion = cmdletResult[0]?.ToString();
localPreview = !string.IsNullOrEmpty(cmdletResult[1]?.ToString());
}
if (localVersion == null)
{
throw new Exception("Unable to obtain old version of " + moduleName + " using the built module manifest.");
}
version = localVersion;
isPreview = localPreview;

using (PowerShell powetshell = PowerShell.Create())
{
powetshell.AddScript("Register-PackageSource -Name PSGallery -Location https://www.powershellgallery.com/api/v2 -ProviderName PowerShellGet");
powetshell.AddScript("Find-Module -Name " + moduleName + " -AllowPrerelease -Repository PSGallery -AllVersions");
var cmdletResult = powetshell.Invoke();
if (cmdletResult.Count != 0)
{
var psVersionImformation = cmdletResult[0].ToString();
Regex reg = new Regex("Version=(.*?)(-|;)");
Match match = reg.Match(psVersionImformation);
psVersion = match.Groups[1].Value;
psPreview = Regex.IsMatch(psVersionImformation, "preview");
}
}
if (psVersion == null)
{
psVersion = "0.1.0";
}
if (new AzurePSVersion(psVersion) > new AzurePSVersion(version))
{
version = psVersion;
isPreview = psPreview;
}

using (PowerShell powetshell = PowerShell.Create())
{
powetshell.AddScript("Register-PackageSource -Name TestGallery -Location https://www.poshtestgallery.com/api/v2 -ProviderName PowerShellGet");
powetshell.AddScript("Find-Module -Name " + moduleName + " -AllowPrerelease -Repository TestGallery -AllVersions");
var cmdletResult = powetshell.Invoke();
if (cmdletResult.Count != 0)
{
var testVersionImformation = cmdletResult[0].ToString();
Regex reg = new Regex("Version=(.*?)(-|;)");
Match match = reg.Match(testVersionImformation);
testVersion = match.Groups[1].Value;
testPreview = Regex.IsMatch(testVersionImformation, "preview");
}
}
if (testVersion == null)
{
testVersion = "0.1.0";
}
if (new AzurePSVersion(testVersion) > new AzurePSVersion(version))
{
version = testVersion;
isPreview = testPreview;
}

return Tuple.Create(version, isPreview);
}

/// <summary>
/// Apply a version bump to a given version.
/// </summary>
Expand Down