aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Wizards/Util/VCRulePropertyStorageHelper.cs
blob: 6b7d8151e106605fd7fb84058e025ca26da020e7 (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
/***************************************************************************************************
 Copyright (C) 2024 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE;

namespace QtVsTools.Wizards.Util
{
    using Core;

    static class VCRulePropertyStorageHelper
    {
        public static void SetQtModules(DTE dte, List<string> modules)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var newModules = modules.ToHashSet();
            if (modules.Count == 0)
                return;

            var qtProject = HelperFunctions.GetSelectedQtProject(dte);
            if (qtProject?.VcProject?.Configurations is not IVCCollection collection)
                return;

            // TODO: There is already code providing such functionality, though it seems overly
            // complicated to use compared to this simple for loop (see VCPropertyStorageProvider).
            foreach (VCConfiguration config in collection) {
                if (config.Rules.Item("QtRule10_Settings") is not IVCRulePropertyStorage props)
                    continue;
                var updatedModules = props.GetUnevaluatedPropertyValue("QtModules")
                    .Split(new[] { ';' }, System.StringSplitOptions.RemoveEmptyEntries)
                    .ToHashSet()
                    .Union(newModules);
                props.SetPropertyValue("QtModules", string.Join(";", updatedModules));
            }
        }
    }
}