use of com.devonfw.cobigen.impl.config.entity.Trigger in project cobigen by devonfw.
the class TemplatesConfigurationReader method getExternalTrigger.
/**
* Tries to read the context.xml file for finding and returning an external trigger
*
* @param triggerToSearch string containing the name of the trigger to search
* @return the found external trigger
*/
private Trigger getExternalTrigger(String triggerToSearch) {
ContextConfigurationReader contextConfigurationReader = new ContextConfigurationReader(this.configurationHolder.readContextConfiguration().getConfigurationPath());
Map<String, Trigger> triggers = contextConfigurationReader.loadTriggers();
Trigger trig = triggers.get(triggerToSearch);
if (trig == null) {
throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Invalid external ref, no trigger '" + triggerToSearch + "' was found on your context.xml!");
}
return trig;
}
use of com.devonfw.cobigen.impl.config.entity.Trigger in project cobigen by devonfw.
the class GenerationProcessorImpl method generate.
@Override
public GenerationReportTo generate(Object input, List<? extends GenerableArtifact> generableArtifacts, Path targetRootPath, boolean forceOverride, Map<String, Object> rawModel, BiConsumer<String, Integer> progressCallback) {
InputValidator.validateInputsUnequalNull(input, generableArtifacts);
List<Class<?>> logicClasses = null;
// only implicit dependency to javaplugin to lower classloader complexity
ClassLoader inputProjectClassLoader = null;
if (input instanceof Class) {
inputProjectClassLoader = ((Class<?>) input).getClassLoader();
} else if (input instanceof Object[]) {
for (Object obj : (Object[]) input) {
if (obj instanceof Class) {
inputProjectClassLoader = ((Class<?>) obj).getClassLoader();
}
}
}
Path templateConfigPath = Paths.get(this.configurationHolder.getConfigurationLocation());
progressCallback.accept("Prepend Templates Classloader", 10);
inputProjectClassLoader = prependTemplatesClassloader(templateConfigPath, inputProjectClassLoader);
if (inputProjectClassLoader != null) {
try {
logicClasses = ConfigurationClassLoaderUtil.resolveUtilClasses(this.configurationHolder, inputProjectClassLoader);
} catch (IOException e) {
LOG.error("An IOException occured while resolving utility classes!", e);
}
}
// initialize
this.forceOverride = forceOverride;
this.input = input;
if (logicClasses != null) {
progressCallback.accept("Load Template logic classes", 20);
loadLogicClasses(progressCallback, logicClasses);
}
progressCallback.accept("Create Temporary Target Directory", 40);
this.rawModel = rawModel;
try {
this.tmpTargetRootPath = Files.createTempDirectory("cobigen-");
LOG.info("Temporary working directory: {}", this.tmpTargetRootPath);
} catch (IOException e) {
throw new CobiGenRuntimeException("Could not create temporary folder.", e);
}
this.targetRootPath = targetRootPath;
this.generationReport = new GenerationReportTo();
progressCallback.accept("Load templates", 50);
LOG.debug("Collecting templates");
Collection<TemplateTo> templatesToBeGenerated = flatten(generableArtifacts);
// generate
Map<File, File> origToTmpFileTrace = Maps.newHashMap();
try {
LOG.debug("Generating {} templates", templatesToBeGenerated.size());
for (TemplateTo template : templatesToBeGenerated) {
try {
Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
InputValidator.validateTriggerInterpreter(triggerInterpreter, trigger);
progressCallback.accept("Generating " + template.getId(), Math.round(1 / (float) templatesToBeGenerated.size() * 800));
generate(template, triggerInterpreter, origToTmpFileTrace, progressCallback);
} catch (CobiGenCancellationException e) {
throw (e);
} catch (CobiGenRuntimeException e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.generationReport.addError(e);
} catch (Throwable e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.generationReport.addError(new CobiGenRuntimeException("Something unexpected happened" + ((e.getMessage() != null) ? ": " + e.getMessage() : "!"), e));
}
}
} catch (CobiGenCancellationException e) {
LOG.error("the Generation has been Canceled.", e);
this.generationReport.setCancelled(true);
}
if (this.generationReport.isCancelled()) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
this.tmpTargetRootPath.toFile().deleteOnExit();
// do nothing if cancelled
} else if (this.generationReport.isSuccessful()) {
try {
for (Entry<File, File> origToTmpFile : origToTmpFileTrace.entrySet()) {
Files.createDirectories(origToTmpFile.getKey().toPath().getParent());
Files.copy(origToTmpFile.getValue().toPath(), origToTmpFile.getKey().toPath(), StandardCopyOption.REPLACE_EXISTING);
this.generationReport.addGeneratedFile(origToTmpFile.getKey().toPath());
}
this.tmpTargetRootPath.toFile().deleteOnExit();
} catch (IOException e) {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
throw new CobiGenRuntimeException("Could not copy generated files to target location!", e);
}
} else {
this.generationReport.setTemporaryWorkingDirectory(this.tmpTargetRootPath);
LOG.warn("Generation finished non-successful. Generated contents can be reviewed in " + this.tmpTargetRootPath.toUri());
}
return this.generationReport;
}
use of com.devonfw.cobigen.impl.config.entity.Trigger 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.Trigger 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");
}
use of com.devonfw.cobigen.impl.config.entity.Trigger in project cobigen by devonfw.
the class TemplatesConfigurationReaderTest method testInvalidTemplateRefOutsideCurrentFile.
/**
* Tests the correct detection of invalid external increment reference.
*
* @throws InvalidConfigurationException expected
*/
@Test(expected = InvalidConfigurationException.class)
public void testInvalidTemplateRefOutsideCurrentFile() {
Path config = Paths.get(new File(testFileRootPath).toURI());
new ContextConfigurationReader(config);
// given
ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "faulty_invalid_external_templateref", configurationHolder);
Trigger trigger = new Trigger("", "asdf", "", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
// when
Map<String, Template> templates = target.loadTemplates(trigger);
target.loadIncrements(templates, trigger);
}
Aggregations