tasksOrEmptyList function

Set<Task> tasksOrEmptyList(
  1. Command command, [
  2. Set<Task>? tasks
])

Get the Task and all tasks it relies on or return empty list if command is invalid.

Implementation

Set<Task> tasksOrEmptyList(Command command,

    /// Injectable Task List for testing purposes.
    /// Any calling class should omit this parameter
    /// and let the function default to [allTasks].
    [Set<Task>? tasks]) {
  /// Get all Tasks for this ScriptName.
  final tasksForScript = (tasks ?? allTasks())
      .where((task) => task.scriptName == command.scriptName)
      .toList();

  /// Find TaskName or return emptyList if not present.
  ///
  /// If TaskName is not found then the command was invalid.
  final hasTask =
      tasksForScript.map((e) => e.taskName).contains(command.taskName);

  if (!hasTask) {
    return <Task>{};
  }

  /// Retrieve the Task which at this point always exists.
  final primaryTask = tasksForScript.firstWhere((task) {
    return task.taskName == command.taskName;
  })
    ..option = command.option;

  final taskList = [primaryTask];

  /// Find al dependencies and add them to the taskList.
  for (final task in primaryTask.dependsOn()) {
    if (!taskList.map((e) => e.taskName).contains(task.taskName)) {
      taskList.add(task);
    }
  }

  /// Sort the taskList to make sure the Tasks are executed in
  /// the correct order e.g. tasks without dependencies before
  /// those with dependencies.
  taskList.sort(compareByDependsOn);

  return taskList.toSet();
}