-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle.kts
320 lines (260 loc) · 11.9 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
@file:Suppress("UnstableApiUsage")
import java.io.FileInputStream
import java.util.*
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension
import com.android.build.gradle.BaseExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import com.android.build.gradle.tasks.PackageAndroidArtifact
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.kohsuke.github.GHReleaseBuilder
import org.kohsuke.github.GitHub
buildscript {
dependencies {
classpath("com.android.tools.build:gradle:8.7.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21")
classpath("org.kohsuke:github-api:1.321")
}
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
allprojects {
// apply in afterEvaluate because other plugins need to be loaded first
afterEvaluate {
val isAndroid = plugins.hasPlugin("com.android.application")
val isAndroidLib = plugins.hasPlugin("com.android.library")
// val isJavaLib = plugins.hasPlugin("java-library")
val isKotlinLib = plugins.hasPlugin("org.jetbrains.kotlin.jvm")
val isKotlinAndroid = plugins.hasPlugin("org.jetbrains.kotlin.android")
// if (isAndroid) println("android")
// if (isAndroidLib) println("android lib")
// if (isJavaLib) println("java lib")
// if (isKotlinLib) println("kotlin lib")
// if (isKotlinAndroid) println("kotlin android")
val javaVersion = JavaVersion.VERSION_17
val commitCount = getCommitCount()
val commitHash = getCommitHash()
val workingTreeClean = getWorkingTreeClean()
val allCommitsPushed = getAllCommitsPushed()
if (isAndroid || isAndroidLib) {
val android = extensions.getByType<BaseExtension>()
with(android) {
val propsFile = rootProject.file("keystore.properties")
if (propsFile.exists()) {
val props = Properties()
props.load(FileInputStream(propsFile))
signingConfigs {
maybeCreate("release").apply {
storeFile = storeFile ?: rootProject.file(props["storeFile"].toString())
check(storeFile != null && storeFile!!.exists()) { "keystore does not exist" }
storePassword = storePassword ?: props["storePassword"].toString()
keyAlias = keyAlias ?: props["keyAlias"].toString()
keyPassword = keyPassword ?: props["keyPassword"].toString()
maybeCreate("debug").let { debug ->
debug.storeFile = storeFile
debug.storePassword = storePassword
debug.keyAlias = keyAlias
debug.keyPassword = keyPassword
}
}
}
defaultConfig {
signingConfig = signingConfigs.getByName("release")
}
} else {
defaultConfig {
signingConfig = signingConfigs.getByName("debug")
}
}
defaultConfig {
versionCode = commitCount
versionName = "$commitCount${if (workingTreeClean) "-" else "+"}$commitHash"
if (isAndroidLib) {
proguardFiles("../proguard-rules.pro", "./proguard-rules.pro")
consumerProguardFiles("consumer-rules.pro")
} else {
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "../proguard-rules.pro")
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = true
multiDexEnabled = false
if (isAndroid) {
isShrinkResources = true
}
}
getByName("debug") {
isMinifyEnabled = false
isShrinkResources = false
multiDexEnabled = false
versionNameSuffix = "-dev"
}
}
compileOptions {
sourceCompatibility = javaVersion
targetCompatibility = javaVersion
}
}
}
if (isAndroid) {
val common = extensions.getByType<ApplicationExtension>()
with(common) {
compileSdk = 34
dependenciesInfo {
includeInApk = false
includeInBundle = false
}
buildFeatures {
buildConfig = true
}
lint {
disable += "DiscouragedApi"
disable += "ExpiredTargetSdkVersion"
disable += "MissingApplicationIcon"
disable += "MissingPermission"
disable += "OldTargetApi"
disable += "PrivateApi"
disable += "UnusedAttribute"
}
}
}
if (isAndroidLib) {
val common = extensions.getByType<LibraryExtension>()
with(common) {
compileSdk = 34
}
}
if (isAndroid) {
val android = extensions.getByType<BaseExtension>()
var logString: String? = null
tasks.register("createGithubRelease") {
val properties = Properties()
val file = rootProject.file("local.properties")
if (file.exists()) {
properties.load(file.inputStream())
}
val repo = properties["github_repo"]
val token = properties["github_api_key"]
check(repo != null && repo is String) { "github_repo not provided in local.properties" }
check(token != null && token is String) { "github_api_key not provided in local.properties" }
if (workingTreeClean && allCommitsPushed) {
dependsOn("assembleRelease")
}
doFirst {
check(workingTreeClean) { "Commit all changes before creating release" }
check(allCommitsPushed) { "Push to remote before creating release" }
val packageRelease = project.tasks.getByName<DefaultTask>("packageRelease")
val outputs = packageRelease.outputs.files
val apks = outputs.filter { it.isDirectory }.flatMap { it.listFiles { file -> file.extension == "apk" }!!.toList() }
val github = GitHub.connectUsingOAuth(token)
val repository = github.getRepository(repo)
val tagName = "${android.namespace}-v$commitCount"
val name = "${project.name}-v$commitCount"
if (repository.getReleaseByTagName(tagName) != null) {
logString = "Release $name already exists"
return@doFirst
}
val release = repository.createRelease(tagName).name(name).draft(true).makeLatest(GHReleaseBuilder.MakeLatest.FALSE).create()
apks.forEach {
release.uploadAsset("${project.name}-v$commitCount.apk", it.inputStream(), "application/vnd.android.package-archive")
}
logString = "Created release ${release.name}: ${release.htmlUrl}"
}
@Suppress("DEPRECATION") gradle.buildFinished {
if (logString != null) {
println(logString)
}
}
}
android.packagingOptions {
resources.excludes += listOf(
"**/*.kotlin_builtins",
"**/*.kotlin_metadata",
"**/*.kotlin_module",
"kotlin-tooling-metadata.json",
)
}
tasks.withType<PackageAndroidArtifact> {
doFirst { appMetadata.asFile.get().writeText("") }
}
}
tasks.withType<KotlinCompile> {
compilerOptions.jvmTarget = JvmTarget.fromTarget(javaVersion.toString())
}
tasks.withType<JavaCompile> {
sourceCompatibility = javaVersion.toString()
targetCompatibility = javaVersion.toString()
}
if (isAndroid || isAndroidLib) {
dependencies {
add("compileOnly", "de.robv.android.xposed:api:82")
}
}
if (isKotlinLib || isKotlinAndroid) {
dependencies {
add("implementation", "org.jetbrains:annotations:26.0.1")
}
}
}
}
tasks.create("clearAllDraftReleases") {
val file = rootProject.file("local.properties")
val properties = Properties()
properties.load(file.inputStream())
val repo = properties["github_repo"]
val token = properties["github_api_key"]
doFirst {
check(repo != null && repo is String) { "github_repo not provided in local.properties" }
check(token != null && token is String) { "github_api_key not provided in local.properties" }
val github = GitHub.connectUsingOAuth(token)
github.getRepository(repo).listReleases().filter { it.isDraft }.forEach { release ->
val name = release.name
release.delete()
println("Deleted release $name")
}
}
}
//<editor-fold desc="Git">
fun Project.getCommitCountExec() = providers.exec {
executable("git")
args("rev-list", "--count", "HEAD")
args(projectDir.absolutePath)
args(rootProject.file("gradle").absolutePath)
rootProject.projectDir.listFiles { file -> file.isFile && file.extension != "md" }!!.forEach { args(it.absolutePath) }
extensions.findByType<BaseExtension>()?.let {
val metadata = rootProject.file("metadata").resolve(it.namespace!!)
if (metadata.exists()) {
args(metadata)
}
}
}
fun Project.getCommitHashExec() = providers.exec {
executable("git")
args("rev-parse", "--short", "HEAD")
}
fun Project.getWorkingTreeCleanExec() = providers.exec {
executable("git")
args("diff", "--quiet", "--exit-code", rootDir.absolutePath)
isIgnoreExitValue = true
}
fun Project.getAllCommitsPushedExec(): ExecOutput {
providers.exec {
executable("git")
args("fetch")
isIgnoreExitValue = true
}
return providers.exec {
executable("git")
args("diff", "--quiet", "--exit-code", "origin/main..main")
isIgnoreExitValue = true
}
}
fun Project.getCommitCount() = getCommitCountExec().standardOutput.asText.get().trim().toInt()
fun Project.getCommitHash() = getCommitHashExec().standardOutput.asText.get().trim()
fun Project.getWorkingTreeClean() = getWorkingTreeCleanExec().result.orNull?.exitValue == 0
fun Project.getAllCommitsPushed() = getAllCommitsPushedExec().result.orNull?.exitValue == 0
//</editor-fold>