aboutsummaryrefslogtreecommitdiff
path: root/src/api/burn/WixToolset.BootstrapperApplicationApi/VerUtilVersion.cs
blob: 65b195a4d48650b577b047fdad3826c68fd68608 (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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.BootstrapperApplicationApi
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    /// An enhanced implementation of SemVer 2.0.
    /// </summary>
    public sealed class VerUtilVersion : IDisposable
    {
        internal VerUtilVersion(VerUtil.VersionHandle handle)
        {
            this.Handle = handle;

            var pVersion = handle.DangerousGetHandle();
            var version = (VerUtil.VersionStruct)Marshal.PtrToStructure(pVersion, typeof(VerUtil.VersionStruct));
            this.Version = Marshal.PtrToStringUni(version.sczVersion);
            this.Prefix = version.chPrefix;
            this.Major = version.dwMajor;
            this.Minor = version.dwMinor;
            this.Patch = version.dwPatch;
            this.Revision = version.dwRevision;
            this.ReleaseLabels = new VerUtilVersionReleaseLabel[version.cReleaseLabels];
            this.Metadata = VerUtil.VersionStringFromOffset(version.sczVersion, version.cchMetadataOffset);
            this.IsInvalid = version.fInvalid;
            this.HasMajor = version.fHasMajor;
            this.HasMinor = version.fHasMinor;
            this.HasPatch = version.fHasPatch;
            this.HasRevision = version.fHasRevision;

            for (var i = 0; i < version.cReleaseLabels; ++i)
            {
                var offset = i * Marshal.SizeOf(typeof(VerUtil.VersionReleaseLabelStruct));
                var pReleaseLabel = new IntPtr(version.rgReleaseLabels.ToInt64() + offset);
                this.ReleaseLabels[i] = new VerUtilVersionReleaseLabel(pReleaseLabel, version.sczVersion);
            }
        }

        /// <summary>
        /// String version, which would have stripped the leading 'v'.
        /// </summary>
        public string Version { get; private set; }

        /// <summary>
        /// Prefix character that was stripped from <c>Version</c>, or the null character if there was no prefix.
        /// </summary>
        public char Prefix { get; private set; }

        /// <summary>
        /// For version A.B.C.D, Major is A. It is 0 if not specified.
        /// </summary>
        public uint Major { get; private set; }

        /// <summary>
        /// For version A.B.C.D, Minor is B. It is 0 if not specified.
        /// </summary>
        public uint Minor { get; private set; }

        /// <summary>
        /// For version A.B.C.D, Patch is C. It is 0 if not specified.
        /// </summary>
        public uint Patch { get; private set; }

        /// <summary>
        /// For version A.B.C.D, Revision is D. It is 0 if not specified.
        /// </summary>
        public uint Revision { get; private set; }

        /// <summary>
        /// For version X.Y.Z-releaselabels+metadata, ReleaseLabels is the parsed information for releaselabels.
        /// </summary>
        public VerUtilVersionReleaseLabel[] ReleaseLabels { get; private set; }

        /// <summary>
        /// For version X.Y.Z-releaselabels+metadata, Metadata is the rest of the string after +.
        /// For invalid versions, it is all of the string after the point where it was an invalid string.
        /// </summary>
        public string Metadata { get; private set; }

        /// <summary>
        /// Whether the version conformed to the spec.
        /// </summary>
        public bool IsInvalid { get; private set; }

        /// <summary>
        /// Whether the Major part was specified.
        /// </summary>
        public bool HasMajor { get; private set; }

        /// <summary>
        /// Whether the Minor part was specified.
        /// </summary>
        public bool HasMinor { get; private set; }

        /// <summary>
        /// Whether the Patch part was specified.
        /// </summary>
        public bool HasPatch { get; private set; }

        /// <summary>
        /// Whether the Revision part was specified.
        /// </summary>
        public bool HasRevision { get; private set; }

        /// <inheritdoc/>
        public void Dispose()
        {
            if (this.Handle != null)
            {
                this.Handle.Dispose();
                this.Handle = null;
            }
        }

        private VerUtil.VersionHandle Handle { get; set; }

        internal VerUtil.VersionHandle GetHandle()
        {
            if (this.Handle == null)
            {
                throw new ObjectDisposedException(this.Version);
            }

            return this.Handle;
        }
    }
}