Skip to content

Commit f2ac0dc

Browse files
committed
Enhance module scanning and wildcard expansion.
Introduced `scanForModules` to dynamically retrieve module names from the file system. Updated export and open handling logic to support wildcard expansion using the scanned modules for improved flexibility.
1 parent 3e85e5b commit f2ac0dc

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/main/java/com/chaoticsomeone/ModInfGen/ModuleInfoGenerator.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.Gson;
66

77
import java.io.*;
8+
import java.util.List;
89
import java.util.regex.Matcher;
910
import java.util.regex.Pattern;
1011

@@ -15,6 +16,7 @@ public class ModuleInfoGenerator {
1516

1617
private final Gson gson = new Gson();
1718
private final String configFilePath = "module-info.json5";
19+
private ModuleInfo moduleInfo;
1820

1921
private boolean collapseWhitespaces;
2022

@@ -27,12 +29,13 @@ public ModuleInfoGenerator() {
2729
}
2830

2931
public void generate() {
30-
ModuleInfo moduleInfo = gson.fromJson(readJsonString(configFilePath), ModuleInfo.class);
32+
moduleInfo = gson.fromJson(readJsonString(configFilePath), ModuleInfo.class);
3133

3234
try {
3335
moduleInfo.validateVariables();
3436
moduleInfo.expandVariables();
3537
moduleInfo.expandRoot();
38+
moduleInfo.scanForModules();
3639

3740
System.out.println(generateModuleInfoContent(moduleInfo));
3841
} catch (IllegalArgumentException e) {
@@ -91,6 +94,23 @@ public void generateTemplate() {
9194
}
9295
}
9396

97+
private void expandWildcard(SmartStringBuilder builder, String baseline, String s) {
98+
String postfix = s.substring(1);
99+
List<String> modules = moduleInfo.getModules().stream().filter(m -> m.endsWith(postfix)).toList();
100+
101+
for (String module : modules) {
102+
builder.appendFmtLn(baseline.replaceFirst("%s", module));
103+
}
104+
}
105+
106+
private void addLine(SmartStringBuilder builder, String line, String value) {
107+
if (value.startsWith("*")) {
108+
expandWildcard(builder, line, value);
109+
} else {
110+
builder.appendFmtLn(line, value);
111+
}
112+
}
113+
94114
private String generateModuleInfoContent(ModuleInfo moduleInfo) {
95115
SmartStringBuilder outputBuilder = new SmartStringBuilder();
96116

@@ -109,17 +129,17 @@ private String generateModuleInfoContent(ModuleInfo moduleInfo) {
109129
outputBuilder.appendSectionLn(() -> {
110130
outputBuilder.appendComment(moduleInfo.getComment("exports"));
111131
for (String export : moduleInfo.getExports()) {
112-
outputBuilder.appendFmtLn("exports %s;", export);
132+
addLine(outputBuilder, "exports %s;", export);
113133
}
114134
});
115135

116136
outputBuilder.appendSectionLn(() -> {
117137
outputBuilder.appendComment(moduleInfo.getComment("opens"));
118138
for (OpensDeclaration opens : moduleInfo.getOpens()) {
119-
String baseLine = String.format("opens %s to %s;", "%s", opens.getTargetModule());
139+
String baseline = String.format("opens %%s to %s;", opens.getTargetModule());
120140

121141
for (String sourceModule : opens.getSourceModules()) {
122-
outputBuilder.appendFmtLn(baseLine, sourceModule);
142+
addLine(outputBuilder, baseline, sourceModule);
123143
}
124144
}
125145
});

src/main/java/com/chaoticsomeone/ModInfGen/model/ModuleInfo.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.gson.annotations.Expose;
44
import com.google.gson.annotations.SerializedName;
55

6+
import java.io.File;
7+
import java.util.ArrayList;
68
import java.util.List;
79
import java.util.Map;
810
import java.util.function.Function;
@@ -37,6 +39,8 @@ public class ModuleInfo {
3739
@Expose
3840
private Map<String, String> comments;
3941

42+
private final List<String> modules = new ArrayList<>();
43+
4044
private final Function<String, String> expandMapper = (s) -> {
4145
if (s.equals(".")) {
4246
return getModuleName();
@@ -47,6 +51,29 @@ public class ModuleInfo {
4751
}
4852
};
4953

54+
public void scanForModules() {
55+
if (getSourceRoot() == null || getSourceRoot().isBlank()) {
56+
return;
57+
}
58+
59+
File root = new File(getSourceRoot(), moduleName.replace(".", "/"));
60+
scanModule(root);
61+
}
62+
63+
private void scanModule(File directory) {
64+
File root = new File(getSourceRoot());
65+
File[] files = directory.listFiles();
66+
modules.add(directory.getPath().replace(root.getPath() + "\\", "").replace("\\", ".").replace("/", "."));
67+
68+
if (files != null) {
69+
for (File file : files) {
70+
if (file.isDirectory()) {
71+
scanModule(file);
72+
}
73+
}
74+
}
75+
}
76+
5077
public void expandRoot() {
5178
this.exports = exports.stream().map(expandMapper).toList();
5279

@@ -160,4 +187,8 @@ public void setComments(Map<String, String> comments) {
160187
public String getComment(String key) {
161188
return comments.get(key);
162189
}
190+
191+
public List<String> getModules() {
192+
return modules;
193+
}
163194
}

0 commit comments

Comments
 (0)