Skip to content

Commit

Permalink
Fixes #423
Browse files Browse the repository at this point in the history
  • Loading branch information
Riduidel committed Mar 7, 2024
1 parent c2e30a9 commit 1b48fe4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.ndx.aadarchi.base.enhancers.scm;

import java.util.logging.Logger;

import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileFilter;
import org.apache.commons.vfs2.FileObject;
import org.ndx.aadarchi.base.AgileArchitectureSection;
import org.ndx.aadarchi.base.Enhancer;
import org.ndx.aadarchi.base.OutputBuilder;
import org.ndx.aadarchi.base.OutputBuilder.Format;
import org.ndx.aadarchi.base.utils.FileContentCache;
import org.ndx.aadarchi.base.utils.StructurizrUtils;
import org.ndx.aadarchi.base.utils.commonsvfs.FileObjectDetector;

import com.kodcu.asciidocfx.MarkdownToAsciidoc;
import com.structurizr.model.Element;
import com.structurizr.model.StaticStructureElement;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

/**
* A component allowing to easily include one file (markdown or asciidoc)
* in a specific documentation part
*/
@Dependent
public class ReadOneFileFromSource {
@Inject Logger logger;

@Inject
FileContentCache cache;

@Inject FileObjectDetector detector;

public void read(StaticStructureElement element,
FileFilter fileFilter,
AgileArchitectureSection section,
OutputBuilder builder,
Enhancer enhancer,
boolean force) {
detector.whenFileDetected(element, fileFilter,
// No file detected
elementRoot -> { logger.severe(String.format(
"Couldn't find any file matching %s for element %s " + "(path is %s)",
StructurizrUtils.getCanonicalPath(element), elementRoot)); },
// One file detected
(elementRoot, readme) -> { writeContentOf(readme, element, section, builder, enhancer, force); },
// on multiple file detected
(elementRoot, detectedFiles) -> { logger.severe(String.format(
"There are more than one valid file matching %s for element %s"
+ "(path is %s)",
StructurizrUtils.getCanonicalPath(element), elementRoot)); }
);
}

void writeContentOf(FileObject readme, Element element, AgileArchitectureSection section, OutputBuilder builder, Enhancer enhancer, boolean force) {
FileObject outputFor = builder.outputFor(section, element, enhancer, Format.adoc);
try {
try {
if (force) {
outputFor.delete();
} else {
if (outputFor.exists()
&& readme.getContent().getLastModifiedTime() < outputFor.getContent().getLastModifiedTime())
return;
}
// Now we have content as asciidoc, so let's write it to the conventional
// location
String fileText = IOUtils.toString(cache.openStreamFor(readme), "UTF-8");
if (readme.getName().getExtension().toLowerCase().equals("md")) {
fileText = MarkdownToAsciidoc.convert(fileText);
}
builder.writeToOutput(section, element, enhancer, Format.adoc, fileText);
} finally {
readme.close();
}
} catch (Exception e) {
throw new CantExtractReadme(String.format(
"Can't extract readme of container %s from file %s",
StructurizrUtils.getCanonicalPath(element), readme), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public class ReadmeReader extends ModelElementAdapter {
@Inject
@ConfigProperty(name = "force", defaultValue = "false")
boolean force;

@Inject
FileContentCache cache;

@Inject FileObjectDetector detector;
@Inject ReadOneFileFromSource readOneFile;

@Override
public int priority() {
Expand All @@ -45,46 +42,8 @@ public int priority() {

@Override
protected void processElement(StaticStructureElement element, OutputBuilder builder) {
detector.whenFileDetected(element, new RegexFileFilter("(readme|README)\\.(adoc|md)"),
// No file detected
elementRoot -> { logger.severe(String.format(
"Couldn't find any Readme for element %s " + "(path is %s)",
StructurizrUtils.getCanonicalPath(element), elementRoot)); },
// One file detected
(elementRoot, readme) -> { writeReadmeFor(readme, element, builder); },
// on multiple file detected
(elementRoot, detectedFiles) -> { logger.severe(String.format(
"There are more than one valid Readme for element %s"
+ "(path is %s)",
StructurizrUtils.getCanonicalPath(element), elementRoot)); }
);
}

void writeReadmeFor(FileObject readme, Element element, OutputBuilder builder) {
FileObject outputFor = builder.outputFor(AgileArchitectureSection.code, element, this, Format.adoc);
try {
try {
if (force) {
outputFor.delete();
} else {
if (outputFor.exists()
&& readme.getContent().getLastModifiedTime() < outputFor.getContent().getLastModifiedTime())
return;
}
// Now we have content as asciidoc, so let's write it to the conventional
// location
String readmeText = IOUtils.toString(cache.openStreamFor(readme), "UTF-8");
if (readme.getName().getExtension().toLowerCase().equals("md")) {
readmeText = MarkdownToAsciidoc.convert(readmeText);
}
builder.writeToOutput(AgileArchitectureSection.code, element, this, Format.adoc, readmeText);
} finally {
readme.close();
}
} catch (Exception e) {
throw new CantExtractReadme(String.format(
"Can't extract readme of container %s from file %s",
StructurizrUtils.getCanonicalPath(element), readme), e);
}
readOneFile.read(element,
new RegexFileFilter("(readme|README)\\.(adoc|md)"),
AgileArchitectureSection.code, builder, this, force);
}
}

0 comments on commit 1b48fe4

Please sign in to comment.