io.github.gmazzo.codeowners.CodeOwnersTransform.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jvm-plugin Show documentation
Show all versions of jvm-plugin Show documentation
CodeOwners JVM Gradle Plugin
package io.github.gmazzo.codeowners
import io.github.gmazzo.codeowners.matcher.CodeOwnersFile
import org.gradle.api.artifacts.transform.CacheableTransform
import org.gradle.api.artifacts.transform.InputArtifact
import org.gradle.api.artifacts.transform.TransformAction
import org.gradle.api.artifacts.transform.TransformOutputs
import org.gradle.api.artifacts.transform.TransformParameters
import org.gradle.api.file.FileSystemLocation
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import java.util.zip.ZipInputStream
@CacheableTransform
internal abstract class CodeOwnersTransform : TransformAction {
@get:PathSensitive(PathSensitivity.NONE)
@get:InputArtifact
abstract val inputJar: Provider
override fun transform(outputs: TransformOutputs) {
val jar = inputJar.get().asFile
outputs.file(jar.nameWithoutExtension + ".codeowners").writer().use { out ->
out.append("# Generated .codeowners for ")
out.appendLine(jar.name)
out.appendLine()
ZipInputStream(jar.inputStream()).use { zip ->
val reader = zip.bufferedReader()
val entries = generateSequence(zip.nextEntry) { zip.nextEntry }
.filter { !it.isDirectory && it.name.endsWith(".codeowners") }
.map {
CodeOwnersFile.Entry(
pattern = it.name.removeSuffix(".codeowners"),
owners = reader.lineSequence().toList(),
)
}
.toList()
.sortedBy { it.pattern }
out.append(CodeOwnersFile(entries).content)
}
}
}
}