
pl.gdela.socomo.visualizer.VisualizerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of socomo-core Show documentation
Show all versions of socomo-core Show documentation
Core parts of the tool for visualizing and analyzing source code modularity of a single java project.
package pl.gdela.socomo.visualizer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.gdela.socomo.composition.Level;
import pl.gdela.socomo.composition.Module;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.io.FileUtils.listFiles;
import static org.apache.commons.io.FileUtils.writeStringToFile;
import static org.apache.commons.io.FilenameUtils.getExtension;
import static pl.gdela.socomo.visualizer.Asset.script;
import static pl.gdela.socomo.visualizer.Asset.style;
/**
* Builds the visualizer single-page app for given module.
*/
public class VisualizerBuilder {
private static final Logger log = LoggerFactory.getLogger(VisualizerBuilder.class);
private Module module;
private Level level;
public void setModule(Module module) {
this.module = module;
}
public void setLevel(Level level) {
this.level = level;
}
public void buildInto(File file) {
log.info("visualizing into {}", file);
try {
VisualizerHtml template = new VisualizerHtml(module);
template.addLevel(level);
template.addAsset(style("https://fonts.googleapis.com/css?family=Lato:400,700"));
template.addAsset(script("https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"));
template.addAsset(script("https://cdn.jsdelivr.net/npm/[email protected]/dist/cytoscape.min.js"));
template.addAsset(script("https://cdn.jsdelivr.net/npm/[email protected]/klay.min.js"));
template.addAsset(script("https://cdn.jsdelivr.net/npm/[email protected]/cytoscape-klay.min.js"));
for (Asset asset : ownAssets(file)) {
template.addAsset(asset);
}
String visualizer = template.render();
writeStringToFile(file, visualizer, UTF_8);
} catch (IOException e) {
throw new RuntimeException("cannot build visualizer", e);
}
}
private List ownAssets(File visualizerFile) throws IOException {
BuilderProperties props = BuilderProperties.read();
if (props.socomoVersion.contains("SNAPSHOT")) {
log.info("snapshot build of socomo discovered");
return localOwnAssets(props.assetsLocation, visualizerFile);
} else {
log.debug("packed release of socomo discovered");
return remoteOwnAssets(props.socomoVersion);
}
}
private List remoteOwnAssets(String socomoVersion) {
List assets = new ArrayList<>();
String baseUrl = "https://cdn.jsdelivr.net/gh/gdela/socomo@" + socomoVersion;
assets.add(script(baseUrl + "/dist/bundle.min.js"));
assets.add(style(baseUrl + "/dist/bundle.min.css"));
return assets;
}
private List localOwnAssets(File assetsLocation, File visualizerFile) {
log.info("using assets from {}", assetsLocation);
if (!assetsLocation.isDirectory()) {
String message = "snapshot assets directory not found: ";
message += "either use released version of socomo, or build snapshot version of socomo yourself";
throw new IllegalStateException(message);
}
Collection assetFiles = listFiles(assetsLocation, new String[]{"js", "css"}, false);
List assets = new ArrayList<>();
for (File assetFile : assetFiles) {
String extension = getExtension(assetFile.getName());
if (extension.equals("js")) {
assets.add(script(shortPath(visualizerFile, assetFile)));
}
if (extension.equals("css")) {
assets.add(style(shortPath(visualizerFile, assetFile)));
}
}
return assets;
}
private static class BuilderProperties {
private String socomoVersion;
private File assetsLocation;
static BuilderProperties read() throws IOException {
URL propsFileUrl = BuilderProperties.class.getResource("builder.properties");
Properties propsInFile = new Properties();
try (InputStream propertiesStream = propsFileUrl.openStream()) {
propsInFile.load(propertiesStream);
}
BuilderProperties props = new BuilderProperties();
props.socomoVersion = propsInFile.getProperty("socomo-version");
File snapshotAssetsRoot = new File(propsInFile.getProperty("snapshot-assets-root"));
props.assetsLocation = new File(snapshotAssetsRoot, BuilderProperties.class.getPackage().getName().replace('.', '/'));
return props;
}
}
private static String shortPath(File from, File to) {
Path fromPath = from.toPath().toAbsolutePath().getParent();
Path toPath = to.toPath().toAbsolutePath();
return fromPath.relativize(toPath).toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy