package bach.info;

import com.github.sormuras.bach.Bach;
import com.github.sormuras.bach.Main;
import com.github.sormuras.bach.Options;
import com.github.sormuras.bach.ProjectInfo;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class CustomBach extends Bach implements MainSpaceBuilder {

  public static void main(String... args) {
    var options = Options.of(args.length == 0 ? new String[] {"build"} : args);
    var bach = provider().newBach(options);
    System.exit(new Main().run(bach));
  }

  public static Provider<CustomBach> provider() {
    return CustomBach::new;
  }

  private CustomBach(Options options) {
    super(options);
  }

  @Override
  public String computeProjectVersion(ProjectInfo info) {
    var now = LocalDateTime.now(ZoneOffset.UTC);
    var timestamp = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(now);
    try {
      return Files.readString(Path.of("VERSION")) + "-custom+" + timestamp;
    } catch (Exception exception) {
      throw new RuntimeException("Read version failed: " + exception);
    }
  }
}

@@ -175,16 +175,54 @@
 ctInfo;%0A
+import java.lang.System.Logger.Level;%0A
 import j
@@ -867,24 +867,144 @@
 nfo info) %7B%0A
+    var value = bach().options().find(Options.Property.PROJECT_VERSION);%0A    if (value.isPresent()) return value.get();%0A
     var now 
@@ -1251,35 +1251,37 @@
     
-throw new RuntimeException(
+logbook().log(Level.WARNING, 
 %22Rea
@@ -1298,19 +1298,20 @@
 failed: 
-%22 +
+%25s%22,
  excepti
@@ -1315,16 +1315,45 @@
 ption);%0A
+      return info.version();%0A
     %7D%0A  

Fix custom version computation