Search in sources :

Example 1 with IncrementTo

use of com.devonfw.cobigen.api.to.IncrementTo in project cobigen by devonfw.

the class CobiGenWrapper method getOverridingFiles.

/**
 * Returns project dependent paths of all resources which are marked to be overridable (just of
 * {@link #getCurrentRepresentingInput()})
 *
 * @param increments to be considered
 * @return The set of all overridable project dependent file paths
 */
public Set<String> getOverridingFiles(Collection<IncrementTo> increments) {
    if (increments.contains(new ComparableIncrement(ALL_INCREMENT_ID, ALL_INCREMENT_NAME, null, Lists.<TemplateTo>newLinkedList(), Lists.<IncrementTo>newLinkedList()))) {
        increments = this.cobiGen.getMatchingIncrements(getCurrentRepresentingInput());
    }
    Set<String> overridablePaths = Sets.newHashSet();
    Map<String, Set<TemplateTo>> templateDestinationPaths = getTemplateDestinationPaths(increments);
    for (Entry<String, Set<TemplateTo>> entry : templateDestinationPaths.entrySet()) {
        if (isOverridableFile(entry.getValue())) {
            overridablePaths.add(entry.getKey());
        }
    }
    return overridablePaths;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) HashSet(java.util.HashSet) Set(java.util.Set) ComparableIncrement(com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 2 with IncrementTo

use of com.devonfw.cobigen.api.to.IncrementTo in project cobigen by devonfw.

the class GenerationProcessorImpl method flatten.

/**
 * Flattens the {@link GenerableArtifact}s to a list of {@link TemplateTo}s also removing duplicates.
 *
 * @param generableArtifacts {@link List} of {@link GenerableArtifact}s to be flattened
 * @return {@link Collection} of collected {@link TemplateTo}s
 */
private Collection<TemplateTo> flatten(List<? extends GenerableArtifact> generableArtifacts) {
    // create Map to remove duplicates by ID
    Map<String, TemplateTo> templateIdToTemplateMap = Maps.newHashMap();
    for (GenerableArtifact artifact : generableArtifacts) {
        if (artifact instanceof TemplateTo) {
            TemplateTo template = (TemplateTo) artifact;
            checkAndAddToTemplateMap(templateIdToTemplateMap, template);
        } else if (artifact instanceof IncrementTo) {
            for (TemplateTo template : ((IncrementTo) artifact).getTemplates()) {
                checkAndAddToTemplateMap(templateIdToTemplateMap, template);
            }
        } else {
            throw new IllegalArgumentException("Unknown GenerableArtifact type '" + artifact.getClass().getCanonicalName() + "'.");
        }
    }
    return new TreeSet<>(templateIdToTemplateMap.values());
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) TreeSet(java.util.TreeSet) GenerableArtifact(com.devonfw.cobigen.api.to.GenerableArtifact) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 3 with IncrementTo

use of com.devonfw.cobigen.api.to.IncrementTo in project cobigen by devonfw.

the class ConfigurationInterpreterImpl method getMatchingIncrements.

@Cached
@Override
public List<IncrementTo> getMatchingIncrements(Object matcherInput) throws InvalidConfigurationException {
    LOG.debug("Matching increments requested.");
    List<IncrementTo> increments = Lists.newLinkedList();
    List<String> matchingTriggerIds = getMatchingTriggerIds(matcherInput);
    for (TemplatesConfiguration templatesConfiguration : getMatchingTemplatesConfigurations(matcherInput)) {
        increments.addAll(convertIncrements(templatesConfiguration.getAllGenerationPackages(), templatesConfiguration.getTrigger(), matchingTriggerIds));
    }
    LOG.debug("{} matching increments found.", increments.size());
    return increments;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) TemplatesConfiguration(com.devonfw.cobigen.impl.config.TemplatesConfiguration) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 4 with IncrementTo

use of com.devonfw.cobigen.api.to.IncrementTo in project cobigen by devonfw.

the class GenerateCommand method search.

/**
 * Search for generable artifacts (increments or templates) matching the user input. Generable artifacts similar to
 * the given search string or containing it are returned.
 *
 * @param <T> The type of generable artifacts
 * @param userInput the user's wished increment or template
 * @param matching all increments or templates that are valid to the input file(s)
 * @param c class type, specifies whether Templates or Increments should be preprocessed
 * @return Increments or templates matching the search string
 */
private <T extends GenerableArtifact> List<T> search(String userInput, List<T> matching, Class<?> c) {
    boolean isIncrements = c.getSimpleName().equals(IncrementTo.class.getSimpleName());
    Map<T, Double> scores = new HashMap<>();
    for (int i = 0; i < matching.size(); i++) {
        if (!isIncrements) {
            String description = ((TemplateTo) matching.get(i)).getId();
            JaccardDistance distance = new JaccardDistance();
            scores.put(matching.get(i), distance.apply(description.toUpperCase(), userInput.toUpperCase()));
        } else {
            String description = ((IncrementTo) matching.get(i)).getDescription();
            String id = ((IncrementTo) matching.get(i)).getId();
            JaccardDistance distance = new JaccardDistance();
            double descriptionDistance = distance.apply(description.toUpperCase(), userInput.toUpperCase());
            double idDistance = distance.apply(id.toUpperCase(), userInput.toUpperCase());
            scores.put(matching.get(i), Math.min(idDistance, descriptionDistance));
        }
    }
    Map<T, Double> sorted = scores.entrySet().stream().sorted(comparingByValue()).collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
    List<T> chosen = new ArrayList<>();
    for (T artifact : sorted.keySet()) {
        T tmp = isIncrements ? artifact : artifact;
        if (!isIncrements) {
            String description = ((TemplateTo) artifact).getId();
            if (description.toUpperCase().contains(userInput.toUpperCase()) || sorted.get(artifact) <= this.SELECTION_THRESHOLD) {
                chosen.add(tmp);
            }
        } else {
            String description = ((IncrementTo) artifact).getDescription();
            String id = ((IncrementTo) artifact).getId();
            if (description.equalsIgnoreCase(userInput) || id.equalsIgnoreCase(userInput)) {
                chosen.add(tmp);
                return chosen;
            }
            if ((description.toUpperCase().contains(userInput.toUpperCase()) || id.toUpperCase().contains(userInput.toUpperCase())) || sorted.get(artifact) <= this.SELECTION_THRESHOLD) {
                chosen.add(tmp);
            }
        }
    }
    return chosen;
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException) Parameters(picocli.CommandLine.Parameters) LoggerFactory(org.slf4j.LoggerFactory) Entry.comparingByValue(java.util.Map.Entry.comparingByValue) HashMap(java.util.HashMap) InputMismatchException(java.util.InputMismatchException) CobiGen(com.devonfw.cobigen.api.CobiGen) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) CobiGenCLI(com.devonfw.cobigen.cli.CobiGenCLI) URI(java.net.URI) FormatterException(com.google.googlejavaformat.java.FormatterException) Command(picocli.CommandLine.Command) Path(java.nio.file.Path) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) ConfigurationFinder(com.devonfw.cobigen.impl.util.ConfigurationFinder) UserAbortException(com.devonfw.cobigen.cli.exceptions.UserAbortException) ValidationUtils(com.devonfw.cobigen.cli.utils.ValidationUtils) Logger(org.slf4j.Logger) Tuple(com.devonfw.cobigen.api.util.Tuple) Files(java.nio.file.Files) MavenUtil(com.devonfw.cobigen.api.util.MavenUtil) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ParsingUtils(com.devonfw.cobigen.cli.utils.ParsingUtils) StandardCharsets(java.nio.charset.StandardCharsets) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenUtils(com.devonfw.cobigen.cli.utils.CobiGenUtils) List(java.util.List) Option(picocli.CommandLine.Option) MessagesConstants(com.devonfw.cobigen.cli.constants.MessagesConstants) Paths(java.nio.file.Paths) GenerableArtifact(com.devonfw.cobigen.api.to.GenerableArtifact) JaccardDistance(org.apache.commons.text.similarity.JaccardDistance) FileSystemUtil(com.devonfw.cobigen.impl.util.FileSystemUtil) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JaccardDistance(org.apache.commons.text.similarity.JaccardDistance) ArrayList(java.util.ArrayList) IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo)

Example 5 with IncrementTo

use of com.devonfw.cobigen.api.to.IncrementTo in project cobigen by devonfw.

the class GenerationTest method testGenerationWithExternalIncrements.

/**
 * Tests whether the generation of external increments works properly.
 *
 * @throws Exception test fails
 */
@Test
public void testGenerationWithExternalIncrements() throws Exception {
    // given
    Object input = PluginMockFactory.createSimpleJavaConfigurationMock();
    File folder = this.tmpFolder.newFolder("GenerationTest");
    File target = new File(folder, "generated.txt");
    FileUtils.write(target, "base");
    // when
    CobiGen cobigen = CobiGenFactory.create(new File(testFileRootPath + "externalIncrementsGeneration").toURI());
    List<TemplateTo> templates = cobigen.getMatchingTemplates(input);
    List<IncrementTo> increments = cobigen.getMatchingIncrements(input);
    List<String> triggersIds = cobigen.getMatchingTriggerIds(input);
    // assert
    IncrementTo externalIncrement = null;
    // we try to get an increment containing external increments
    for (IncrementTo inc : increments) {
        if (inc.getId().equals("3")) {
            externalIncrement = inc;
        }
    }
    assertThat(templates).hasSize(5);
    // We expect increment 3 to have an external increment 0 containing one template
    assertThat(externalIncrement).isNotNull();
    assertThat(externalIncrement.getDependentIncrements().get(0).getTemplates().size()).isEqualTo(1);
    // We expect two triggers, the main one and the external one
    assertThat(triggersIds.size()).isEqualTo(2);
}
Also used : IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) CobiGen(com.devonfw.cobigen.api.CobiGen) File(java.io.File) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) AbstractApiTest(com.devonfw.cobigen.systemtest.common.AbstractApiTest) Test(org.junit.Test)

Aggregations

IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)14 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)8 CobiGen (com.devonfw.cobigen.api.CobiGen)5 File (java.io.File)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 Test (org.junit.Test)4 GenerableArtifact (com.devonfw.cobigen.api.to.GenerableArtifact)3 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 ComparableIncrement (com.devonfw.cobigen.eclipse.generator.entity.ComparableIncrement)3 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)3 ArrayList (java.util.ArrayList)2 Cached (com.devonfw.cobigen.api.annotation.Cached)1 InputReaderException (com.devonfw.cobigen.api.exception.InputReaderException)1 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 MavenUtil (com.devonfw.cobigen.api.util.MavenUtil)1 Tuple (com.devonfw.cobigen.api.util.Tuple)1 CobiGenCLI (com.devonfw.cobigen.cli.CobiGenCLI)1 MessagesConstants (com.devonfw.cobigen.cli.constants.MessagesConstants)1 UserAbortException (com.devonfw.cobigen.cli.exceptions.UserAbortException)1