aboutsummaryrefslogtreecommitdiff
path: root/src/TaskFile.cs
blob: 6eef779993b2a3b36a1d4e8c2e2c23cd3a26cf2a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Tools.Build.Executor.Model;
using VNLib.Tools.Build.Executor.Constants;
using static VNLib.Tools.Build.Executor.Constants.Utils;

namespace VNLib.Tools.Build.Executor
{
    public enum TaskfileComamnd
    {
        Clean,
        Build,
        Upload,
        Update,
        PostbuildSuccess,
        PostbuildFailure,
        Publish,
        Test,
    }

    /// <summary>
    /// Represents a controller for the TaskFile build system
    /// </summary>
    public sealed class TaskFile(string taskFilePath, Func<string> moduleName)
    {
        /// <summary>
        /// Executes the desired Taskfile command with the given user args for 
        /// the configured manager.
        /// </summary>
        /// <param name="command">The command to execute</param>
        /// <param name="userArgs">Additional user arguments to pass to Task </param>
        /// <returns>A task that completes with the status code of the operation</returns>
        public async Task ExecCommandAsync(ITaskfileScope scope, TaskfileComamnd command, bool throwIfFailed)
        {
            //Get working copy of vars
            IReadOnlyDictionary<string, string> vars = scope.TaskVars.GetVariables();

            //Specify taskfile if it is set
            List<string> args = [];
            if(!string.IsNullOrWhiteSpace(scope.TaskfileName))
            {
                //If taskfile is set, we need to make sure it is in the working dir to execute it, otherwise just exit
                if(!File.Exists(Path.Combine(scope.WorkingDir.FullName, scope.TaskfileName)))
                {
                    return;
                }

                args.Add("-t");
                args.Add(scope.TaskfileName);
            }

            //Always add command last
            args.Add(GetCommand(command));

            //Exec task in the module dir
            int result = await RunProcessAsync(taskFilePath, scope.WorkingDir.FullName, args.ToArray(), vars);
            
            if(throwIfFailed)
            {
                ThrowIfStepFailed(result, command);
            }
        }

        private static string GetCommand(TaskfileComamnd cmd)
        {
            return cmd switch
            {
                TaskfileComamnd.Clean => "clean",
                TaskfileComamnd.Build => "build",
                TaskfileComamnd.Upload => "upload",
                TaskfileComamnd.Update => "update",
                TaskfileComamnd.PostbuildSuccess => "postbuild_success",
                TaskfileComamnd.PostbuildFailure => "postbuild_failed",
                TaskfileComamnd.Publish => "publish",
                TaskfileComamnd.Test => "test",
                _ => throw new NotImplementedException()
            };
        }

        private void ThrowIfStepFailed(int result, TaskfileComamnd cmd)
        {
            switch (result)
            {
                case 200:   //Named task not found
                    return;
                case 201:
                    Utils.ThrowIfStepFailed(false, $"Task failed to execute task command {cmd}", moduleName.Invoke());
                    return;
            }
        }
    }
}