name.remal.gradle_plugins.dsl.artifact.CachedArtifactsCollection.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Show all versions of gradle-plugins-kotlin-dsl Show documentation
Remal Gradle plugins: gradle-plugins-kotlin-dsl
package name.remal.gradle_plugins.dsl.artifact
import java.io.File
import java.io.InputStream
import java.util.TreeMap
import kotlin.collections.set
class CachedArtifactsCollection(val artifacts: Set) : BaseHasEntries() {
constructor(files: Iterable) : this(files.mapTo(mutableSetOf(), ArtifactsCache::get))
constructor(vararg files: Iterable) : this(files.toList().flatten())
constructor(vararg files: File) : this(files.toList())
constructor(file: File) : this(setOf(ArtifactsCache[file]))
fun getArtifactForEntry(entryName: String): Artifact? {
return artifacts.firstOrNull { entryName in it.entryNames }
}
val entryMapping: Map by lazy {
val result = sortedMapOf()
artifacts.forEach { artifact ->
artifact.entryNames.forEach {
if (it !in result) result[it] = artifact
}
}
return@lazy result.toMap()
}
override val entryNames: Set by lazy { entryMapping.keys }
override fun openStream(entryName: String): InputStream {
val artifact = entryMapping[entryName] ?: throw ArtifactEntryNotFoundException("Artifact entry not found: $entryName")
return artifact.openStream(entryName)
}
override fun forEachEntry(pattern: String?, action: (entry: HasEntries.Entry) -> Unit) {
artifacts.forEach { it.forEachEntry(pattern, action) }
}
override val annotationClassNames: Set by lazy {
sortedSetOf().also { result ->
artifacts.forEach { artifact ->
result.addAll(artifact.annotationClassNames)
}
}.toSet()
}
override val annotationsMapping: Map> by lazy {
mutableMapOf>().apply {
artifacts.forEach { artifact ->
artifact.annotationsMapping.forEach { annotationClassName, annotatedClassNames ->
computeIfAbsent(annotationClassName, { mutableSetOf() }).addAll(annotatedClassNames)
}
}
}.toMap()
}
override fun toString(): String {
return "${javaClass.simpleName}[$artifacts]"
}
}