use of com.devonfw.cobigen.impl.config.entity.Variables in project cobigen by devonfw.
the class ModelBuilderImpl method enrichByContextVariables.
/**
* Enriches the model by the context variables of the trigger.
*
* @param model to be enriched
* @param triggerInterpreter {@link TriggerInterpreter} to resolve the variables
* @param template the internal {@link Template} representation
* @param targetRootPath root path template destinations should be resolved against
* @param report is getting filled as side-effect
* @return the adapted model reference.
*/
public Map<String, Object> enrichByContextVariables(Map<String, Object> model, TriggerInterpreter triggerInterpreter, Template template, Path targetRootPath, GenerationReportTo report) {
Map<String, String> variables = Maps.newHashMap();
Map<String, String> contextVariables = new ContextVariableResolver(this.generatorInput, this.trigger).resolveVariables(triggerInterpreter, report).asMap();
Map<String, String> templateProperties = template.getVariables().asMap();
Properties targetCobiGenProperties = CobiGenPropertiesReader.load(targetRootPath);
// if there are properties overriding each other, throw an exception for better usability.
// This is most probably a not intended mechanism such that we simply will not support it.
Set<String> intersection = new HashSet<>(contextVariables.keySet());
intersection.retainAll(templateProperties.keySet());
Set<String> intersection2 = new HashSet<>(contextVariables.keySet());
intersection2.retainAll(targetCobiGenProperties.keySet());
if (!intersection.isEmpty() || !intersection2.isEmpty()) {
throw new CobiGenRuntimeException("There are conflicting variables coming from the context configuration " + "as well as coming from the " + ConfigurationConstants.COBIGEN_PROPERTIES + " file. " + "This is most probably an unintended behavior and thus is not supported. The following variables are " + "declared twice (once in " + ConfigurationConstants.CONTEXT_CONFIG_FILENAME + " and once in " + ConfigurationConstants.COBIGEN_PROPERTIES + " file): " + Arrays.toString(intersection.toArray()));
}
variables.putAll(contextVariables);
variables.putAll(templateProperties);
variables.putAll(new Variables(targetCobiGenProperties).asMap());
model.put(NS_VARIABLES, variables);
return model;
}
use of com.devonfw.cobigen.impl.config.entity.Variables in project cobigen by devonfw.
the class ContextVariableResolver method resolveVariables.
/**
* Resolves all {@link VariableAssignment}s by using the given {@link TriggerInterpreter}
*
* @param triggerInterpreter to be used
* @param report is getting filled as side-effect
* @param parent the parent {@link Variables} to inherit.
* @return the mapping of variable to value
* @throws InvalidConfigurationException if there are {@link VariableAssignment}s, which could not be resolved
*/
public Variables resolveVariables(TriggerInterpreter triggerInterpreter, Variables parent, GenerationReportTo report) throws InvalidConfigurationException {
Variables variables = new Variables(parent);
for (Matcher m : this.trigger.getMatcher()) {
MatcherTo matcherTo = new MatcherTo(m.getType(), m.getValue(), this.input);
if (triggerInterpreter.getMatcher().matches(matcherTo)) {
Map<String, String> resolvedVariables;
try {
resolvedVariables = triggerInterpreter.getMatcher().resolveVariables(matcherTo, getVariableAssignments(m), report);
} catch (InvalidConfigurationException e) {
throw e;
} catch (Throwable e) {
throw new PluginProcessingException(e);
}
InputValidator.validateResolvedVariables(resolvedVariables);
variables.putAll(resolvedVariables);
}
}
return variables;
}
use of com.devonfw.cobigen.impl.config.entity.Variables in project cobigen by devonfw.
the class ConfigurationInterpreterImpl method resolveTemplateDestinationPath.
@Override
public Path resolveTemplateDestinationPath(Path targetRootPath, TemplateTo template, Object input) {
Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
InputValidator.validateTrigger(trigger);
TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
// the GenerationReportTo won't be further processed
Variables variables = new ContextVariableResolver(input, trigger).resolveVariables(triggerInterpreter, new GenerationReportTo());
Template templateEty = this.configurationHolder.readTemplatesConfiguration(trigger).getTemplate(template.getId());
try {
String resolvedDestinationPath = new PathExpressionResolver(variables).evaluateExpressions(templateEty.getUnresolvedTargetPath());
return targetRootPath.resolve(resolvedDestinationPath).normalize();
} catch (UnknownContextVariableException e) {
throw new CobiGenRuntimeException("Could not resolve path '" + templateEty.getUnresolvedTargetPath() + "' for input '" + (input instanceof Object[] ? Arrays.toString((Object[]) input) : input.toString()) + "' and template '" + templateEty.getAbsoluteTemplatePath() + "'. Available variables: " + variables.toString());
}
}
Aggregations