From 0750e81b4508bd6f28a674c136c519e3702801db Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 6 Feb 2021 08:16:29 +0800 Subject: [PATCH] Setup sonatype remote --- buildSrc/src/main/kotlin/JvmPublishing.kt | 75 +++++++------ buildSrc/src/main/kotlin/keys/SecretKeys.kt | 114 ++++++++++++++++++++ buildSrc/src/main/kotlin/upload/Bintray.kt | 85 ++------------- 3 files changed, 167 insertions(+), 107 deletions(-) create mode 100644 buildSrc/src/main/kotlin/keys/SecretKeys.kt diff --git a/buildSrc/src/main/kotlin/JvmPublishing.kt b/buildSrc/src/main/kotlin/JvmPublishing.kt index ee0b54258..0ff429720 100644 --- a/buildSrc/src/main/kotlin/JvmPublishing.kt +++ b/buildSrc/src/main/kotlin/JvmPublishing.kt @@ -13,6 +13,7 @@ ) import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import keys.SecretKeys import org.gradle.api.Project import org.gradle.api.publish.maven.MavenPublication import org.gradle.api.tasks.bundling.Jar @@ -21,16 +22,6 @@ import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.register import org.gradle.kotlin.dsl.registering -/* - * 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 - */ - - fun Project.configureRemoteRepos() { tasks.register("ensureBintrayAvailable") { doLast { @@ -39,10 +30,27 @@ fun Project.configureRemoteRepos() { } } } + publishing { + // sonatype + val keys = SecretKeys.getCache(project) + repositories { + val sonatype = keys.loadKey("sonatype") + if (sonatype.isValid) { + maven { + // Maven Central + setUrl("https://oss.sonatype.org/service/local/staging/deploy/maven2") - if (isBintrayAvailable()) { - publishing { - repositories { + credentials { + username = sonatype.user + password = sonatype.password + } + } + } else { + println("SonaType is not available") + } + + + if (isBintrayAvailable()) { maven { setUrl("https://api.bintray.com/maven/him188moe/mirai/mirai-core/;publish=1;override=1") @@ -51,7 +59,10 @@ fun Project.configureRemoteRepos() { password = Bintray.getKey(project) } } + } else { + println("bintray isn't available.") } + } } } @@ -66,27 +77,24 @@ inline fun Project.configurePublishing( configureRemoteRepos() apply() - if (!project.isBintrayAvailable()) { - println("bintray isn't available. NO PUBLICATIONS WILL BE SET") - return - } + if (project.isBintrayAvailable()) { + bintray { + user = Bintray.getUser(project) + key = Bintray.getKey(project) - bintray { - user = Bintray.getUser(project) - key = Bintray.getKey(project) + setPublications("mavenJava") + setConfigurations("archives") - setPublications("mavenJava") - setConfigurations("archives") + publish = true + override = true - publish = true - override = true - - pkg.apply { - repo = bintrayRepo - name = bintrayPkgName - setLicenses("AGPLv3") - publicDownloadNumbers = true - vcsUrl = vcs + pkg.apply { + repo = bintrayRepo + name = bintrayPkgName + setLicenses("AGPLv3") + publicDownloadNumbers = true + vcsUrl = vcs + } } } @@ -94,6 +102,10 @@ inline fun Project.configurePublishing( archiveClassifier.set("sources") from(sourceSets["main"].allSource) } + val stubJavadoc = tasks.register("javadocJar", Jar::class) { + @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") + archiveClassifier.set("javadoc") + } publishing { publications { @@ -110,6 +122,7 @@ inline fun Project.configurePublishing( ) artifact(sourcesJar.get()) + artifact(stubJavadoc.get()) } } configGpgSign(this@configurePublishing) diff --git a/buildSrc/src/main/kotlin/keys/SecretKeys.kt b/buildSrc/src/main/kotlin/keys/SecretKeys.kt new file mode 100644 index 000000000..6338cffec --- /dev/null +++ b/buildSrc/src/main/kotlin/keys/SecretKeys.kt @@ -0,0 +1,114 @@ +/* + * Copyright 2019-2021 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 + */ + +package keys + +import org.gradle.api.Project + +open class SecretKeys( + val type: String, + val user: String, + val password: String +) { + class Invalid( + type: String, + override val isDisabled: Boolean = false + ) : SecretKeys(type, "", "") { + override val isValid: Boolean get() = false + override fun requireNotInvalid(): Nothing { + error( + """ + Key $type not found. + Please lease specify by creating a file $type.key in projectDir/build-secret-keys + or by providing JVM parameter '$type.user', `$type.password` + """.trimIndent() + ) + } + } + + companion object { + val keyCaches = mutableMapOf() + + @JvmStatic + fun getCache(project: Project): ProjectKeysCache = + keyCaches.computeIfAbsent(project, SecretKeys::ProjectKeysCache) + } + + class ProjectKeysCache(val project: Project) { + val keys = mutableMapOf() + fun loadKey(type: String) = keys.computeIfAbsent(type, this::loadKey0) + + private fun loadKey0(type: String): SecretKeys { + + project.parent?.let { parent -> + getCache(parent).loadKey(type).takeIf { + it.isValid || it.isDisabled + }?.let { return it } + } + + val secretKeys = project.projectDir.resolve("build-secret-keys") + + kotlin.run { + val secretKeyFile = secretKeys.resolve("$type.disable").takeIf { it.isFile } + ?: secretKeys.resolve("$type.disable.txt") + if (secretKeyFile.isFile) return Invalid(type, true) // Disabled + } + + // Load from secretKeys/$type.key + kotlin.run { + val secretKeyFile = secretKeys.resolve("$type.key").takeIf { it.isFile } + ?: secretKeys.resolve("$type.key.txt") + if (secretKeyFile.isFile) { + secretKeyFile.bufferedReader().use { + return SecretKeys(type, it.readLine().trim(), it.readLine().trim()) + } + } + } + // Load from project/%type.key, user + kotlin.run { + val userFile = project.projectDir.resolve("$type.user.txt") + val keyFile = project.projectDir.resolve("$type.key.txt") + if (userFile.isFile && keyFile.isFile) { + return SecretKeys(type, userFile.readText().trim(), keyFile.readText().trim()) + } + } + + + // Load from property $type.user, $type.password + + fun findProperty(type: String): String? { + val p = project.findProperty(type) + ?: System.getProperty(type) + ?: System.getenv(type) + + return p?.toString() + } + + val tUser = findProperty("$type.user") + ?: findProperty("${type}_user") + + val tPassword = findProperty("$type.password") + ?: findProperty("$type.passwd") + ?: findProperty("$type.key") + ?: findProperty("${type}_password") + ?: findProperty("${type}_passwd") + ?: findProperty("${type}_key") + + if (tUser != null && tPassword != null) { + return SecretKeys(type, tUser, tPassword) + } + + return Invalid(type) + } + } + + open val isValid: Boolean get() = true + open val isDisabled: Boolean get() = false + open fun requireNotInvalid(): SecretKeys = this +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/upload/Bintray.kt b/buildSrc/src/main/kotlin/upload/Bintray.kt index 1041b7e03..615c330b1 100644 --- a/buildSrc/src/main/kotlin/upload/Bintray.kt +++ b/buildSrc/src/main/kotlin/upload/Bintray.kt @@ -7,18 +7,9 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ +import keys.SecretKeys import org.gradle.api.Project -import org.gradle.kotlin.dsl.provideDelegate -import java.io.File -/* - * Copyright 2019-2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 with Mamoe Exceptions license that can be found via the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ fun Project.isBintrayAvailable() = Bintray.isBintrayAvailable(project) @Suppress("DuplicatedCode") @@ -34,76 +25,18 @@ object Bintray { @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'" - ) + return SecretKeys.getCache(project) + .loadKey("bintray") + .requireNotInvalid() + .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'" - ) + return SecretKeys.getCache(project) + .loadKey("bintray") + .requireNotInvalid() + .password } } \ No newline at end of file