aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/MsBuild/MsBuildProjectFormat.cs
blob: f8638e8fba3710722af36d26792c51b34308761e (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
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

using System;
using Microsoft.VisualStudio.VCProjectEngine;

namespace QtVsTools.Core
{
    public static class MsBuildProjectFormat
    {
        /// <summary>
        /// An enumeration containing the version numbers used during the Qt VS Tools development
        /// cycle.
        /// </summary>
        public enum Version
        {
            /// <summary>
            /// The version number cannot be read or is out of range.
            /// </summary>
            Unknown = 0,
            /// <summary>
            /// Deprecated, do not use.
            /// </summary>
            V1 = 100,
            /// <summary>
            /// Deprecated, do not use.
            /// </summary>
            V2 = 200,
            /// <summary>
            /// Minimum format version for Qt settings as project properties.
            /// </summary>
            V3 = 300,
            /// <summary>
            /// Minimum format version for shared compiler properties
            /// </summary>
            V3ClProperties = 300,
            /// <summary>
            /// Minimum format version for global QtMsBuild property.
            /// </summary>
            V3GlobalQtMsBuildProperty = 302,
            /// <summary>
            /// Minimum format version for correct ordering of property evaluation. (QTVSADDINBUG-787)
            /// </summary>
            V3PropertyEval = 303,
            /// <summary>
            /// Latest version of Qt VS Tools, also used as right part of the version tag.
            /// <para>See also: <seealso cref="QtVsVersionTag"/> </para>
            /// </summary>
            Latest = 304
        }

        // Old Qt VS project tag
        public const string KeywordV2 = "Qt4VS";

        /// <summary>
        /// The latest left part of the Qt VS Tools version tag.
        /// </summary>
        public const string KeywordLatest = "QtVS";

        /// <summary>
        /// Qt VS tool version tag used as 'Keyword' inside project files.
        /// Combination of latest keyword and format version.
        /// <para>See also:
        /// <seealso cref="KeywordLatest"/> and <seealso cref="Version.Latest"/>
        /// </para>
        /// </summary>
        public static readonly string QtVsVersionTag = $"{KeywordLatest}_v{(int)Version.Latest}";

        /// <summary>
        /// Tries to retrieve the format version from the project.
        /// <para>Attempts to find the format version based on the following conditions:</para>
        /// <example>
        /// <code>
        /// * Prerequisite: The project is a C++ project.
        /// * The project contains a Qt specific attribute that starts with Qt4VS.
        /// * The project contains a Qt specific attribute that starts with QtVS.
        /// </code>
        /// </example>
        /// </summary>
        /// <param name="project">The project to get the format version from.</param>
        /// <returns></returns>
        public static Version GetVersion(VCProject project)
        {
            var keyword = project?.keyword;
            if (string.IsNullOrEmpty(keyword))
                return Version.Unknown;

            if (keyword.StartsWith(KeywordV2, StringComparison.Ordinal))
                return Version.V1;

            if (!keyword.StartsWith(KeywordLatest, StringComparison.Ordinal))
                return Version.Unknown;

            if (!int.TryParse(keyword.Substring(6), out var tmp))
                return Version.Unknown;
            return (Version)tmp is >= Version.V3 and <= Version.Latest ? (Version)tmp : Version.Unknown;
        }
    }
}