use of com.devonfw.cobigen.impl.config.entity.Increment in project cobigen by devonfw.
the class TemplatesConfigurationReader method loadExternalIncrement.
/**
* Tries to load an external increment. It loads the trigger of the external increment and all its increments for
* finding the needed one
*
* @param ref incrementRef to load and store on the root increment
* @return the referenced child increment
*/
private Increment loadExternalIncrement(IncrementRef ref) {
Increment childPkg;
String[] split = splitExternalRef(ref.getRef());
String refTrigger = split[0];
String refIncrement = split[1];
com.devonfw.cobigen.impl.config.TemplatesConfiguration externalTemplatesConfiguration = loadExternalConfig(refTrigger);
Map<String, Increment> externalIncrements = externalTemplatesConfiguration.getIncrements();
childPkg = externalIncrements.get(refIncrement);
if (childPkg == null) {
throw new InvalidConfigurationException("No Increment found for ref=" + ref.getRef());
}
return childPkg;
}
use of com.devonfw.cobigen.impl.config.entity.Increment in project cobigen by devonfw.
the class TemplatesConfigurationReader method loadIncrements.
/**
* Loads all increments of the static configuration into the local representation.
*
* @return the mapping of increment names to the corresponding {@link Increment}
* @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
* @param trigger {@link Trigger} for which the templates should be loaded
* @throws InvalidConfigurationException if there is an invalid ref attribute
*/
public Map<String, Increment> loadIncrements(Map<String, Template> templates, Trigger trigger) throws InvalidConfigurationException {
Map<String, Increment> increments = new HashMap<>();
Increments incrementsNode = this.configNode.getIncrements();
if (incrementsNode != null) {
// Add first all increments informally be able to resolve recursive increment references
for (com.devonfw.cobigen.impl.config.entity.io.Increment source : incrementsNode.getIncrement()) {
if (!increments.containsKey(source.getName())) {
increments.put(source.getName(), new Increment(source.getName(), source.getDescription(), trigger));
} else {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Duplicate increment found with name='" + source.getName() + "'.");
}
}
// Collect templates
for (com.devonfw.cobigen.impl.config.entity.io.Increment p : this.configNode.getIncrements().getIncrement()) {
Increment target = increments.get(p.getName());
addAllTemplatesRecursively(target, p, templates, increments);
}
}
return increments;
}
use of com.devonfw.cobigen.impl.config.entity.Increment in project cobigen by devonfw.
the class TemplatesConfigurationReader method loadSpecificIncrement.
/**
* Loads an specific increment of the static configuration into the local representation. The return object must be a
* map because maybe this increment references other increments
*
* @return the mapping of increment names to the corresponding {@link Increment}
* @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
* @param trigger {@link Trigger} for which the templates should be loaded
* @param incrementName the increment to search
* @throws InvalidConfigurationException if there is an invalid ref attribute
*/
public Map<String, Increment> loadSpecificIncrement(Map<String, Template> templates, Trigger trigger, String incrementName) throws InvalidConfigurationException {
Map<String, Increment> increments = new HashMap<>();
Increments incrementsNode = this.configNode.getIncrements();
if (incrementsNode != null) {
// We only add the specific increment we want
com.devonfw.cobigen.impl.config.entity.io.Increment source = getSpecificIncrement(incrementsNode.getIncrement(), incrementName);
if (source == null) {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No increment found with name='" + incrementName + "' on the external templates.xml folder.");
}
increments.put(source.getName(), new Increment(source.getName(), source.getDescription(), trigger));
// Collect templates for our specific increment
Increment target = increments.get(source.getName());
addAllTemplatesRecursively(target, source, templates, increments);
}
return increments;
}
use of com.devonfw.cobigen.impl.config.entity.Increment in project cobigen by devonfw.
the class TemplatesConfigurationReader method addAllTemplatesRecursively.
/**
* Adds all templates defined within the increment and sub increments recursively.
*
* @param rootIncrement the {@link Increment} on which the templates should be added
* @param current the source {@link com.devonfw.cobigen.impl.config.entity.io.Increment} from which to retrieve the
* data
* @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
* @param increments {@link Map} of all retrieved increments
* @throws InvalidConfigurationException if there is an invalid ref attribute
*/
private void addAllTemplatesRecursively(Increment rootIncrement, com.devonfw.cobigen.impl.config.entity.io.Increment current, Map<String, Template> templates, Map<String, Increment> increments) throws InvalidConfigurationException {
for (TemplateRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof TemplateRef).map(e -> (TemplateRef) e).collect(Collectors.toList())) {
Template temp = templates.get(ref.getRef());
if (temp == null) {
if (isExternalRef(ref.getRef())) {
rootIncrement.addTemplate(loadExternalTemplate(ref));
} else {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No template found for ref='" + ref.getRef() + "'!");
}
} else {
rootIncrement.addTemplate(temp);
}
}
for (IncrementRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof IncrementRef).map(e -> (IncrementRef) e).collect(Collectors.toList())) {
Increment parentPkg = increments.get(current.getName());
Increment childPkg = increments.get(ref.getRef());
if (childPkg == null) {
// We try to find the increment inside our templates.xml file
Increments incrementsNode = this.configNode.getIncrements();
com.devonfw.cobigen.impl.config.entity.io.Increment source = null;
if (incrementsNode != null) {
// We only add the specific increment we want
source = getSpecificIncrement(incrementsNode.getIncrement(), ref.getRef());
if (source != null) {
addAllTemplatesRecursively(rootIncrement, source, templates, increments);
} else // incrementRef contains "::". That would mean we have to search on another folder.
if (isExternalRef(ref.getRef())) {
parentPkg.addIncrementDependency(loadExternalIncrement(ref));
} else {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No increment found for ref='" + ref.getRef() + "'!");
}
}
} else {
parentPkg.addIncrementDependency(childPkg);
com.devonfw.cobigen.impl.config.entity.io.Increment pkg = getIncrementDeclaration(ref);
addAllTemplatesRecursively(rootIncrement, pkg, templates, increments);
}
}
for (TemplateScanRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof TemplateScanRef).map(e -> (TemplateScanRef) e).collect(Collectors.toList())) {
List<String> scannedTemplateNames = this.templateScanTemplates.get(ref.getRef());
if (scannedTemplateNames == null) {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No templateScan found for ref='" + ref.getRef() + "'!");
} else {
for (String scannedTemplateName : scannedTemplateNames) {
rootIncrement.addTemplate(templates.get(scannedTemplateName));
}
}
}
}
use of com.devonfw.cobigen.impl.config.entity.Increment in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testTemplateRefOutsideCurrentFile.
/**
* Tests the correct resolution of TemplateRef from outside the current templates file.
*/
@Test
public void testTemplateRefOutsideCurrentFile() {
// given
Trigger trigger = new Trigger("testingTrigger", "asdf", "valid_external_templateref", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
Path configPath = Paths.get(new File(testFileRootPath).toURI());
ConfigurationHolder configurationHolder = new ConfigurationHolder(configPath.toUri());
TemplatesConfiguration templatesConfiguration = configurationHolder.readTemplatesConfiguration(trigger);
Map<String, Increment> increments = templatesConfiguration.getIncrements();
assertThat(templatesConfiguration.getTrigger().getId()).isEqualTo("testingTrigger");
// validation
Increment incrementThree = increments.get("3");
LinkedList<String> templateNamesThree = new LinkedList<>();
for (Template tmplate : incrementThree.getTemplates()) {
templateNamesThree.add(tmplate.getName());
}
assertThat(templateNamesThree).containsExactly("templateDecl");
Increment incrementFour = increments.get("4");
LinkedList<String> templateNamesFour = new LinkedList<>();
for (Template tmplate : incrementFour.getTemplates()) {
templateNamesFour.add(tmplate.getName());
}
assertThat(templateNamesFour).containsExactly("ExplicitlyDefined");
}
Aggregations