Search in sources :

Example 21 with Trigger

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

the class HealthCheckImpl method perform.

@Override
public HealthCheckReport perform(Path configurationPath) {
    // 1. Get configuration resources
    // determine expected template configurations to be defined
    ContextConfiguration contextConfiguration = new ContextConfiguration(configurationPath);
    List<String> expectedTemplatesConfigurations = Lists.newArrayList();
    Set<String> hasConfiguration = Sets.newHashSet();
    Set<String> isAccessible = Sets.newHashSet();
    Map<String, Path> upgradeableConfigurations = Maps.newHashMap();
    Set<String> upToDateConfigurations = Sets.newHashSet();
    Path pathForCobigenTemplates = null;
    for (Trigger t : contextConfiguration.getTriggers()) {
        expectedTemplatesConfigurations.add(t.getTemplateFolder());
    }
    // 2. Determine current state
    TemplateConfigurationUpgrader templateConfigurationUpgrader = new TemplateConfigurationUpgrader();
    pathForCobigenTemplates = configurationPath.resolve("src" + File.separator + "main" + File.separator + "templates");
    for (String expectedTemplateFolder : expectedTemplatesConfigurations) {
        if (Files.exists(pathForCobigenTemplates)) {
            String configPath = (configurationPath + File.separator + "src" + File.separator + "main" + File.separator + "templates").toString();
            hasConfiguration.add(configPath);
            isAccessible.add(configPath);
            Path expectedTemplateFolderForResolvedVer = pathForCobigenTemplates.resolve(expectedTemplateFolder.replace("/", File.separator).toString());
            TemplatesConfigurationVersion resolvedVersion = templateConfigurationUpgrader.resolveLatestCompatibleSchemaVersion(expectedTemplateFolderForResolvedVer);
            if (resolvedVersion != null) {
                if (resolvedVersion != TemplatesConfigurationVersion.getLatest()) {
                    upgradeableConfigurations.put(expectedTemplateFolder, pathForCobigenTemplates);
                } else {
                    upToDateConfigurations.add(expectedTemplateFolder);
                }
            }
        } else {
            Path templateFolder = configurationPath.resolve(expectedTemplateFolder);
            Path templatesConfigurationPath = templateFolder.resolve(ConfigurationConstants.TEMPLATES_CONFIG_FILENAME);
            File templatesConfigurationFile = templatesConfigurationPath.toFile();
            if (templatesConfigurationFile.exists()) {
                hasConfiguration.add(expectedTemplateFolder);
                if (templatesConfigurationFile.canWrite()) {
                    isAccessible.add(expectedTemplateFolder);
                    TemplatesConfigurationVersion resolvedVersion = templateConfigurationUpgrader.resolveLatestCompatibleSchemaVersion(templateFolder);
                    if (resolvedVersion != null) {
                        if (resolvedVersion != TemplatesConfigurationVersion.getLatest()) {
                            upgradeableConfigurations.put(expectedTemplateFolder, templateFolder);
                        } else {
                            upToDateConfigurations.add(expectedTemplateFolder);
                        }
                    }
                }
            }
        }
    }
    this.healthCheckReport.setExpectedTemplatesConfigurations(expectedTemplatesConfigurations);
    this.healthCheckReport.setHasConfiguration(hasConfiguration);
    this.healthCheckReport.setIsAccessible(isAccessible);
    this.healthCheckReport.setUpgradeableConfigurations(upgradeableConfigurations);
    this.healthCheckReport.setUpToDateConfigurations(upToDateConfigurations);
    return this.healthCheckReport;
}
Also used : Path(java.nio.file.Path) TemplateConfigurationUpgrader(com.devonfw.cobigen.impl.config.upgrade.TemplateConfigurationUpgrader) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContextConfiguration(com.devonfw.cobigen.impl.config.ContextConfiguration) TemplatesConfigurationVersion(com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion) File(java.io.File)

Example 22 with Trigger

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

the class TriggerMatchingEvaluatorImpl method getMatchingTriggers.

@Cached
@Override
public List<Trigger> getMatchingTriggers(Object matcherInput) {
    LOG.debug("Retrieve matching trigger. input {}, hash: {}", matcherInput, matcherInput.hashCode());
    List<Trigger> matchingTrigger = Lists.newLinkedList();
    for (Trigger trigger : this.configurationHolder.readContextConfiguration().getTriggers()) {
        TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
        if (triggerInterpreter == null) {
            continue;
        // trigger interpreter not yet activated as the plug-in was not yet used.
        // unfortunately the invariant here is, that the CobiGen user has once called CobigenImpl#read
        // to get the matcher input
        }
        InputValidator.validateTriggerInterpreter(triggerInterpreter, trigger);
        LOG.debug("Check {} to match the input.", trigger);
        if (triggerInterpreter.getInputReader().isValidInput(matcherInput)) {
            LOG.debug("Matcher input is marked as valid.");
            boolean triggerMatches = this.matcherEvaluator.matches(matcherInput, trigger.getMatcher(), triggerInterpreter);
            if (triggerMatches) {
                matchingTrigger.add(trigger);
            }
            // recognized as a container.
            if (!triggerMatches) {
                LOG.debug("Check container matchers ...");
                FOR_CONTAINERMATCHER: for (ContainerMatcher containerMatcher : trigger.getContainerMatchers()) {
                    MatcherTo containerMatcherTo = new MatcherTo(containerMatcher.getType(), containerMatcher.getValue(), matcherInput);
                    LOG.debug("Check {} ...", containerMatcherTo);
                    if (triggerInterpreter.getMatcher().matches(containerMatcherTo)) {
                        LOG.debug("Match! Retrieve objects from container ...", containerMatcherTo);
                        // keep backward-compatibility
                        List<Object> containerResources;
                        if (containerMatcher.isRetrieveObjectsRecursively()) {
                            containerResources = triggerInterpreter.getInputReader().getInputObjectsRecursively(matcherInput, Charsets.UTF_8);
                        } else {
                            // the charset does not matter as we just want to see whether there is one
                            // matcher for one of the container resources
                            containerResources = triggerInterpreter.getInputReader().getInputObjects(matcherInput, Charsets.UTF_8);
                        }
                        LOG.debug("{} objects retrieved.", containerResources.size());
                        // check if at least one container element matches the matcher declarations
                        for (Object resource : containerResources) {
                            if (this.matcherEvaluator.matches(resource, trigger.getMatcher(), triggerInterpreter)) {
                                LOG.debug("At least one object from container matches.");
                                triggerMatches = true;
                                break FOR_CONTAINERMATCHER;
                            }
                        }
                        LOG.debug("No element of the container is matched.");
                    }
                }
                if (triggerMatches) {
                    matchingTrigger.add(new Trigger(trigger, true));
                }
            }
            LOG.debug("{} {}", trigger, triggerMatches ? "matches." : "does not match.");
        }
    }
    return matchingTrigger;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) List(java.util.List) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 23 with Trigger

use of com.devonfw.cobigen.impl.config.entity.Trigger 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)

Example 24 with Trigger

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

use of com.devonfw.cobigen.impl.config.entity.Trigger 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)

Aggregations

Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)30 File (java.io.File)20 Template (com.devonfw.cobigen.impl.config.entity.Template)19 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)18 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)17 AbstractUnitTest (com.devonfw.cobigen.unittest.config.common.AbstractUnitTest)17 Test (org.junit.Test)17 TemplatesConfigurationReader (com.devonfw.cobigen.impl.config.reader.TemplatesConfigurationReader)15 Path (java.nio.file.Path)9 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)5 Increment (com.devonfw.cobigen.impl.config.entity.Increment)5 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)4 Cached (com.devonfw.cobigen.api.annotation.Cached)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)2 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)2 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)2