-
Notifications
You must be signed in to change notification settings - Fork 68
/
build.gradle.kts
459 lines (416 loc) · 15.4 KB
/
build.gradle.kts
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright (C) 2022 Slack Technologies, LLC
// SPDX-License-Identifier: Apache-2.0
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.CommonExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.TestExtension
import com.diffplug.gradle.spotless.SpotlessExtension
import com.diffplug.gradle.spotless.SpotlessExtensionPredeclare
import com.diffplug.spotless.LineEnding
import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPluginExtension
import com.squareup.anvil.plugin.AnvilExtension
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import java.net.URI
import org.jetbrains.dokka.gradle.DokkaTaskPartial
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin
import org.jetbrains.kotlin.gradle.targets.js.ir.DefaultIncrementalSyncTask
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import wtf.emulator.EwExtension
buildscript { dependencies { classpath(platform(libs.kotlin.plugins.bom)) } }
plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.kotlin.plugin.parcelize) apply false
alias(libs.plugins.kotlin.plugin.serialization) apply false
alias(libs.plugins.agp.application) apply false
alias(libs.plugins.agp.library) apply false
alias(libs.plugins.agp.test) apply false
alias(libs.plugins.anvil) apply false
alias(libs.plugins.detekt) apply false
alias(libs.plugins.spotless)
alias(libs.plugins.mavenPublish) apply false
alias(libs.plugins.dokka)
alias(libs.plugins.ksp) apply false
alias(libs.plugins.versionsPlugin)
alias(libs.plugins.dependencyGuard) apply false
alias(libs.plugins.compose) apply false
alias(libs.plugins.kotlin.plugin.compose) apply false
alias(libs.plugins.baselineprofile) apply false
alias(libs.plugins.emulatorWtf) apply false
}
val ktfmtVersion = libs.versions.ktfmt.get()
val detektVersion = libs.versions.detekt.get()
val twitterDetektPlugin = libs.detektPlugins.twitterCompose
tasks.dokkaHtmlMultiModule {
outputDirectory.set(rootDir.resolve("docs/api/0.x"))
includes.from(project.layout.projectDirectory.file("README.md"))
}
allprojects {
apply(plugin = "com.diffplug.spotless")
val spotlessFormatters: SpotlessExtension.() -> Unit = {
lineEndings = LineEnding.PLATFORM_NATIVE
format("misc") {
target("*.md", ".gitignore")
trimTrailingWhitespace()
endWithNewline()
}
kotlin {
target("src/**/*.kt")
ktfmt(ktfmtVersion).googleStyle()
trimTrailingWhitespace()
endWithNewline()
targetExclude("**/spotless.kt")
}
kotlinGradle {
target("*.kts")
ktfmt(ktfmtVersion).googleStyle()
trimTrailingWhitespace()
endWithNewline()
licenseHeaderFile(
rootProject.file("spotless/spotless.kt"),
"(import|plugins|buildscript|dependencies|pluginManagement|dependencyResolutionManagement)",
)
}
// Apply license formatting separately for kotlin files so we can prevent it from overwriting
// copied files
format("license") {
licenseHeaderFile(rootProject.file("spotless/spotless.kt"), "(package|@file:)")
target("src/**/*.kt")
targetExclude(
"**/circuit/backstack/**/*.kt",
"**/HorizontalPagerIndicator.kt",
"**/FilterList.kt",
"**/Remove.kt",
"**/Pets.kt",
"**/SystemUiController.kt",
)
}
}
configure<SpotlessExtension> {
spotlessFormatters()
if (project.rootProject == project) {
predeclareDeps()
}
}
if (project.rootProject == project) {
configure<SpotlessExtensionPredeclare> { spotlessFormatters() }
}
}
val knownBomConfigurations =
setOf(
"implementation",
"testImplementation",
"androidTestImplementation",
"compileOnly",
"testCompileOnly",
"kapt",
"ksp",
)
fun Project.configureComposeBom(dependencyHandler: DependencyHandler) {
dependencyHandler.apply {
val composeBom = platform(libs.androidx.compose.bom)
configurations
.matching { configuration ->
knownBomConfigurations.any { configuration.name.contains(it, ignoreCase = true) }
}
.configureEach { add(name, composeBom) }
}
}
val jvmTargetVersion = libs.versions.jvmTarget
val publishedJvmTargetVersion = libs.versions.publishedJvmTarget
subprojects {
val isPublished = project.hasProperty("POM_ARTIFACT_ID")
val jvmTargetProject = if (isPublished) publishedJvmTargetVersion else jvmTargetVersion
pluginManager.withPlugin("java") {
configure<JavaPluginExtension> {
toolchain {
languageVersion.set(
JavaLanguageVersion.of(libs.versions.jdk.get().removeSuffix("-ea").toInt())
)
}
}
tasks.withType<JavaCompile>().configureEach {
options.release.set(
jvmTargetProject.map(JavaVersion::toVersion).map { it.majorVersion.toInt() }
)
}
// This is the default base plugin applied on all projects, so safe to add this hook here
configureComposeBom(dependencies)
}
val hasCompose = !project.hasProperty("circuit.noCompose")
plugins.withType<KotlinBasePlugin> {
tasks.withType<KotlinCompilationTask<*>>().configureEach {
// Don't double apply to stub gen
if (this is KaptGenerateStubsTask) {
// TODO due to Anvil we need to force language version 1.9
compilerOptions {
progressiveMode.set(false)
languageVersion.set(KotlinVersion.KOTLIN_1_9)
}
return@configureEach
}
val isWasmTask = name.contains("wasm", ignoreCase = true)
compilerOptions {
if (isWasmTask && this is KotlinJsCompilerOptions) {
// TODO https://youtrack.jetbrains.com/issue/KT-64115
allWarningsAsErrors.set(false)
} else {
allWarningsAsErrors.set(true)
}
if (this is KotlinJvmCompilerOptions) {
jvmTarget.set(
jvmTargetProject
.map(JavaVersion::toVersion)
.map { it.toString() }
.map(JvmTarget::fromTarget)
)
// Stub gen copies args from the parent compilation
if (this@configureEach !is KaptGenerateStubsTask) {
freeCompilerArgs.addAll(
"-Xjsr305=strict",
// Match JVM assertion behavior:
// https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/
"-Xassertions=jvm",
// Potentially useful for static analysis tools or annotation processors.
"-Xemit-jvm-type-annotations",
// Enable new jvm-default behavior
// https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/
"-Xjvm-default=all",
// https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations
"-Xtype-enhancement-improvements-strict-mode",
"-Xjspecify-annotations=strict",
)
}
}
progressiveMode.set(true)
}
}
if (!project.path.startsWith(":samples") && !project.path.startsWith(":internal")) {
extensions.configure<KotlinProjectExtension> { explicitApi() }
}
// region Detekt
project.apply(plugin = "io.gitlab.arturbosch.detekt")
configure<DetektExtension> {
toolVersion = detektVersion
allRules = true
config.from(rootProject.file("config/detekt/detekt.yml"))
buildUponDefaultConfig = true
}
tasks.withType<Detekt>().configureEach {
jvmTarget = "11"
reports {
html.required.set(true)
xml.required.set(true)
txt.required.set(true)
}
}
dependencies.add("detektPlugins", twitterDetektPlugin)
// endregion
}
// Teach Gradle that full guava replaces listenablefuture.
// This bypasses the dependency resolution that transitively bumps listenablefuture to a 9999.0
// version that is empty.
dependencies.modules {
module("com.google.guava:listenablefuture") { replacedBy("com.google.guava:guava") }
}
pluginManager.withPlugin("org.jetbrains.kotlin.kapt") {
tasks.withType<KaptGenerateStubsTask>().configureEach { useK2Kapt.set(true) }
}
pluginManager.withPlugin("com.vanniktech.maven.publish") {
apply(plugin = "org.jetbrains.dokka")
tasks.withType<DokkaTaskPartial>().configureEach {
moduleName.set(project.path.removePrefix(":").replace(":", "/"))
outputDirectory.set(layout.buildDirectory.dir("docs/partial"))
dokkaSourceSets.configureEach {
val readMeProvider = project.layout.projectDirectory.file("README.md")
if (readMeProvider.asFile.exists()) {
includes.from(readMeProvider)
}
if (name.contains("androidTest", ignoreCase = true)) {
suppress.set(true)
}
skipDeprecated.set(true)
// Skip internal packages
perPackageOption {
// language=RegExp
matchingRegex.set(".*\\.internal\\..*")
suppress.set(true)
}
// AndroidX and Android docs are automatically added by the Dokka plugin.
// Add source links
sourceLink {
localDirectory.set(layout.projectDirectory.dir("src").asFile)
val relPath = rootProject.projectDir.toPath().relativize(projectDir.toPath())
remoteUrl.set(
providers.gradleProperty("POM_SCM_URL").map { scmUrl ->
URI("$scmUrl/tree/main/$relPath/src").toURL()
}
)
remoteLineSuffix.set("#L")
}
}
}
apply(plugin = "com.dropbox.dependency-guard")
configure<DependencyGuardPluginExtension> {
if (project.name == "circuit-codegen") {
configuration("runtimeClasspath") {
baselineMap = {
// Remove the version
it.substringBeforeLast(":")
}
}
} else if (project.path == ":circuitx:android") {
// Android-only project
configuration("releaseRuntimeClasspath") {
baselineMap = {
// Remove the version
it.substringBeforeLast(":")
}
}
} else {
configuration("androidReleaseRuntimeClasspath") {
baselineMap = {
// Remove the version
it.substringBeforeLast(":")
}
}
configuration("jvmRuntimeClasspath") {
baselineMap = {
// Remove the version
it.substringBeforeLast(":")
}
}
}
}
configure<MavenPublishBaseExtension> {
publishToMavenCentral(automaticRelease = true)
signAllPublications()
}
}
// Common android config
val commonAndroidConfig: CommonExtension<*, *, *, *, *, *>.() -> Unit = {
compileSdk = 34
if (hasCompose) {
buildFeatures { compose = true }
}
compileOptions {
sourceCompatibility = jvmTargetProject.map(JavaVersion::toVersion).get()
targetCompatibility = jvmTargetProject.map(JavaVersion::toVersion).get()
}
lint {
// https://issuetracker.google.com/issues/243267012
disable += "Instantiatable"
checkTestSources = true
lintConfig = rootProject.file("config/lint/lint.xml")
}
dependencies { add("lintChecks", libs.lints.compose) }
}
// Android library config
pluginManager.withPlugin("com.android.library") {
with(extensions.getByType<LibraryExtension>()) {
commonAndroidConfig()
defaultConfig { minSdk = 21 }
testOptions { targetSdk = 34 }
}
// Single-variant libraries
extensions.configure<LibraryAndroidComponentsExtension> {
beforeVariants { builder ->
if (builder.buildType == "debug") {
builder.enable = false
}
}
}
}
pluginManager.withPlugin("com.android.test") {
with(extensions.getByType<TestExtension>()) {
commonAndroidConfig()
defaultConfig { minSdk = 28 }
}
}
// Android app config
pluginManager.withPlugin("com.android.application") {
with(extensions.getByType<ApplicationExtension>()) {
commonAndroidConfig()
buildTypes {
maybeCreate("debug").apply { matchingFallbacks += listOf("release") }
maybeCreate("release").apply {
isMinifyEnabled = true
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks += listOf("release")
}
}
compileOptions { isCoreLibraryDesugaringEnabled = true }
}
dependencies.add("coreLibraryDesugaring", libs.desugarJdkLibs)
}
pluginManager.withPlugin("org.jetbrains.compose") {
apply(plugin = "org.jetbrains.kotlin.plugin.compose")
configure<ComposeCompilerGradlePluginExtension> { includeSourceInformation = true }
}
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
// Enforce Kotlin BOM
dependencies { add("implementation", platform(libs.kotlin.bom)) }
}
pluginManager.withPlugin("org.jetbrains.kotlin.android") {
// Enforce Kotlin BOM
dependencies { add("implementation", platform(libs.kotlin.bom)) }
}
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
// Enforce Kotlin BOM
configure<KotlinMultiplatformExtension> {
sourceSets {
commonMain {
dependencies {
// KGP doesn't support catalogs https://youtrack.jetbrains.com/issue/KT-55351
implementation(
// https://youtrack.jetbrains.com/issue/KT-58759
project.dependencies.platform(
"org.jetbrains.kotlin:kotlin-bom:${libs.versions.kotlin.get()}"
)
)
}
}
}
}
// Workaround for missing task dependency in WASM
val executableCompileSyncTasks = tasks.withType(DefaultIncrementalSyncTask::class.java)
tasks.withType(KotlinJsTest::class.java).configureEach {
mustRunAfter(executableCompileSyncTasks)
}
}
pluginManager.withPlugin("wtf.emulator.gradle") {
val emulatorWtfToken = providers.gradleProperty("emulatorWtfToken")
configure<EwExtension> {
devices.set(listOf(mapOf("model" to "Pixel2Atd", "version" to "30", "atd" to "true")))
if (emulatorWtfToken.isPresent) {
token.set(emulatorWtfToken)
}
}
// We don't always run emulator.wtf on CI (forks can't access it), so we add this helper
// lifecycle task that depends on connectedCheck as an alternative. We do this only on projects
// that apply emulator.wtf though as we don't want to run _all_ connected checks on CI since
// that would include benchmarks.
tasks.register("ciConnectedCheck") { dependsOn("connectedCheck") }
}
subprojects {
pluginManager.withPlugin("dev.zacsweers.anvil") {
configure<AnvilExtension> {
useKsp(contributesAndFactoryGeneration = true, componentMerging = true)
}
}
}
}