Skip to content

Commit 50d3716

Browse files
committed
Refactor generate method and add dry-run support
Modified the generate method to return a String instead of printing directly, enabling better output handling. Introduced a dry-run option in the main method to preview generated output without writing to a file.
1 parent c5c7831 commit 50d3716

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ModuleInfoGenerator() {
2828
this(false);
2929
}
3030

31-
public void generate() {
31+
public String generate() {
3232
moduleInfo = gson.fromJson(readJsonString(configFilePath), ModuleInfo.class);
3333

3434
try {
@@ -37,10 +37,12 @@ public void generate() {
3737
moduleInfo.expandRoot();
3838
moduleInfo.scanForModules();
3939

40-
System.out.println(generateModuleInfoContent(moduleInfo));
40+
return generateModuleInfoContent(moduleInfo);
4141
} catch (IllegalArgumentException e) {
4242
e.printStackTrace();
4343
}
44+
45+
return null;
4446
}
4547

4648
public void generateTemplate() {
@@ -197,21 +199,37 @@ private void setCollapseWhitespaces(boolean collapseWhitespaces) {
197199
this.collapseWhitespaces = collapseWhitespaces;
198200
}
199201

202+
private File getOutputFile() {
203+
return new File(moduleInfo.getSourceRoot(), "module-info.java");
204+
}
205+
200206
public static void main(String[] args) {
201207
ModuleInfoGenerator generator = new ModuleInfoGenerator(false);
208+
202209
boolean doGenerate = true;
210+
boolean isDryRun = false;
203211

204212
for (String arg : args) {
205213
if (arg.equalsIgnoreCase("-w")) {
206214
generator.setCollapseWhitespaces(true);
207215
} else if (arg.equalsIgnoreCase("-n")) {
208216
generator.generateTemplate();
209217
doGenerate = false;
218+
} else if (arg.equalsIgnoreCase("--dry-run")) {
219+
isDryRun = true;
210220
}
211221
}
212222

213223
if (doGenerate) {
214-
generator.generate();
224+
String output = generator.generate();
225+
226+
if (!isDryRun) {
227+
try (BufferedWriter bw = new BufferedWriter(new FileWriter(generator.getOutputFile()))) {
228+
bw.write(output);
229+
} catch (IOException e) {
230+
e.printStackTrace();
231+
}
232+
}
215233
}
216234
}
217235
}

src/main/java/module-info.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
// This is a test header comment
2+
13
module com.chaoticsomeone.ModInfGen {
4+
// Dependencies are below
25
requires com.google.gson;
6+
7+
// These modules are exported
38
exports com.chaoticsomeone.ModInfGen;
9+
exports com.chaoticsomeone.ModInfGen.model;
10+
11+
// These modules are opened to reflection
412
opens com.chaoticsomeone.ModInfGen.model to com.google.gson;
5-
}
13+
14+
// These are the legacy lines
15+
opens com.chaoticsomeone.ModInfGen to com.google.gson;
16+
}
17+
18+
// This is the closing comment

0 commit comments

Comments
 (0)