/*
 * 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.
 */

import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Version;
import java.nio.file.Path;
import java.util.List;

/** Bach's own build program. */
class Build {

  public static void main(String... args) {
    var project = project("Bach.java", "11.0-ea");
    var bach = new Bach();
    bach.print("Build %s using %s", project.toNameAndVersion(), bach);
    bach.print("%s", project);
    // bach.build(project).assertSuccessful();
  }

  private static Bach.Project project(String name, String version) {
    return new Bach.Project(
        name,
        Version.parse(version),
        new Bach.Structure(List.of(mainRealm(), testRealm(), testPreviewRealm())));
  }

  private static Bach.ModuleCollection mainRealm() {
    return new Bach.ModuleCollection(
        "main",
        11,
        false,
        List.of(
            new Bach.ModuleDescription(
                ModuleDescriptor.newModule("de.sormuras.bach").build(),
                List.of(new Bach.Directory(Path.of("src/de.sormuras.bach/main/java"), 0))) //
            ) //
        );
  }

  private static Bach.ModuleCollection testRealm() {
    return new Bach.ModuleCollection(
        "test",
        11,
        false,
        List.of(
            //
            new Bach.ModuleDescription(
                ModuleDescriptor.newOpenModule("de.sormuras.bach").build(),
                List.of(
                    new Bach.Directory(Path.of("src/de.sormuras.bach/test/java"), 0),
                    new Bach.Directory(Path.of("src/de.sormuras.bach/test/java-module"), 0))),
            //
            new Bach.ModuleDescription(
                ModuleDescriptor.newOpenModule("test.base").build(),
                List.of(new Bach.Directory(Path.of("src/test.base/test/java"), 0))),
            //
            new Bach.ModuleDescription(
                ModuleDescriptor.newOpenModule("test.modules").build(),
                List.of(new Bach.Directory(Path.of("src/test.modules/test/java"), 0)))
            //
            ));
  }

  private static Bach.ModuleCollection testPreviewRealm() {
    var descriptor = ModuleDescriptor.newOpenModule("test.preview").build();
    var release = Runtime.version().feature();
    var directory = new Bach.Directory(Path.of("src/test.preview/test-preview/java"), release);
    var unit = new Bach.ModuleDescription(descriptor, List.of(directory));
    return new Bach.ModuleCollection("test-preview", release, true, List.of(unit));
  }
}

@@ -1602,36 +1602,24 @@
             
-List.of(new 
 Bach.Directo
@@ -1612,32 +1612,39 @@
   Bach.Directory
+.listOf
 (Path.of(%22src/de
@@ -1666,50 +1666,13 @@
 main
-/java%22), 0))) //%0A            ) //%0A        
+%22))))
 );%0A 
@@ -1977,127 +1977,8 @@
     
-List.of(%0A                    new Bach.Directory(Path.of(%22src/de.sormuras.bach/test/java%22), 0),%0A                    new 
 Bach
@@ -1979,32 +1979,39 @@
   Bach.Directory
+.listOf
 (Path.of(%22src/de
@@ -2033,25 +2033,9 @@
 test
-/java-module%22), 0
+%22
 ))),
@@ -2171,36 +2171,24 @@
             
-List.of(new 
 Bach.Directo
@@ -2181,32 +2181,39 @@
   Bach.Directory
+.listOf
 (Path.of(%22src/te
@@ -2224,26 +2224,17 @@
 ase/test
-/java%22), 0
+%22
 ))),%0A   
@@ -2377,20 +2377,8 @@
     
-List.of(new 
 Bach
@@ -2379,32 +2379,39 @@
   Bach.Directory
+.listOf
 (Path.of(%22src/te
@@ -2429,49 +2429,12 @@
 test
-/java%22), 0)))%0A            //%0A            
+%22)))
 ));%0A
@@ -2506,70 +2506,64 @@
     
-var descriptor = ModuleDescriptor.newOpenModule(
+return new Bach.ModuleCollection(%0A        
 %22test
-.
+-
 preview%22
 ).bu
@@ -2562,36 +2562,17 @@
 iew%22
-).build();%0A    var release =
+,%0A       
  Run
@@ -2599,261 +2599,238 @@
 re()
-;
+,
 %0A    
-var directory = new Bach.Directory(Path.of(%22src/test.preview/test-preview/java%22), release);%0A    var unit = new Bach.ModuleDescription(descriptor, List.of(directory));%0A    return new Bach.ModuleCollection(%22
+    true,%0A        List.of(%0A            new Bach.ModuleDescription(%0A                ModuleDescriptor.newOpenModule(%22test.preview%22).build(),%0A                Bach.Directory.listOf(Path.of(%22src/
 test
--
+.
 preview
-%22, release, true, List.of(unit
+/test-preview/java%22)))
 ));%0A

Update build program