public int CompareTo(SemanticVersion value)
 {
     if (value == null)
     {
         return 1;
     }
     if (Major != value.Major)
     {
         if (Major > value.Major)
         {
             return 1;
         }
         return -1;
     }
     if (Minor != value.Minor)
     {
         if (Minor > value.Minor)
         {
             return 1;
         }
         return -1;
     }
     if (Patch != value.Patch)
     {
         if (Patch > value.Patch)
         {
             return 1;
         }
         return -1;
     }
     if (Stability != value.Stability)
     {
         if (Stability > value.Stability)
         {
             return 1;
         }
         return -1;
     }
     if (PreReleasePartOne != value.PreReleasePartOne)
     {
         if (PreReleasePartOne > value.PreReleasePartOne)
         {
             return 1;
         }
         return -1;
     }
     if (PreReleasePartTwo != value.PreReleasePartTwo)
     {
         if (PreReleasePartTwo > value.PreReleasePartTwo)
         {
             return 1;
         }
         return -1;
     }
     return -1;
 }
        static string GetPreRelease(SemanticVersion version)
        {
            if (version.PreReleasePartOne == null)
            {
                throw new Exception("pre-release number is required");
            }
            if (version.PreReleasePartTwo == null)
            {
                return version.PreReleasePartOne.ToString();
            }

            return string.Format("{0}.{1}", version.PreReleasePartOne, version.PreReleasePartTwo);
        }
 public bool Equals(SemanticVersion obj)
 {
     if (obj == null)
     {
         return false;
     }
     return Major == obj.Major &&
            Minor == obj.Minor &&
            Patch == obj.Patch &&
            PreReleasePartOne == obj.PreReleasePartOne &&
            PreReleasePartOne == obj.PreReleasePartTwo &&
            Stability == obj.Stability &&
            Suffix == obj.Suffix;
 }
        public static bool TryParse(string versionString, out SemanticVersion semanticVersion)
        {
            var parts = versionString.Split('-');
            if (parts.Length > 2)
            {
                semanticVersion = null;
                return false;
            }
            var stableParts = parts.First().Split('.');

            if (stableParts.Length > 3)
            {
                semanticVersion = null;
                return false;
            }

            int major;
            if (!int.TryParse(stableParts[0], out major))
            {
                semanticVersion = null;
                return false;
            }
            var parsedVersion = new SemanticVersion
                                {
                                    Major = major
                                };
            if (stableParts.Length > 1)
            {
                int minor;
                if (!int.TryParse(stableParts[1], out minor))
                {
                    semanticVersion = null;
                    return false;
                }
                parsedVersion.Minor = minor;
            }

            if (stableParts.Length > 2)
            {
                int patch;
                if (!int.TryParse(stableParts[2], out patch))
                {
                    semanticVersion = null;
                    return false;
                }
                parsedVersion.Patch = patch;
            }

            if (parts.Length > 1)
            {
                var prereleaseString = parts[1];

                var buildIndex = prereleaseString.IndexOfAny("0123456789".ToCharArray());
                if (buildIndex < 0)
                {
                    semanticVersion = null;
                    return false;
                }
                var stageString = prereleaseString.Substring(0, buildIndex);

                if (stageString == "RC")
                {
                    parsedVersion.Stability = Stability.ReleaseCandidate;
                }
                else
                {
                    Stability stability;
                    if (!Enum.TryParse(stageString, true, out stability))
                    {
                        semanticVersion = null;
                        return false;
                    }
                    parsedVersion.Stability = stability;
                }

                int preReleasePartOne;
                var preReleaseString = prereleaseString.Substring(buildIndex);
                var preReleaseParts = preReleaseString.Split('.');
                if (preReleaseParts.Length > 2)
                {

                    semanticVersion = null;
                    return false;
                }
                if (!int.TryParse(preReleaseParts[0], out preReleasePartOne))
                {
                    semanticVersion = null;
                    return false;
                }
                parsedVersion.PreReleasePartOne = preReleasePartOne;

                if ((preReleaseParts.Length > 1))
                {
                    int preReleasePartTwo;
                    if ((!int.TryParse(preReleaseParts[1], out preReleasePartTwo)))
                    {
                        semanticVersion = null;
                        return false;
                    }

                    parsedVersion.PreReleasePartTwo = preReleasePartTwo;
                }
            }
            else
            {
                parsedVersion.Stability = Stability.Final;
            }
            semanticVersion = parsedVersion;
            return true;
        }