buildSrc/src/main/kotlin/SourceLicenseChecker.kt
changeset 105 4dd59e91dc99
parent 75 534a19e25217
equal deleted inserted replaced
104:d21949038794 105:4dd59e91dc99
    37 import org.gradle.kotlin.dsl.*
    37 import org.gradle.kotlin.dsl.*
    38 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
    38 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
    39 import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
    39 import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
    40 import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper
    40 import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper
    41 import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
    41 import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper
       
    42 import java.io.File
    42 import java.util.Locale
    43 import java.util.Locale
    43 
    44 
    44 internal fun Project.configureSourceLicenseChecker() {
    45 internal fun Project.configureSourceLicenseChecker() {
    45     apply<LicensePlugin>()
    46     apply<LicensePlugin>()
    46 
    47 
    47     configure<LicenseExtension> {
    48     configure<LicenseExtension> {
    48         header = file("$rootDir/config/license/header.txt")
    49         header = file("$rootDir/config/license/header.txt")
    49         mapping("java", "SLASHSTAR_STYLE")
    50         mapping("java", "SLASHSTAR_STYLE")
       
    51         mapping("json", "SLASHSTAR_STYLE")
    50         mapping("kt", "SLASHSTAR_STYLE")
    52         mapping("kt", "SLASHSTAR_STYLE")
    51 
    53 
    52         excludes(listOf("**/*.webp", "**/*.png"))
    54         excludes(listOf("**/*.webp", "**/*.png"))
    53     }
    55     }
    54 
    56 
    55     // the LicensePlugin doesn't configure itself properly on DynamicFeaturePlugin
    57     // the LicensePlugin doesn't configure itself properly on DynamicFeaturePlugin
    56     // Copied the code to configure it
    58     // Copied the code to configure it
    57     plugins.withType(DynamicFeaturePlugin::class.java) {
    59     plugins.withType<DynamicFeaturePlugin> {
    58         configureAndroid()
    60         configureAndroid()
    59     }
    61     }
    60     // make the license tasks looks for kotlin files in an Android project
    62     // make the license tasks looks for kotlin files in an Android project
    61     plugins.withType(KotlinAndroidPluginWrapper::class.java) {
    63     plugins.withType(KotlinAndroidPluginWrapper::class.java) {
    62         configureKotlinAndroid()
    64         configureKotlinAndroid()
    63     }
    65     }
    64 
    66 
    65     // make the license tasks for kotlin js project
    67     // make the license tasks for kotlin js project
    66     plugins.withType(KotlinJsPluginWrapper::class.java) {
    68     plugins.withType<KotlinJsPluginWrapper> {
    67         configureKotlin()
    69         configureKotlin()
    68     }
    70     }
    69 
    71 
    70     plugins.withType(KotlinMultiplatformPluginWrapper::class.java) {
    72     plugins.withType<KotlinMultiplatformPluginWrapper> {
    71         configureKotlin()
    73         configureKotlin()
       
    74         configureComposeResources()
       
    75     }
       
    76 
       
    77     tasks.withType<License>().configureEach {
       
    78         notCompatibleWithConfigurationCache("License tasks calls getProject() at execution time")
    72     }
    79     }
    73 }
    80 }
    74 
    81 
    75 @OptIn(ExperimentalStdlibApi::class)
    82 @OptIn(ExperimentalStdlibApi::class)
    76 private fun Project.configureKotlin() {
    83 private fun Project.configureKotlin() {
   116             source(kotlinSource.kotlin, manifest.srcFile)
   123             source(kotlinSource.kotlin, manifest.srcFile)
   117         }
   124         }
   118     }
   125     }
   119 }
   126 }
   120 
   127 
       
   128 private fun Project.configureComposeResources() {
       
   129     val kotlin = the<KotlinProjectExtension>()
       
   130     val taskInfix = "ComposeResources"
       
   131     kotlin.sourceSets.configureEach {
       
   132         val kotlinSource = this
       
   133         val sourceSetTaskName =
       
   134             "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
       
   135         if (name.startsWith("generated")) {
       
   136             logger.info("Skip sourceSet $name because it's generated code")
       
   137             return@configureEach
       
   138         }
       
   139         val resourceDir = kotlinSource.resources.sourceDirectories.files.first()
       
   140         val composeResourceDir = File(resourceDir, "../composeResources")
       
   141 
       
   142         val configureLicenseCheckTaskLambda: LicenseCheck.() -> Unit = {
       
   143             source(composeResourceDir)
       
   144         }
       
   145         if (sourceSetTaskName in tasks.names) {
       
   146             // tasks may have already been added by configuration for the Android plugin
       
   147             logger.info("Tasks $sourceSetTaskName already exists. configure it")
       
   148             tasks.named(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda)
       
   149         } else {
       
   150             logger.info("Adding ${project.name}:$sourceSetTaskName task for sourceSet ${kotlinSource.name}")
       
   151             tasks.register(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda)
       
   152         }
       
   153 
       
   154         val configureLicenseFormatTaskLambda: LicenseFormat.() -> Unit = {
       
   155             source(composeResourceDir)
       
   156         }
       
   157         val sourceSetFormatTaskName =
       
   158             "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
       
   159         if (sourceSetFormatTaskName in tasks.names) {
       
   160             // tasks may have already been added by configuration for the Android plugin
       
   161             logger.info("Tasks $sourceSetFormatTaskName already exists. configure it")
       
   162             tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda)
       
   163         } else {
       
   164             logger.info("Adding ${project.name}:$sourceSetFormatTaskName task for sourceSet ${kotlinSource.name}")
       
   165             tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda)
       
   166         }
       
   167     }
       
   168 }
       
   169 
       
   170 
   121 
   171 
   122 private fun Project.configureAndroid() {
   172 private fun Project.configureAndroid() {
   123     val android = the<BaseExtension>()
   173     val android = the<BaseExtension>()
   124     configureSourceSetRule(android.sourceSets, "Android") { ss ->
   174     configureSourceSetRule(android.sourceSets, "Android") { ss ->
   125         @Suppress("DEPRECATION")
   175         @Suppress("DEPRECATION")