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

gradle-plugins.kotlin-library.0.1.0-rc.24.source-code.KotlinLibraryType.kt Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.25
Show newest version
package com.javiersc.gradle.plugins.kotlin.library

import com.android.build.api.dsl.LibraryExtension
import com.javiersc.gradle.plugins.core.isAndroidLibrary
import com.javiersc.gradle.plugins.core.isGradlePlugin
import com.javiersc.gradle.plugins.core.isKotlinJvm
import com.javiersc.gradle.plugins.core.isKotlinMultiplatform
import com.javiersc.gradle.plugins.core.isKotlinMultiplatformWithAndroid
import com.javiersc.kotlin.stdlib.AnsiColor
import com.javiersc.kotlin.stdlib.ansiColor
import java.util.Locale
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal sealed class KotlinLibraryType {

    open fun configure(project: Project) {
        project.tasks.withType(KotlinCompile::class.java) { kotlinCompile ->
            kotlinCompile.kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() }
            kotlinCompile.sourceCompatibility = JavaVersion.VERSION_1_8.toString()
            kotlinCompile.targetCompatibility = JavaVersion.VERSION_1_8.toString()
        }
    }

    class Android(
        private val compileSdk: Int = AndroidSdk.compileSdk,
        private val minSdk: Int = AndroidSdk.minSdk,
        private val isKmp: Boolean
    ) : KotlinLibraryType() {

        override fun configure(project: Project) {
            super.configure(project)

            project.extensions.findByType(LibraryExtension::class.java)?.apply {
                compileSdk = [email protected]

                defaultConfig { minSdk = [email protected] }

                compileOptions {
                    sourceCompatibility(JavaVersion.VERSION_1_8)
                    targetCompatibility(JavaVersion.VERSION_1_8)
                }

                if (isKmp.not()) {
                    sourceSets.all {
                        it.assets.setSrcDirs(listOf("${it.name}/assets"))
                        it.java.setSrcDirs(listOf("${it.name}/java", "${it.name}/kotlin"))
                        it.manifest.srcFile("${it.name}/AndroidManifest.xml")
                        it.res.setSrcDirs(listOf("${it.name}/res"))
                        it.resources.setSrcDirs(listOf("${it.name}/resources"))
                    }
                }
            }
        }
    }

    object GradlePlugin : KotlinLibraryType() {
        override fun configure(project: Project) {
            super.configure(project)

            check(JavaVersion.current() >= JavaVersion.VERSION_11) {
                """   
                    |Using `javiersc-kotlin-library` to configure Gradle plugins needs Java 11"
                    |  - Use Java 11 to build via adding to your path or whatever other solution.
                    |  - Projects can be still compatible with Java 8 (`KotlinCompile.jvmTarget`)
                    |
                """.trimMargin()
            }

            project.configureJavaAndKotlinSourceSets()

            project.tasks.withType(KotlinCompile::class.java) { kotlinCompile ->
                kotlinCompile.kotlinOptions { jvmTarget = JavaVersion.VERSION_11.toString() }
                kotlinCompile.sourceCompatibility = JavaVersion.VERSION_11.toString()
                kotlinCompile.targetCompatibility = JavaVersion.VERSION_11.toString()
            }
        }
    }

    object KotlinJVM : KotlinLibraryType() {

        override fun configure(project: Project) {
            super.configure(project)

            project.configureJavaAndKotlinSourceSets()
        }
    }

    object KotlinMultiplatform : KotlinLibraryType() {

        override fun configure(project: Project) {
            super.configure(project)

            project.configureJavaAndKotlinSourceSets()
        }
    }

    object KotlinMultiplatformWithAndroid : KotlinLibraryType() {

        override fun configure(project: Project) {
            KotlinMultiplatform.configure(project)
            Android(isKmp = true).configure(project)

            project.extensions.findByType(LibraryExtension::class.java)?.apply {
                sourceSets.all {
                    it.manifest.srcFile("android${it.name.capitalize()}/AndroidManifest.xml")
                }
            }
        }
    }

    companion object {
        fun build(project: Project): Unit =
            with(project) {
                when {
                    isGradlePlugin -> GradlePlugin.configure(this)
                    isKotlinMultiplatformWithAndroid -> {
                        KotlinMultiplatformWithAndroid.configure(this)
                    }
                    isKotlinMultiplatform -> {
                        KotlinMultiplatform.configure(this)
                    }
                    isKotlinJvm -> KotlinJVM.configure(this)
                    isAndroidLibrary -> Android(isKmp = false).configure(this)
                    else -> {
                        errorMessage(
                            "`javiersc-kotlin-library` doesn't support this type of project yet"
                        )
                    }
                }
            }
    }
}

object AndroidSdk {

    const val compileSdk = 31
    const val minSdk = 21
}

internal fun Project.errorMessage(message: String) =
    logger.lifecycle(message.ansiColor(AnsiColor.Foreground.Yellow))

private fun String.capitalize(): String = replaceFirstChar {
    if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
}

private fun Project.configureJavaAndKotlinSourceSets() {
    extensions.findByType(JavaPluginExtension::class.java)?.apply {
        sourceSets.all {
            it.java.setSrcDirs(listOf("${it.name}/java"))
            it.resources.setSrcDirs(listOf("${it.name}/resources"))
        }
    }
    extensions.findByType(KotlinJvmProjectExtension::class.java)?.apply {
        sourceSets.all {
            it.kotlin.setSrcDirs(listOf("${it.name}/kotlin"))
            it.resources.setSrcDirs(listOf("${it.name}/resources"))
        }
    }

    extensions.findByType(KotlinMultiplatformExtension::class.java)?.apply {
        sourceSets.all {
            it.addDefaultLanguageSettings()
            it.kotlin.setSrcDirs(listOf("${it.name}/kotlin"))
            it.resources.setSrcDirs(listOf("${it.name}/resources"))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy