| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  | namespace ServiceLib.Common; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class SemanticVersion | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     private readonly int major; | 
					
						
							|  |  |  |     private readonly int minor; | 
					
						
							|  |  |  |     private readonly int patch; | 
					
						
							|  |  |  |     private readonly string version; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public SemanticVersion(int major, int minor, int patch) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         this.major = major; | 
					
						
							|  |  |  |         this.minor = minor; | 
					
						
							|  |  |  |         this.patch = patch; | 
					
						
							|  |  |  |         version = $"{major}.{minor}.{patch}"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public SemanticVersion(string? version) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         try | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             if (string.IsNullOrEmpty(version)) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-03-06 02:47:31 +00:00
										 |  |  |                 major = 0; | 
					
						
							|  |  |  |                 minor = 0; | 
					
						
							|  |  |  |                 patch = 0; | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             this.version = version.RemovePrefix('v'); | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             var parts = this.version.Split('.'); | 
					
						
							|  |  |  |             if (parts.Length == 2) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 major = int.Parse(parts.First()); | 
					
						
							|  |  |  |                 minor = int.Parse(parts.Last()); | 
					
						
							|  |  |  |                 patch = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (parts.Length is 3 or 4) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 major = int.Parse(parts[0]); | 
					
						
							|  |  |  |                 minor = int.Parse(parts[1]); | 
					
						
							|  |  |  |                 patch = int.Parse(parts[2]); | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 throw new ArgumentException("Invalid version string"); | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |         catch | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             major = 0; | 
					
						
							|  |  |  |             minor = 0; | 
					
						
							|  |  |  |             patch = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public override bool Equals(object? obj) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (obj is SemanticVersion other) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return major == other.major && minor == other.minor && patch == other.patch; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             return false; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public override int GetHashCode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return major.GetHashCode() ^ minor.GetHashCode() ^ patch.GetHashCode(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     /// <summary> | 
					
						
							|  |  |  |     /// Use ToVersionString(string? prefix) instead if possible. | 
					
						
							|  |  |  |     /// </summary> | 
					
						
							|  |  |  |     /// <returns>major.minor.patch</returns> | 
					
						
							|  |  |  |     public override string ToString() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return version; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public string ToVersionString(string? prefix = null) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (prefix == null) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-03-06 02:47:31 +00:00
										 |  |  |             return version; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |         else | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             return $"{prefix}{version}"; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public static bool operator ==(SemanticVersion v1, SemanticVersion v2) | 
					
						
							|  |  |  |     { return v1.Equals(v2); } | 
					
						
							| 
									
										
										
										
											2023-05-14 09:25:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public static bool operator !=(SemanticVersion v1, SemanticVersion v2) | 
					
						
							|  |  |  |     { return !v1.Equals(v2); } | 
					
						
							| 
									
										
										
										
											2023-05-14 09:25:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public static bool operator >=(SemanticVersion v1, SemanticVersion v2) | 
					
						
							|  |  |  |     { return v1.GreaterEquals(v2); } | 
					
						
							| 
									
										
										
										
											2023-05-14 09:25:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     public static bool operator <=(SemanticVersion v1, SemanticVersion v2) | 
					
						
							|  |  |  |     { return v1.LessEquals(v2); } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     #region Private | 
					
						
							| 
									
										
										
										
											2023-05-14 09:25:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     private bool GreaterEquals(SemanticVersion other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (major < other.major) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (major > other.major) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             if (minor < other.minor) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             else if (minor > other.minor) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 if (patch < other.patch) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 else if (patch > other.patch) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                     return true; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |     private bool LessEquals(SemanticVersion other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (major < other.major) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (major > other.major) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (minor < other.minor) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |             else if (minor > other.minor) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |             { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 if (patch < other.patch) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 { | 
					
						
							|  |  |  |                     return true; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                 else if (patch > other.patch) | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 { | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  |                     return true; | 
					
						
							| 
									
										
										
										
											2023-05-09 13:54:56 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-04-02 03:44:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     #endregion Private | 
					
						
							| 
									
										
										
										
											2025-03-06 02:47:31 +00:00
										 |  |  | } |