编程语言
首页 > 编程语言> > c# – Cake:构建脚本进度

c# – Cake:构建脚本进度

作者:互联网

我目前正在构建一个与团队城市互动的Cake构建脚本,我想提供进度消息.我可以轻松报告任务名称,但我无法找出构建脚本中的进度.想想(“执行任务1的5”).

有没有办法在构建脚本中找到进度?

最佳答案:

您可以使用IScriptHost全球提供的TaskSetupTasks并始终可用.

以下是一个例子:

string target = "A";
int taskCounter = 0;
TaskSetup(
    taskSetupContext => {
        ICakeTaskInfo task = taskSetupContext.Task;
        Information("Executing Task {0} of {1} (Name: {2}, Description: {3}, Dependencies: {4})",
            ++taskCounter,
            Tasks.Count,
            task.Name,
            task.Description,
            string.Join(",",
                    task.Dependencies
                    )
            );
    });


Task("A")
    .Description("Alpha")
    .IsDependentOn("B")
    .Does(()=>{});

Task("B")
    .Description("Beta")
    .IsDependentOn("C")
    .Does(()=>{});

Task("C")
    .Description("Charlie")
    .IsDependentOn("D")
    .Does(()=>{});

Task("D")
    .Description("Delta")
    .Does(()=>{});

RunTarget(target);

此脚本的输出如下所示:
Cake Script Output

这有一个缺陷,但是如果你的注册任务没有包含在目前的目标中,他们也会计算在内.

例如,如果我添加这些任务:

Task("E")
    .Description("Echo")
    .Does(()=>{});

Task("F")
    .Description("Foxtrot")
    .Does(()=>{});

输出Tasks.Count将为6,输出将如下所示:
Cake Build Script Output 6 Tasks

任务图是内部的,虽然不是最优的(但更正确),您可以自己走依赖图并计算有多少独特任务,例如下面的例子:

string target = "A";
int taskCounter = 0;
int taskCount = 0;

Setup(context => {
            // declare recursive task count function
            Func<string, List<string>, int> countTask = null;
            countTask = (taskName, countedTasks) => {
                    if (string.IsNullOrEmpty(taskName) || countedTasks.Contains(taskName))
                    {
                        return 0;
                    }

                    countedTasks.Add(taskName);

                    var task = Tasks.Where(t=>t.Name == taskName).FirstOrDefault();
                    if (task == null)
                    {
                        return 0;
                    }

                    int result = 1;
                    countedTasks.Add(taskName);
                    foreach(var dependecy in task.Dependencies)
                    {
                        result+=countTask(dependecy, countedTasks);
                    }

                    return result;
            };

        // count the task and store in globally available variable
        taskCount = countTask(target, new List<string>());
    });

TaskSetup(
    taskSetupContext => {
        ICakeTaskInfo task = taskSetupContext.Task;
        Information("Executing Task {0} of {1} (Name: {2}, Description: {3}, Dependencies: {4})",
            ++taskCounter,
            taskCount,
            task.Name,
            task.Description,
            string.Join(",",
                    task.Dependencies
                    )
            );
    });

Task("A")
    .Description("Alpha")
    .IsDependentOn("B")
    .Does(()=>{});

Task("B")
    .Description("Beta")
    .IsDependentOn("C")
    .Does(()=>{});

Task("C")
    .Description("Charlie")
    .IsDependentOn("D")
    .Does(()=>{});

Task("D")
    .Description("Delta")
    .Does(()=>{});

Task("E")
    .Description("Echo")
    .Does(()=>{});

Task("F")
    .Description("Foxtrot")
    .Does(()=>{});


RunTarget(target);

此脚本的输出将是:
Cake script output

总而言之,您可以通过以下方式实现您的目标:

> Setup任务使用递归函数计算给定目标可用的Tasks,将结果存储在全局可用变量中.
> TaskSetup显示您的消息

标签:c,cakebuild
来源: https://codeday.me/bug/20190516/1115129.html