/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 Christian Stein
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.sormuras.bach;

import java.util.List;
import java.util.StringJoiner;

/** A piece of work to be done or undertaken. */
public /*static*/ class Task {

  public static Task sequence(String label, Task... tasks) {
    return sequence(label, List.of(tasks));
  }

  public static Task sequence(String label, List<Task> tasks) {
    return new Task(label, tasks);
  }

  private final String label;
  private final List<Task> list;

  public Task() {
    this("", List.of());
  }

  public Task(String label, List<Task> list) {
    this.label = label.isBlank() ? getClass().getSimpleName() : label;
    this.list = list;
  }

  public String getLabel() {
    return label;
  }

  public List<Task> getList() {
    return list;
  }

  @Override
  public String toString() {
    return new StringJoiner(", ", Task.class.getSimpleName() + "[", "]")
        .add("label='" + label + "'")
        .add("list.size=" + list.size())
        .toString();
  }

  public void execute(Bach bach) throws Exception {}
}

@@ -654,16 +654,111 @@
 .bach;%0A%0A
+import java.io.PrintWriter;%0Aimport java.io.StringWriter;%0Aimport java.lang.System.Logger.Level;%0A
 import j
@@ -802,16 +802,51 @@
 gJoiner;
+%0Aimport java.util.spi.ToolProvider;
 %0A%0A/** A 
@@ -1134,16 +1134,303 @@
 );%0A  %7D%0A%0A
+  public static Task runTool(String name, Object... arguments) %7B%0A    var tool = ToolProvider.findFirst(name).orElseThrow();%0A    var args = new String%5Barguments.length%5D;%0A    for (int i = 0; i %3C args.length; i++) args%5Bi%5D = arguments%5Bi%5D.toString();%0A    return new RunTool(tool, args);%0A  %7D%0A%0A
   privat
@@ -2055,11 +2055,760 @@
 ption %7B%7D
+%0A%0A  static class RunTool extends Task %7B%0A%0A    private final ToolProvider tool;%0A    private final String%5B%5D args;%0A%0A    public RunTool(ToolProvider tool, String... args) %7B%0A      super(tool.name() + %22 %22 + String.join(%22 %22, args), List.of());%0A      this.tool = tool;%0A      this.args = args;%0A    %7D%0A%0A    @Override%0A    public void execute(Bach bach) %7B%0A      var out = new StringWriter();%0A      var err = new StringWriter();%0A      bach.execute(tool, new PrintWriter(out), new PrintWriter(err), args);%0A      var outString = out.toString().strip();%0A      if (!outString.isEmpty()) bach.getLogger().log(Level.DEBUG, outString);%0A      var errString = err.toString().strip();%0A      if (!errString.isEmpty()) bach.getLogger().log(Level.WARNING, outString);%0A    %7D%0A  %7D
 %0A%7D%0A

Add tool run task