/*
 * 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 de.sormuras.bach.api.Project;

import java.lang.System.Logger.Level;
import java.lang.module.ModuleDescriptor.Version;
import java.util.Objects;
import java.util.function.Consumer;

/** Bach - Java Shell Builder. */
public class Bach {

  /** Version of the Java Shell Builder. */
  public static final Version VERSION = Version.parse("11.0-ea");

  /** Main entry-point. */
  public static void main(String... args) {
    Main.main(args);
  }

  /** Line-based message printing consumer. */
  private final Consumer<String> printer;

  /** Verbosity flag. */
  private final boolean verbose;

  /** Dry-run flag. */
  private final boolean dryRun;

  /** Initialize this instance with default values. */
  public Bach() {
    this(
        System.out::println,
        Boolean.getBoolean("ebug") || "".equals(System.getProperty("ebug")),
        Boolean.getBoolean("ry-run") || "".equals(System.getProperty("ry-run")));
  }

  /** Initialize this instance with the specified line printer and verbosity flag. */
  public Bach(Consumer<String> printer, boolean verbose, boolean dryRun) {
    this.printer = Objects.requireNonNull(printer, "printer");
    this.verbose = verbose;
    this.dryRun = dryRun;
    print(Level.DEBUG, "%s initialized", this);
    print(Level.TRACE, "\tverbose=%s", verbose);
    print(Level.TRACE, "\tdry-run=%s", dryRun);
  }

  /** Print a message at information level. */
  public String print(String format, Object... args) {
    return print(Level.INFO, format, args);
  }

  /** Print a message at specified level. */
  public String print(Level level, String format, Object... args) {
    var message = String.format(format, args);
    if (verbose || level.getSeverity() >= Level.INFO.getSeverity()) printer.accept(message);
    return message;
  }

  /** Build the given project using default settings. */
  public void build(Project project) {
    if (verbose) project.toStrings().forEach(this::print);
    if (project.structure().collections().isEmpty()) print("No module collection present");
    if (dryRun) return;
    print(Level.DEBUG, "TODO build(%s)", project.toNameAndVersion());
  }

  @Override
  public String toString() {
    return "Bach.java " + VERSION;
  }
}

@@ -2790,16 +2790,602 @@
 );%0A  %7D%0A%0A
+  /** Execute the given task recursively. */%0A  public void execute(Task task) %7B%0A    var executor = new Task.Executor(this);%0A    print(Level.TRACE, %22Execute task: %22 + task.name());%0A    var summary = executor.execute(task).assertSuccessful();%0A    if (verbose) %7B%0A      print(%22Task Execution Overview%22);%0A      print(%22%7C    %7CThread%7CDuration%7C Task%22);%0A      summary.getOverviewLines().forEach(this::print);%0A      var count = summary.getTaskCount();%0A      var duration = summary.getDuration().toMillis();%0A      print(Level.DEBUG, %22Execution of %25d tasks took %25d ms%22, count, duration);%0A    %7D%0A  %7D%0A%0A
   @Overr

Extend bach with task execution