me.xx2bab.polyfill.artifact.DefaultArtifactsRepository.kt Maven / Gradle / Ivy
package me.xx2bab.polyfill.artifact
import com.android.build.api.artifact.Artifact
import com.android.build.api.artifact.ArtifactKind
import com.android.build.api.variant.Variant
import com.android.build.api.variant.VariantExtension
import me.xx2bab.polyfill.*
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
/**
* For all artifacts management.
*/
abstract class DefaultArtifactsRepository(
private val project: Project,
private val variant: Variant
) : ArtifactsRepository, VariantExtension {
private val singleArtifactStorage = mutableMapOf, SingleArtifactContainer<*>>()
private val multipleArtifactStorage = mutableMapOf, MultipleArtifactContainer<*>>()
init {
val ext = project.extensions.getByType(PolyfillExtension::class.java)
ext.singleArtifactMap.forEach { (artifactType, _) ->
if (artifactType.kind == ArtifactKind.FILE) {
singleArtifactStorage[artifactType] = SingleArtifactContainer(
artifactType, project, variant
)
} else if (artifactType.kind == ArtifactKind.DIRECTORY) {
singleArtifactStorage[artifactType] = SingleArtifactContainer(
artifactType, project, variant
)
}
}
ext.multipleArtifactMap.forEach { (artifactType, _) ->
if (artifactType.kind == ArtifactKind.FILE) {
multipleArtifactStorage[artifactType] = MultipleArtifactContainer(
artifactType, project, variant
)
} else if (artifactType.kind == ArtifactKind.DIRECTORY) {
multipleArtifactStorage[artifactType] = MultipleArtifactContainer(
artifactType, project, variant
)
}
}
}
override fun get(
type: PolyfilledSingleArtifact,
): Provider = getSingleArtifactContainer(type).get()
override fun get(
type: Artifact.Single
): Provider = variant.artifacts.toImplementation().get(type)
override fun getAll(
type: PolyfilledMultipleArtifact
): Provider> = getMultipleArtifactContainer(type).get()
override fun getAll(
type: Artifact.Multiple
): Provider> = variant.artifacts.toImplementation().getAll(type)
override fun use(
action: PolyfillAction,
toInPlaceUpdate: PolyfilledSingleArtifact
) {
getSingleArtifactContainer(toInPlaceUpdate).inPlaceUpdate(action)
}
override fun use(
action: PolyfillAction>,
toInPlaceUpdate: PolyfilledMultipleArtifact
) {
getMultipleArtifactContainer(toInPlaceUpdate).inPlaceUpdate(action)
}
@Suppress("UNCHECKED_CAST")
private fun getSingleArtifactContainer(
artifactType: PolyfilledSingleArtifact
): SingleArtifactContainer = singleArtifactStorage[artifactType] as SingleArtifactContainer
@Suppress("UNCHECKED_CAST")
private fun getMultipleArtifactContainer(
artifactType: PolyfilledMultipleArtifact
): MultipleArtifactContainer =
multipleArtifactStorage[artifactType] as MultipleArtifactContainer
}
class ApplicationArtifactsRepository(p: Project, v: Variant) :
DefaultArtifactsRepository(p, v)
class LibraryArtifactsRepository(p: Project, v: Variant) :
DefaultArtifactsRepository(p, v)