From dd55f9d0bd4718328d9be87968739c80cd1c4794 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 10 May 2020 14:22:33 +0800 Subject: [PATCH] Setup bintray publishing wokrflow --- .github/workflows/bintray.yml | 48 ++++++++ .gitignore | 3 + build.gradle.kts | 15 ++- buildSrc/src/main/kotlin/upload/Bintray.kt | 102 ++++++++++++++++ buildSrc/src/main/kotlin/upload/CuiCloud.kt | 53 +++++++- buildSrc/src/main/kotlin/versions.kt | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- mirai-console-graphical/build.gradle.kts | 53 +------- mirai-console/build.gradle.kts | 127 +++++++++++--------- 9 files changed, 281 insertions(+), 124 deletions(-) create mode 100644 .github/workflows/bintray.yml create mode 100644 buildSrc/src/main/kotlin/upload/Bintray.kt diff --git a/.github/workflows/bintray.yml b/.github/workflows/bintray.yml new file mode 100644 index 000000000..8ec02b178 --- /dev/null +++ b/.github/workflows/bintray.yml @@ -0,0 +1,48 @@ +# This is a basic workflow to help you get started with Actions + +name: Bintray Publish + +# Controls when the action will run. Triggers the workflow on push or pull request +# events but only for the master branch +on: + release: + types: + - created + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: Gradle clean + run: ./gradlew clean + - name: Gradle build + run: ./gradlew build # if test's failed, don't publish + - name: Check keys + run: ./gradlew :mirai-console:ensureBintrayAvailable -Dbintray_user=${{ secrets.BINTRAY_USER }} -Pbintray_user=${{ secrets.BINTRAY_USER }} -Dbintray_key=${{ secrets.BINTRAY_KEY }} -Pbintray_key=${{ secrets.BINTRAY_KEY }} + - name: Gradle :mirai-console:bintrayUpload + run: ./gradlew :mirai-console:bintrayUpload -Dbintray_user=${{ secrets.BINTRAY_USER }} -Pbintray_user=${{ secrets.BINTRAY_USER }} -Dbintray_key=${{ secrets.BINTRAY_KEY }} -Pbintray_key=${{ secrets.BINTRAY_KEY }} + + +# - name: Upload artifact +# uses: actions/upload-artifact@v1.0.0 +# with: +# # Artifact name +# name: mirai-core +# # Directory containing files to upload +# path: "mirai-core/build/libs/mirai-core-*-all.jar" +# - name: Upload artifact +# uses: actions/upload-artifact@v1.0.0 +# with: +# # Artifact name +# name: mirai-core-qqandroid-all +# # Directory containing files to upload +# path: "mirai-core-qqandroid/build/libs/mirai-core-qqandroid-*-all.jar" diff --git a/.gitignore b/.gitignore index 65da8682d..0e3b5b181 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ local.properties # Maven publishing credits keys.properties /plugins/ + +bintray.user.txt +bintray.key.txt \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index e21f6c092..ad1429f2e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -112,17 +112,20 @@ subprojects { } -fun Project.findLatestFile(): Map.Entry? { +fun Project.findLatestFile(): Map.Entry { return File(projectDir, "build/libs").walk() .filter { it.isFile } .onEach { println("all files=$it") } - .filter { it.name.matches(Regex("""${project.name}-([0-9]|\.)*\.jar""")) } + .filter { it.name.matches(Regex("""${project.name}-[0-9][0-9]*(\.[0-9]*)*.*\.jar""")) } .onEach { println("matched file: ${it.name}") } .associateBy { it.nameWithoutExtension.substringAfterLast('-') } .onEach { println("versions: $it") } - .maxBy { - it.key.split('.').foldRightIndexed(0) { index: Int, s: String, acc: Int -> - acc + 100.0.pow(2 - index).toInt() * (s.toIntOrNull() ?: 0) + .maxBy { (version, file) -> + version.split('.').let { + if (it.size == 2) it + "0" + else it + }.reversed().foldIndexed(0) { index: Int, acc: Int, s: String -> + acc + 100.0.pow(index).toInt() * (s.toIntOrNull() ?: 0) } - } + } ?: error("cannot find any file to upload") } diff --git a/buildSrc/src/main/kotlin/upload/Bintray.kt b/buildSrc/src/main/kotlin/upload/Bintray.kt new file mode 100644 index 000000000..f6c792dd1 --- /dev/null +++ b/buildSrc/src/main/kotlin/upload/Bintray.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +@file:Suppress("DuplicatedCode") + +package upload + +import org.gradle.api.Project +import org.gradle.kotlin.dsl.provideDelegate +import java.io.File + +object Bintray { + + @JvmStatic + fun isBintrayAvailable(project: Project): Boolean { + return kotlin.runCatching { + getUser(project) + getKey(project) + }.isSuccess + } + + @JvmStatic + fun getUser(project: Project): String { + kotlin.runCatching { + @Suppress("UNUSED_VARIABLE", "LocalVariableName") + val bintray_user: String by project + return bintray_user + } + + kotlin.runCatching { + @Suppress("UNUSED_VARIABLE", "LocalVariableName") + val bintray_user: String by project.rootProject + return bintray_user + } + + System.getProperty("bintray_user", null)?.let { + return it.trim() + } + + File(File(System.getProperty("user.dir")).parent, "/bintray.user.txt").let { local -> + if (local.exists()) { + return local.readText().trim() + } + } + + File(File(System.getProperty("user.dir")), "/bintray.user.txt").let { local -> + if (local.exists()) { + return local.readText().trim() + } + } + + error( + "Cannot find bintray user, " + + "please specify by creating a file bintray.user.txt in project dir, " + + "or by providing JVM parameter 'bintray_user'" + ) + } + + @JvmStatic + fun getKey(project: Project): String { + kotlin.runCatching { + @Suppress("UNUSED_VARIABLE", "LocalVariableName") + val bintray_key: String by project + return bintray_key + } + + kotlin.runCatching { + @Suppress("UNUSED_VARIABLE", "LocalVariableName") + val bintray_key: String by project.rootProject + return bintray_key + } + + System.getProperty("bintray_key", null)?.let { + return it.trim() + } + + File(File(System.getProperty("user.dir")).parent, "/bintray.key.txt").let { local -> + if (local.exists()) { + return local.readText().trim() + } + } + + File(File(System.getProperty("user.dir")), "/bintray.key.txt").let { local -> + if (local.exists()) { + return local.readText().trim() + } + } + + error( + "Cannot find bintray key, " + + "please specify by creating a file bintray.key.txt in project dir, " + + "or by providing JVM parameter 'bintray_key'" + ) + } + +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/upload/CuiCloud.kt b/buildSrc/src/main/kotlin/upload/CuiCloud.kt index c617d2882..1f26807a9 100644 --- a/buildSrc/src/main/kotlin/upload/CuiCloud.kt +++ b/buildSrc/src/main/kotlin/upload/CuiCloud.kt @@ -15,12 +15,16 @@ import io.ktor.client.request.post import io.ktor.client.statement.HttpResponse import io.ktor.http.isSuccess import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.gradle.api.Project import org.gradle.kotlin.dsl.provideDelegate import java.io.File import java.util.* +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract @Suppress("DEPRECATION") object CuiCloud { @@ -77,13 +81,26 @@ object CuiCloud { val cuiCloudUrl = getUrl(project) val key = getKey(project) + + val bytes = file.readBytes() + runBlocking { - uploadToCuiCloud( - cuiCloudUrl, - key, - "/mirai/${project.name}/${file.nameWithoutExtension}.mp4", - file.readBytes() - ) + var first = true + retryCatching(1000) { + if (!first) { + println() + println() + println("Upload failed. Waiting 15s") + delay(15_000) + } + first = false + uploadToCuiCloud( + cuiCloudUrl, + key, + "/mirai/${project.name}/${file.nameWithoutExtension}.mp4", + bytes + ) + }.getOrThrow() } } @@ -129,6 +146,30 @@ object CuiCloud { } } + +@OptIn(ExperimentalContracts::class) +@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") +@kotlin.internal.InlineOnly +internal inline fun retryCatching(n: Int, block: () -> R): Result { + contract { + callsInPlace(block, InvocationKind.AT_LEAST_ONCE) + } + require(n >= 0) { "param n for retryCatching must not be negative" } + var exception: Throwable? = null + repeat(n) { + try { + return Result.success(block()) + } catch (e: Throwable) { + try { + exception?.addSuppressed(e) + } catch (e: Throwable) { + } + exception = e + } + } + return Result.failure(exception!!) +} + inline fun buildList(builderAction: MutableList.() -> Unit): List { return ArrayList().apply(builderAction) } diff --git a/buildSrc/src/main/kotlin/versions.kt b/buildSrc/src/main/kotlin/versions.kt index 64eeb3ec6..06d9eb893 100644 --- a/buildSrc/src/main/kotlin/versions.kt +++ b/buildSrc/src/main/kotlin/versions.kt @@ -18,7 +18,7 @@ object Versions { } object Kotlin { - const val stdlib = "1.3.71" + const val stdlib = "1.3.72" const val coroutines = "1.3.5" const val serialization = "0.20.0" const val ktor = "1.3.2" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a47f646fb..77f95ee0c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ #Wed Mar 04 22:27:09 CST 2020 -distributionUrl=https\://services.gradle.org/distributions/gradle-6.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists diff --git a/mirai-console-graphical/build.gradle.kts b/mirai-console-graphical/build.gradle.kts index c56bcf821..252e6ac07 100644 --- a/mirai-console-graphical/build.gradle.kts +++ b/mirai-console-graphical/build.gradle.kts @@ -1,5 +1,3 @@ -import java.util.* - plugins { id("kotlinx-serialization") id("org.openjfx.javafxplugin") version "0.0.8" @@ -28,26 +26,7 @@ tasks.withType() { version = Versions.Mirai.consoleGraphical -description = "Console Graphical Version with plugin support for mirai" -bintray { - val keyProps = Properties() - val keyFile = file("../keys.properties") - if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } - if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } - - user = keyProps.getProperty("bintrayUser") - key = keyProps.getProperty("bintrayKey") - setPublications("mavenJava") - setConfigurations("archives") - - pkg.apply { - repo = "mirai" - name = "mirai-console-graphical" - setLicenses("AGPLv3") - publicDownloadNumbers = true - vcsUrl = "https://github.com/mamoe/mirai" - } -} +description = "Graphical frontend for mirai-console" dependencies { compileOnly("net.mamoe:mirai-core:${Versions.Mirai.core}") @@ -71,34 +50,4 @@ tasks.withType { val sourcesJar by tasks.registering(Jar::class) { classifier = "sources" from(sourceSets.main.get().allSource) -} - - -publishing { - /* - repositories { - maven { - // change to point to your repo, e.g. http://my.org/repo - url = uri("$buildDir/repo") - } - }*/ - publications { - register("mavenJava", MavenPublication::class) { - from(components["java"]) - - groupId = rootProject.group.toString() - artifactId = "mirai-console-graphical" - version = Versions.Mirai.consoleGraphical - - pom.withXml { - val root = asNode() - root.appendNode("description", description) - root.appendNode("name", project.name) - root.appendNode("url", "https://github.com/mamoe/mirai") - root.children().last() - } - - artifact(sourcesJar.get()) - } - } } \ No newline at end of file diff --git a/mirai-console/build.gradle.kts b/mirai-console/build.gradle.kts index 5ea5e70a3..2e4eadd7a 100644 --- a/mirai-console/build.gradle.kts +++ b/mirai-console/build.gradle.kts @@ -1,4 +1,5 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import upload.Bintray import java.util.* plugins { @@ -36,65 +37,8 @@ dependencies { version = Versions.Mirai.console -description = "Console with plugin support for mirai" -bintray { - val keyProps = Properties() - val keyFile = file("../keys.properties") - if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } - if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } +description = "Console backend for mirai" - user = keyProps.getProperty("bintrayUser") - key = keyProps.getProperty("bintrayKey") - setPublications("mavenJava") - setConfigurations("archives") - - pkg.apply { - repo = "mirai" - name = "mirai-console" - setLicenses("AGPLv3") - publicDownloadNumbers = true - vcsUrl = "https://github.com/mamoe/mirai" - } -} - -@Suppress("DEPRECATION") -val sourcesJar by tasks.registering(Jar::class) { - classifier = "sources" - from(sourceSets.main.get().allSource) -} - -publishing { - /* - repositories { - maven { - // change to point to your repo, e.g. http://my.org/repo - url = uri("$buildDir/repo") - } - }*/ - publications { - register("mavenJava", MavenPublication::class) { - from(components["java"]) - - groupId = rootProject.group.toString() - artifactId = "mirai-console" - version = version - - pom.withXml { - val root = asNode() - root.appendNode("description", description) - root.appendNode("name", project.name) - root.appendNode("url", "https://github.com/mamoe/mirai") - root.children().last() - } - - artifact(sourcesJar.get()) - } - } -} -repositories { - maven("https://dl.bintray.com/kotlin/kotlin-eap") - mavenCentral() -} val compileKotlin: KotlinCompile by tasks compileKotlin.kotlinOptions { jvmTarget = "1.8" @@ -110,3 +54,70 @@ java { tasks.withType(JavaCompile::class.java) { options.encoding = "UTF8" } + + + +tasks.register("ensureBintrayAvailable") { + doLast { + if (!Bintray.isBintrayAvailable(project)) { + error("bintray isn't available. ") + } + } +} + +if (Bintray.isBintrayAvailable(project)) { + bintray { + val keyProps = Properties() + val keyFile = file("../keys.properties") + if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } + if (keyFile.exists()) keyFile.inputStream().use { keyProps.load(it) } + + user = Bintray.getUser(project) + key = Bintray.getKey(project) + setPublications("mavenJava") + setConfigurations("archives") + + pkg.apply { + repo = "mirai" + name = "mirai-console" + setLicenses("AGPLv3") + publicDownloadNumbers = true + vcsUrl = "https://github.com/mamoe/mirai-console" + } + } + + @Suppress("DEPRECATION") + val sourcesJar by tasks.registering(Jar::class) { + classifier = "sources" + from(sourceSets.main.get().allSource) + } + + publishing { + /* + repositories { + maven { + // change to point to your repo, e.g. http://my.org/repo + url = uri("$buildDir/repo") + } + }*/ + publications { + register("mavenJava", MavenPublication::class) { + from(components["java"]) + + groupId = rootProject.group.toString() + artifactId = "mirai-console" + version = version + + pom.withXml { + val root = asNode() + root.appendNode("description", description) + root.appendNode("name", project.name) + root.appendNode("url", "https://github.com/mamoe/mirai") + root.children().last() + } + + artifact(sourcesJar.get()) + } + } + } +} else println("bintray isn't available. NO PUBLICATIONS WILL BE SET") \ No newline at end of file