@@ -76,7 +76,6 @@ public class Documenter {
76
76
77
77
private static final Map <DependencyType , String > DEPENDENCY_DESCRIPTIONS = new LinkedHashMap <>();
78
78
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" ;
80
79
81
80
private static final String DEFAULT_COMPONENTS_FILE = "components.puml" ;
82
81
private static final String DEFAULT_MODULE_COMPONENTS_FILE = "module-%s.puml" ;
@@ -90,7 +89,7 @@ public class Documenter {
90
89
private final Workspace workspace ;
91
90
private final Container container ;
92
91
private final ConfigurationProperties properties ;
93
- private final String outputFolder ;
92
+ private final Options options ;
94
93
95
94
private Map <ApplicationModule , Component > components ;
96
95
@@ -111,22 +110,28 @@ public Documenter(Class<?> modulithType) {
111
110
* @param modules must not be {@literal null}.
112
111
*/
113
112
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!" );
115
121
}
116
122
117
123
/**
118
124
* Creates a new {@link Documenter} for the given {@link ApplicationModules} and output folder.
119
125
*
120
126
* @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}.
122
128
*/
123
- public Documenter (ApplicationModules modules , String outputFolder ) {
129
+ public Documenter (ApplicationModules modules , Options options ) {
124
130
125
131
Assert .notNull (modules , "Modules must not be null!" );
126
- Assert .hasText (outputFolder , "Output folder must not be null or empty!" );
127
132
128
133
this .modules = modules ;
129
- this .outputFolder = outputFolder ;
134
+ this .options = options ;
130
135
this .workspace = new Workspace ("Modulith" , "" );
131
136
132
137
workspace .getViews ().getConfiguration ()
@@ -184,6 +189,10 @@ public Documenter writeDocumentation() {
184
189
*/
185
190
public Documenter writeDocumentation (DiagramOptions options , CanvasOptions canvasOptions ) {
186
191
192
+ if (this .options .clean ) {
193
+ clear ();
194
+ }
195
+
187
196
return writeModulesAsPlantUml (options )
188
197
.writeIndividualModulesAsPlantUml (options )
189
198
.writeModuleCanvases (canvasOptions )
@@ -601,10 +610,21 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
601
610
.createComponentView (container , prefix + options .toString (), "" );
602
611
}
603
612
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
+
604
622
private Path recreateFile (String name ) {
605
623
606
624
try {
607
625
626
+ var outputFolder = options .outputFolder ;
627
+
608
628
Files .createDirectories (Paths .get (outputFolder ));
609
629
Path filePath = Paths .get (outputFolder , name );
610
630
Files .deleteIfExists (filePath );
@@ -1250,6 +1270,38 @@ boolean hasOnlyFallbackGroup() {
1250
1270
}
1251
1271
}
1252
1272
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
+
1253
1305
private static class CustomizedPlantUmlExporter extends StructurizrPlantUMLExporter {
1254
1306
1255
1307
@ Override
0 commit comments