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

org.jetbrains.kotlin.gradle.plugin.hierarchy.KotlinHierarchy.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.gradle.plugin.hierarchy

import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.tooling.core.withClosure

internal data class KotlinHierarchy(
    val node: Node, val children: Set = emptySet(),
) {

    val childrenClosure: Set =
        children.withClosure { it.children }

    sealed class Node {
        abstract suspend fun sharedSourceSetName(compilation: KotlinCompilation<*>): String?

        object Root : Node() {
            override suspend fun sharedSourceSetName(compilation: KotlinCompilation<*>): String? = null
        }

        data class Group(val name: String) : Node() {
            override suspend fun sharedSourceSetName(compilation: KotlinCompilation<*>): String? {
                val sourceSetTree = KotlinSourceSetTree.orNull(compilation)?.name ?: return null
                return lowerCamelCaseName(name, sourceSetTree)
            }
        }

        final override fun toString(): String = when (this) {
            is Group -> name
            is Root -> ""
        }
    }

    override fun toString(): String {
        if (children.isEmpty()) return node.toString()
        return node.toString() + "\n" + children.joinToString("\n").prependIndent("----")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy