// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
namespace Flax.Build.Graph
{
///
/// The base class for build system Task Graph.
///
[Serializable]
public class Task
{
///
/// The collection of the files required by this task to run.
///
public List PrerequisiteFiles = new List();
///
/// The collection of the files produced by this task.
///
public List ProducedFiles = new List();
///
/// The working directory for any process invoked by this task.
///
public string WorkingDirectory = string.Empty;
///
/// The command to call upon task execution.
///
public Action Command;
///
/// The command to run to create produced files.
///
public string CommandPath = string.Empty;
///
/// Command-line parameters to pass via command line to the invoked process.
///
public string CommandArguments = string.Empty;
///
/// The message to print on task execution start.
///
public string InfoMessage;
///
/// The estimated action cost. Unitless but might be estimation of milliseconds required to perform this task. It's just an raw estimation (based on input files count or size).
///
public int Cost = 0;
///
/// The start time of the task execution.
///
public DateTime StartTime = DateTime.MinValue;
///
/// The end time of the task execution.
///
public DateTime EndTime = DateTime.MinValue;
///
/// Gets the task execution duration.
///
public TimeSpan Duration => EndTime != DateTime.MinValue ? EndTime - StartTime : DateTime.Now - StartTime;
///
/// The dependent tasks cached for this task (limited depth). Might be null.
///
public HashSet DependentTasks;
///
/// Gets the dependent tasks count.
///
public int DependentTasksCount => DependentTasks?.Count ?? 0;
///
/// Gets a value indicating whether task results from the previous execution are still valid. Can be used to skip task execution.
///
public bool HasValidCachedResults;
///
/// Disables caching this task results.
///
public bool DisableCache;
///
/// The task execution result.
///
public int Result;
///
/// Gets a value indicating whether task execution has failed.
///
public bool Failed => Result != 0;
///
/// Compares two tasks to sort them for the parallel execution.
///
/// The first task to compare.
/// The second task to compare.
public static int Compare(Task a, Task b)
{
// Sort by total number of dependent files
var dependentTasksCountDiff = b.DependentTasksCount - a.DependentTasksCount;
if (dependentTasksCountDiff != 0)
{
return Math.Sign(dependentTasksCountDiff);
}
// Sort by the cost
var costDiff = b.Cost - a.Cost;
if (costDiff != 0)
{
return Math.Sign(costDiff);
}
// Sort by amount of input files
return Math.Sign(b.PrerequisiteFiles.Count - a.PrerequisiteFiles.Count);
}
///
public override string ToString()
{
return InfoMessage ?? base.ToString();
}
}
}