summaryrefslogtreecommitdiff
path: root/src/SMAPI/SemanticVersion.cs
blob: 4a175efecd1ff0466799b45cc0c5f62640e37010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using Newtonsoft.Json;

namespace StardewModdingAPI
{
    /// <summary>A semantic version with an optional release tag.</summary>
    public class SemanticVersion : ISemanticVersion
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying semantic version implementation.</summary>
        private readonly ISemanticVersion Version;


        /*********
        ** Accessors
        *********/
        /// <summary>The major version incremented for major API changes.</summary>
        public int MajorVersion => this.Version.MajorVersion;

        /// <summary>The minor version incremented for backwards-compatible changes.</summary>
        public int MinorVersion => this.Version.MinorVersion;

        /// <summary>The patch version for backwards-compatible bug fixes.</summary>
        public int PatchVersion => this.Version.PatchVersion;

        /// <summary>An optional prerelease tag.</summary>
        public string PrereleaseTag => this.Version.PrereleaseTag;

        /// <summary>Optional build metadata. This is ignored when determining version precedence.</summary>
        public string BuildMetadata => this.Version.BuildMetadata;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="majorVersion">The major version incremented for major API changes.</param>
        /// <param name="minorVersion">The minor version incremented for backwards-compatible changes.</param>
        /// <param name="patchVersion">The patch version for backwards-compatible bug fixes.</param>
        /// <param name="prereleaseTag">An optional prerelease tag.</param>
        /// <param name="buildMetadata">Optional build metadata. This is ignored when determining version precedence.</param>
        public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, string prereleaseTag = null, string buildMetadata = null)
            : this(majorVersion, minorVersion, patchVersion, 0, prereleaseTag, buildMetadata) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="majorVersion">The major version incremented for major API changes.</param>
        /// <param name="minorVersion">The minor version incremented for backwards-compatible changes.</param>
        /// <param name="patchVersion">The patch version for backwards-compatible bug fixes.</param>
        /// <param name="prereleaseTag">An optional prerelease tag.</param>
        /// <param name="platformRelease">The platform-specific version (if applicable).</param>
        /// <param name="buildMetadata">Optional build metadata. This is ignored when determining version precedence.</param>
        [JsonConstructor]
        internal SemanticVersion(int majorVersion, int minorVersion, int patchVersion, int platformRelease, string prereleaseTag = null, string buildMetadata = null)
            : this(new Toolkit.SemanticVersion(majorVersion, minorVersion, patchVersion, platformRelease, prereleaseTag, buildMetadata)) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="version">The semantic version string.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>
        /// <exception cref="FormatException">The <paramref name="version"/> is not a valid semantic version.</exception>
        public SemanticVersion(string version)
            : this(version, allowNonStandard: false) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="version">The semantic version string.</param>
        /// <param name="allowNonStandard">Whether to recognize non-standard semver extensions.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>
        /// <exception cref="FormatException">The <paramref name="version"/> is not a valid semantic version.</exception>
        internal SemanticVersion(string version, bool allowNonStandard)
            : this(new Toolkit.SemanticVersion(version, allowNonStandard)) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="version">The assembly version.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="version"/> is null.</exception>
        public SemanticVersion(Version version)
            : this(new Toolkit.SemanticVersion(version)) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="version">The underlying semantic version implementation.</param>
        internal SemanticVersion(ISemanticVersion version)
        {
            this.Version = version;
        }

        /// <summary>Whether this is a prerelease version.</summary>
        public bool IsPrerelease()
        {
            return this.Version.IsPrerelease();
        }

        /// <summary>Get an integer indicating whether this version precedes (less than 0), supersedes (more than 0), or is equivalent to (0) the specified version.</summary>
        /// <param name="other">The version to compare with this instance.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="other"/> value is null.</exception>
        /// <remarks>The implementation is defined by Semantic Version 2.0 (https://semver.org/).</remarks>
        public int CompareTo(ISemanticVersion other)
        {
            return this.Version.CompareTo(other);
        }

        /// <summary>Get whether this version is older than the specified version.</summary>
        /// <param name="other">The version to compare with this instance.</param>
        public bool IsOlderThan(ISemanticVersion other)
        {
            return this.Version.IsOlderThan(other);
        }

        /// <summary>Get whether this version is older than the specified version.</summary>
        /// <param name="other">The version to compare with this instance.</param>
        /// <exception cref="FormatException">The specified version is not a valid semantic version.</exception>
        public bool IsOlderThan(string other)
        {
            return this.Version.IsOlderThan(other);
        }

        /// <summary>Get whether this version is newer than the specified version.</summary>
        /// <param name="other">The version to compare with this instance.</param>
        public bool IsNewerThan(ISemanticVersion other)
        {
            return this.Version.IsNewerThan(other);
        }

        /// <summary>Get whether this version is newer than the specified version.</summary>
        /// <param name="other">The version to compare with this instance.</param>
        /// <exception cref="FormatException">The specified version is not a valid semantic version.</exception>
        public bool IsNewerThan(string other)
        {
            return this.Version.IsNewerThan(other);
        }

        /// <summary>Get whether this version is between two specified versions (inclusively).</summary>
        /// <param name="min">The minimum version.</param>
        /// <param name="max">The maximum version.</param>
        public bool IsBetween(ISemanticVersion min, ISemanticVersion max)
        {
            return this.Version.IsBetween(min, max);
        }

        /// <summary>Get whether this version is between two specified versions (inclusively).</summary>
        /// <param name="min">The minimum version.</param>
        /// <param name="max">The maximum version.</param>
        /// <exception cref="FormatException">One of the specified versions is not a valid semantic version.</exception>
        public bool IsBetween(string min, string max)
        {
            return this.Version.IsBetween(min, max);
        }

        /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
        /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(ISemanticVersion other)
        {
            return other != null && this.CompareTo(other) == 0;
        }

        /// <summary>Get a string representation of the version.</summary>
        public override string ToString()
        {
            return this.Version.ToString();
        }

        /// <summary>Whether the version uses non-standard extensions, like four-part game versions on some platforms.</summary>
        public bool IsNonStandard()
        {
            return this.Version.IsNonStandard();
        }

        /// <summary>Parse a version string without throwing an exception if it fails.</summary>
        /// <param name="version">The version string.</param>
        /// <param name="parsed">The parsed representation.</param>
        /// <returns>Returns whether parsing the version succeeded.</returns>
        public static bool TryParse(string version, out ISemanticVersion parsed)
        {
            if (Toolkit.SemanticVersion.TryParse(version, out ISemanticVersion versionImpl))
            {
                parsed = new SemanticVersion(versionImpl);
                return true;
            }

            parsed = null;
            return false;
        }
    }
}