Skip to content

Commit 08430ad

Browse files
tobHaiodrotbohm
authored andcommitted
GH-644 - Add option to clean the output directory in documentation generation.
1 parent b4dae61 commit 08430ad

File tree

2 files changed

+76
-6
lines changed
  • spring-modulith-docs/src/main/java/org/springframework/modulith/docs
  • spring-modulith-integration-test/src/test/java/org/springframework/modulith/docs

2 files changed

+76
-6
lines changed

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ public Documenter writeDocumentation() {
183183
* <li>The Module Canvas for each module.</li>
184184
* </ul>
185185
*
186-
* @param options must not be {@literal null}.
186+
* @param diagramOptions must not be {@literal null}.
187187
* @param canvasOptions must not be {@literal null}.
188188
* @return the current instance, will never be {@literal null}.
189189
*/
190-
public Documenter writeDocumentation(DiagramOptions options, CanvasOptions canvasOptions) {
190+
public Documenter writeDocumentation(DiagramOptions diagramOptions, CanvasOptions canvasOptions) {
191191

192192
if (this.options.clean) {
193-
clear();
193+
clearOutputFolder();
194194
}
195195

196196
return writeModulesAsPlantUml(options)
@@ -610,10 +610,15 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
610610
.createComponentView(container, prefix + options.toString(), "");
611611
}
612612

613-
private void clear() {
613+
private void clearOutputFolder() {
614614

615-
try {
616-
Files.deleteIfExists(Paths.get(options.outputFolder));
615+
Path outputPath = Paths.get(options.outputFolder);
616+
if (!outputPath.toFile().exists()) {
617+
return;
618+
}
619+
620+
try (Stream<Path> paths = Files.walk(outputPath)) {
621+
paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
617622
} catch (IOException o_O) {
618623
throw new RuntimeException(o_O);
619624
}
@@ -1289,14 +1294,37 @@ private Options(@Nullable String outputFolder, boolean clean) {
12891294
this.clean = clean;
12901295
}
12911296

1297+
/**
1298+
* Creates a default {@link Options} instance configuring a default output folder based on the detected build tool (see {@link Options#DEFAULT_LOCATION}).
1299+
* Use {@link #withOutputFolder(String)} if you want to customize the output folder.
1300+
* Per default the output folder is wiped before any files are written to it.
1301+
* Use {@link #withoutClean()} to disable cleaning of the output folder.
1302+
*
1303+
* @return will never be {@literal null}.
1304+
* @see #withoutClean()
1305+
* @see #withOutputFolder(String)
1306+
*/
12921307
public static Options defaults() {
12931308
return new Options(DEFAULT_LOCATION, true);
12941309
}
12951310

1311+
/**
1312+
* Disables the cleaning of the output folder before any file is written.
1313+
*
1314+
* @return will never be {@literal null}.
1315+
*/
12961316
public Options withoutClean() {
12971317
return new Options(outputFolder, false);
12981318
}
12991319

1320+
/**
1321+
* Configures the output folder for the created files.
1322+
* The given directory is wiped before any files are written to it.
1323+
*
1324+
* @param folder if null the default location based on the detected build tool will be used (see {@link Options#DEFAULT_LOCATION}).
1325+
* The given folder will be created if it does not exist already. Existing folders are supported as well.
1326+
* @return will never be {@literal null}.
1327+
*/
13001328
public Options withOutputFolder(String folder) {
13011329
return new Options(folder, clean);
13021330
}

spring-modulith-integration-test/src/test/java/org/springframework/modulith/docs/DocumenterTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.util.stream.Stream;
2828

2929
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.io.TempDir;
3031
import org.springframework.modulith.core.ApplicationModule;
3132
import org.springframework.modulith.core.ApplicationModules;
3233
import org.springframework.modulith.core.DependencyType;
3334
import org.springframework.modulith.docs.Documenter.DiagramOptions;
35+
import org.springframework.modulith.docs.Documenter.Options;
3436
import org.springframework.util.function.ThrowingConsumer;
3537

3638
import com.acme.myproject.Application;
@@ -139,6 +141,46 @@ void doesNotCreateAggregatingDocumentIfNoPartialsExist() throws Exception {
139141
});
140142
}
141143

144+
@Test
145+
void shouldCleanOutputLocation(@TempDir Path outputDirectory) throws IOException {
146+
147+
var filePath = createTestFile(outputDirectory);
148+
var nestedFiledPath = createTestFileInSubdirectory(outputDirectory);
149+
150+
new Documenter(ApplicationModules.of(Application.class), outputDirectory.toString()).writeDocumentation();
151+
152+
assertThat(filePath).doesNotExist();
153+
assertThat(nestedFiledPath).doesNotExist();
154+
assertThat(Files.list(outputDirectory)).isNotEmpty();
155+
}
156+
157+
@Test
158+
void shouldNotCleanOutputLocation(@TempDir Path outputDirectory) throws IOException {
159+
160+
var filePath = createTestFile(outputDirectory);
161+
var nestedFiledPath = createTestFileInSubdirectory(outputDirectory);
162+
163+
new Documenter(ApplicationModules.of(Application.class),
164+
Options.defaults().withOutputFolder(outputDirectory.toString()).withoutClean())
165+
.writeDocumentation();
166+
167+
assertThat(filePath).exists();
168+
assertThat(nestedFiledPath).exists();
169+
assertThat(Files.list(outputDirectory)).isNotEmpty();
170+
}
171+
172+
private static Path createTestFile(Path tempDir) throws IOException {
173+
return createFile(tempDir.resolve("some-old-module.adoc"));
174+
}
175+
176+
private static Path createTestFileInSubdirectory(Path tempDir) throws IOException {
177+
return createFile(tempDir.resolve("some-subdirectory").resolve("old-module.adoc"));
178+
}
179+
180+
private static Path createFile(Path filePath) throws IOException {
181+
return Files.createDirectories(filePath);
182+
}
183+
142184
private static void deleteDirectoryContents(Path path) throws IOException {
143185

144186
if (Files.exists(path) && Files.isDirectory(path)) {

0 commit comments

Comments
 (0)