Skip to content

Commit

Permalink
Small optimization raised by #337 (but unrelated to) : do not write e…
Browse files Browse the repository at this point in the history
…nhancers files when they're empty
  • Loading branch information
Riduidel committed Mar 20, 2023
1 parent 292dc14 commit 9392490
Showing 1 changed file with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.NameScope;
Expand All @@ -22,6 +23,7 @@ public UnableToWriteOutput(String message, Throwable cause) {
super(message, cause);
}
}

private final FileObject enhancementsBase;
public static final String SECTION_PATTERN = "%02d-%s";

Expand All @@ -33,76 +35,73 @@ public SimpleOutputBuilder(FileObject outputBase) {
@Override
public FileObject outputFor(AgileArchitectureSection section, Element element, Enhancer enhancer, String format) {
// Yup, we use hex values for priority, to have less characters
String path = String.format("%s/"+SECTION_PATTERN+"/_%08x-%s.%s",
sanitize(StructurizrUtils.getCanonicalPath(element)),
section.index(), section.name(),
enhancer.priority(), enhancer.getClass().getSimpleName(), format
);
String path = String.format("%s/" + SECTION_PATTERN + "/_%08x-%s.%s",
sanitize(StructurizrUtils.getCanonicalPath(element)), section.index(), section.name(),
enhancer.priority(), enhancer.getClass().getSimpleName(), format);
return outputFor(path);
}

private FileObject outputFor(String path) {
try {
while(path.startsWith("/")) {
while (path.startsWith("/")) {
path = path.substring(1);
}
return enhancementsBase.resolveFile(path, NameScope.DESCENDENT);
} catch (FileSystemException e) {
throw new CantToResolvePath(String.format("Unable to resolve path %s relatively to %s", path, enhancementsBase), e);
throw new CantToResolvePath(
String.format("Unable to resolve path %s relatively to %s", path, enhancementsBase), e);
}
}

@Override
public FileObject outputDirectoryFor(AgileArchitectureSection section, Element element) {
String path = String.format("%s/"+SECTION_PATTERN,
sanitize(StructurizrUtils.getCanonicalPath(element)),
section.index(), section.name()
);
String path = String.format("%s/" + SECTION_PATTERN, sanitize(StructurizrUtils.getCanonicalPath(element)),
section.index(), section.name());
return outputFor(path);
}

private String sanitize(String canonicalPath) {
return Stream.of(canonicalPath.split("\\/"))
.map(name -> name.replaceAll("[:\\\\/*?|<>]", "_"))
return Stream.of(canonicalPath.split("\\/")).map(name -> name.replaceAll("[:\\\\/*?|<>]", "_"))
.collect(Collectors.joining("/"));
}

@Override
public FileObject outputFor(AgileArchitectureSection section, Element element, Enhancer enhancer, HandledFormat format) {
public FileObject outputFor(AgileArchitectureSection section, Element element, Enhancer enhancer,
HandledFormat format) {
return outputFor(section, element, enhancer, format.getExtension());
}

public FileObject writeToOutput(AgileArchitectureSection section, Element element, Enhancer enhancer,
HandledFormat format, CharSequence text, boolean append) {
FileObject returned = outputFor(section, element, enhancer, format);
try {
if(!append)
returned.delete();
returned.getParent().createFolder();
try(OutputStream outputStream = returned.getContent().getOutputStream()) {
IOUtils.write(format.createCommentForEnhancer(enhancer), outputStream, format.encoding());
IOUtils.write(text, outputStream, format.encoding());
} finally {
returned.getContent().close();
if (!StringUtils.isBlank(text)) {
try {
if (!append)
returned.delete();
returned.getParent().createFolder();
try (OutputStream outputStream = returned.getContent().getOutputStream()) {
IOUtils.write(format.createCommentForEnhancer(enhancer), outputStream, format.encoding());
IOUtils.write(text, outputStream, format.encoding());
} finally {
returned.getContent().close();
}
} catch (IOException e) {
throw new UnableToWriteOutput(
String.format("Unable to write output to file %s", returned.getName().getPath()), e);
}
} catch(IOException e) {
throw new UnableToWriteOutput(
String.format("Unable to write output to file %s", returned.getName().getPath()),
e
);
}
return returned;
}

@Override
public FileObject writeToOutput(AgileArchitectureSection section, Element element, Enhancer enhancer,
HandledFormat format, CharSequence text) {
HandledFormat format, CharSequence text) {
return writeToOutput(section, element, enhancer, format, text, false);
}

@Override
public FileObject appendToOutput(AgileArchitectureSection section, Element element,
Enhancer enhancer, HandledFormat format, CharSequence text) {
public FileObject appendToOutput(AgileArchitectureSection section, Element element, Enhancer enhancer,
HandledFormat format, CharSequence text) {
return writeToOutput(section, element, enhancer, format, text, true);
}

Expand Down

0 comments on commit 9392490

Please sign in to comment.