JD2022-TU1/main/tools/apps/TeaBox/Script/BuildFarm/TaskFlow.cs

72 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TeaBox.Macros;
using static TeaBox.BuildFarm.LogUtils;
namespace TeaBox.BuildFarm
{
public abstract class TaskFlow
{
private readonly IExecutionContext _context;
private readonly Ticket _ticket;
protected readonly List<ITask> Tasks;
protected TaskFlow(IExecutionContext context, Ticket ticket)
{
_context = context;
_ticket = ticket;
Tasks = new List<ITask>();
}
public void Execute()
{
using (var steps = _context.ExecuteSteps())
{
foreach (var task in Tasks)
{
steps.AddStep(task.Name, stepContext =>
{
// Create a custom context to log out of teabox
var customContext = new CustomContext(stepContext);
task.Execute(customContext, _ticket);
ProcessContextOutput(customContext);
});
}
}
}
/// <summary>
/// Executes all the tasks listed in a taskflow defined by the taskflow type of the ticket
/// </summary>
/// <param name="context"></param>
/// <param name="ticket"></param>
public static void ExecuteFlow(IExecutionContext context, Ticket ticket)
{
var taskType = GetTaskFlowType(ticket);
if (taskType != null)
{
var taskFlow = Activator.CreateInstance(taskType, context, ticket) as TaskFlow; //Instantiates the type (calls new)
taskFlow?.Execute();
}
}
/// <summary>
/// Return a list of tasks for a given flow
/// </summary>
/// <returns>A range of tasks</returns>
public IEnumerable<ITask> GetFlowTasks()
{
return Tasks;
}
public static TypeInfo GetTaskFlowType(Ticket ticket)
{
var taskTypes = typeof(TaskFlow).Assembly.DefinedTypes.Where(t =>
typeof(TaskFlow).IsAssignableFrom(t)); //Gets all types inheriting from ITaskFlow
var currentTaskType = taskTypes.FirstOrDefault(x => x.Name == ticket.TaskFlowType.ToString()); //Gets first type named "CodePreflightFlow"
return currentTaskType;
}
}
}