Search in sources :

Example 6 with Buildable

use of org.gradle.api.Buildable in project gradle by gradle.

the class ScalaRuntime method inferScalaClasspath.

/**
 * Searches the specified class path for a 'scala-library' Jar, and returns a class path
 * containing a corresponding (same version) 'scala-compiler' Jar and its dependencies.
 *
 * <p>The returned class path may be empty, or may fail to resolve when asked for its contents.
 *
 * @param classpath a class path containing a 'scala-library' Jar
 * @return a class path containing a corresponding 'scala-compiler' Jar and its dependencies
 */
public FileCollection inferScalaClasspath(final Iterable<File> classpath) {
    // would differ in the following ways: 1. live (not sure if we want live here) 2. no autowiring (probably want autowiring here)
    return new LazilyInitializedFileCollection() {

        @Override
        public String getDisplayName() {
            return "Scala runtime classpath";
        }

        @Override
        public FileCollection createDelegate() {
            try {
                return inferScalaClasspath();
            } catch (RuntimeException e) {
                return new FailingFileCollection(getDisplayName(), e);
            }
        }

        private Configuration inferScalaClasspath() {
            File scalaLibraryJar = findScalaJar(classpath, "library");
            File scala3LibraryJar = findScalaJar(classpath, "library_3");
            boolean isScala3 = scala3LibraryJar != null;
            if (scalaLibraryJar == null && scala3LibraryJar == null) {
                throw new GradleException(String.format("Cannot infer Scala class path because no Scala library Jar was found. " + "Does %s declare dependency to scala-library? Searched classpath: %s.", project, classpath));
            }
            String scalaVersion;
            if (isScala3) {
                scalaVersion = getScalaVersion(scala3LibraryJar);
            } else {
                scalaVersion = getScalaVersion(scalaLibraryJar);
            }
            if (scalaVersion == null) {
                throw new AssertionError(String.format("Unexpectedly failed to parse version of Scala Jar file: %s in %s", scalaLibraryJar, project));
            }
            String zincVersion = project.getExtensions().getByType(ScalaPluginExtension.class).getZincVersion().get();
            DefaultExternalModuleDependency compilerBridgeJar = getScalaBridgeDependency(scalaVersion, zincVersion);
            compilerBridgeJar.setTransitive(false);
            compilerBridgeJar.artifact(artifact -> {
                if (!isScala3) {
                    artifact.setClassifier("sources");
                }
                artifact.setType("jar");
                artifact.setExtension("jar");
                artifact.setName(compilerBridgeJar.getName());
            });
            DefaultExternalModuleDependency compilerInterfaceJar = getScalaCompilerInterfaceDependency(scalaVersion, zincVersion);
            Configuration scalaRuntimeClasspath = isScala3 ? project.getConfigurations().detachedConfiguration(getScalaCompilerDependency(scalaVersion), compilerBridgeJar, compilerInterfaceJar, getScaladocDependency(scalaVersion)) : project.getConfigurations().detachedConfiguration(getScalaCompilerDependency(scalaVersion), compilerBridgeJar, compilerInterfaceJar);
            jvmEcosystemUtilities.configureAsRuntimeClasspath(scalaRuntimeClasspath);
            return scalaRuntimeClasspath;
        }

        // let's override this so that delegate isn't created at autowiring time (which would mean on every build)
        @Override
        public void visitDependencies(TaskDependencyResolveContext context) {
            if (classpath instanceof Buildable) {
                context.add(classpath);
            }
        }
    };
}
Also used : FailingFileCollection(org.gradle.api.internal.file.collections.FailingFileCollection) Configuration(org.gradle.api.artifacts.Configuration) TaskDependencyResolveContext(org.gradle.api.internal.tasks.TaskDependencyResolveContext) DefaultExternalModuleDependency(org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency) GradleException(org.gradle.api.GradleException) LazilyInitializedFileCollection(org.gradle.api.internal.file.collections.LazilyInitializedFileCollection) File(java.io.File) Buildable(org.gradle.api.Buildable)

Example 7 with Buildable

use of org.gradle.api.Buildable in project gradle by gradle.

the class TaskDependencyMatchers method builtBy.

@Factory
public static <T extends Buildable> Matcher<T> builtBy(final Matcher<? extends Iterable<String>> matcher) {
    return new BaseMatcher<T>() {

        @Override
        public boolean matches(Object o) {
            Buildable task = (Buildable) o;
            Set<String> names = new HashSet<String>();
            Set<? extends Task> depTasks = task.getBuildDependencies().getDependencies(null);
            for (Task depTask : depTasks) {
                names.add(depTask.getName());
            }
            boolean matches = matcher.matches(names);
            if (!matches) {
                StringDescription description = new StringDescription();
                matcher.describeTo(description);
                System.out.println(String.format("expected %s, got %s.", description.toString(), names));
            }
            return matches;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("a Buildable that is built by ").appendDescriptionOf(matcher);
        }
    };
}
Also used : Task(org.gradle.api.Task) Buildable(org.gradle.api.Buildable) HashSet(java.util.HashSet)

Aggregations

Buildable (org.gradle.api.Buildable)7 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Callable (java.util.concurrent.Callable)3 Task (org.gradle.api.Task)3 Configuration (org.gradle.api.artifacts.Configuration)3 Closure (groovy.lang.Closure)2 Path (java.nio.file.Path)2 GradleException (org.gradle.api.GradleException)2 Dependency (org.gradle.api.artifacts.Dependency)2 FailingFileCollection (org.gradle.api.internal.file.collections.FailingFileCollection)2 LazilyInitializedFileCollection (org.gradle.api.internal.file.collections.LazilyInitializedFileCollection)2 TaskDependencyResolveContext (org.gradle.api.internal.tasks.TaskDependencyResolveContext)2 TaskDependency (org.gradle.api.tasks.TaskDependency)2 EtaDependency (com.typelead.gradle.eta.api.EtaDependency)1 EtaProjectDependency (com.typelead.gradle.eta.api.EtaProjectDependency)1 ConfigurationUtils (com.typelead.gradle.eta.internal.ConfigurationUtils)1 DefaultEtaConfiguration (com.typelead.gradle.eta.internal.DefaultEtaConfiguration)1 EtaBasePlugin (com.typelead.gradle.eta.plugins.EtaBasePlugin)1