Skip to content

Commit

Permalink
GH-644 - Initial draft.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Jul 15, 2024
1 parent 6906aa0 commit b4dae61
Showing 1 changed file with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class Documenter {

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

private static final String DEFAULT_COMPONENTS_FILE = "components.puml";
private static final String DEFAULT_MODULE_COMPONENTS_FILE = "module-%s.puml";
Expand All @@ -90,7 +89,7 @@ public class Documenter {
private final Workspace workspace;
private final Container container;
private final ConfigurationProperties properties;
private final String outputFolder;
private final Options options;

private Map<ApplicationModule, Component> components;

Expand All @@ -111,22 +110,28 @@ public Documenter(Class<?> modulithType) {
* @param modules must not be {@literal null}.
*/
public Documenter(ApplicationModules modules) {
this(modules, getDefaultOutputDirectory());
this(modules, Options.defaults());
}

public Documenter(ApplicationModules modules, String outputFolder) {

this(modules, new Options(outputFolder, true));

Assert.hasText(outputFolder, "Output folder must not be null or empty!");
}

/**
* Creates a new {@link Documenter} for the given {@link ApplicationModules} and output folder.
*
* @param modules must not be {@literal null}.
* @param outputFolder must not be {@literal null} or empty.
* @param options must not be {@literal null}.
*/
public Documenter(ApplicationModules modules, String outputFolder) {
public Documenter(ApplicationModules modules, Options options) {

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

this.modules = modules;
this.outputFolder = outputFolder;
this.options = options;
this.workspace = new Workspace("Modulith", "");

workspace.getViews().getConfiguration()
Expand Down Expand Up @@ -184,6 +189,10 @@ public Documenter writeDocumentation() {
*/
public Documenter writeDocumentation(DiagramOptions options, CanvasOptions canvasOptions) {

if (this.options.clean) {
clear();
}

return writeModulesAsPlantUml(options)
.writeIndividualModulesAsPlantUml(options)
.writeModuleCanvases(canvasOptions)
Expand Down Expand Up @@ -601,10 +610,21 @@ private ComponentView createComponentView(DiagramOptions options, @Nullable Appl
.createComponentView(container, prefix + options.toString(), "");
}

private void clear() {

try {
Files.deleteIfExists(Paths.get(options.outputFolder));
} catch (IOException o_O) {
throw new RuntimeException(o_O);
}
}

private Path recreateFile(String name) {

try {

var outputFolder = options.outputFolder;

Files.createDirectories(Paths.get(outputFolder));
Path filePath = Paths.get(outputFolder, name);
Files.deleteIfExists(filePath);
Expand Down Expand Up @@ -1250,6 +1270,38 @@ boolean hasOnlyFallbackGroup() {
}
}

public static class Options {

private static final String DEFAULT_LOCATION = (new File("pom.xml").exists() ? "target" : "build")
.concat("/spring-modulith-docs");

private final String outputFolder;

private final boolean clean;

/**
* @param outputFolder the folder to write the files to, can be {@literal null}.
* @param clean whether to clean the target directory on rendering.
*/
private Options(@Nullable String outputFolder, boolean clean) {

this.outputFolder = outputFolder == null ? DEFAULT_LOCATION : outputFolder;
this.clean = clean;
}

public static Options defaults() {
return new Options(DEFAULT_LOCATION, true);
}

public Options withoutClean() {
return new Options(outputFolder, false);
}

public Options withOutputFolder(String folder) {
return new Options(folder, clean);
}
}

private static class CustomizedPlantUmlExporter extends StructurizrPlantUMLExporter {

@Override
Expand Down

0 comments on commit b4dae61

Please sign in to comment.