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

io.github.gmazzo.codeowners.compiler.CodeOwnersMappings.kt Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package io.github.gmazzo.codeowners.compiler

import io.github.gmazzo.codeowners.matcher.CodeOwnersFile
import io.github.gmazzo.codeowners.matcher.CodeOwnersMatcher
import java.io.File

class CodeOwnersMappings(
    private val matcher: CodeOwnersMatcher,
    private var mappingFile: File?,
) {

    private val mappings = mutableMapOf()

    fun resolve(file: File) = mappings.computeIfAbsent(file) {
        matcher.ownerOf(file)?.let { Mapping(owners = it) }
    }

    fun noteFrontedFinished() = mappingFile?.let { file ->
        mappingFile = null

        val entries = mappings.entries.asSequence()
            .onEach { (file) -> check(file.isFile) { "$file does not exists!" } }
            .filter { it.value != null }
            .flatMap { (_, mapping) ->
                mapping!!.classes.asSequence().map {
                    CodeOwnersFile.Entry(pattern = it, mapping.owners.toList())
                }
            }
            .toList()

        file.parentFile?.mkdirs()
        file.writeText(CodeOwnersFile(entries).content)
    }

    data class Mapping(
        val classes: MutableSet = mutableSetOf(),
        val owners: Set,
    )

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy