19 using System.Collections.Generic;
24 using System.Text.RegularExpressions;
28 #region Prerelease taxonomy enumeration 30 public enum Prerelease
46 #region Public properties 48 public int Major {
get;
set; }
49 public int Minor {
get;
set; }
50 public int Patch {
get;
set; }
51 public Prerelease Prerelease {
get;
set; }
52 public int PreMajor {
get;
set; }
53 public int PreMinor {
get;
set; }
54 public int PrePatch {
get;
set; }
55 public string Build {
get;
set; }
59 #region Private fields 61 private string _version;
89 var versionFile = from resources in assembly.GetManifestResourceNames()
90 where resources.EndsWith(
".VERSION")
92 Stream stream = assembly.GetManifestResourceStream(versionFile.First());
93 StreamReader text =
new StreamReader(stream);
94 ParseString(text.ReadLine());
103 return (lower.CompareTo(higher) < 0);
108 return (lower.CompareTo(higher) < 0);
113 return (lower.CompareTo(higher) <= 0);
118 return (lower.CompareTo(higher) <= 0);
125 return (v1.Equals(v2));
127 catch (NullReferenceException)
129 return (
object)v2 == null;
137 return (!v1.Equals(v2));
139 catch (NullReferenceException)
141 return (
object)v2 != null;
149 public int CompareTo(
object obj)
152 if (this.Major < other.Major)
156 else if (this.Major > other.Major)
162 if (this.Minor < other.Minor)
166 else if (this.Minor > other.Minor)
172 if (this.Patch < other.Patch)
176 else if (this.Patch > other.Patch)
182 if (this.Prerelease < other.Prerelease)
186 else if (this.Prerelease > other.Prerelease)
192 if (this.Prerelease == Versioning.Prerelease.Numeric)
194 if (this.PreMajor < other.PreMajor)
198 else if (this.PreMajor > other.PreMajor)
204 if (this.PreMinor < other.PreMinor)
208 else if (this.PreMinor > other.PreMinor)
214 if (this.PrePatch < other.PrePatch)
218 else if (this.PrePatch> other.PrePatch)
231 if (this.PrePatch < other.PrePatch)
235 else if (this.PrePatch > other.PrePatch)
250 public override bool Equals(
object obj)
254 return (this.CompareTo(obj) == 0);
265 #region Object overrides 277 public override int GetHashCode()
280 return _version.GetHashCode();
285 #region Internal logic 294 @"(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)" +
295 @"(-(?<pre>((?<preMajor>\d+)\.(?<preMinor>\d+)\.|"+
296 @"((?<alpha>alpha)|(?<beta>beta)|(?<rc>rc))\.)(?<prePatch>\d+)))?" +
297 @"(\+(?<build>[a-zA-Z0-9]+))?");
298 Match m = r.Match(s);
306 Major = Convert.ToInt32(m.Groups[
"major"].Value);
307 Minor = Convert.ToInt32(m.Groups[
"minor"].Value);
308 Patch = Convert.ToInt32(m.Groups[
"patch"].Value);
310 if (m.Groups[
"pre"].Success)
312 if (m.Groups[
"alpha"].Success)
314 Prerelease = Versioning.Prerelease.Alpha;
316 else if (m.Groups[
"beta"].Success)
318 Prerelease = Versioning.Prerelease.Beta;
320 else if (m.Groups[
"rc"].Success)
322 Prerelease = Versioning.Prerelease.RC;
326 Prerelease = Versioning.Prerelease.Numeric;
327 PreMajor = Convert.ToInt32(m.Groups[
"preMajor"].Value);
328 PreMinor = Convert.ToInt32(m.Groups[
"preMinor"].Value);
333 Prerelease = Versioning.Prerelease.None;
335 if (m.Groups[
"prePatch"].Success)
337 PrePatch = Convert.ToInt32(m.Groups[
"prePatch"].Value);
340 Build = m.Groups[
"build"].Value;
343 protected void BuildString()
345 string s = String.Format(
"{0}.{1}.{2}", Major, Minor, Patch);
346 if (Prerelease != Prerelease.None)
348 if (Prerelease == Prerelease.Numeric)
350 s += String.Format(
"-{0}.{1}.{2}", PreMajor, PreMinor, PrePatch);
354 s += String.Format(
"-{0}.{1}", Prerelease.ToString().ToLower(), PrePatch);
357 if (!
string.IsNullOrWhiteSpace(Build))
359 s += String.Format(
"+{0}", Build);
SemanticVersion(string version)
Instantiates the class from a given version string.
Class that handles semantic versioning.
SemanticVersion(Assembly assembly)
Creates an instance with the current version information, which must be contained in a file called "V...
override string ToString()
Returns the full version string.
void ParseString(string s)
Parses a string that complies with semantic versioning, V.