package com.github.sormuras.bach.workflow;

import com.github.sormuras.bach.Bach;
import com.github.sormuras.bach.api.CodeSpace;
import com.github.sormuras.bach.internal.Paths;
import com.github.sormuras.bach.tool.JLinkCall;
import java.nio.file.Path;

public class GenerateImageWorkflow extends BachWorkflow {

  public GenerateImageWorkflow(Bach bach) {
    super(bach);
  }

  public void generate() {
    generateCustomRuntimeImage();
  }

  public void generateCustomRuntimeImage() {
    var project = bach().project();
    if (!project.tools().enabled("jlink")) return;
    if (project.spaces().main().modules().isEmpty()) {
      bach().log("Main module list is empty, nothing to do here.");
      return;
    }

    bach().say("Generate custom runtime image");

    var image = project.folders().workspace("image");
    Paths.deleteDirectories(image);
    bach().run(jlink(image));
  }

  protected JLinkCall jlink(Path image) {
    var project = bach().project();
    var main = project.spaces().main();
    var test = project.spaces().test();
    var tweaks = project.tools().tweaks();
    return new JLinkCall()
        .with("--add-modules", main.modules().toNames(","))
        .ifPresent(test.paths().pruned(), (jlink, paths) -> jlink.with("--module-path", paths))
        .withAll(tweaks.arguments(CodeSpace.MAIN, "jlink"))
        .with("--output", image);
  }
}

@@ -244,16 +244,39 @@
 le.Path;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -997,20 +997,23 @@
     var 
-main
+folders
  = proje
@@ -1015,30 +1015,109 @@
 project.
-spaces().main(
+folders();%0A    var paths = List.of(folders.modules(CodeSpace.MAIN), folders.externalModules()
 );%0A    v
@@ -1119,20 +1119,20 @@
     var 
-test
+main
  = proje
@@ -1143,20 +1143,20 @@
 paces().
-test
+main
 ();%0A    
@@ -1221,16 +1221,92 @@
 kCall()%0A
+        .ifTrue(bach().options().verbose(), args -%3E args.with(%22--verbose%22))%0A
         
@@ -1369,65 +1369,8 @@
     
-.ifPresent(test.paths().pruned(), (jlink, paths) -%3E jlink
 .wit
@@ -1394,17 +1394,16 @@
 , paths)
-)
 %0A       

Fix module-path computation