-
Notifications
You must be signed in to change notification settings - Fork 54
/
BinaryCompatibilityValidatorPlugin.kt
351 lines (311 loc) · 13.3 KB
/
BinaryCompatibilityValidatorPlugin.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright 2016-2020 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/
package kotlinx.validation
import com.android.build.gradle.LibraryExtension
import org.gradle.api.*
import org.gradle.api.plugins.*
import org.gradle.api.provider.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.compile.JavaCompile
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.*
import java.io.*
const val API_DIR = "api"
class BinaryCompatibilityValidatorPlugin : Plugin<Project> {
override fun apply(target: Project): Unit = with(target) {
val extension = extensions.create("apiValidation", ApiValidationExtension::class.java)
validateExtension(extension)
allprojects {
configureProject(it, extension)
}
}
private fun Project.validateExtension(extension: ApiValidationExtension) {
afterEvaluate {
val ignored = extension.ignoredProjects
val all = allprojects.map { it.name }
for (project in ignored) {
require(project in all) { "Cannot find excluded project $project in all projects: $all" }
}
}
}
private fun configureProject(project: Project, extension: ApiValidationExtension) {
configureKotlinPlugin(project, extension)
configureAndroidPlugin(project, extension)
configureMultiplatformPlugin(project, extension)
}
private fun configurePlugin(
name: String,
project: Project,
extension: ApiValidationExtension,
action: Action<AppliedPlugin>
) = project.pluginManager.withPlugin(name) {
if (project.name in extension.ignoredProjects) return@withPlugin
action.execute(it)
}
private fun configureMultiplatformPlugin(
project: Project,
extension: ApiValidationExtension
) = configurePlugin("kotlin-multiplatform", project, extension) {
if (project.name in extension.ignoredProjects) return@configurePlugin
val kotlin = project.extensions.getByName("kotlin") as KotlinMultiplatformExtension
// Create common tasks for multiplatform
val commonApiDump = project.tasks.register("apiDump") {
it.group = "other"
it.description = "Task that collects all target specific dump tasks"
}
val commonApiCheck: TaskProvider<Task> = project.tasks.register("apiCheck") {
it.group = "verification"
it.description = "Shortcut task that depends on all specific check tasks"
}.apply { project.tasks.named("check") { it.dependsOn(this) } }
val jvmTargetCountProvider = project.provider {
kotlin.targets.count {
it.platformType in arrayOf(
KotlinPlatformType.jvm,
KotlinPlatformType.androidJvm
)
}
}
val dirConfig = jvmTargetCountProvider.map {
if (it == 1) DirConfig.COMMON else DirConfig.TARGET_DIR
}
kotlin.targets.matching {
it.platformType == KotlinPlatformType.jvm || it.platformType == KotlinPlatformType.androidJvm
}.all { target ->
val targetConfig = TargetConfig(project, target.name, dirConfig)
if (target.platformType == KotlinPlatformType.jvm) {
target.compilations.matching { it.name == "main" }.all {
project.configureKotlinCompilation(it, extension, targetConfig, commonApiDump, commonApiCheck)
}
} else if (target.platformType == KotlinPlatformType.androidJvm) {
target.compilations.matching { it.name == "release" }.all {
project.configureKotlinCompilation(
it,
extension,
targetConfig,
commonApiDump,
commonApiCheck,
useOutput = true
)
}
}
}
}
private fun configureAndroidPlugin(
project: Project,
extension: ApiValidationExtension
) {
val kotlinPluginPresent = project.plugins
.withType(KotlinAndroidPluginWrapper::class.java)
.isEmpty().not()
if (kotlinPluginPresent) {
configureAndroidPluginForKotlinLibrary(project, extension)
} else {
configureAndroidPluginForJavaLibrary(project, extension)
}
}
private fun configureAndroidPluginForKotlinLibrary(
project: Project,
extension: ApiValidationExtension
) = configurePlugin("kotlin-android", project, extension) {
val androidExtension = project.extensions
.getByName("kotlin") as KotlinAndroidProjectExtension
androidExtension.target.compilations.matching {
it.compilationName == "release"
}.all {
project.configureKotlinCompilation(it, extension, useOutput = true)
}
}
private fun configureAndroidPluginForJavaLibrary(
project: Project,
extension: ApiValidationExtension
) = configurePlugin("com.android.library", project, extension) {
val androidExtension = project.extensions.getByType(LibraryExtension::class.java)
androidExtension.libraryVariants.matching {
it.name == "release"
}.all {
project.configureJavaCompilation(it.javaCompileProvider, extension)
}
}
private fun configureKotlinPlugin(
project: Project,
extension: ApiValidationExtension
) = configurePlugin("kotlin", project, extension) {
project.sourceSets.all { sourceSet ->
if (sourceSet.name != SourceSet.MAIN_SOURCE_SET_NAME) {
return@all
}
project.configureApiTasks(sourceSet, extension, TargetConfig(project))
}
}
}
private class TargetConfig constructor(
project: Project,
val targetName: String? = null,
private val dirConfig: Provider<DirConfig>? = null,
) {
private val API_DIR_PROVIDER = project.provider { API_DIR }
fun apiTaskName(suffix: String) = when (targetName) {
null, "" -> "api$suffix"
else -> "${targetName}Api$suffix"
}
val apiDir
get() = dirConfig?.map { dirConfig ->
when (dirConfig) {
DirConfig.COMMON -> API_DIR
else -> "$API_DIR/$targetName"
}
} ?: API_DIR_PROVIDER
}
private enum class DirConfig {
/**
* `api` directory for .api files.
* Used in single target projects
*/
COMMON,
/**
* Target-based directory, used in multitarget setups.
* E.g. for the project with targets jvm and android,
* the resulting paths will be
* `/api/jvm/project.api` and `/api/android/project.api`
*/
TARGET_DIR,
}
private fun Project.configureKotlinCompilation(
compilation: KotlinCompilation<KotlinCommonOptions>,
extension: ApiValidationExtension,
targetConfig: TargetConfig = TargetConfig(this),
commonApiDump: TaskProvider<Task>? = null,
commonApiCheck: TaskProvider<Task>? = null,
useOutput: Boolean = false,
) {
val projectName = project.name
val apiDirProvider = targetConfig.apiDir
val apiBuildDir = apiDirProvider.map { buildDir.resolve(it) }
val apiBuild = task<KotlinApiBuildTask>(targetConfig.apiTaskName("Build")) {
// Do not enable task for empty umbrella modules
isEnabled =
apiCheckEnabled(projectName, extension) && compilation.allKotlinSourceSets.any { it.kotlin.srcDirs.any { it.exists() } }
// 'group' is not specified deliberately, so it will be hidden from ./gradlew tasks
description =
"Builds Kotlin API for 'main' compilations of $projectName. Complementary task and shouldn't be called manually"
if (useOutput) {
// Workaround for #4
inputClassesDirs =
files(provider<Any> { if (isEnabled) compilation.output.classesDirs else emptyList<Any>() })
inputDependencies =
files(provider<Any> { if (isEnabled) compilation.output.classesDirs else emptyList<Any>() })
} else {
inputClassesDirs =
files(provider<Any> { if (isEnabled) compilation.output.classesDirs else emptyList<Any>() })
inputDependencies =
files(provider<Any> { if (isEnabled) compilation.compileDependencyFiles else emptyList<Any>() })
}
outputApiDir = apiBuildDir.get()
}
configureCheckTasks(apiBuildDir, apiBuild, extension, targetConfig, commonApiDump, commonApiCheck)
}
internal val Project.sourceSets: SourceSetContainer
get() = extensions.getByName("sourceSets") as SourceSetContainer
internal val Project.apiValidationExtensionOrNull: ApiValidationExtension?
get() =
generateSequence(this) { it.parent }
.map { it.extensions.findByType(ApiValidationExtension::class.java) }
.firstOrNull { it != null }
private fun Project.configureJavaCompilation(
configurableFileCollection: TaskProvider<JavaCompile>,
extension: ApiValidationExtension,
targetConfig: TargetConfig = TargetConfig(this),
) {
val projectName = project.name
val apiDirProvider = targetConfig.apiDir
val apiBuildDir = apiDirProvider.map { buildDir.resolve(it) }
val apiBuild = task<KotlinApiBuildTask>(targetConfig.apiTaskName("Build"), extension) {
isEnabled = apiCheckEnabled(projectName, extension)
// 'group' is not specified deliberately, so it will be hidden from ./gradlew tasks
description = "Builds Java API for 'main' compilations of $projectName. " +
"Complementary task and shouldn't be called manually"
inputClassesDirs = files(provider<Any> {
if (isEnabled) configurableFileCollection.get().outputs.files
else emptyList<Any>()
})
inputDependencies = files(provider<Any> {
if (isEnabled) configurableFileCollection.get().outputs.files
else emptyList<Any>()
})
outputApiDir = apiBuildDir.get()
}
configureCheckTasks(
apiBuildDir,
apiBuild,
extension,
targetConfig
)
}
fun apiCheckEnabled(projectName: String, extension: ApiValidationExtension): Boolean =
projectName !in extension.ignoredProjects && !extension.validationDisabled
private fun Project.configureApiTasks(
sourceSet: SourceSet,
extension: ApiValidationExtension,
targetConfig: TargetConfig = TargetConfig(this),
) {
val projectName = project.name
val apiBuildDir = targetConfig.apiDir.map { buildDir.resolve(it) }
val apiBuild = task<KotlinApiBuildTask>(targetConfig.apiTaskName("Build")) {
isEnabled = apiCheckEnabled(projectName, extension)
// 'group' is not specified deliberately so it will be hidden from ./gradlew tasks
description =
"Builds Kotlin API for 'main' compilations of $projectName. Complementary task and shouldn't be called manually"
inputClassesDirs = files(provider<Any> { if (isEnabled) sourceSet.output.classesDirs else emptyList<Any>() })
inputDependencies = files(provider<Any> { if (isEnabled) sourceSet.output.classesDirs else emptyList<Any>() })
outputApiDir = apiBuildDir.get()
}
configureCheckTasks(apiBuildDir, apiBuild, extension, targetConfig)
}
private fun Project.configureCheckTasks(
apiBuildDir: Provider<File>,
apiBuild: TaskProvider<KotlinApiBuildTask>,
extension: ApiValidationExtension,
targetConfig: TargetConfig,
commonApiDump: TaskProvider<Task>? = null,
commonApiCheck: TaskProvider<Task>? = null,
) {
val projectName = project.name
val apiCheckDir = targetConfig.apiDir.map {
projectDir.resolve(it).also { r ->
logger.debug("Configuring api for ${targetConfig.targetName ?: "jvm"} to $r")
}
}
val apiCheck = task<KotlinApiCompareTask>(targetConfig.apiTaskName("Check")) {
isEnabled = apiCheckEnabled(projectName, extension) && apiBuild.map { it.enabled }.getOrElse(true)
group = "verification"
description = "Checks signatures of public API against the golden value in API folder for $projectName"
compareApiDumps(apiReferenceDir = apiCheckDir.get(), apiBuildDir = apiBuildDir.get())
dependsOn(apiBuild)
}
val apiDump = task<Sync>(targetConfig.apiTaskName("Dump")) {
isEnabled = apiCheckEnabled(projectName, extension) && apiBuild.map { it.enabled }.getOrElse(true)
group = "other"
description = "Syncs API from build dir to ${targetConfig.apiDir} dir for $projectName"
from(apiBuildDir)
into(apiCheckDir)
dependsOn(apiBuild)
}
commonApiDump?.configure { it.dependsOn(apiDump) }
when (commonApiCheck) {
null -> project.tasks.named("check").configure { it.dependsOn(apiCheck) }
else -> commonApiCheck.configure { it.dependsOn(apiCheck) }
}
}
inline fun <reified T : Task> Project.task(
name: String,
noinline configuration: T.() -> Unit,
): TaskProvider<T> = tasks.register(name, T::class.java, Action(configuration))
inline fun <reified T : Task> Project.task(
name: String,
extension: ApiValidationExtension,
noinline configuration: T.() -> Unit,
): TaskProvider<T> = tasks.register(name, T::class.java, extension).also {
it.configure(Action(configuration))
}