Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
Adds some debug to help diagnose problems with running Graphviz.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Jul 6, 2023
1 parent f5173b1 commit 4719fea
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 123 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {

dependencies {
api 'com.structurizr:structurizr-export:1.14.0'
implementation 'org.apache.logging.log4j:log4j-api:2.17.1'

testImplementation 'com.structurizr:structurizr-client:1.24.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
Expand All @@ -23,7 +24,7 @@ targetCompatibility = 1.8

description = 'Automatic layout facilities for Structurizr views'
group = 'com.structurizr'
version = '2.0.1'
version = '2.1.0'

test {
useJUnitPlatform()
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.0 (6th July 2023)

- Adds some debug to help diagnose problems with running Graphviz.

## 2.0.1 (5th June 2023)

- Fixes an issue with some older client libraries not setting the rank direction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import com.structurizr.Workspace;
import com.structurizr.export.Diagram;
import com.structurizr.view.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.Locale;

/**
Expand All @@ -17,6 +20,11 @@
*/
public class GraphvizAutomaticLayout {

private static final Log log = LogFactory.getLog(GraphvizAutomaticLayout.class);

private static final String DOT_EXECUTABLE = "dot";
private static final String USE_SVG_OUTPUT_FORMAT_OPTION = "-Tsvg";
private static final String AUTOMATICALLY_GENERATE_OUTPUT_FILE_OPTION = "-O";
private static final String DOT_FILE_EXTENSION = ".dot";

private final File path;
Expand Down Expand Up @@ -76,10 +84,15 @@ private DOTExporter createDOTExporter() {

private void writeFile(Diagram diagram) throws Exception {
File file = new File(path, diagram.getKey() + DOT_FILE_EXTENSION);
log.debug("Writing " + file.getAbsolutePath());
BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8);
writer.write(diagram.getDefinition());
writer.flush();
writer.close();

if (!file.exists()) {
log.error(file.getAbsolutePath() + " does not exist");
}
}

private SVGReader createSVGReader() {
Expand All @@ -88,55 +101,83 @@ private SVGReader createSVGReader() {

private void runGraphviz(View view) throws Exception {
ProcessBuilder processBuilder = new ProcessBuilder().inheritIO();
processBuilder.command("dot", new File(path, view.getKey() + DOT_FILE_EXTENSION).getAbsolutePath(), "-Tsvg", "-O");
List<String> command = List.of(
DOT_EXECUTABLE,
new File(path, view.getKey() + DOT_FILE_EXTENSION).getAbsolutePath(),
USE_SVG_OUTPUT_FORMAT_OPTION,
AUTOMATICALLY_GENERATE_OUTPUT_FILE_OPTION
);

processBuilder.command(command);

StringBuilder buf = new StringBuilder();
for (String s : command) {
buf.append(s);
buf.append(" ");
}
log.debug(buf);

Process process = processBuilder.start();
int exitCode = process.waitFor();
assert exitCode == 0;

String input = new String(process.getInputStream().readAllBytes());
String error = new String(process.getErrorStream().readAllBytes());

log.debug("stdout: " + input);
log.debug("stderr: " + error);
}

public void apply(CustomView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(SystemLandscapeView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(SystemContextView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(ContainerView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(ComponentView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(DynamicView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
createSVGReader().parseAndApplyLayout(view);
}

public void apply(DeploymentView view) throws Exception {
log.debug("Running Graphviz for view with key " + view.getKey());
Diagram diagram = createDOTExporter().export(view);
writeFile(diagram);
runGraphviz(view);
Expand Down
Loading

0 comments on commit 4719fea

Please sign in to comment.