From 17c1c9095730311d2315abe928a50761f0d959a6 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 13 Apr 2022 02:54:03 +0200 Subject: [PATCH] Added Module-Info support --- .../builder/PrimitiveCollectionsBuilder.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java b/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java index c2454921..158061a3 100644 --- a/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java +++ b/src/builder/java/speiger/src/builder/PrimitiveCollectionsBuilder.java @@ -1,6 +1,8 @@ package speiger.src.builder; +import java.io.BufferedWriter; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -10,7 +12,9 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringJoiner; import java.util.function.Consumer; +import java.util.stream.Stream; import speiger.src.builder.processor.TemplateProcess; import speiger.src.builder.processor.TemplateProcessor; @@ -65,6 +69,23 @@ public class PrimitiveCollectionsBuilder extends TemplateProcessor return false; } + @Override + protected void afterFinish() + { + if(getVersion() > 8) + { + Path basePath = Paths.get("src/main/java"); + try(BufferedWriter writer = Files.newBufferedWriter(basePath.resolve("module-info.java"))) + { + writer.write(getModuleInfo(basePath)); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + } + @Override protected void init() { @@ -162,6 +183,50 @@ public class PrimitiveCollectionsBuilder extends TemplateProcessor return variables; } + private String getModuleInfo(Path basePath) { + StringJoiner joiner = new StringJoiner("\n", "", "\n"); + try(Stream stream = Files.walk(getOutputFolder())) + { + stream.filter(Files::isDirectory) + .filter(this::containsFiles) + .map(basePath::relativize) + .map(Path::toString) + .map(T -> T.replace("\\", ".")) + .forEach(T -> joiner.add("\texports "+T+";")); + } + catch(Exception e) + { + e.printStackTrace(); + throw new RuntimeException(e); + } + StringBuilder builder = new StringBuilder(); + builder.append("module ").append(basePath.relativize(getOutputFolder()).toString().replace("\\", ".")).append(" {\n"); + builder.append(joiner.toString()).append("}"); + return builder.toString(); + } + + private boolean containsFiles(Path path) { + try(Stream stream = Files.walk(path, 1)) + { + return stream.filter(Files::isRegularFile).findFirst().isPresent(); + } + catch(Exception e) + { + e.printStackTrace(); + } + return false; + } + + private int getVersion() { + String version = System.getProperty("java.version"); + if(version.startsWith("1.")) { + version = version.substring(2, 3); + } else { + int dot = version.indexOf("."); + if(dot != -1) { version = version.substring(0, dot); } + } return Integer.parseInt(version); + } + public static void main(String...args) { try