| | 1 | | using System.Text; |
| | 2 | | using System.Text.Json.Serialization; |
| | 3 | | using Dotnet.Installer.Core.Converters; |
| | 4 | |
|
| | 5 | | namespace Dotnet.Installer.Core.Types; |
| | 6 | |
|
| | 7 | | [JsonConverter(typeof(DotnetVersionJsonConverter))] |
| | 8 | | public partial class DotnetVersion : IEquatable<DotnetVersion>, IComparable<DotnetVersion> |
| | 9 | | { |
| 142 | 10 | | public DotnetVersion(int major, int minor, int patch, bool isPreview = false, bool isRc = false, |
| 142 | 11 | | int? previewIdentifier = null, int? revision = null) |
| 142 | 12 | | { |
| 142 | 13 | | if (isPreview && isRc) |
| 1 | 14 | | { |
| 1 | 15 | | throw new ApplicationException("The .NET version can either be a preview, an RC, or none."); |
| | 16 | | } |
| | 17 | |
|
| 141 | 18 | | if ((isPreview || isRc) && previewIdentifier is null) |
| 2 | 19 | | { |
| 2 | 20 | | throw new ApplicationException( |
| 2 | 21 | | "You must specify a Preview Identifier if version is either a preview of an RC."); |
| | 22 | | } |
| | 23 | |
|
| 139 | 24 | | if (!isPreview && !isRc && previewIdentifier is not null) |
| 1 | 25 | | { |
| 1 | 26 | | throw new ApplicationException( |
| 1 | 27 | | "You can't specify a Preview Identifier if the version is neither a preview or an RC."); |
| | 28 | | } |
| | 29 | |
|
| 138 | 30 | | if (revision.HasValue && revision.Value == 0) |
| 1 | 31 | | { |
| 1 | 32 | | throw new ApplicationException( |
| 1 | 33 | | "A revision can only contain a value starting with 1, null otherwise."); |
| | 34 | | } |
| | 35 | |
|
| 137 | 36 | | Major = major; |
| 137 | 37 | | Minor = minor; |
| 137 | 38 | | Patch = patch; |
| | 39 | |
|
| 137 | 40 | | IsPreview = isPreview; |
| 137 | 41 | | IsRc = isRc; |
| | 42 | |
|
| 137 | 43 | | PreviewIdentifier = previewIdentifier; |
| | 44 | |
|
| 137 | 45 | | Revision = revision; |
| 137 | 46 | | } |
| | 47 | |
|
| 146 | 48 | | public int Major { get; } |
| 146 | 49 | | public int Minor { get; } |
| 260 | 50 | | public int Patch { get; } |
| | 51 | |
|
| 278 | 52 | | public bool IsPreview { get; private set; } |
| 266 | 53 | | public bool IsRc { get; private set; } |
| 40 | 54 | | public bool IsStable => !IsPreview && !IsRc; |
| 356 | 55 | | public int? PreviewIdentifier { get; private set; } = null; |
| | 56 | |
|
| 280 | 57 | | public int? Revision { get; set; } |
| | 58 | |
|
| 59 | 59 | | public bool IsRuntime => Patch < 100; |
| 41 | 60 | | public bool IsSdk => !IsRuntime; |
| | 61 | |
|
| 26 | 62 | | public int? FeatureBand => !IsSdk ? default(int?) : int.Parse($"{Patch.ToString()[..1]}00"); |
| | 63 | |
|
| | 64 | | public static DotnetVersion Parse(string version) |
| 23 | 65 | | { |
| 23 | 66 | | int? revision = default; |
| 23 | 67 | | var revisionSplit = version.Split('+'); |
| 23 | 68 | | if (revisionSplit.Length > 1) |
| 4 | 69 | | { |
| 4 | 70 | | revision = int.Parse(revisionSplit[1]); |
| 4 | 71 | | } |
| | 72 | |
|
| 23 | 73 | | var previewSplit = revisionSplit[0].Split('-'); |
| 23 | 74 | | var versionSections = previewSplit[0].Split('.'); |
| 23 | 75 | | var parsedVersion = new DotnetVersion |
| 23 | 76 | | ( |
| 23 | 77 | | int.Parse(versionSections[0]), |
| 23 | 78 | | int.Parse(versionSections[1]), |
| 23 | 79 | | int.Parse(versionSections[2]) |
| 23 | 80 | | ); |
| | 81 | |
|
| 19 | 82 | | if (previewSplit.Length > 1) |
| 7 | 83 | | { |
| 7 | 84 | | var previewVersionSections = previewSplit[1].Split('.'); |
| | 85 | |
|
| 7 | 86 | | if (string.Equals("preview", previewVersionSections[0])) |
| 4 | 87 | | { |
| 4 | 88 | | parsedVersion.IsPreview = true; |
| 4 | 89 | | } |
| 3 | 90 | | else if (string.Equals("rc", previewVersionSections[0])) |
| 3 | 91 | | { |
| 3 | 92 | | parsedVersion.IsRc = true; |
| 3 | 93 | | } |
| | 94 | |
|
| 7 | 95 | | parsedVersion.PreviewIdentifier = int.Parse(previewVersionSections[1]); |
| 7 | 96 | | } |
| | 97 | |
|
| 19 | 98 | | parsedVersion.Revision = revision; |
| | 99 | |
|
| 19 | 100 | | return parsedVersion; |
| 19 | 101 | | } |
| | 102 | |
|
| | 103 | | public override string ToString() |
| 9 | 104 | | { |
| 9 | 105 | | var versionBuilder = new StringBuilder(); |
| | 106 | |
|
| 9 | 107 | | versionBuilder.Append(Major); |
| 9 | 108 | | versionBuilder.Append('.'); |
| 9 | 109 | | versionBuilder.Append(Minor); |
| 9 | 110 | | versionBuilder.Append('.'); |
| 9 | 111 | | versionBuilder.Append(Patch); |
| | 112 | |
|
| 12 | 113 | | if (IsPreview) versionBuilder.Append("-preview."); |
| 12 | 114 | | if (IsRc) versionBuilder.Append("-rc."); |
| | 115 | |
|
| 9 | 116 | | versionBuilder.Append(PreviewIdentifier.ToString()); |
| | 117 | |
|
| 12 | 118 | | if (Revision is not null) versionBuilder.Append('+'); |
| 9 | 119 | | versionBuilder.Append(Revision.ToString()); |
| | 120 | |
|
| 9 | 121 | | return versionBuilder.ToString(); |
| 9 | 122 | | } |
| | 123 | | } |