/*
 * 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.Realm mainRealm() {
    return new Bach.Realm(
        "main",
        11,
        false,
        List.of(
            new Bach.Unit(
                ModuleDescriptor.newModule("de.sormuras.bach").build(),
                List.of(new Bach.Folder(Path.of("src/de.sormuras.bach/main/java"), 0)) //
                ) //
            ) //
        );
  }

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

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

@@ -1349,21 +1349,32 @@
 ic Bach.
-Realm
+ModuleCollection
  mainRea
@@ -1392,37 +1392,48 @@
 return new Bach.
-Realm
+ModuleCollection
 (%0A        %22main%22
@@ -1491,36 +1491,49 @@
        new Bach.
-Unit
+ModuleDescription
 (%0A              
@@ -1615,38 +1615,41 @@
 ist.of(new Bach.
-Folder
+Directory
 (Path.of(%22src/de
@@ -1683,28 +1683,8 @@
  0))
- //%0A                
 ) //
@@ -1735,29 +1735,40 @@
 static Bach.
-Realm
+ModuleCollection
  testRealm()
@@ -1790,21 +1790,32 @@
 ew Bach.
-Realm
+ModuleCollection
 (%0A      
@@ -1896,36 +1896,49 @@
        new Bach.
-Unit
+ModuleDescription
 (%0A              
@@ -2045,38 +2045,41 @@
        new Bach.
-Folder
+Directory
 (Path.of(%22src/de
@@ -2131,38 +2131,41 @@
        new Bach.
-Folder
+Directory
 (Path.of(%22src/de
@@ -2233,36 +2233,49 @@
        new Bach.
-Unit
+ModuleDescription
 (%0A              
@@ -2354,38 +2354,41 @@
 ist.of(new Bach.
-Folder
+Directory
 (Path.of(%22src/te
@@ -2450,20 +2450,33 @@
 ew Bach.
-Unit
+ModuleDescription
 (%0A      
@@ -2566,38 +2566,41 @@
 ist.of(new Bach.
-Folder
+Directory
 (Path.of(%22src/te
@@ -2686,21 +2686,32 @@
 ic Bach.
-Realm
+ModuleCollection
  testPre
@@ -2860,41 +2860,38 @@
 var 
-folders = List.of(new Bach.Folder
+directory = new Bach.Directory
 (Pat
@@ -2942,17 +2942,16 @@
 release)
-)
 ;%0A    va
@@ -2972,32 +2972,56 @@
 ach.
-Unit(descriptor, folders
+ModuleDescription(descriptor, List.of(directory)
 );%0A 
@@ -3039,21 +3039,32 @@
 ew Bach.
-Realm
+ModuleCollection
 (%22test-p

Update build program