149 lines
4.5 KiB
C#
149 lines
4.5 KiB
C#
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices.ComTypes;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using Jint.Parser.Ast;
|
|
using ln.build.semver;
|
|
using Microsoft.VisualBasic;
|
|
|
|
namespace ln.build.semver
|
|
{
|
|
|
|
public class SemVersion
|
|
{
|
|
public int Major { get; set; }
|
|
public int Minor { get; set; }
|
|
public int Patch { get; set; }
|
|
public string PreRelease { get; set; } = String.Empty;
|
|
|
|
public bool IsPreRelease => !PreRelease?.Equals(string.Empty) ?? false;
|
|
public bool IsRelease => PreRelease?.Equals(string.Empty) ?? true;
|
|
|
|
public SemVersion(int major,int minor,int patch) : this(major, minor, patch, ""){ }
|
|
public SemVersion(int major,int minor,int patch,string prerelease)
|
|
{
|
|
Major = major;
|
|
Minor = minor;
|
|
Patch = patch;
|
|
PreRelease = prerelease;
|
|
}
|
|
private SemVersion(){}
|
|
public SemVersion(SemVersion source)
|
|
:this(source.Major, source.Minor, source.Patch, source.PreRelease)
|
|
{ }
|
|
|
|
|
|
public void Increment(SemVerLevels level){
|
|
switch (level)
|
|
{
|
|
case SemVerLevels.MAJOR:
|
|
Major++;
|
|
Minor = 0;
|
|
Patch = 0;
|
|
break;
|
|
case SemVerLevels.MINOR:
|
|
Minor++;
|
|
Patch = 0;
|
|
break;
|
|
case SemVerLevels.PATCH:
|
|
Patch++;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
stringBuilder.AppendFormat("{0}.{1}.{2}", Major, Minor, Patch);
|
|
if (!PreRelease?.Equals(string.Empty) ?? false)
|
|
stringBuilder.AppendFormat("-{0}", PreRelease);
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
|
|
public static Regex rexVersion = new Regex("^(?<major>\\d+).(?<minor>\\d+).(?<patch>\\d+)(-(?<prerelease>.+))?$");
|
|
public static SemVersion Parse(string versionString)
|
|
{
|
|
Match match = rexVersion.Match(versionString);
|
|
if (!match.Success)
|
|
throw new FormatException(String.Format("{0} is no valid SemVer", versionString));
|
|
|
|
SemVersion version = new SemVersion();
|
|
version.Major = int.Parse(match.Groups["major"].Value);
|
|
version.Minor = int.Parse(match.Groups["minor"].Value);
|
|
version.Patch = int.Parse(match.Groups["patch"].Value);
|
|
if (match.Groups["prerelease"].Success)
|
|
version.PreRelease = match.Groups["prerelease"].Value;
|
|
|
|
return version;
|
|
}
|
|
|
|
public static bool operator <(SemVersion a,SemVersion b)
|
|
{
|
|
if (a is null || b is null)
|
|
return false;
|
|
|
|
if (a.Major < b.Major)
|
|
return true;
|
|
if (a.Major > b.Major)
|
|
return false;
|
|
|
|
if (a.Minor < b.Minor)
|
|
return true;
|
|
if (a.Minor > b.Minor)
|
|
return false;
|
|
|
|
if (a.Patch < b.Patch)
|
|
return true;
|
|
if (a.Patch > b.Patch)
|
|
return false;
|
|
|
|
if (a.IsPreRelease && b.IsRelease)
|
|
return true;
|
|
if (a.IsRelease && b.IsPreRelease)
|
|
return false;
|
|
|
|
if (a.IsRelease && b.IsRelease)
|
|
return false;
|
|
|
|
return a.PreRelease.CompareTo(b.PreRelease) > 0;
|
|
}
|
|
public static bool operator >(SemVersion b,SemVersion a)
|
|
{
|
|
if (a is null || b is null)
|
|
return false;
|
|
|
|
if (a.Major < b.Major)
|
|
return true;
|
|
if (a.Major > b.Major)
|
|
return false;
|
|
|
|
if (a.Minor < b.Minor)
|
|
return true;
|
|
if (a.Minor > b.Minor)
|
|
return false;
|
|
|
|
if (a.Patch < b.Patch)
|
|
return true;
|
|
if (a.Patch > b.Patch)
|
|
return false;
|
|
|
|
if (a.IsPreRelease && b.IsRelease)
|
|
return true;
|
|
if (a.IsRelease && b.IsPreRelease)
|
|
return false;
|
|
|
|
if (a.IsRelease && b.IsRelease)
|
|
return false;
|
|
|
|
return a.PreRelease.CompareTo(b.PreRelease) > 0;
|
|
}
|
|
|
|
public static explicit operator SemVersion(string versionString) => versionString == null ? null : SemVersion.Parse(versionString);
|
|
|
|
}
|
|
|
|
} |