Search in sources :

Example 11 with Matcher

use of com.devonfw.cobigen.impl.config.entity.Matcher in project cobigen by devonfw.

the class MatcherEvaluatorImpl method matches.

@Cached
@Override
public boolean matches(Object matcherInput, List<Matcher> matcherList, TriggerInterpreter triggerInterpreter) {
    boolean matcherSetMatches = false;
    LOG.debug("Check matchers for TriggerInterpreter[type='{}'] ...", triggerInterpreter.getType());
    MATCHER_LOOP: for (Matcher matcher : matcherList) {
        MatcherTo matcherTo = new MatcherTo(matcher.getType(), matcher.getValue(), matcherInput);
        LOG.trace("Check {} ...", matcherTo);
        if (triggerInterpreter.getMatcher().matches(matcherTo)) {
            switch(matcher.getAccumulationType()) {
                case NOT:
                    LOG.trace("NOT Matcher matches -> trigger match fails.");
                    matcherSetMatches = false;
                    break MATCHER_LOOP;
                case OR:
                case AND:
                    LOG.trace("Matcher matches.");
                    matcherSetMatches = true;
                    break;
                default:
            }
        } else {
            if (matcher.getAccumulationType() == AccumulationType.AND) {
                LOG.trace("AND Matcher does not match -> trigger match fails.");
                matcherSetMatches = false;
                break MATCHER_LOOP;
            }
        }
    }
    LOG.debug("Matcher declarations " + (matcherSetMatches ? "match the input." : "do not match the input."));
    return matcherSetMatches;
}
Also used : Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 12 with Matcher

use of com.devonfw.cobigen.impl.config.entity.Matcher 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 13 with Matcher

use of com.devonfw.cobigen.impl.config.entity.Matcher in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testRelocate_overlappingTemplateExtensionAndScan.

/**
 * Tests the rewriting of the destination path of a scanned template by using the {@link TemplateExtension}
 * configuration element. The explicitly configured destination path from the configuration should have precedence
 * over the relocated path of the template scan.
 */
@Test
public void testRelocate_overlappingTemplateExtensionAndScan() {
    // given
    String templateScanDestinationPath = "src/main/java/";
    String templatesConfigurationRoot = testFileRootPath + "valid_relocate_templateExt_vs_scan/";
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate_templateExt_vs_scan/");
    Trigger trigger = new Trigger("id", "type", "valid_relocate", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    assertThat(templates).hasSize(2);
    // validation
    String staticRelocationPrefix = "../api/";
    String scanRelTemplatePath = "$_rootpackage_$/$_component_$/common/api/";
    Template template = verifyScannedTemplate(templates, "$_EntityName_$.java", scanRelTemplatePath, templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
    String templateName = "$_EntityName_$2.java";
    template = templates.get(templateName);
    assertThat(template).isNotNull();
    String pathWithName = scanRelTemplatePath + templateName;
    assertThat(template.getRelativeTemplatePath()).isEqualTo("templates/" + pathWithName);
    assertThat(template.getAbsoluteTemplatePath().toString().replace('\\', '/')).isEqualTo(templatesConfigurationRoot + "templates/" + pathWithName);
    assertThat(template.getUnresolvedTemplatePath()).isEqualTo(templateScanDestinationPath + scanRelTemplatePath + templateName);
    assertThat(template.getUnresolvedTargetPath()).isEqualTo(templateName);
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 14 with Matcher

use of com.devonfw.cobigen.impl.config.entity.Matcher in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testRelocate.

/**
 * Test the basic valid configuration of <a href="https://github.com/devonfw/cobigen/issues/157">issue 157</a> for
 * relocation of templates to support multi-module generation.
 */
@Test
public void testRelocate() {
    // given
    String noRelocation = "";
    String templateScanDestinationPath = "src/main/java/";
    String templatesConfigurationRoot = testFileRootPath + "valid_relocate/";
    TemplatesConfigurationReader target = new TemplatesConfigurationReader(new File(testFileRootPath).toPath(), "valid_relocate/");
    Trigger trigger = new Trigger("id", "type", "valid_relocate", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    // when
    Map<String, Template> templates = target.loadTemplates(trigger);
    // validation
    assertThat(templates).hasSize(3);
    String staticRelocationPrefix = "../api/";
    verifyScannedTemplate(templates, "$_EntityName_$Entity.java", "$_rootpackage_$/$_component_$/dataaccess/api/", templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
    staticRelocationPrefix = "../api2/";
    verifyScannedTemplate(templates, "$_EntityName_$Eto.java", "$_rootpackage_$/$_component_$/logic/api/to/", templatesConfigurationRoot, staticRelocationPrefix, templateScanDestinationPath);
    verifyScannedTemplate(templates, "$_Component_$.java", "$_rootpackage_$/$_component_$/logic/api/", templatesConfigurationRoot, noRelocation, templateScanDestinationPath);
}
Also used : Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) TemplatesConfigurationReader(com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader) File(java.io.File) Template(com.devonfw.cobigen.impl.config.entity.Template) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Example 15 with Matcher

use of com.devonfw.cobigen.impl.config.entity.Matcher in project cobigen by devonfw.

the class TemplatesConfigurationReaderTest method testIncrementRefOutsideCurrentFile.

/**
 * Tests the correct resolution of incrementsRef from outside the current templates file. (Issue #678)
 */
@Test
public void testIncrementRefOutsideCurrentFile() {
    // given
    Trigger trigger = new Trigger("testingTrigger", "asdf", "valid_external_incrementref", Charset.forName("UTF-8"), new LinkedList<Matcher>(), new LinkedList<ContainerMatcher>());
    Path config = Paths.get(new File(testFileRootPath).toURI());
    ConfigurationHolder configurationHolder = new ConfigurationHolder(config.toUri());
    TemplatesConfiguration templatesConfiguration = configurationHolder.readTemplatesConfiguration(trigger);
    Map<String, Increment> increments = templatesConfiguration.getIncrements();
    // validation
    assertThat(templatesConfiguration.getTrigger().getId()).isEqualTo("testingTrigger");
    assertThat(increments).containsOnlyKeys("3", "4", "5");
    Increment incrementThree = increments.get("3").getDependentIncrements().get(0);
    assertThat(incrementThree.getName()).isEqualTo("0");
    assertThat(incrementThree.getTemplates().size()).isEqualTo(1);
    Increment incrementFour = increments.get("4").getDependentIncrements().get(0);
    assertThat(incrementFour.getName()).isEqualTo("1");
    assertThat(incrementFour.getTemplates().size()).isEqualTo(4);
    Increment incrementFive = increments.get("5").getDependentIncrements().get(0);
    assertThat(incrementFive.getName()).isEqualTo("2");
    assertThat(incrementFive.getTemplates().size()).isEqualTo(4);
}
Also used : Path(java.nio.file.Path) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) Increment(com.devonfw.cobigen.impl.config.entity.Increment) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) File(java.io.File) TemplatesConfiguration(com.devonfw.cobigen.impl.config.TemplatesConfiguration) AbstractUnitTest(com.devonfw.cobigen.unittest.config.common.AbstractUnitTest) Test(org.junit.Test)

Aggregations

Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)19 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)17 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)17 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)17 File (java.io.File)17 Test (org.junit.Test)17 Template (com.devonfw.cobigen.impl.config.entity.Template)16 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)15 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)4 Increment (com.devonfw.cobigen.impl.config.entity.Increment)4 Path (java.nio.file.Path)4 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)2 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)2 ContextConfigurationReader (com.devonfw.cobigen.impl.config.reader.ContextConfigurationReader)2 Cached (com.devonfw.cobigen.api.annotation.Cached)1 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 Variables (com.devonfw.cobigen.impl.config.entity.Variables)1 PluginProcessingException (com.devonfw.cobigen.impl.exceptions.PluginProcessingException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1