aboutsummaryrefslogtreecommitdiff
path: root/src/Projects/NativeProjectDom.cs
blob: f23a4a562dd5264d5755eff2871d4991ed466a50 (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
using System;
using System.IO;
using System.Text.Json;
using System.Collections.Generic;

using VNLib.Tools.Build.Executor.Model;

namespace VNLib.Tools.Build.Executor.Projects
{
    internal sealed class NativeProjectDom : IProjectData
    {
        private Dictionary<string, string> _properties;

        internal NativeProjectDom()
        {
            _properties = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        }

        public string? this[string index] => _properties.GetValueOrDefault(index);

        public string? Description => this["description"];
        public string? Authors => this["author"];
        public string? Copyright => this["copyright"];
        public string? VersionString => this["version"];
        public string? CompanyName => this["company"];
        public string? Product => this["name"];
        public string? RepoUrl => this["repository"];
        public string? OutputDir => this["output_dir"];

        public string[] GetProjectRefs()
        {
            return Array.Empty<string>();
        }

        public void Load(Stream stream)
        {
            //Read the json file
            using JsonDocument doc = JsonDocument.Parse(stream, new JsonDocumentOptions
            {
                CommentHandling = JsonCommentHandling.Skip,
                AllowTrailingCommas = true,
            });

            //Clear old properties
            _properties.Clear();

            //Load new properties that are strings only
            foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
            {
                if(prop.Value.ValueKind == JsonValueKind.String)
                {
                    _properties[prop.Name] = prop.Value.GetString() ?? string.Empty;
                }
            }
        }
    }
}