package build;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.spi.ToolProvider;

class Bootstrap {
  public static void main(String... args) throws Exception {
    var module = "com.github.sormuras.bach";
    var version = "16-ea+BOOTSTRAP";

    var classes = Path.of(".bach/workspace/.bootstrap");
    run(
        "javac",
        "--module=" + module,
        "--module-version=" + version + "-" + Instant.now(),
        "--module-source-path=./*/main/java",
        "-g",
        "-parameters",
        "-Werror",
        "-Xlint",
        "-encoding",
        "UTF-8",
        "-d",
        classes.toString());

    var file = module + "@" + version + ".jar";
    var jar = Files.createDirectories(Path.of(".bach/cache")).resolve(file);
    run(
        "jar",
        "--create",
        "--file=" + jar,
        "--main-class=" + module + ".Main",
        "-C",
        classes.resolve(module).toString(),
        ".");
  }

  static void run(String name, String... args) {
    System.out.println(">> " + name + " " + String.join(" ", args));
    var tool = ToolProvider.findFirst(name).orElseThrow();
    var code = tool.run(System.out, System.err, args);
    if (code != 0) throw new Error("Non-zero exit code: " + code);
  }
}

@@ -90,16 +90,45 @@
 nstant;%0A
+import java.util.Comparator;%0A
 import j
@@ -317,16 +317,41 @@
 TRAP%22;%0A%0A
+    deleteWorkspaces();%0A%0A
     var 
@@ -1033,16 +1033,445 @@
 );%0A  %7D%0A%0A
+  static void deleteWorkspaces() throws Exception %7B%0A    try (var stream = Files.walk(Path.of(%22%22))) %7B%0A      var selected = stream%0A          .filter(path -%3E path.toUri().toString().contains(%22.bach/workspace%22))%0A          .sorted(Comparator.reverseOrder())%0A          .toArray(Path%5B%5D::new);%0A      for (var path : selected) Files.delete(path);%0A      System.out.println(%22%3E%3E %22 + selected.length + %22 workspace files deleted%22);%0A    %7D%0A  %7D%0A%0A
   static

Delete workspace-related files on bootstrap