Skip to content

Commit c06e6d4

Browse files
committed
create createGithubRelease task
1 parent 4af5b29 commit c06e6d4

File tree

5 files changed

+119
-14
lines changed

5 files changed

+119
-14
lines changed

build.gradle.kts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.*
2+
import org.kohsuke.github.GitHub
3+
14
plugins {
25
idea
36
}
@@ -28,3 +31,25 @@ allprojects {
2831
}
2932
}
3033
}
34+
35+
tasks.create("clearAllDraftReleases") {
36+
val file = rootProject.file("local.properties")
37+
val properties = Properties()
38+
properties.load(file.inputStream())
39+
40+
val repo = properties["github_repo"]
41+
check(repo != null && repo is String) { "github_repo not provided in local.properties " }
42+
val token = properties["github_api_key"]
43+
check(token != null && token is String) { "github_api_key not provided in local.properties" }
44+
45+
46+
doFirst {
47+
val github = GitHub.connectUsingOAuth(token)
48+
49+
github.getRepository(repo).listReleases().filter { it.isDraft }.forEach { release ->
50+
val name = release.name
51+
release.delete()
52+
println("Deleted release $name")
53+
}
54+
}
55+
}

buildSrc/build.gradle.kts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ plugins {
22
`kotlin-dsl`
33
}
44

5-
repositories {
6-
google()
7-
mavenCentral()
8-
maven("https://jitpack.io")
9-
maven("https://api.xposed.info/")
10-
}
11-
125
dependencies {
6+
implementation(kotlin("bom"))
137
implementation("com.android.tools.build:gradle:8.1.1")
148
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10")
9+
implementation("org.kohsuke:github-api:1.316")
1510
}

buildSrc/settings.gradle.kts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@file:Suppress("UnstableApiUsage")
2+
3+
pluginManagement {
4+
repositories {
5+
gradlePluginPortal()
6+
google()
7+
mavenCentral()
8+
}
9+
}
10+
dependencyResolutionManagement {
11+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
17+
maven("https://jitpack.io")
18+
maven("https://api.xposed.info/")
19+
}
20+
}

buildSrc/src/main/kotlin/common.gradle.kts

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import java.io.FileInputStream
22
import java.util.*
33
import com.android.build.gradle.BaseExtension
44
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
5+
import org.kohsuke.github.GitHub
56

67
val isAndroid = plugins.hasPlugin("com.android.application")
78
val isAndroidLib = plugins.hasPlugin("com.android.library")
@@ -18,8 +19,20 @@ if (isKotlinAndroid) println("kotlin android")
1819
val javaVersion = JavaVersion.VERSION_17
1920
val javaVersionInt = javaVersion.majorVersion.toInt()
2021

22+
val commitCountExec = providers.exec {
23+
executable("git")
24+
args("rev-list", "--count", "HEAD", projectDir.absolutePath)
25+
}
26+
val commitCount = commitCountExec.standardOutput.asText.get().trim().toInt()
27+
28+
val commitHashExec = providers.exec {
29+
executable("git")
30+
args("rev-parse", "--short", "HEAD")
31+
}
32+
33+
val commitHash = commitHashExec.standardOutput.asText.get().trim()
34+
2135
if (isAndroid || isAndroidLib) {
22-
2336
val android = extensions.getByType<BaseExtension>()
2437
with(android) {
2538

@@ -41,11 +54,20 @@ if (isAndroid || isAndroidLib) {
4154
}
4255
defaultConfig {
4356
signingConfig = signingConfigs.getByName("release")
44-
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "../proguard-rules.pro")
45-
46-
if (isAndroidLib) {
47-
consumerProguardFiles("consumer-rules.pro")
48-
}
57+
}
58+
} else {
59+
defaultConfig {
60+
signingConfig = signingConfigs.getByName("debug")
61+
}
62+
}
63+
64+
defaultConfig {
65+
versionCode = commitCount
66+
versionName = "$commitCount-$commitHash"
67+
68+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "../proguard-rules.pro")
69+
if (isAndroidLib) {
70+
consumerProguardFiles("consumer-rules.pro")
4971
}
5072
}
5173

@@ -73,6 +95,49 @@ if (isAndroid || isAndroidLib) {
7395
}
7496
}
7597

98+
if (isAndroid) {
99+
val android = extensions.getByType<BaseExtension>()
100+
101+
tasks.create("createGithubRelease") {
102+
val file = rootProject.file("local.properties")
103+
val properties = Properties()
104+
properties.load(file.inputStream())
105+
106+
val repo = properties["github_repo"]
107+
check(repo != null && repo is String) { "github_repo not provided in local.properties " }
108+
val token = properties["github_api_key"]
109+
check(token != null && token is String) { "github_api_key not provided in local.properties" }
110+
111+
dependsOn("assembleRelease")
112+
113+
doFirst {
114+
val packageRelease = project.tasks.getByName<DefaultTask>("packageRelease")
115+
116+
val outputs = packageRelease.outputs.files
117+
val apks = outputs.filter { it.isDirectory }.flatMap { it.listFiles { file -> file.extension == "apk" }!!.toList() }
118+
119+
val github = GitHub.connectUsingOAuth(token)
120+
val repository = github.getRepository(repo)
121+
122+
val tagName = "${android.namespace}-v$commitCount"
123+
val name = "${project.name}-v$commitCount"
124+
125+
if (repository.getReleaseByTagName(tagName) != null) {
126+
println("Release $name already exists")
127+
return@doFirst
128+
}
129+
130+
val release = repository.createRelease(tagName).name(name).draft(true).create()
131+
132+
apks.forEach {
133+
release.uploadAsset("${project.name}-v$commitCount.apk", it.inputStream(), "application/vnd.android.package-archive")
134+
}
135+
136+
println("Created release ${release.name}: ${release.htmlUrl}")
137+
}
138+
}
139+
}
140+
76141
tasks.withType<KotlinCompile> {
77142
kotlinOptions.jvmTarget = javaVersion.toString()
78143
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ android.enableResourceOptimizations=false
1111
android.defaults.buildfeatures.buildconfig=true
1212
android.nonTransitiveRClass=true
1313
android.nonFinalResIds=true
14-
org.gradle.unsafe.configuration-cache=true
14+
org.gradle.unsafe.configuration-cache=false

0 commit comments

Comments
 (0)