aboutsummaryrefslogtreecommitdiff
path: root/src/gradlecomp/java/com/amadornes/artifactural/gradle/DependencyResolver.java
blob: 9040b6380c4c27910e9fd6dedc53e3ca94352dc8 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Artifactural
 * Copyright (c) 2018.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation version 2.1
 * of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.amadornes.artifactural.gradle;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ClientModule;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyArtifact;
import org.gradle.api.artifacts.FileCollectionDependency;
import org.gradle.api.artifacts.ModuleDependency;

import java.io.File;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class DependencyResolver {

    private final Project project;
    private final AtomicInteger counter = new AtomicInteger(0);
    private final Cache<String, CompletableFuture<Set<File>>> resolved = CacheBuilder.newBuilder().expireAfterWrite(30, TimeUnit.MINUTES).build();

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

    /**
     * Resolves a dependency, downloading the file and its transitives
     * if not cached and returns the set of files.
     */
    public Set<File> resolveDependency(Dependency dependency) {
        if (dependency instanceof FileCollectionDependency) {
            return ((FileCollectionDependency) dependency).getFiles().getFiles();
        }
        String name = dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion();
        if (dependency instanceof ModuleDependency) {
            Set<DependencyArtifact> artifacts = ((ModuleDependency) dependency).getArtifacts();
            if (!artifacts.isEmpty()) {
                DependencyArtifact artifact = artifacts.iterator().next();
                name += ":" + artifact.getClassifier() + "@" + artifact.getExtension();
            }
        }

        // If this dep is being resolved on another thread, let it do it
        CompletableFuture<Set<File>> future;
        boolean found = true;
        synchronized (resolved) {
            future = resolved.getIfPresent(name);
            if (future == null) {
                resolved.put(name, future = new CompletableFuture<>());
                found = false;
            }
        }

        if (found) {
            try {
                return future.get();
            } catch (InterruptedException | ExecutionException ex) {
                throw new RuntimeException(ex);
            }
        }

        // No other thread is resolving this dep and we've claimed it, so let's go!
        int currentID = counter.getAndIncrement();
        Configuration cfg = project.getConfigurations().maybeCreate("resolve_dep_" + currentID);
        cfg.getDependencies().add(dependency);
        Set<File> files = cfg.resolve();
        project.getConfigurations().remove(cfg);
        future.complete(files);
        return files;
    }

    /**
     * Resolves a dependency, downloading the file and its transitives
     * if not cached and returns the set of files.
     */
    public Set<File> resolveDependency(Object dependency) {
        Dependency dep = project.getDependencies().create(dependency);
        return resolveDependency(dep);
    }

    /**
     * Resolves a dependency, downloading the file and its transitives
     * if not cached and returns the set of files.
     */
    public Set<File> resolveDependency(Object dependency, boolean transitive) {
        Dependency dep = project.getDependencies().create(dependency);
        if (dep instanceof ClientModule) {
            dep = ((ClientModule) dep).copy().setTransitive(transitive);
        }
        return resolveDependency(dep);
    }

    /**
     * Resolves a single dependency without any of its transitives
     * if not cached and returns the file.
     */
    public File resolveSingleDependency(Object dependency) {
        return resolveDependency(dependency, false).iterator().next();
    }

}