execute function

Future<String> execute({
  1. required ScriptName script,
  2. required String pathToRoot,
  3. required List<String> arguments,
})

Main entrypoint for executing Klutter command line tasks.

Implementation

Future<String> execute({
  required ScriptName script,
  required String pathToRoot,
  required List<String> arguments,
}) async {
  /// Parse user input to a Command.
  final command = Command.from(
    task: arguments.join(),
    script: script,
  );

  /// Retrieve all tasks for the specified command.
  ///
  /// Set is empty if command is null or task processing failed.
  final tasks = command == null ? <Task>{} : service.tasksOrEmptyList(command);

  /// When there are no tasks then the user input is incorrect.
  ///
  /// Stop processing and print list of available tasks.
  if (tasks.isEmpty) {
    return """
    |KLUTTER: Received invalid command.
    |
    |${service.printTasksAsCommands}
    """
        .format
        .nok;
  }

  /// Process all the given tasks.
  else {
    final s = command!.scriptName.name;
    final t = command.taskName.name;
    final o = command.option;

    for (final task in tasks) {
      final result = task.execute(pathToRoot);

      /// Stop executing tasks when one has failed.
      if (!result.isOk) {
        return """
          |KLUTTER: ${result.message}
          |KLUTTER: Task '$s $t $o' finished unsuccessfully."""
            .format
            .nok;
      }
    }

    return "KLUTTER: Task '$s $t $o' finished successful.".format.ok;
  }
}