Search in sources :

Example 1 with Tuple

use of com.devonfw.cobigen.api.util.Tuple in project cobigen by devonfw.

the class GenerateCommand method preprocess.

/**
 * For each input file it is going to get its matching templates or increments and then performs an intersection
 * between all of them, so that the user gets only the templates or increments that will work
 *
 * @param <T> type of generable artifacts to be pre-processed
 *
 * @param cg CobiGen initialized instance
 * @param c class type, specifies whether Templates or Increments should be preprocessed
 * @return List of templates that the user will be able to use
 */
@SuppressWarnings("unchecked")
private <T extends GenerableArtifact> Tuple<List<Object>, List<T>> preprocess(CobiGen cg, Class<T> c) {
    boolean isIncrements = c.getSimpleName().equals(IncrementTo.class.getSimpleName());
    boolean firstIteration = true;
    List<T> finalTos = new ArrayList<>();
    List<Object> generationInputs = new ArrayList<>();
    for (Path inputFile : this.inputFiles) {
        String extension = inputFile.getFileName().toString().toLowerCase();
        boolean isJavaInput = extension.endsWith(".java");
        boolean isOpenApiInput = extension.endsWith(".yaml") || extension.endsWith(".yml");
        // checks for output root path and project root being detectable
        if (this.outputRootPath == null && MavenUtil.getProjectRoot(inputFile, false) == null) {
            LOG.info("Did not detect the input as part of a maven project, the root directory of the maven project was not found.");
            LOG.info("Would you like to take '{}' as a root directory for output generation? \n" + "type yes/y to continue or no/n to cancel (or hit return for yes).", System.getProperty("user.dir"));
            setRootOutputDirectoryWithPrompt();
        }
        try {
            Object input = cg.read(inputFile, StandardCharsets.UTF_8);
            List<T> matching = (List<T>) (isIncrements ? cg.getMatchingIncrements(input) : cg.getMatchingTemplates(input));
            if (matching.isEmpty()) {
                ValidationUtils.throwNoTriggersMatched(inputFile, isJavaInput, isOpenApiInput);
            }
            if (firstIteration) {
                finalTos = matching;
                firstIteration = false;
            } else {
                // We do the intersection between the previous increments and the new ones
                finalTos = (List<T>) (isIncrements ? CobiGenUtils.retainAllIncrements(toIncrementTo(finalTos), toIncrementTo(matching)) : CobiGenUtils.retainAllTemplates(toTemplateTo(finalTos), toTemplateTo(matching)));
            }
            generationInputs.add(input);
        } catch (InputReaderException e) {
            LOG.error("Invalid input for CobiGen, please check your input file '{}'", inputFile.toString());
        }
    }
    if (finalTos.isEmpty()) {
        LOG.error("There are no common Templates/Increments which could be generated from every of your inputs. Please think about executing generation one by one input file.");
        throw new InputMismatchException("No compatible input files.");
    }
    List<T> selectedGenerableArtifacts = (List<T>) (isIncrements ? generableArtifactSelection(this.increments, toIncrementTo(finalTos), IncrementTo.class) : generableArtifactSelection(this.templates, toTemplateTo(finalTos), TemplateTo.class));
    return new Tuple<>(generationInputs, selectedGenerableArtifacts);
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) InputMismatchException(java.util.InputMismatchException) InputReaderException(com.devonfw.cobigen.api.exception.InputReaderException) IncrementTo(com.devonfw.cobigen.api.to.IncrementTo) ArrayList(java.util.ArrayList) List(java.util.List) TemplateTo(com.devonfw.cobigen.api.to.TemplateTo) Tuple(com.devonfw.cobigen.api.util.Tuple)

Aggregations

InputReaderException (com.devonfw.cobigen.api.exception.InputReaderException)1 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 Tuple (com.devonfw.cobigen.api.util.Tuple)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 InputMismatchException (java.util.InputMismatchException)1 List (java.util.List)1