All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.gmazzo.codeowners.CodeOwnersTransform.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
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)
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy