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

kotlinx.kover.gradle.plugin.locators.Jvm.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0-RC
Show newest version
/*
 * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.kover.gradle.plugin.locators

import kotlinx.kover.gradle.plugin.commons.CompilationUnit
import kotlinx.kover.gradle.plugin.dsl.internal.KoverProjectExtensionImpl
import kotlinx.kover.gradle.plugin.util.DynamicBean
import org.gradle.api.Task
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskProvider
import java.io.File

/**
 * Extract JVM compilation unit form Kotlin compilation ([this]).
 */
internal inline fun DynamicBean.asJvmCompilationUnit(
    excludeJava: Boolean,
    isJavaOutput: (File) -> Boolean
): CompilationUnit {
    val sources = beanCollection("allKotlinSourceSets").flatMap {
        it["kotlin"].valueCollection("srcDirs")
    }.toSet()

    val outputs = get("output").value("classesDirs").files.filterNot {
        excludeJava && isJavaOutput(it)
    }.toSet()

    val compileTasks = mutableListOf()
    compileTasks += value("compileKotlinTask")
    if (!excludeJava) {
        valueOrNull?>("compileJavaTaskProvider")?.orNull?.let { task -> compileTasks += task }
    }

    return CompilationUnit(sources, outputs, compileTasks)
}

internal fun DynamicBean.extractJvmCompilations(
    koverExtension: KoverProjectExtensionImpl,
    isJavaOutput: (File) -> Boolean
): Map {
    if (koverExtension.disabled) {
        // If the Kover plugin is disabled, then it does not provide any directories and compilation tasks to its artifacts.
        return emptyMap()
    }

    val compilations = this.beanCollection("compilations").filter {
        // always ignore test source set by default
        val name = it.value("name")
        name != SourceSet.TEST_SOURCE_SET_NAME
                // ignore specified JVM source sets
                && name !in koverExtension.sourceSets.sourceSets
    }

    return compilations.associate { compilation ->
        val name = compilation.value("name")
        name to extractJvmCompilation(koverExtension, compilation, isJavaOutput)
    }
}

private fun extractJvmCompilation(
    koverExtension: KoverProjectExtensionImpl,
    compilation: DynamicBean,
    isJavaOutput: (File) -> Boolean
): CompilationUnit {
    return if (koverExtension.disabled) {
        // If the Kover plugin is disabled, then it does not provide any directories and compilation tasks to its artifacts.
        CompilationUnit()
    } else {
        compilation.asJvmCompilationUnit(koverExtension.excludeJava, isJavaOutput)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy