22 package com.geekorum.build  | 
    22 package com.geekorum.build  | 
    23   | 
    23   | 
    24 import com.android.build.api.dsl.AndroidSourceSet  | 
    24 import com.android.build.api.dsl.AndroidSourceSet  | 
    25 import com.android.build.gradle.BaseExtension  | 
    25 import com.android.build.gradle.BaseExtension  | 
    26 import com.android.build.gradle.DynamicFeaturePlugin  | 
    26 import com.android.build.gradle.DynamicFeaturePlugin  | 
         | 
    27 import com.android.build.gradle.TestPlugin  | 
    27 import com.hierynomus.gradle.license.LicenseBasePlugin  | 
    28 import com.hierynomus.gradle.license.LicenseBasePlugin  | 
    28 import com.hierynomus.gradle.license.tasks.LicenseCheck  | 
    29 import com.hierynomus.gradle.license.tasks.LicenseCheck  | 
    29 import com.hierynomus.gradle.license.tasks.LicenseFormat  | 
    30 import com.hierynomus.gradle.license.tasks.LicenseFormat  | 
    30 import nl.javadude.gradle.plugins.license.License  | 
    31 import nl.javadude.gradle.plugins.license.License  | 
    31 import nl.javadude.gradle.plugins.license.LicenseExtension  | 
    32 import nl.javadude.gradle.plugins.license.LicenseExtension  | 
    37 import org.gradle.kotlin.dsl.*  | 
    38 import org.gradle.kotlin.dsl.*  | 
    38 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension  | 
    39 import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension  | 
    39 import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper  | 
    40 import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper  | 
    40 import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper  | 
    41 import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper  | 
    41 import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper  | 
    42 import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper  | 
    42 import java.util.*  | 
    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> { | 
    55     // the LicensePlugin doesn't configure itself properly on DynamicFeaturePlugin  | 
    56     // the LicensePlugin doesn't configure itself properly on DynamicFeaturePlugin  | 
    56     // Copied the code to configure it  | 
    57     // Copied the code to configure it  | 
    57     plugins.withType(DynamicFeaturePlugin::class.java) { | 
    58     plugins.withType(DynamicFeaturePlugin::class.java) { | 
    58         configureAndroid()  | 
    59         configureAndroid()  | 
    59     }  | 
    60     }  | 
         | 
    61   | 
         | 
    62     // the LicensePlugin doesn't configure itself properly on Android Test plugin  | 
         | 
    63     // Copied the code to configure it  | 
         | 
    64     plugins.withType(TestPlugin::class.java) { | 
         | 
    65         configureAndroid()  | 
         | 
    66     }  | 
         | 
    67   | 
    60     // make the license tasks looks for kotlin files in an Android project  | 
    68     // make the license tasks looks for kotlin files in an Android project  | 
    61     plugins.withType(KotlinAndroidPluginWrapper::class.java) { | 
    69     plugins.withType(KotlinAndroidPluginWrapper::class.java) { | 
    62         configureKotlinAndroid()  | 
    70         configureKotlinAndroid()  | 
    63     }  | 
    71     }  | 
    64   | 
    72   | 
    78     val taskInfix = ""  | 
    86     val taskInfix = ""  | 
    79     kotlin.sourceSets.configureEach { | 
    87     kotlin.sourceSets.configureEach { | 
    80         val kotlinSource = this  | 
    88         val kotlinSource = this  | 
    81         val sourceSetTaskName =  | 
    89         val sourceSetTaskName =  | 
    82             "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
    90             "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
    83         logger.info("Adding $sourceSetTaskName task for sourceSet ${kotlinSource.name}") | 
    91         if (name.startsWith("generated")) { | 
         | 
    92             logger.info("Skip sourceSet $name because it's generated code") | 
         | 
    93             return@configureEach  | 
         | 
    94         }  | 
         | 
    95         val configureLicenseCheckTaskLambda: LicenseCheck.() -> Unit = { | 
         | 
    96             source(kotlinSource.kotlin)  | 
         | 
    97             exclude { | 
         | 
    98                 // exclude generated files, notably protobuf, ksp, hilt  | 
         | 
    99                 "/generated/" in it.file.path  | 
         | 
   100             }  | 
         | 
   101         }  | 
    84         if (sourceSetTaskName in tasks.names) { | 
   102         if (sourceSetTaskName in tasks.names) { | 
    85             // tasks may have already been added by configuration for the Android plugin  | 
   103             // tasks may have already been added by configuration for the Android plugin  | 
    86             logger.info("Tasks $sourceSetTaskName already exists. Skip") | 
   104             logger.info("Tasks $sourceSetTaskName already exists. configure it") | 
    87             return@configureEach  | 
   105             tasks.named(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda)  | 
    88         }  | 
   106         } else { | 
    89         tasks.register(sourceSetTaskName, LicenseCheck::class.java) { | 
   107             logger.info("Adding ${project.name}:$sourceSetTaskName task for sourceSet ${kotlinSource.name}") | 
         | 
   108             tasks.register(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda)  | 
         | 
   109         }  | 
         | 
   110   | 
         | 
   111         val configureLicenseFormatTaskLambda: LicenseFormat.() -> Unit = { | 
    90             source(kotlinSource.kotlin)  | 
   112             source(kotlinSource.kotlin)  | 
         | 
   113             exclude { | 
         | 
   114                 // exclude generated files, notably protobuf, ksp, hilt  | 
         | 
   115                 "/generated/" in it.file.path  | 
         | 
   116             }  | 
    91         }  | 
   117         }  | 
    92         val sourceSetFormatTaskName =  | 
   118         val sourceSetFormatTaskName =  | 
    93             "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
   119             "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
    94         tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java) { | 
   120         if (sourceSetFormatTaskName in tasks.names) { | 
    95             source(kotlinSource.kotlin)  | 
   121             // tasks may have already been added by configuration for the Android plugin  | 
         | 
   122             logger.info("Tasks $sourceSetFormatTaskName already exists. configure it") | 
         | 
   123             tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda)  | 
         | 
   124         } else { | 
         | 
   125             logger.info("Adding ${project.name}:$sourceSetFormatTaskName task for sourceSet ${kotlinSource.name}") | 
         | 
   126             tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda)  | 
    96         }  | 
   127         }  | 
    97     }  | 
   128     }  | 
    98 }  | 
   129 }  | 
    99   | 
   130   | 
   100 @OptIn(ExperimentalStdlibApi::class)  | 
   131 @OptIn(ExperimentalStdlibApi::class)  | 
   107         logger.info("Adding kotlin sources from sourceSet $name to License plugin tasks") | 
   138         logger.info("Adding kotlin sources from sourceSet $name to License plugin tasks") | 
   108         val sourceSetTaskName =  | 
   139         val sourceSetTaskName =  | 
   109             "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
   140             "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
   110         tasks.named(sourceSetTaskName, LicenseCheck::class.java) { | 
   141         tasks.named(sourceSetTaskName, LicenseCheck::class.java) { | 
   111             source(kotlinSource.kotlin, manifest.srcFile)  | 
   142             source(kotlinSource.kotlin, manifest.srcFile)  | 
         | 
   143             exclude { | 
         | 
   144                 // exclude generated files, notably protobuf  | 
         | 
   145                 "/generated/" in it.file.path  | 
         | 
   146             }  | 
   112         }  | 
   147         }  | 
   113         val sourceSetFormatTaskName =  | 
   148         val sourceSetFormatTaskName =  | 
   114             "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
   149             "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}" | 
   115         tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java) { | 
   150         tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java) { | 
   116             source(kotlinSource.kotlin, manifest.srcFile)  | 
   151             source(kotlinSource.kotlin, manifest.srcFile)  | 
         | 
   152             exclude { | 
         | 
   153                 // exclude generated files, notably protobuf  | 
         | 
   154                 "/generated/" in it.file.path  | 
         | 
   155             }  | 
   117         }  | 
   156         }  | 
   118     }  | 
   157     }  | 
   119 }  | 
   158 }  | 
   120   | 
   159   | 
   121   | 
   160   |