@@ -75,7 +75,6 @@ public class Documenter {
75
75
76
76
private static final Map <DependencyType , String > DEPENDENCY_DESCRIPTIONS = new LinkedHashMap <>();
77
77
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" ;
79
78
80
79
private static final String DEFAULT_COMPONENTS_FILE = "components.puml" ;
81
80
private static final String DEFAULT_MODULE_COMPONENTS_FILE = "module-%s.puml" ;
@@ -89,7 +88,7 @@ public class Documenter {
89
88
private final Workspace workspace ;
90
89
private final Container container ;
91
90
private final ConfigurationProperties properties ;
92
- private final String outputFolder ;
91
+ private final Options options ;
93
92
94
93
private Map <ApplicationModule , Component > components ;
95
94
@@ -110,22 +109,28 @@ public Documenter(Class<?> modulithType) {
110
109
* @param modules must not be {@literal null}.
111
110
*/
112
111
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!" );
114
120
}
115
121
116
122
/**
117
123
* Creates a new {@link Documenter} for the given {@link ApplicationModules} and output folder.
118
124
*
119
125
* @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}.
121
127
*/
122
- public Documenter (ApplicationModules modules , String outputFolder ) {
128
+ public Documenter (ApplicationModules modules , Options options ) {
123
129
124
130
Assert .notNull (modules , "Modules must not be null!" );
125
- Assert .hasText (outputFolder , "Output folder must not be null or empty!" );
126
131
127
132
this .modules = modules ;
128
- this .outputFolder = outputFolder ;
133
+ this .options = options ;
129
134
this .workspace = new Workspace ("Modulith" , "" );
130
135
131
136
workspace .getViews ().getConfiguration ()
@@ -183,6 +188,10 @@ public Documenter writeDocumentation() {
183
188
*/
184
189
public Documenter writeDocumentation (DiagramOptions options , CanvasOptions canvasOptions ) {
185
190
191
+ if (this .options .clean ) {
192
+ clear ();
193
+ }
194
+
186
195
return writeModulesAsPlantUml (options )
187
196
.writeIndividualModulesAsPlantUml (options ) //
188
197
.writeModuleCanvases (canvasOptions );
@@ -522,10 +531,21 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
522
531
.createComponentView (container , prefix + options .toString (), "" );
523
532
}
524
533
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
+
525
543
private Path recreateFile (String name ) {
526
544
527
545
try {
528
546
547
+ var outputFolder = options .outputFolder ;
548
+
529
549
Files .createDirectories (Paths .get (outputFolder ));
530
550
Path filePath = Paths .get (outputFolder , name );
531
551
Files .deleteIfExists (filePath );
@@ -574,15 +594,6 @@ private static <T> String addTableRow(List<T> types, String header, Function<Lis
574
594
return options .hideEmptyLines && types .isEmpty () ? "" : writeTableRow (header , mapper .apply (types ));
575
595
}
576
596
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
-
586
597
private static record Connection (Element source , Element target ) {
587
598
public static Connection of (Relationship relationship ) {
588
599
return new Connection (relationship .getSource (), relationship .getDestination ());
@@ -1164,6 +1175,38 @@ boolean hasOnlyFallbackGroup() {
1164
1175
}
1165
1176
}
1166
1177
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
+
1167
1210
private static class CustomizedPlantUmlExporter extends StructurizrPlantUMLExporter {
1168
1211
1169
1212
@ Override
0 commit comments