2020-01-31 11:47:32 +01:00
|
|
|
package sonia.scm.plugin;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
class SmpNodeBuilder {
|
|
|
|
|
|
|
|
|
|
List<PluginNode> buildNodeTree(ExplodedSmp[] smps) {
|
|
|
|
|
return buildNodeTree(Arrays.asList(smps));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<PluginNode> buildNodeTree(Collection<ExplodedSmp> smps) {
|
2020-01-31 12:37:12 +01:00
|
|
|
Set<String> availablePlugins = getAvailablePluginNames(smps);
|
|
|
|
|
smps.forEach(smp -> this.assertDependenciesFulfilled(availablePlugins, smp));
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
List<PluginNode> nodes = createNodes(smps);
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
nodes.forEach(node ->
|
|
|
|
|
nodes.forEach(otherNode -> appendIfDependsOnOtherNode(node, otherNode))
|
|
|
|
|
);
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
nodes.forEach(this::checkForCircle);
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
return nodes;
|
|
|
|
|
}
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
private Set<String> getAvailablePluginNames(Collection<ExplodedSmp> smps) {
|
|
|
|
|
return smps.stream()
|
|
|
|
|
.map(ExplodedSmp::getPlugin)
|
|
|
|
|
.map(InstalledPluginDescriptor::getInformation)
|
|
|
|
|
.map(PluginInformation::getName)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
}
|
2020-01-31 12:27:02 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
private void appendIfDependsOnOtherNode(PluginNode node, PluginNode otherNode) {
|
|
|
|
|
if (node.getPlugin().getPlugin().getDependenciesInclusiveOptionals().contains(otherNode.getId())
|
|
|
|
|
&& !otherNode.getChildren().contains(node)) {
|
|
|
|
|
otherNode.addChild(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-31 11:47:32 +01:00
|
|
|
|
2020-01-31 12:37:12 +01:00
|
|
|
private List<PluginNode> createNodes(Collection<ExplodedSmp> smps) {
|
|
|
|
|
return smps.stream().map(PluginNode::new).collect(Collectors.toList());
|
2020-01-31 11:47:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertDependenciesFulfilled(Set<String> availablePlugins, ExplodedSmp smp) {
|
|
|
|
|
smp.getPlugin().getDependencies().forEach(dependency -> {
|
|
|
|
|
if (!availablePlugins.contains(dependency)) {
|
|
|
|
|
throw new PluginNotInstalledException(
|
|
|
|
|
String.format(
|
|
|
|
|
"dependency %s of %s is not installed",
|
|
|
|
|
dependency,
|
|
|
|
|
smp.getPlugin().getInformation().getName()
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-31 12:27:02 +01:00
|
|
|
|
|
|
|
|
private void checkForCircle(PluginNode pluginNode) {
|
|
|
|
|
pluginNode.getChildren().forEach(child -> assertDoesNotContainsDependency(pluginNode, child));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertDoesNotContainsDependency(PluginNode pluginNode, PluginNode childNode) {
|
|
|
|
|
if (childNode == pluginNode) {
|
|
|
|
|
throw new PluginCircularDependencyException("circular dependency detected: " +
|
|
|
|
|
childNode.getId() + " depends on " + pluginNode.getId() + " and " +
|
|
|
|
|
pluginNode.getId() + " depends on " + childNode.getId()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
childNode.getChildren().forEach(child -> assertDoesNotContainsDependency(pluginNode, child));
|
|
|
|
|
}
|
2020-01-31 11:47:32 +01:00
|
|
|
}
|