WinGetVersion.cs (4715B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="WinGetVersion.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Client.Engine.Helpers 8 { 9 using System; 10 using Microsoft.WinGet.Common.Command; 11 12 /// <summary> 13 /// WinGetVersion. Parse the string version returned by winget --version to allow comparisons. 14 /// </summary> 15 internal class WinGetVersion 16 { 17 /// <summary> 18 /// Initializes a new instance of the <see cref="WinGetVersion"/> class. 19 /// </summary> 20 /// <param name="version">String Version.</param> 21 public WinGetVersion(string version) 22 { 23 if (string.IsNullOrEmpty(version)) 24 { 25 throw new ArgumentNullException(); 26 } 27 28 string toParseVersion = version; 29 30 // WinGet version starts with v 31 if (toParseVersion[0] == 'v') 32 { 33 this.TagVersion = version; 34 toParseVersion = toParseVersion.Substring(1); 35 } 36 else 37 { 38 // WinGet version always start with v. 39 this.TagVersion = 'v' + version; 40 } 41 42 // WinGet version might end with -preview 43 if (toParseVersion.EndsWith("-preview")) 44 { 45 this.IsPrerelease = true; 46 toParseVersion = toParseVersion.Substring(0, toParseVersion.IndexOf('-')); 47 } 48 49 this.Version = Version.Parse(toParseVersion); 50 } 51 52 /// <summary> 53 /// Gets the version as it appears as a tag. 54 /// </summary> 55 public string TagVersion { get; } 56 57 /// <summary> 58 /// Gets the version. 59 /// </summary> 60 public System.Version Version { get; } 61 62 /// <summary> 63 /// Gets a value indicating whether is this version is a prerelease. 64 /// </summary> 65 public bool IsPrerelease { get; } 66 67 /// <summary> 68 /// Gets the version of the installed winget. 69 /// </summary> 70 /// <param name="pwshCmdlet">PowerShell cmdlet.</param> 71 /// <returns>The WinGetVersion.</returns> 72 public static WinGetVersion InstalledWinGetVersion(PowerShellCmdlet pwshCmdlet) 73 { 74 // Try getting the version through COM if it is available (user might have an older build installed) 75 string? comVersion = PackageManagerWrapper.Instance.GetVersion(); 76 if (comVersion != null) 77 { 78 return new WinGetVersion(comVersion); 79 } 80 81 var wingetCliWrapper = new WingetCLIWrapper(); 82 var result = wingetCliWrapper.RunCommand(pwshCmdlet, "--version"); 83 return new WinGetVersion(result.StdOut.Replace(Environment.NewLine, string.Empty)); 84 } 85 86 /// <summary> 87 /// Version.CompareTo taking into account prerelease. 88 /// From semver: Pre-release versions have a lower precedence than the associated normal version. 89 /// </summary> 90 /// <param name="otherVersion">Other winget version.</param> 91 /// <returns> 92 /// A signed integer that indicates the relative values of the two objects. Less than 0 93 /// means this version is before other version. 0 means they are equal. Greater than 0 94 /// means this version is greater. 95 /// </returns> 96 public int CompareTo(WinGetVersion otherVersion) 97 { 98 if (this.IsPrerelease && !otherVersion.IsPrerelease) 99 { 100 return -1; 101 } 102 103 if (!this.IsPrerelease && otherVersion.IsPrerelease) 104 { 105 return 1; 106 } 107 108 return this.Version.CompareTo(otherVersion.Version); 109 } 110 111 /// <summary> 112 /// Deployment doesn't care about semver or prerelease builds. 113 /// </summary> 114 /// <param name="otherVersion">Other version.</param> 115 /// <returns> 116 /// A signed integer that indicates the relative values of the two objects. Less than 0 117 /// means this version is before other version. 0 means they are equal. Greater than 0 118 /// means this version is greater. 119 /// </returns> 120 public int CompareAsDeployment(WinGetVersion otherVersion) 121 { 122 return this.Version.CompareTo(otherVersion.Version); 123 } 124 } 125 }