org.gradle.configurationcache.serialization.codecs.LocalFileDependencyBackedArtifactSetCodec.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("DEPRECATION")
package org.gradle.configurationcache.serialization.codecs
import org.gradle.api.Action
import org.gradle.api.artifacts.FileCollectionDependency
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.transform.TransformAction
import org.gradle.api.artifacts.transform.TransformParameters
import org.gradle.api.artifacts.transform.VariantTransform
import org.gradle.api.artifacts.type.ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE
import org.gradle.api.attributes.Attribute
import org.gradle.api.attributes.AttributeContainer
import org.gradle.api.capabilities.CapabilitiesMetadata
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.CollectionCallbackActionDecorator
import org.gradle.api.internal.artifacts.ArtifactTransformRegistration
import org.gradle.api.internal.artifacts.VariantTransformRegistry
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.LocalFileDependencyBackedArtifactSet
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvableArtifact
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedArtifactSet
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariant
import org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ResolvedVariantSet
import org.gradle.api.internal.artifacts.transform.ArtifactTransformDependencies
import org.gradle.api.internal.artifacts.transform.DefaultArtifactTransformDependencies
import org.gradle.api.internal.artifacts.transform.ExtraExecutionGraphDependenciesResolverFactory
import org.gradle.api.internal.artifacts.transform.TransformUpstreamDependencies
import org.gradle.api.internal.artifacts.transform.TransformUpstreamDependenciesResolver
import org.gradle.api.internal.artifacts.transform.Transformation
import org.gradle.api.internal.artifacts.transform.TransformationStep
import org.gradle.api.internal.artifacts.transform.TransformedVariantFactory
import org.gradle.api.internal.artifacts.transform.VariantDefinition
import org.gradle.api.internal.artifacts.transform.VariantSelector
import org.gradle.api.internal.artifacts.type.DefaultArtifactTypeRegistry
import org.gradle.api.internal.attributes.AttributeContainerInternal
import org.gradle.api.internal.attributes.AttributesSchemaInternal
import org.gradle.api.internal.attributes.EmptySchema
import org.gradle.api.internal.attributes.ImmutableAttributes
import org.gradle.api.internal.attributes.ImmutableAttributesFactory
import org.gradle.api.internal.file.FileCollectionFactory
import org.gradle.api.internal.file.FileCollectionInternal
import org.gradle.api.internal.tasks.TaskDependencyResolveContext
import org.gradle.api.specs.Spec
import org.gradle.configurationcache.serialization.Codec
import org.gradle.configurationcache.serialization.ReadContext
import org.gradle.configurationcache.serialization.WriteContext
import org.gradle.configurationcache.serialization.decodePreservingSharedIdentity
import org.gradle.configurationcache.serialization.encodePreservingSharedIdentityOf
import org.gradle.configurationcache.serialization.readCollection
import org.gradle.configurationcache.serialization.readNonNull
import org.gradle.configurationcache.serialization.writeCollection
import org.gradle.internal.Describables
import org.gradle.internal.DisplayName
import org.gradle.internal.Try
import org.gradle.internal.component.external.model.ImmutableCapabilities
import org.gradle.internal.component.local.model.LocalFileDependencyMetadata
import org.gradle.internal.component.model.VariantResolveMetadata
import org.gradle.internal.model.CalculatedValueContainerFactory
import org.gradle.internal.reflect.Instantiator
class LocalFileDependencyBackedArtifactSetCodec(
private val instantiator: Instantiator,
private val attributesFactory: ImmutableAttributesFactory,
private val fileCollectionFactory: FileCollectionFactory,
private val calculatedValueContainerFactory: CalculatedValueContainerFactory
) : Codec {
override suspend fun WriteContext.encode(value: LocalFileDependencyBackedArtifactSet) {
// TODO - When the set of files is fixed (eg `gradleApi()` or some hard-coded list of files):
// - calculate the attributes for each of the files eagerly rather than writing the mappings
// - when the selector would not apply a transform, then write only the files and nothing else
// - otherwise, write only the transform and attributes for each file rather than writing the transform registry
val noRequestedAttributes = value.selector.requestedAttributes.isEmpty
writeBoolean(noRequestedAttributes)
write(value.dependencyMetadata.componentId)
write(value.dependencyMetadata.files)
write(value.componentFilter)
// Write the file extension -> attributes mappings
// TODO - move this to an encoder
encodePreservingSharedIdentityOf(value.artifactTypeRegistry) {
val mappings = value.artifactTypeRegistry.create()!!
writeCollection(mappings) {
writeString(it.name)
write(it.attributes)
}
}
if (!noRequestedAttributes) {
// Write the file extension -> transformation mappings
// This currently uses a dummy set of variants to calculate the mappings.
// Do not write this if it will not be used
// TODO - simplify extracting the mappings
// TODO - deduplicate this data, as the mapping is project scoped and almost always the same across all projects of a given type
val matchingOnArtifactFormat = value.selector.requestedAttributes.keySet().contains(ARTIFACT_TYPE_ATTRIBUTE)
writeBoolean(matchingOnArtifactFormat)
val mappings = mutableMapOf()
value.artifactTypeRegistry.visitArtifactTypes { sourceAttributes ->
val recordingSet = RecordingVariantSet(value.dependencyMetadata.files, sourceAttributes)
val selected = value.selector.select(recordingSet, recordingSet)
if (selected == ResolvedArtifactSet.EMPTY) {
// Don't need to record the mapping
} else if (recordingSet.targetAttributes != null) {
mappings[sourceAttributes] = TransformMapping(recordingSet.targetAttributes!!, recordingSet.transformation!!)
} else {
mappings[sourceAttributes] = IdentityMapping
}
}
write(mappings)
}
}
override suspend fun ReadContext.decode(): LocalFileDependencyBackedArtifactSet {
val noRequestedAttributes = readBoolean()
val componentId = read() as ComponentIdentifier?
val files = readNonNull()
val filter = readNonNull>()
// TODO - use an immutable registry implementation
val artifactTypeRegistry = decodePreservingSharedIdentity {
val registry = DefaultArtifactTypeRegistry(instantiator, attributesFactory, CollectionCallbackActionDecorator.NOOP, EmptyVariantTransformRegistry)
val mappings = registry.create()!!
readCollection {
val name = readString()
val attributes = readNonNull()
val mapping = mappings.create(name).attributes
@Suppress("UNCHECKED_CAST")
for (attribute in attributes.keySet() as Set>) {
mapping.attribute(attribute, attributes.getAttribute(attribute) as Any)
}
}
registry
}
val selector = if (noRequestedAttributes) {
NoTransformsSelector()
} else {
val matchingOnArtifactFormat = readBoolean()
val transforms = readNonNull
© 2015 - 2025 Weber Informatics LLC | Privacy Policy