Skip to content

Commit 3fbd6a2

Browse files
committed
Hacking.
1 parent 5b2a002 commit 3fbd6a2

File tree

1 file changed

+59
-16
lines changed
  • spring-modulith-docs/src/main/java/org/springframework/modulith/docs

1 file changed

+59
-16
lines changed

spring-modulith-docs/src/main/java/org/springframework/modulith/docs/Documenter.java

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public class Documenter {
7575

7676
private static final Map<DependencyType, String> DEPENDENCY_DESCRIPTIONS = new LinkedHashMap<>();
7777
private static final String INVALID_FILE_NAME_PATTERN = "Configured file name pattern does not include a '%s' placeholder for the module name!";
78-
private static final String DEFAULT_LOCATION = "spring-modulith-docs";
7978

8079
private static final String DEFAULT_COMPONENTS_FILE = "components.puml";
8180
private static final String DEFAULT_MODULE_COMPONENTS_FILE = "module-%s.puml";
@@ -89,7 +88,7 @@ public class Documenter {
8988
private final Workspace workspace;
9089
private final Container container;
9190
private final ConfigurationProperties properties;
92-
private final String outputFolder;
91+
private final Options options;
9392

9493
private Map<ApplicationModule, Component> components;
9594

@@ -110,22 +109,28 @@ public Documenter(Class<?> modulithType) {
110109
* @param modules must not be {@literal null}.
111110
*/
112111
public Documenter(ApplicationModules modules) {
113-
this(modules, getDefaultOutputDirectory());
112+
this(modules, Options.defaults());
113+
}
114+
115+
public Documenter(ApplicationModules modules, String outputFolder) {
116+
117+
this(modules, new Options(outputFolder, true));
118+
119+
Assert.hasText(outputFolder, "Output folder must not be null or empty!");
114120
}
115121

116122
/**
117123
* Creates a new {@link Documenter} for the given {@link ApplicationModules} and output folder.
118124
*
119125
* @param modules must not be {@literal null}.
120-
* @param outputFolder must not be {@literal null} or empty.
126+
* @param options must not be {@literal null}.
121127
*/
122-
public Documenter(ApplicationModules modules, String outputFolder) {
128+
public Documenter(ApplicationModules modules, Options options) {
123129

124130
Assert.notNull(modules, "Modules must not be null!");
125-
Assert.hasText(outputFolder, "Output folder must not be null or empty!");
126131

127132
this.modules = modules;
128-
this.outputFolder = outputFolder;
133+
this.options = options;
129134
this.workspace = new Workspace("Modulith", "");
130135

131136
workspace.getViews().getConfiguration()
@@ -183,6 +188,10 @@ public Documenter writeDocumentation() {
183188
*/
184189
public Documenter writeDocumentation(DiagramOptions options, CanvasOptions canvasOptions) {
185190

191+
if (this.options.clean) {
192+
clear();
193+
}
194+
186195
return writeModulesAsPlantUml(options)
187196
.writeIndividualModulesAsPlantUml(options) //
188197
.writeModuleCanvases(canvasOptions);
@@ -522,10 +531,21 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
522531
.createComponentView(container, prefix + options.toString(), "");
523532
}
524533

534+
private void clear() {
535+
536+
try {
537+
Files.deleteIfExists(Paths.get(options.outputFolder));
538+
} catch (IOException o_O) {
539+
throw new RuntimeException(o_O);
540+
}
541+
}
542+
525543
private Path recreateFile(String name) {
526544

527545
try {
528546

547+
var outputFolder = options.outputFolder;
548+
529549
Files.createDirectories(Paths.get(outputFolder));
530550
Path filePath = Paths.get(outputFolder, name);
531551
Files.deleteIfExists(filePath);
@@ -574,15 +594,6 @@ private static <T> String addTableRow(List<T> types, String header, Function<Lis
574594
return options.hideEmptyLines && types.isEmpty() ? "" : writeTableRow(header, mapper.apply(types));
575595
}
576596

577-
/**
578-
* Returns the default output directory based on the detected build system.
579-
*
580-
* @return will never be {@literal null}.
581-
*/
582-
private static String getDefaultOutputDirectory() {
583-
return (new File("pom.xml").exists() ? "target" : "build").concat("/").concat(DEFAULT_LOCATION);
584-
}
585-
586597
private static record Connection(Element source, Element target) {
587598
public static Connection of(Relationship relationship) {
588599
return new Connection(relationship.getSource(), relationship.getDestination());
@@ -1164,6 +1175,38 @@ boolean hasOnlyFallbackGroup() {
11641175
}
11651176
}
11661177

1178+
public static class Options {
1179+
1180+
private static final String DEFAULT_LOCATION = (new File("pom.xml").exists() ? "target" : "build")
1181+
.concat("/spring-modulith-docs");
1182+
1183+
private final String outputFolder;
1184+
1185+
private final boolean clean;
1186+
1187+
/**
1188+
* @param outputFolder the folder to write the files to, can be {@literal null}.
1189+
* @param clean whether to clean the target directory on rendering.
1190+
*/
1191+
private Options(@Nullable String outputFolder, boolean clean) {
1192+
1193+
this.outputFolder = outputFolder == null ? DEFAULT_LOCATION : outputFolder;
1194+
this.clean = clean;
1195+
}
1196+
1197+
public static Options defaults() {
1198+
return new Options(DEFAULT_LOCATION, true);
1199+
}
1200+
1201+
public Options withoutClean() {
1202+
return new Options(outputFolder, false);
1203+
}
1204+
1205+
public Options withOutputFolder(String folder) {
1206+
return new Options(folder, clean);
1207+
}
1208+
}
1209+
11671210
private static class CustomizedPlantUmlExporter extends StructurizrPlantUMLExporter {
11681211

11691212
@Override

0 commit comments

Comments
 (0)