aboutsummaryrefslogtreecommitdiff
path: root/src/SleetFeedManager.cs
blob: bc77ff1f57a476995fdbbb1c3842c85a96a37ecb (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
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;

using VNLib.Tools.Build.Executor.Model;

namespace VNLib.Tools.Build.Executor
{
    internal sealed class SleetFeedManager : IFeedManager
    {
        private readonly string SleetConfigFile;

        ///<inheritdoc/>
        public string FeedOutputDir { get; }

        private SleetFeedManager(string indexFilex, string outputDir)
        {
            //Search for the sleet file in the build dir
            SleetConfigFile = indexFilex;
            FeedOutputDir = outputDir;
        }

        ///<inheritdoc/>
        public void AddVariables(TaskfileVars vars)
        {
            vars.Set("SLEET_DIR", FeedOutputDir);
            vars.Set("SLEET_CONFIG_PATH", SleetConfigFile);
        }

        /// <summary>
        /// Attempts to create a new sleet feed manager from the given directory index
        /// if the index contains a sleet feed. Returns null if no sleet feed was found
        /// </summary>
        /// <returns>The feed manager if found, null otherwise</returns>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="ArgumentException"></exception>
        [return:NotNullIfNotNull(nameof(feedPath))]
        public static IFeedManager? GetSleetFeed(string? feedPath)
        {
            if(string.IsNullOrWhiteSpace(feedPath))
            {
                return null;
            }

            //Read the sleet config file
            byte[] sleetIndexFile = File.ReadAllBytes(feedPath);
            using JsonDocument doc = JsonDocument.Parse(sleetIndexFile);
            string rootDir = doc.RootElement.GetProperty("root").GetString() ?? throw new ArgumentException("The sleet output directory was not specified");
            return new SleetFeedManager(feedPath, rootDir);
        }
    }
}