aboutsummaryrefslogtreecommitdiffstats
path: root/config-class-plugin/src/main/java/com/yahoo/vespa/CloverChecker.java
blob: 5b9e1d9a408a9b7529284104349ccc9f03e2f976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Predicate;

/**
 * @author Tony Vaagenes
 */
class CloverChecker {
    private final Log log;

    CloverChecker(Log log) {
        this.log = log;
    }

    @SuppressWarnings("unchecked") //getCompileSourceRoots() returns List instead of List<String>
    public boolean isForkedCloverLifecycle(MavenProject project, Path outputDirectory) {
        return "clover".equals(project.getArtifact().getClassifier()) &&
                project.getCompileSourceRoots().stream().anyMatch(
                        equalsPathAbsolutely(regularOutputDirectory(project, outputDirectory)));
    }

    /*
     * Regular output directory for generated classes,
     * i.e. not the clover output directory.
     *
     * Example:
     * If outputDirectory is target/clover/generated-sources/vespa-configgen-plugin,
     * return target/generated-sources/vespa-configgen-plugin.
     */
    private Path regularOutputDirectory(MavenProject project, Path outputDirectory) {
        Path cloverTargetPath = Paths.get(project.getBuild().getDirectory());
        Path targetPath = cloverTargetPath.getParent();

        if (!targetPath.endsWith("target")) {
            log.warn("Guessing that target directory is " + targetPath + ", this might not be correct.");
        }

        Path outputDirectoryRelativeToCloverDirectory = cloverTargetPath.relativize(outputDirectory);
        return targetPath.resolve(outputDirectoryRelativeToCloverDirectory);
    }

    private Predicate<String> equalsPathAbsolutely(Path path) {
        Path absolutePath = path.toAbsolutePath();

        return candidateStringPath -> Paths.get(candidateStringPath).toAbsolutePath().equals(absolutePath);
    }
}