14 using System.Text.RegularExpressions;
15 using SiliconStudio.Core;
16 using SiliconStudio.Core.Serialization;
17 using SiliconStudio.Core.Serialization.Serializers;
19 namespace SiliconStudio.Assets
25 [DataContract(
"PackageVersion")]
29 private const RegexOptions
Flags = RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture;
30 private static readonly Regex SemanticVersionRegex =
new Regex(
@"^(?<Version>\d+(\s*\.\s*\d+){0,3})(?<Release>-[a-z][0-9a-z-]*)?$",
Flags);
31 private static readonly Regex StrictSemanticVersionRegex =
new Regex(
@"^(?<Version>\d+(\.\d+){2})(?<Release>-[a-z][0-9a-z-]*)?$",
Flags);
32 private readonly
string originalString;
39 : this(Parse(version))
43 originalString = version;
54 : this(new Version(major, minor, build, revision))
65 public PackageVersion(
int major,
int minor,
int build,
string specialVersion)
66 : this(new Version(major, minor, build), specialVersion)
75 : this(version, String.Empty)
85 : this(version, specialVersion, null)
89 private PackageVersion(Version version,
string specialVersion,
string originalString)
93 throw new ArgumentNullException(
"version");
95 Version = NormalizeVersionValue(version);
96 SpecialVersion = specialVersion ?? String.Empty;
97 this.originalString = String.IsNullOrEmpty(originalString) ? version.ToString() + (!String.IsNullOrEmpty(specialVersion) ?
'-' + specialVersion : null) : originalString;
100 internal PackageVersion(PackageVersion semVer)
102 originalString = semVer.ToString();
103 Version = semVer.Version;
104 SpecialVersion = semVer.SpecialVersion;
110 public Version Version {
get;
private set; }
115 public string SpecialVersion {
get;
private set; }
119 if (!String.IsNullOrEmpty(originalString))
124 int dashIndex = originalString.IndexOf(
'-');
128 original = originalString.Substring(0, dashIndex);
132 original = originalString;
135 return SplitAndPadVersionString(original);
139 return SplitAndPadVersionString(Version.ToString());
143 private static string[] SplitAndPadVersionString(
string version)
145 string[]
a = version.Split(
'.');
154 var
b =
new string[4] {
"0",
"0",
"0",
"0" };
155 Array.Copy(
a, 0,
b, 0, a.Length);
165 if (String.IsNullOrEmpty(version))
167 throw new ArgumentNullException(
"version",
"cannot be null or empty");
171 if (!TryParse(version, out semVer))
173 throw new ArgumentException(String.Format(
"Invalid version format [{0}]", version),
"version");
183 return TryParseInternal(version, SemanticVersionRegex, out value);
191 return TryParseInternal(version, StrictSemanticVersionRegex, out value);
194 private static bool TryParseInternal(
string version, Regex regex, out
PackageVersion semVer)
197 if (String.IsNullOrEmpty(version))
202 var match = regex.Match(version.Trim());
203 Version versionValue;
204 if (!match.Success || !Version.TryParse(match.Groups[
"Version"].Value, out versionValue))
208 if (Int32.TryParse(version, out versionNumber) && versionNumber > 0)
210 semVer =
new PackageVersion(
new Version(versionNumber, 0));
217 semVer =
new PackageVersion(NormalizeVersionValue(versionValue), match.Groups[
"Release"].Value.TrimStart(
'-'), version.Replace(
" ",
""));
228 TryParse(version, out semVer);
232 private static Version NormalizeVersionValue(Version version)
234 return new Version(version.Major,
236 Math.Max(version.Build, 0),
237 Math.Max(version.Revision, 0));
242 if (
Object.ReferenceEquals(obj, null))
249 throw new ArgumentException(
"Object must be a SemanticVersion",
"obj");
251 return CompareTo(other);
256 if (
Object.ReferenceEquals(other, null))
261 int result = Version.CompareTo(other.Version);
268 bool empty = String.IsNullOrEmpty(SpecialVersion);
269 bool otherEmpty = String.IsNullOrEmpty(other.SpecialVersion);
270 if (empty && otherEmpty)
282 return StringComparer.OrdinalIgnoreCase.Compare(SpecialVersion, other.SpecialVersion);
287 if (
Object.ReferenceEquals(version1, null))
289 return Object.ReferenceEquals(version2, null);
291 return version1.Equals(version2);
296 return !(version1 == version2);
301 if (version1 == null)
303 throw new ArgumentNullException(
"version1");
305 return version1.CompareTo(version2) < 0;
310 return (version1 == version2) || (version1 < version2);
315 if (version1 == null)
317 throw new ArgumentNullException(
"version1");
319 return version2 < version1;
324 return (version1 == version2) || (version1 > version2);
329 return originalString;
334 return !Object.ReferenceEquals(null, other) &&
335 Version.Equals(other.
Version) &&
336 SpecialVersion.Equals(other.SpecialVersion, StringComparison.OrdinalIgnoreCase);
342 return !Object.ReferenceEquals(null, semVer) && Equals(semVer);
347 int hashCode = Version.GetHashCode();
348 if (SpecialVersion != null)
350 hashCode = hashCode*4567 + SpecialVersion.GetHashCode();
356 internal class PackageVersionDataSerializer :
DataSerializer<PackageVersion>
359 public override bool IsBlittable {
get {
return true; } }
366 string version = null;
367 stream.Serialize(ref version);
368 obj = PackageVersion.Parse(version);
372 string version = obj.ToString();
373 stream.Serialize(ref version);
378 internal static PackageVersion FromSemanticVersion(NuGet.SemanticVersion semanticVersion)
380 if (semanticVersion == null)
384 return new PackageVersion(semanticVersion.ToString());
string[] GetOriginalVersionComponents()
static PackageVersion ParseOptionalVersion(string version)
Attempts to parse the version token as a SemanticVersion.
PackageVersion(int major, int minor, int build, string specialVersion)
Initializes a new instance of the PackageVersion class.
Flags
Enumeration of the new Assimp's flags.
static bool TryParse(string version, out PackageVersion value)
Parses a version string using loose semantic versioning rules that allows 2-4 version components foll...
Base class for implementation of SerializationStream.
static bool TryParseStrict(string version, out PackageVersion value)
Parses a version string using strict semantic versioning rules that allows exactly 3 components and a...
PackageVersion(Version version, string specialVersion)
Initializes a new instance of the PackageVersion class.
Version Version
Gets the normalized version portion.
PackageVersion(string version)
Initializes a new instance of the PackageVersion class.
bool Equals(PackageVersion other)
PackageVersion(int major, int minor, int build, int revision)
Initializes a new instance of the PackageVersion class.
int CompareTo(PackageVersion other)
int CompareTo(object obj)
Describes how to serialize and deserialize an object without knowing its type. Used as a common base ...
ArchiveMode
Enumerates the different mode of serialization (either serialization or deserialization).
override int GetHashCode()
PackageVersion(Version version)
Initializes a new instance of the PackageVersion class.
A hybrid implementation of SemVer that supports semantic versioning as described at http://semver...
static PackageVersion Parse(string version)
Parses a version string using loose semantic versioning rules that allows 2-4 version components foll...
override bool Equals(object obj)
override string ToString()