aboutsummaryrefslogtreecommitdiff
path: root/src/Projects/DotnetProjectDom.cs
blob: 9b447470a6321615cd10d30c438d5f8c5ae7d8a7 (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

using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;

using VNLib.Tools.Build.Executor.Model;

namespace VNLib.Tools.Build.Executor.Projects
{

    internal sealed class DotnetProjectDom : IProjectData
    {
        private readonly XmlDocument _dom;

        public DotnetProjectDom()
        {
            _dom = new();
        }

        public void Load(Stream stream)
        {
            _dom.Load(stream);
        }

        private XmlNode? project => _dom["Project"];

        public string? Description => GetProperty("Description");
        public string? Authors => GetProperty("Authors");
        public string? Copyright => GetProperty("Copyright");
        public string? VersionString => GetProperty("Version");
        public string? CompanyName => GetProperty("Company");
        public string? Product => GetProperty("Product");
        public string? RepoUrl => GetProperty("RepositoryUrl");

        public string? this[string index] => GetProperty(index);

        public string? GetProperty(string name) => GetItemAtPath($"PropertyGroup/{name}");

        public string? GetItemAtPath(string path) => project!.SelectSingleNode(path)?.InnerText;

        public string[] GetProjectRefs()
        {
            //Get the item group attr
            XmlNodeList? projectRefs = project?.SelectNodes("ItemGroup/ProjectReference");

            if (projectRefs != null)
            {
                List<string> refs = new();
                foreach (XmlNode projRef in projectRefs)
                {
                    //Get the project ref via its include attribute
                    refs.Add(projRef.Attributes["Include"].Value!);
                }
                return refs.ToArray();
            }
            return Array.Empty<string>();
        }
    }
}