aboutsummaryrefslogtreecommitdiff
path: root/src/main/groovy/frege/gradle/plugins/FregeRuntime.java
blob: 4265d0b6a9952e119250e5020f761a236c203e58 (plain)
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
54
55
56
57
58
59
60
61
62
63
package frege.gradle.plugins;

import com.google.common.collect.Lists;
import org.gradle.api.Buildable;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.collections.LazilyInitializedFileCollection;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;

import java.io.File;
import java.util.List;

public class FregeRuntime {

    private final Project project;

    public FregeRuntime(Project project) {
        this.project = project;
    }


    public FileCollection inferFregeClasspath(final Iterable<File> classpath) {
        return new LazilyInitializedFileCollection() {
            public String getDisplayName() {
                return "Frege runtime classpath";
            }

            public FileCollection createDelegate() {
                final FregeJarFile fregeJar = FregeRuntime.this.findFregeJarFile(classpath);
                if (fregeJar == null) {
                    throw new GradleException(String.format("Cannot infer Frege class path because no Frege Jar was found on class path: %s", classpath));
                }
                String notation = fregeJar.getDependencyNotation();
                List<Dependency> dependencies = Lists.newArrayList();
                dependencies.add(project.getDependencies().create(notation));
                return project.getConfigurations().detachedConfiguration(dependencies.toArray(new Dependency[dependencies.size()]));
            }

            public void visitDependencies(TaskDependencyResolveContext context) {
                if (classpath instanceof Buildable) {
                    context.add(classpath);
                }
            }

        };
    }

    private FregeJarFile findFregeJarFile(Iterable<File> classpath) {
        if (classpath == null) {
            return null;
        }
        for (File file : classpath) {
            FregeJarFile fregeJar = FregeJarFile.parse(file);
            if (fregeJar != null) {
                return fregeJar;
            }
        }
        return null;
    }

}