Cake:构建脚本进度

时间:2016-08-04 08:08:36

标签: c# cakebuild

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

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

1 个答案:

答案 0 :(得分:6)

您可以使用TaskSetup全球提供的TasksIScriptHost并始终可用。

以下示例:

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显示您的消息