Search in sources :

Example 1 with FailingFileCollection

use of org.gradle.api.internal.file.collections.FailingFileCollection 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 2 with FailingFileCollection

use of org.gradle.api.internal.file.collections.FailingFileCollection in project gradle by gradle.

the class GroovyRuntime method inferGroovyClasspath.

/**
 * Searches the specified class path for Groovy Jars ({@code groovy(-indy)}, {@code groovy-all(-indy)}) and returns a corresponding class path for executing Groovy tools such as the Groovy
 * compiler and Groovydoc tool. The tool versions will match those of the Groovy Jars found. If no Groovy Jars are found on the specified class path, a class path with the contents of the {@code
 * groovy} configuration will be returned.
 *
 * <p>The returned class path may be empty, or may fail to resolve when asked for its contents.
 *
 * @param classpath a class path containing Groovy Jars
 * @return a corresponding class path for executing Groovy tools such as the Groovy compiler and Groovydoc tool
 */
public FileCollection inferGroovyClasspath(final Iterable<File> classpath) {
    // would differ in at least the following ways: 1. live 2. no autowiring
    return new LazilyInitializedFileCollection() {

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

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

        private FileCollection inferGroovyClasspath() {
            GroovyJarFile groovyJar = findGroovyJarFile(classpath);
            if (groovyJar == null) {
                throw new GradleException(String.format("Cannot infer Groovy class path because no Groovy Jar was found on class path: %s", Iterables.toString(classpath)));
            }
            if (groovyJar.isGroovyAll()) {
                return project.getLayout().files(groovyJar.getFile());
            }
            VersionNumber groovyVersion = groovyJar.getVersion();
            // Groovy 3 does not have groovy-all yet we may have the required pieces on classpath via localGroovy()
            if (groovyVersion.getMajor() == 3) {
                return inferGroovy3Classpath(groovyVersion);
            }
            String notation = groovyJar.getDependencyNotation();
            List<Dependency> dependencies = new ArrayList<>();
            addDependencyTo(dependencies, notation);
            if (groovyVersion.compareTo(GROOVY_VERSION_WITH_SEPARATE_ANT) >= 0) {
                // add groovy-ant to bring in Groovydoc for Groovy 2.0+
                addGroovyDependency(notation, dependencies, "groovy-ant");
            }
            if (groovyVersion.compareTo(GROOVY_VERSION_REQUIRING_TEMPLATES) >= 0) {
                // add groovy-templates for Groovy 2.5+
                addGroovyDependency(notation, dependencies, "groovy-templates");
            }
            return detachedRuntimeClasspath(dependencies.toArray(new Dependency[0]));
        }

        private void addGroovyDependency(String groovyDependencyNotion, List<Dependency> dependencies, String otherDependency) {
            String notation = groovyDependencyNotion.replace(":groovy:", ":" + otherDependency + ":");
            addDependencyTo(dependencies, notation);
        }

        private void addDependencyTo(List<Dependency> dependencies, String notation) {
            // project.getDependencies().create(String) seems to be the only feasible way to create a Dependency with a classifier
            dependencies.add(project.getDependencies().create(notation));
        }

        private FileCollection inferGroovy3Classpath(VersionNumber groovyVersion) {
            Set<String> groovyJarNames = groovyJarNamesFor(groovyVersion);
            List<File> groovyClasspath = collectJarsFromClasspath(classpath, groovyJarNames);
            if (groovyClasspath.size() == GROOVY3_LIBS.size()) {
                return project.getLayout().files(groovyClasspath);
            }
            return detachedRuntimeClasspath(GROOVY3_LIBS.stream().map(libName -> project.getDependencies().create("org.codehaus.groovy:" + libName + ":" + groovyVersion)).toArray(Dependency[]::new));
        }

        private Configuration detachedRuntimeClasspath(Dependency... dependencies) {
            Configuration classpath = project.getConfigurations().detachedConfiguration(dependencies);
            jvmEcosystemUtilities().configureAsRuntimeClasspath(classpath);
            return classpath;
        }

        // 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 : GroovyJarFile(org.gradle.api.internal.plugins.GroovyJarFile) Configuration(org.gradle.api.artifacts.Configuration) TaskDependencyResolveContext(org.gradle.api.internal.tasks.TaskDependencyResolveContext) ArrayList(java.util.ArrayList) LazilyInitializedFileCollection(org.gradle.api.internal.file.collections.LazilyInitializedFileCollection) Dependency(org.gradle.api.artifacts.Dependency) VersionNumber(org.gradle.util.internal.VersionNumber) FailingFileCollection(org.gradle.api.internal.file.collections.FailingFileCollection) GradleException(org.gradle.api.GradleException) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) GroovyJarFile(org.gradle.api.internal.plugins.GroovyJarFile) File(java.io.File) Buildable(org.gradle.api.Buildable)

Aggregations

File (java.io.File)2 Buildable (org.gradle.api.Buildable)2 GradleException (org.gradle.api.GradleException)2 Configuration (org.gradle.api.artifacts.Configuration)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 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 Dependency (org.gradle.api.artifacts.Dependency)1 DefaultExternalModuleDependency (org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency)1 GroovyJarFile (org.gradle.api.internal.plugins.GroovyJarFile)1 VersionNumber (org.gradle.util.internal.VersionNumber)1