Search in sources :

Example 1 with Buildable

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

the class BuildDependenciesOnlyFileCollectionResolveContext method add.

@Override
public FileCollectionResolveContext add(Object element) {
    // TODO - need to sync with DefaultFileCollectionResolveContext
    if (element instanceof Buildable) {
        taskContext.add(element);
    } else if (element instanceof Task) {
        taskContext.add(element);
    } else if (element instanceof TaskOutputs) {
        TaskOutputs outputs = (TaskOutputs) element;
        taskContext.add(outputs.getFiles());
    } else if (element instanceof RegularFileProperty || element instanceof DirectoryProperty) {
        taskContext.add(element);
    } else if (element instanceof Closure) {
        Closure closure = (Closure) element;
        Object closureResult = closure.call();
        if (closureResult != null) {
            add(closureResult);
        }
    } else if (element instanceof Callable) {
        Callable callable = (Callable) element;
        Object callableResult = uncheckedCall(callable);
        if (callableResult != null) {
            add(callableResult);
        }
    } else if (element instanceof Iterable && !(element instanceof Path)) {
        // Ignore Path
        Iterable<?> iterable = (Iterable) element;
        for (Object value : iterable) {
            add(value);
        }
    } else if (element instanceof Object[]) {
        Object[] array = (Object[]) element;
        for (Object value : array) {
            add(value);
        }
    }
    // Everything else assume has no dependencies
    return this;
}
Also used : Path(java.nio.file.Path) Task(org.gradle.api.Task) DirectoryProperty(org.gradle.api.file.DirectoryProperty) Closure(groovy.lang.Closure) RegularFileProperty(org.gradle.api.file.RegularFileProperty) TaskOutputs(org.gradle.api.tasks.TaskOutputs) Buildable(org.gradle.api.Buildable) Callable(java.util.concurrent.Callable)

Example 2 with Buildable

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

the class ArtifactTransformingVisitor method visitArtifact.

@Override
public void visitArtifact(String variantName, AttributeContainer variantAttributes, ResolvableArtifact artifact) {
    TransformArtifactOperation operation = artifactResults.get(artifact);
    if (operation.getFailure() != null) {
        visitor.visitFailure(operation.getFailure());
        return;
    }
    ResolvedArtifact sourceArtifact = artifact.toPublicView();
    List<File> transformedFiles = operation.getResult();
    TaskDependency buildDependencies = ((Buildable) artifact).getBuildDependencies();
    for (File output : transformedFiles) {
        IvyArtifactName artifactName = DefaultIvyArtifactName.forFile(output, sourceArtifact.getClassifier());
        ComponentArtifactIdentifier newId = new ComponentFileArtifactIdentifier(sourceArtifact.getId().getComponentIdentifier(), artifactName);
        DefaultResolvedArtifact resolvedArtifact = new DefaultResolvedArtifact(sourceArtifact.getModuleVersion().getId(), artifactName, newId, buildDependencies, output);
        visitor.visitArtifact(variantName, target, resolvedArtifact);
    }
}
Also used : TaskDependency(org.gradle.api.tasks.TaskDependency) ComponentArtifactIdentifier(org.gradle.api.artifacts.component.ComponentArtifactIdentifier) DefaultResolvedArtifact(org.gradle.api.internal.artifacts.DefaultResolvedArtifact) ResolvedArtifact(org.gradle.api.artifacts.ResolvedArtifact) DefaultResolvedArtifact(org.gradle.api.internal.artifacts.DefaultResolvedArtifact) ComponentFileArtifactIdentifier(org.gradle.internal.component.local.model.ComponentFileArtifactIdentifier) DefaultIvyArtifactName(org.gradle.internal.component.model.DefaultIvyArtifactName) IvyArtifactName(org.gradle.internal.component.model.IvyArtifactName) File(java.io.File) Buildable(org.gradle.api.Buildable)

Example 3 with Buildable

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

the class DefaultTaskDependency method visitDependencies.

@Override
public void visitDependencies(final TaskDependencyResolveContext context) {
    Set<Object> mutableValues = getMutableValues();
    if (mutableValues.isEmpty() && immutableValues.isEmpty()) {
        return;
    }
    final Deque<Object> queue = new ArrayDeque<Object>(mutableValues.size() + immutableValues.size());
    queue.addAll(immutableValues);
    queue.addAll(mutableValues);
    while (!queue.isEmpty()) {
        Object dependency = queue.removeFirst();
        if (dependency instanceof Buildable) {
            context.add(dependency);
        } else if (dependency instanceof Task) {
            context.add(dependency);
        } else if (dependency instanceof TaskDependency) {
            context.add(dependency);
        } else if (dependency instanceof ProviderInternal) {
            // When a Provider is used as a task dependency (rather than as a task input), need to unpack the value
            ProviderInternal<?> provider = (ProviderInternal<?>) dependency;
            ValueSupplier.ValueProducer producer = provider.getProducer();
            if (producer.isKnown()) {
                producer.visitProducerTasks(context);
            } else {
                // The provider does not know how to produce the value, so use the value instead
                queue.addFirst(provider.get());
            }
        } else if (dependency instanceof TaskDependencyContainer) {
            ((TaskDependencyContainer) dependency).visitDependencies(context);
        } else if (dependency instanceof Closure) {
            Closure closure = (Closure) dependency;
            Object closureResult = closure.call(context.getTask());
            if (closureResult != null) {
                queue.addFirst(closureResult);
            }
        } else if (dependency instanceof List) {
            List<?> list = (List) dependency;
            if (list instanceof RandomAccess) {
                for (int i = list.size() - 1; i >= 0; i--) {
                    queue.addFirst(list.get(i));
                }
            } else {
                ListIterator<?> iterator = list.listIterator(list.size());
                while (iterator.hasPrevious()) {
                    Object item = iterator.previous();
                    queue.addFirst(item);
                }
            }
        } else if (dependency instanceof Iterable && !(dependency instanceof Path)) {
            // Path is Iterable, but we don't want to unpack it
            Iterable<?> iterable = Cast.uncheckedNonnullCast(dependency);
            addAllFirst(queue, toArray(iterable, Object.class));
        } else if (dependency instanceof Map) {
            Map<?, ?> map = Cast.uncheckedNonnullCast(dependency);
            addAllFirst(queue, map.values().toArray());
        } else if (dependency instanceof Object[]) {
            Object[] array = (Object[]) dependency;
            addAllFirst(queue, array);
        } else if (dependency instanceof Callable) {
            Callable<?> callable = Cast.uncheckedNonnullCast(dependency);
            Object callableResult = uncheckedCall(Cast.uncheckedNonnullCast(callable));
            if (callableResult != null) {
                queue.addFirst(callableResult);
            }
        } else if (resolver != null && dependency instanceof CharSequence) {
            context.add(resolver.resolveTask(dependency.toString()));
        } else {
            List<String> formats = new ArrayList<String>();
            if (resolver != null) {
                formats.add("A String or CharSequence task name or path");
            }
            formats.add("A Task instance");
            formats.add("A TaskReference instance");
            formats.add("A Buildable instance");
            formats.add("A TaskDependency instance");
            formats.add("A Provider that represents a task output");
            formats.add("A Provider instance that returns any of these types");
            formats.add("A Closure instance that returns any of these types");
            formats.add("A Callable instance that returns any of these types");
            formats.add("An Iterable, Collection, Map or array instance that contains any of these types");
            throw new UnsupportedNotationException(dependency, String.format("Cannot convert %s to a task.", dependency), null, formats);
        }
    }
}
Also used : TaskDependency(org.gradle.api.tasks.TaskDependency) Task(org.gradle.api.Task) Closure(groovy.lang.Closure) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) List(java.util.List) Buildable(org.gradle.api.Buildable) Path(java.nio.file.Path) RandomAccess(java.util.RandomAccess) ArrayDeque(java.util.ArrayDeque) ProviderInternal(org.gradle.api.internal.provider.ProviderInternal) UnsupportedNotationException(org.gradle.internal.typeconversion.UnsupportedNotationException) Map(java.util.Map)

Example 4 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 5 with Buildable

use of org.gradle.api.Buildable 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

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