aboutsummaryrefslogtreecommitdiff
path: root/src/Model/TaskfileVars.cs
blob: c0e6cdc859c9b6a3214dfcb04099e86440296189 (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
using System;
using System.Collections.Generic;

namespace VNLib.Tools.Build.Executor.Model
{
    /// <summary>
    /// Represents a collection of taskfile "environment" variables
    /// </summary>
    public sealed class TaskfileVars
    {
        private readonly Dictionary<string, string> vars;

        public TaskfileVars()
        {
            vars = new(StringComparer.OrdinalIgnoreCase);
        }

        private TaskfileVars(IEnumerable<KeyValuePair<string, string>> values)
        {
            vars = new(values, StringComparer.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Gets all variables as a readonly dictionary
        /// </summary>
        /// <returns>The collection of environment variables</returns>
        public IReadOnlyDictionary<string, string> GetVariables() => vars;

        /// <summary>
        /// Sets a taskfile environment variable
        /// </summary>
        /// <param name="key">The variable name</param>
        /// <param name="value">The optional variable value</param>
        public void Set(string key, string? value) => vars[key] = value ?? string.Empty;

        /// <summary>
        /// Removes a taskfile environment variable
        /// </summary>
        /// <param name="key">The name of the variable to remove</param>
        public void Remove(string key) => vars.Remove(key);

        /// <summary>
        /// Clones the current taskfile variables into an independent instance
        /// </summary>
        /// <returns>The new <see cref="TaskfileVars"/> instance</returns>
        public TaskfileVars Clone() => new (vars);
    }
}