我目前正在构建一个与teamcity交互的Cake构建脚本,我想提供进度消息。我可以轻松报告任务名称,但我无法找出构建脚本中的进度。想想(“执行任务1的5”)。
有没有办法在构建脚本中找到进度?
答案 0 :(得分:6)
您可以使用TaskSetup全球提供的Tasks和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(()=>{});
任务图是内部的,虽然不是最优的(但是更正确),您可以自己走依赖图并计算有多少独特任务,例如:
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);
总而言之,您可以通过以下方式实现您的目标: