SemanticVersion.cs (2510B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="SemanticVersion.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.Processor.Helpers 8 { 9 using System; 10 11 /// <summary> 12 /// A semantic version. 13 /// </summary> 14 internal class SemanticVersion 15 { 16 private const string MaxRange = "999999999"; 17 18 private readonly string semanticVersion; 19 20 /// <summary> 21 /// Initializes a new instance of the <see cref="SemanticVersion"/> class. 22 /// </summary> 23 /// <param name="version">Version.</param> 24 public SemanticVersion(string version) 25 { 26 this.semanticVersion = GetMaximumVersion(version); 27 28 // Prerelease versions append the prerelease tag after a - 29 // PowerShell doesn't handle semantic versions. 30 if (this.semanticVersion.Contains("-")) 31 { 32 var indexOf = this.semanticVersion.IndexOf("-"); 33 this.Version = new Version(this.semanticVersion[..indexOf]); 34 this.PrereleaseTag = this.semanticVersion[(indexOf + 1) ..]; 35 } 36 else 37 { 38 this.Version = new Version(this.semanticVersion); 39 } 40 } 41 42 /// <summary> 43 /// Gets the version without a prerelease tag. 44 /// </summary> 45 public Version Version { get; } 46 47 /// <summary> 48 /// Gets prerelease tag if any. 49 /// </summary> 50 public string? PrereleaseTag { get; } = null; 51 52 /// <summary> 53 /// Gets a value indicating whether if the semantic version is prerelease. 54 /// </summary> 55 public bool IsPrerelease => !string.IsNullOrEmpty(this.PrereleaseTag); 56 57 /// <inheritdoc/> 58 public override string ToString() 59 { 60 return this.semanticVersion; 61 } 62 63 /// <summary> 64 /// Max out a version by replacing * if needed. 65 /// </summary> 66 /// <param name="version">Version.</param> 67 /// <returns>Maxed version.</returns> 68 private static string GetMaximumVersion(string version) 69 { 70 return version.Replace("*", MaxRange); 71 } 72 } 73 }