aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Core/CMake/CMakeProject.BuildConfiguration.cs
blob: d453581305f916eb76ff3e1ffe1103bd2ab643bb (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
// 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 System.IO;
using Newtonsoft.Json.Linq;

namespace QtVsTools.Core.CMake
{
    public partial class CMakeProject
    {
        public string GetConfigurationFileName()
        {
            return HelperFunctions.ToNativeSeparator($@"{RootPath}\.vs\ProjectSettings.json");
        }

        public string GetActiveConfigurationName()
        {
            // first try the file created by Visual Studio
            var activeConfigFilename = GetConfigurationFileName();
            if (File.Exists(activeConfigFilename)) {
                try {
                    var json = JObject.Parse(File.ReadAllText(activeConfigFilename));
                    return json["CurrentProjectSetting"]?.ToString();
                } catch (Exception exception) {
                    exception.Log();
                }
            }

            // resort to CMakeUserPresets.json if we failed previously
            var userPresets = $@"{RootPath}\CMakeUserPresets.json";
            if (!File.Exists(userPresets))
                return null;

            try {
                var json = JObject.Parse(File.ReadAllText(userPresets));
                var configurePresets = json["configurePresets"] as JArray;
                var firstPreset = configurePresets?[0] as JObject;
                return firstPreset?["displayName"]?.ToString();
            } catch (Exception exception) {
                exception.Log();
            }

            return null;
        }
    }
}