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;
}
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);
}
}
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);
}
}
}
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);
}
}
};
}
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);
}
}
};
}
Aggregations