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;
}
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());
}
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;
}
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;
}
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);
}
Aggregations