-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathbuild.gradle.kts
197 lines (171 loc) · 7.48 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
/*
* Copyright 2020 Louis Cognault Ayeva Derman. Use of this source code is governed by the Apache 2.0 license.
*/
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.*
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import retrofit2.Response
import kotlin.time.*
buildscript {
repositories { mavenCentral() }
dependencies.classpath(Square.okHttp3.okHttp)
dependencies.classpath(Square.retrofit2.retrofit)
}
plugins {
id("com.android.library")
kotlin("multiplatform")
}
fun bintrayProperty(keySuffix: String): String = project.property("splitties.bintray.$keySuffix") as String
val bintrayUsername = bintrayProperty("user")
val bintrayApiKey = project.property("bintray_api_key") as String
val isDevVersion = project.isDevVersion
val bintrayRepoName = bintrayProperty(if (isDevVersion) "repo.dev" else "repo.release")
val bintrayPackageName = bintrayProperty("package")
repositories {
maven("https://dl.bintray.com/$bintrayUsername/$bintrayRepoName/") {
name = "bintray/$bintrayUsername/$bintrayRepoName"
credentials {
username = bintrayUsername
password = bintrayApiKey
}
}.ensureGroups("com.louiscad.splitties")
}
tasks.matching { it.name.startsWith("lint") }.configureEach { enabled = false }
android {
setDefaults()
}
fun KotlinTarget.targetsSamePlatformAs(other: KotlinTarget): Boolean = when (this) {
is KotlinNativeTarget -> other is KotlinNativeTarget && konanTarget == other.konanTarget
else -> platformType == other.platformType
}
val KotlinTarget.mainSourceSet get() = when {
platformType == androidJvm && compilations.isEmpty() -> {
project.the<KotlinMultiplatformExtension>().sourceSets.androidMain
}
else -> compilations.getByName("main").defaultSourceSet
}
kotlin {
android()
jvm()
js { browser(); nodejs() }
macosX64()
iosArm32(); iosArm64(); iosX64()
watchosArm32(); watchosArm64(); watchosX86()
mingw(x64 = true)
linux(x64 = true)
val group = "com.louiscad.splitties"
val version = thisLibraryVersion
sourceSets {
commonMain {
dependencies {
// Needs to be put back if published with HMPP-only,
// e.g. without this property: kotlin.mpp.enableCompatibilityMetadataVariant=true,
// Otherwise, the dependencies task fails with an obscure error:
// "Couldn't resolve metadata artifact for ModuleDependencyIdentifier(groupId=org.jetbrains.kotlinx, moduleId=kotlinx-coroutines-core) in configuration ':tools:publication-checker:iosArm32CompileKlibraries'"
//TODO: Report the issue before HMPP becomes the default.
//implementation(KotlinX.coroutines.core)
}
}
}
rootProject.project(":modules").subprojects {
if (plugins.hasPlugin("org.jetbrains.kotlin.multiplatform").not()) return@subprojects
val splitTargets = the<KotlinMultiplatformExtension>().targets.also {
check(it.isNotEmpty())
}
targets.forEach { target ->
if (target.platformType == common) return@forEach
val sourceSet: KotlinSourceSet = target.mainSourceSet
if (splitTargets.any { it.targetsSamePlatformAs(target) }) {
sourceSet.dependencies {
implementation("$group:splitties-${project.name}") {
version { strictly(version) }
}
}
}
}
}
rootProject.project(":fun-packs").subprojects {
if (plugins.hasPlugin("org.jetbrains.kotlin.multiplatform").not()) return@subprojects
val splitTargets = the<KotlinMultiplatformExtension>().targets.also {
check(it.isNotEmpty())
}
targets.forEach { target ->
if (target.platformType == common) return@forEach
val sourceSet: KotlinSourceSet = target.mainSourceSet
if (splitTargets.any { it.targetsSamePlatformAs(target) }) {
sourceSet.dependencies {
implementation("$group:splitties-fun-pack-${project.name}") {
version { strictly(version) }
}
}
}
}
}
}
rootProject.tasks.register("publishBintrayRelease") {
group = "publishing"
description = "Publishes the artifacts uploaded on bintray for this version"
dependsOn(tasks.named("build")) // Ensure publish happens after concurrent build check.
@Suppress("experimental_is_not_enabled")
@OptIn(ExperimentalTime::class)
doFirst {
// These durations might need to be raised for larger projects...
// ...(but at this point, isn't the project too large?)
val requestReadTimeout = 10.minutes
val retryBackOff = 30.seconds
val giveUpAfter = 1.hours
val subject = bintrayUsername
val repo = bintrayRepoName
val version = thisLibraryVersion
val `package` = bintrayPackageName
val request = okhttp3.Request.Builder()
.header(
name = "Authorization",
value = okhttp3.Credentials.basic(username = bintrayUsername, password = bintrayApiKey)
)
// Bintray API reference: https://bintray.com/docs/api/#_publish_discard_uploaded_content
.url("https://bintray.com/api/v1/content/$subject/$repo/$`package`/$version/publish")
.post("""{"publish_wait_for_secs":-1}""".toRequestBody("application/json".toMediaType()))
.build()
val httpClient = okhttp3.OkHttpClient.Builder()
.readTimeout(requestReadTimeout.toJavaDuration())
.build()
/**
* If [isLastAttempt] is true, any failure will be thrown as an exception.
*/
fun attemptPublishing(isLastAttempt: Boolean = false): Boolean {
println("Attempting bintray publish")
try {
httpClient.newCall(request).execute().use { response ->
if (response.isSuccessful) {
println(response.body?.string())
return true
}
if (isLastAttempt.not()) when (val code = response.code) {
408, 405 -> {
logger.error("Publish attempt failed (http $code)")
logger.info(response.body?.string() ?: "")
return false
}
}
throw retrofit2.HttpException(Response.error<Any?>(response.code, response.body!!))
}
} catch (e: java.io.IOException) {
if (isLastAttempt) throw e
logger.error("Publish attempt failed with ${e.javaClass.simpleName}: ${e.message}")
return false
}
}
println("Will attempt bintray publishing on ${request.url}")
val deadline = TimeSource.Monotonic.markNow() + giveUpAfter
do {
val didSucceed = attemptPublishing(isLastAttempt = deadline.hasPassedNow())
if (didSucceed.not()) Thread.sleep(retryBackOff.toLongMilliseconds())
} while (didSucceed.not())
println("Bintray publishing successful!")
}
}