Search in sources :

Example 1 with Variables

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;
}
Also used : Variables(com.devonfw.cobigen.impl.config.entity.Variables) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 2 with Variables

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;
}
Also used : Variables(com.devonfw.cobigen.impl.config.entity.Variables) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) PluginProcessingException(com.devonfw.cobigen.impl.exceptions.PluginProcessingException) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 3 with 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());
    }
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Variables(com.devonfw.cobigen.impl.config.entity.Variables) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) ContextVariableResolver(com.devonfw.cobigen.impl.model.ContextVariableResolver) PathExpressionResolver(com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException) Template(com.devonfw.cobigen.impl.config.entity.Template)

Aggregations

Variables (com.devonfw.cobigen.impl.config.entity.Variables)3 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)2 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)1 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)1 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)1 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 PathExpressionResolver (com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver)1 PluginProcessingException (com.devonfw.cobigen.impl.exceptions.PluginProcessingException)1 UnknownContextVariableException (com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException)1 ContextVariableResolver (com.devonfw.cobigen.impl.model.ContextVariableResolver)1 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1