| author | Da Risk <da_risk@geekorum.com> |
| Mon, 18 May 2026 20:00:02 -0400 | |
| changeset 105 | 4dd59e91dc99 |
| parent 75 | 534a19e25217 |
| permissions | -rw-r--r-- |
| 10 | 1 |
/* |
2 |
* Geekdroid is a utility library for development on the Android |
|
3 |
* Platform. |
|
4 |
* |
|
|
75
534a19e25217
build: update license headers
Da Risk <da_risk@geekorum.com>
parents:
72
diff
changeset
|
5 |
* Copyright (C) 2017-2025 by Frederic-Charles Barthelery. |
| 10 | 6 |
* |
7 |
* This file is part of Geekdroid. |
|
8 |
* |
|
9 |
* Geekdroid is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* Geekdroid is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with Geekdroid. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
| 1 | 22 |
package com.geekorum.build |
23 |
||
| 19 | 24 |
import com.android.build.api.dsl.AndroidSourceSet |
25 |
import com.android.build.gradle.BaseExtension |
|
26 |
import com.android.build.gradle.DynamicFeaturePlugin |
|
| 1 | 27 |
import com.hierynomus.gradle.license.LicenseBasePlugin |
28 |
import com.hierynomus.gradle.license.tasks.LicenseCheck |
|
29 |
import com.hierynomus.gradle.license.tasks.LicenseFormat |
|
| 19 | 30 |
import nl.javadude.gradle.plugins.license.License |
| 1 | 31 |
import nl.javadude.gradle.plugins.license.LicenseExtension |
32 |
import nl.javadude.gradle.plugins.license.LicensePlugin |
|
| 19 | 33 |
import org.gradle.api.NamedDomainObjectContainer |
| 1 | 34 |
import org.gradle.api.Project |
| 19 | 35 |
import org.gradle.api.file.FileTree |
36 |
import org.gradle.api.tasks.TaskProvider |
|
37 |
import org.gradle.kotlin.dsl.* |
|
38 |
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension |
|
39 |
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper |
|
40 |
import org.jetbrains.kotlin.gradle.plugin.KotlinJsPluginWrapper |
|
41 |
import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper |
|
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
42 |
import java.io.File |
| 72 | 43 |
import java.util.Locale |
| 1 | 44 |
|
| 19 | 45 |
internal fun Project.configureSourceLicenseChecker() {
|
| 1 | 46 |
apply<LicensePlugin>() |
47 |
||
48 |
configure<LicenseExtension> {
|
|
49 |
header = file("$rootDir/config/license/header.txt")
|
|
| 19 | 50 |
mapping("java", "SLASHSTAR_STYLE")
|
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
51 |
mapping("json", "SLASHSTAR_STYLE")
|
| 19 | 52 |
mapping("kt", "SLASHSTAR_STYLE")
|
| 1 | 53 |
|
54 |
excludes(listOf("**/*.webp", "**/*.png"))
|
|
55 |
} |
|
56 |
||
| 19 | 57 |
// the LicensePlugin doesn't configure itself properly on DynamicFeaturePlugin |
58 |
// Copied the code to configure it |
|
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
59 |
plugins.withType<DynamicFeaturePlugin> {
|
| 19 | 60 |
configureAndroid() |
61 |
} |
|
62 |
// make the license tasks looks for kotlin files in an Android project |
|
63 |
plugins.withType(KotlinAndroidPluginWrapper::class.java) {
|
|
64 |
configureKotlinAndroid() |
|
65 |
} |
|
66 |
||
67 |
// make the license tasks for kotlin js project |
|
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
68 |
plugins.withType<KotlinJsPluginWrapper> {
|
| 19 | 69 |
configureKotlin() |
70 |
} |
|
71 |
||
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
72 |
plugins.withType<KotlinMultiplatformPluginWrapper> {
|
| 19 | 73 |
configureKotlin() |
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
74 |
configureComposeResources() |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
75 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
76 |
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
77 |
tasks.withType<License>().configureEach {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
78 |
notCompatibleWithConfigurationCache("License tasks calls getProject() at execution time")
|
| 19 | 79 |
} |
80 |
} |
|
| 1 | 81 |
|
| 19 | 82 |
@OptIn(ExperimentalStdlibApi::class) |
83 |
private fun Project.configureKotlin() {
|
|
84 |
val kotlin = the<KotlinProjectExtension>() |
|
85 |
val taskInfix = "" |
|
86 |
kotlin.sourceSets.configureEach {
|
|
87 |
val kotlinSource = this |
|
88 |
val sourceSetTaskName = |
|
| 72 | 89 |
"${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
| 19 | 90 |
logger.info("Adding $sourceSetTaskName task for sourceSet ${kotlinSource.name}")
|
91 |
if (sourceSetTaskName in tasks.names) {
|
|
92 |
// tasks may have already been added by configuration for the Android plugin |
|
93 |
logger.info("Tasks $sourceSetTaskName already exists. Skip")
|
|
94 |
return@configureEach |
|
| 1 | 95 |
} |
| 19 | 96 |
tasks.register(sourceSetTaskName, LicenseCheck::class.java) {
|
97 |
source(kotlinSource.kotlin) |
|
| 1 | 98 |
} |
| 19 | 99 |
val sourceSetFormatTaskName = |
| 72 | 100 |
"${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
| 19 | 101 |
tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java) {
|
102 |
source(kotlinSource.kotlin) |
|
| 1 | 103 |
} |
104 |
} |
|
105 |
} |
|
| 19 | 106 |
|
107 |
@OptIn(ExperimentalStdlibApi::class) |
|
108 |
private fun Project.configureKotlinAndroid() {
|
|
109 |
val kotlin = the<KotlinProjectExtension>() |
|
110 |
val android = the<BaseExtension>() |
|
111 |
val taskInfix = "Android" |
|
112 |
android.sourceSets.configureEach {
|
|
113 |
val kotlinSource = kotlin.sourceSets[name] |
|
114 |
logger.info("Adding kotlin sources from sourceSet $name to License plugin tasks")
|
|
115 |
val sourceSetTaskName = |
|
| 72 | 116 |
"${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
| 19 | 117 |
tasks.named(sourceSetTaskName, LicenseCheck::class.java) {
|
118 |
source(kotlinSource.kotlin, manifest.srcFile) |
|
119 |
} |
|
120 |
val sourceSetFormatTaskName = |
|
| 72 | 121 |
"${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
| 19 | 122 |
tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java) {
|
123 |
source(kotlinSource.kotlin, manifest.srcFile) |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
||
|
105
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
128 |
private fun Project.configureComposeResources() {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
129 |
val kotlin = the<KotlinProjectExtension>() |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
130 |
val taskInfix = "ComposeResources" |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
131 |
kotlin.sourceSets.configureEach {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
132 |
val kotlinSource = this |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
133 |
val sourceSetTaskName = |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
134 |
"${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
135 |
if (name.startsWith("generated")) {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
136 |
logger.info("Skip sourceSet $name because it's generated code")
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
137 |
return@configureEach |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
138 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
139 |
val resourceDir = kotlinSource.resources.sourceDirectories.files.first() |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
140 |
val composeResourceDir = File(resourceDir, "../composeResources") |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
141 |
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
142 |
val configureLicenseCheckTaskLambda: LicenseCheck.() -> Unit = {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
143 |
source(composeResourceDir) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
144 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
145 |
if (sourceSetTaskName in tasks.names) {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
146 |
// tasks may have already been added by configuration for the Android plugin |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
147 |
logger.info("Tasks $sourceSetTaskName already exists. configure it")
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
148 |
tasks.named(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
149 |
} else {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
150 |
logger.info("Adding ${project.name}:$sourceSetTaskName task for sourceSet ${kotlinSource.name}")
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
151 |
tasks.register(sourceSetTaskName, LicenseCheck::class.java, configureLicenseCheckTaskLambda) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
152 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
153 |
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
154 |
val configureLicenseFormatTaskLambda: LicenseFormat.() -> Unit = {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
155 |
source(composeResourceDir) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
156 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
157 |
val sourceSetFormatTaskName = |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
158 |
"${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
159 |
if (sourceSetFormatTaskName in tasks.names) {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
160 |
// tasks may have already been added by configuration for the Android plugin |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
161 |
logger.info("Tasks $sourceSetFormatTaskName already exists. configure it")
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
162 |
tasks.named(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
163 |
} else {
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
164 |
logger.info("Adding ${project.name}:$sourceSetFormatTaskName task for sourceSet ${kotlinSource.name}")
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
165 |
tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java, configureLicenseFormatTaskLambda) |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
166 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
167 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
168 |
} |
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
169 |
|
|
4dd59e91dc99
build: SourceLicenseChecker mark license task not compatible with gradle configuration cache
Da Risk <da_risk@geekorum.com>
parents:
75
diff
changeset
|
170 |
|
| 19 | 171 |
|
172 |
private fun Project.configureAndroid() {
|
|
173 |
val android = the<BaseExtension>() |
|
174 |
configureSourceSetRule(android.sourceSets, "Android") { ss ->
|
|
175 |
@Suppress("DEPRECATION")
|
|
176 |
when (ss) {
|
|
177 |
// the dsl.AndroidSourceSet don't expose any getter, so we still need to cast it |
|
178 |
is com.android.build.gradle.api.AndroidSourceSet -> {
|
|
179 |
ss.java.getSourceFiles() + ss.res.getSourceFiles() + fileTree(ss.manifest.srcFile) |
|
180 |
} |
|
181 |
else -> fileTree() |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Dynamically create a task for each sourceSet, and register with check |
|
188 |
*/ |
|
189 |
@Suppress("DefaultLocale")
|
|
190 |
private fun Project.configureSourceSetRule(androidSourceSetContainer: NamedDomainObjectContainer<out AndroidSourceSet>, |
|
191 |
taskInfix: String, sourceSetSources: (AndroidSourceSet) -> FileTree) {
|
|
192 |
// This follows the other check task pattern |
|
193 |
androidSourceSetContainer.configureEach {
|
|
194 |
val sourceSetTaskName = "${LicenseBasePlugin.getLICENSE_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
|
195 |
logger.info("Adding $sourceSetTaskName task for sourceSet $name")
|
|
196 |
||
197 |
val checkTask = tasks.register(sourceSetTaskName, LicenseCheck::class.java) |
|
198 |
configureForSourceSet(this, checkTask, sourceSetSources) |
|
199 |
||
200 |
// Add independent license task, which will perform format |
|
201 |
val sourceSetFormatTaskName = "${LicenseBasePlugin.getFORMAT_TASK_BASE_NAME()}${taskInfix}${name.capitalize()}"
|
|
202 |
val formatTask = tasks.register(sourceSetFormatTaskName, LicenseFormat::class.java) |
|
203 |
configureForSourceSet(this, formatTask, sourceSetSources) |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
private fun configureForSourceSet(sourceSet: AndroidSourceSet, task: TaskProvider<out License>, sourceSetSources: (AndroidSourceSet) -> FileTree) {
|
|
208 |
task.configure {
|
|
209 |
// Explicitly set description |
|
210 |
description = "Scanning license on ${sourceSet.name} files"
|
|
211 |
||
212 |
// Default to all source files from SourceSet |
|
213 |
source = sourceSetSources(sourceSet) |
|
214 |
} |
|
215 |
} |
|
| 72 | 216 |
|
217 |
private fun String.capitalize() = |
|
218 |
replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
|