Skip to content

Commit b4dae61

Browse files
committed
GH-644 - Initial draft.
1 parent 6906aa0 commit b4dae61

File tree

1 file changed

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

1 file changed

+59
-7
lines changed

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

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

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

8180
private static final String DEFAULT_COMPONENTS_FILE = "components.puml";
8281
private static final String DEFAULT_MODULE_COMPONENTS_FILE = "module-%s.puml";
@@ -90,7 +89,7 @@ public class Documenter {
9089
private final Workspace workspace;
9190
private final Container container;
9291
private final ConfigurationProperties properties;
93-
private final String outputFolder;
92+
private final Options options;
9493

9594
private Map<ApplicationModule, Component> components;
9695

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

117123
/**
118124
* Creates a new {@link Documenter} for the given {@link ApplicationModules} and output folder.
119125
*
120126
* @param modules must not be {@literal null}.
121-
* @param outputFolder must not be {@literal null} or empty.
127+
* @param options must not be {@literal null}.
122128
*/
123-
public Documenter(ApplicationModules modules, String outputFolder) {
129+
public Documenter(ApplicationModules modules, Options options) {
124130

125131
Assert.notNull(modules, "Modules must not be null!");
126-
Assert.hasText(outputFolder, "Output folder must not be null or empty!");
127132

128133
this.modules = modules;
129-
this.outputFolder = outputFolder;
134+
this.options = options;
130135
this.workspace = new Workspace("Modulith", "");
131136

132137
workspace.getViews().getConfiguration()
@@ -184,6 +189,10 @@ public Documenter writeDocumentation() {
184189
*/
185190
public Documenter writeDocumentation(DiagramOptions options, CanvasOptions canvasOptions) {
186191

192+
if (this.options.clean) {
193+
clear();
194+
}
195+
187196
return writeModulesAsPlantUml(options)
188197
.writeIndividualModulesAsPlantUml(options)
189198
.writeModuleCanvases(canvasOptions)
@@ -601,10 +610,21 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
601610
.createComponentView(container, prefix + options.toString(), "");
602611
}
603612

613+
private void clear() {
614+
615+
try {
616+
Files.deleteIfExists(Paths.get(options.outputFolder));
617+
} catch (IOException o_O) {
618+
throw new RuntimeException(o_O);
619+
}
620+
}
621+
604622
private Path recreateFile(String name) {
605623

606624
try {
607625

626+
var outputFolder = options.outputFolder;
627+
608628
Files.createDirectories(Paths.get(outputFolder));
609629
Path filePath = Paths.get(outputFolder, name);
610630
Files.deleteIfExists(filePath);
@@ -1250,6 +1270,38 @@ boolean hasOnlyFallbackGroup() {
12501270
}
12511271
}
12521272

1273+
public static class Options {
1274+
1275+
private static final String DEFAULT_LOCATION = (new File("pom.xml").exists() ? "target" : "build")
1276+
.concat("/spring-modulith-docs");
1277+
1278+
private final String outputFolder;
1279+
1280+
private final boolean clean;
1281+
1282+
/**
1283+
* @param outputFolder the folder to write the files to, can be {@literal null}.
1284+
* @param clean whether to clean the target directory on rendering.
1285+
*/
1286+
private Options(@Nullable String outputFolder, boolean clean) {
1287+
1288+
this.outputFolder = outputFolder == null ? DEFAULT_LOCATION : outputFolder;
1289+
this.clean = clean;
1290+
}
1291+
1292+
public static Options defaults() {
1293+
return new Options(DEFAULT_LOCATION, true);
1294+
}
1295+
1296+
public Options withoutClean() {
1297+
return new Options(outputFolder, false);
1298+
}
1299+
1300+
public Options withOutputFolder(String folder) {
1301+
return new Options(folder, clean);
1302+
}
1303+
}
1304+
12531305
private static class CustomizedPlantUmlExporter extends StructurizrPlantUMLExporter {
12541306

12551307
@Override

0 commit comments

Comments
 (0)