summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/ProjectExtensions.cs
blob: 36961eb45ee878244a05fcaf43c11f3d6f61fc60 (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
using GodotTools.Core;
using System;
using DotNet.Globbing;
using Microsoft.Build.Construction;

namespace GodotTools.ProjectEditor
{
    public static class ProjectExtensions
    {
        public static bool HasItem(this ProjectRootElement root, string itemType, string include)
        {
            GlobOptions globOptions = new GlobOptions();
            globOptions.Evaluation.CaseInsensitive = false;

            string normalizedInclude = include.NormalizePath();

            foreach (var itemGroup in root.ItemGroups)
            {
                if (itemGroup.Condition.Length != 0)
                    continue;

                foreach (var item in itemGroup.Items)
                {
                    if (item.ItemType != itemType)
                        continue;

                    var glob = Glob.Parse(item.Include.NormalizePath(), globOptions);

                    if (glob.IsMatch(normalizedInclude))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
        {
            if (!root.HasItem(itemType, include))
            {
                root.AddItem(itemType, include);
                return true;
            }

            return false;
        }

        public static Guid GetGuid(this ProjectRootElement root)
        {
            foreach (var property in root.Properties)
            {
                if (property.Name == "ProjectGuid")
                    return Guid.Parse(property.Value);
            }

            return Guid.Empty;
        }
    }
}