package de.sormuras.bach.task;

import de.sormuras.bach.Bach;
import de.sormuras.bach.Log;
import de.sormuras.bach.Task;
import de.sormuras.bach.util.Paths;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.Collectors;

public class SummaryTask implements Task {

  @Override
  public void execute(Bach bach) throws Exception {
    var log = bach.getLog();
    var project = bach.getProject();
    var realms = project.structure().realms();
    if (realms.isEmpty()) {
      log.warning("No realm configured in project: %s", project);
    }
    for (var realm : realms) {
      log.info("Modules of %s realm", realm.name());
      var modules = project.folder().modules(realm.name());
      if (Files.notExists(modules)) {
        if (!project.units(realm).isEmpty()) {
          log.warning("Modules folder not found: %s", modules);
        }
        continue;
      }
      var jars = Paths.list(modules, "*.jar");
      log.info("%d jar(s) found in: %s", jars.size(), modules.toUri());
      for (var jar : jars) {
        log.info("%,11d %s", Files.size(jar), jar.getFileName());
      }
    }

    var duration = Duration.between(log.getInstant(), Instant.now());
    log.info("Build %d took millis.", duration.toMillis());

    var entries = log.getEntries();
    var timestamp = log.getInstant().toString().replace(":", "-");
    var summary = project.folder().log("summary-" + timestamp + ".log");
    Files.createDirectories(project.folder().log());
    Files.write(summary, entries.stream().map(this::toString).collect(Collectors.toList()));
    Files.copy(summary, project.folder().out("summary.log"), StandardCopyOption.REPLACE_EXISTING);
  }

  private String toString(Log.Entry entry) {
    return String.format("%s|%s|%s", entry.instant(), entry.level(), entry.message());
  }
}

@@ -114,16 +114,56 @@
 h.Task;%0A
+import de.sormuras.bach.project.Folder;%0A
 import d
@@ -312,16 +312,44 @@
 nstant;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1387,16 +1387,325 @@
 is());%0A%0A
+    writeSummaryLog(log, project.folder());%0A    writeSummaryMarkdown(log, project.folder());%0A  %7D%0A%0A  private String toString(Log.Entry entry) %7B%0A    return String.format(%22%25s%7C%25s%7C%25s%22, entry.instant(), entry.level(), entry.message());%0A  %7D%0A%0A  private void writeSummaryLog(Log log, Folder folder) throws Exception %7B%0A
     var 
@@ -1817,32 +1817,22 @@
 mmary = 
-project.
 folder
-()
 .log(%22su
@@ -1890,32 +1890,22 @@
 ctories(
-project.
 folder
-()
 .log());
@@ -2022,32 +2022,22 @@
 ummary, 
-project.
 folder
-()
 .out(%22su
@@ -2106,127 +2106,662 @@
 ate 
-String toString(Log.Entry entry) %7B%0A    return String.format(%22%25s%7C%25s%7C%25s%22, entry.instant(), entry.level(), entry.message()
+void writeSummaryMarkdown(Log log, Folder folder) throws Exception %7B%0A    var lines = new ArrayList%3CString%3E();%0A    lines.add(%22# Summary%22);%0A    lines.add(%22%22);%0A    lines.add(%22## Messages logged at INFO and more severe levels%22);%0A    lines.add(%22%22);%0A    for (var entry : log.getEntries()) %7B%0A      if (entry.level().getSeverity() %3E= System.Logger.Level.INFO.getSeverity()) %7B%0A        lines.add(%22- %22 + entry.message());%0A      %7D%0A    %7D%0A    lines.add(%22%22);%0A    lines.add(%22## All log entries%22);%0A    lines.add(%22%22);%0A    for (var entry : log.getEntries()) %7B%0A      lines.add(%22- %22 + entry.message());%0A    %7D%0A    lines.add(%22%22);%0A    Files.write(folder.out(%22summary.md%22), lines
 );%0A 

Create summary.md file