Cake:构建脚本进度

Cake: Build script progress

我目前正在构建一个与 teamcity 交互的 Cake 构建脚本,我想提供进度消息。我可以很容易地报告任务名称,但我无法找出构建脚本中的进度。想想 ("executing task 1 of 5").

有什么方法可以在构建脚本中找到进度吗?

您可以使用 TaskSetup and Tasks which are provided globally on the IScriptHost 并且随时可用。

下面的例子:

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);

此脚本的输出如下所示:

它有一个缺陷,但如果您注册的任务未包含在当前目标中,它们也会被计算在内。

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

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

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

输出 Tasks.Count 将为 6,输出将如下所示:

任务图是内部的,尽管不是最优的(但更正确),您可以自己遍历依赖图并计算有多少个独特的任务,下面的示例:

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);

此脚本的输出为:

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

  • Setup task to count Tasks 给定目标使用递归函数可用,将结果存储在全局可用变量中。
  • TaskSetup 显示您的消息