[publish] Delete GpgSigner

This commit is contained in:
Karlatemp 2022-11-29 02:37:41 +08:00
parent 531ef65f5e
commit 889a78e572
No known key found for this signature in database
GPG Key ID: BA173CA2B9956C59
8 changed files with 2 additions and 232 deletions

View File

@ -46,8 +46,6 @@ plugins {
osDetector = osdetector
BuildSrcRootProjectHolder.value = rootProject
GpgSigner.setup(project)
analyzes.CompiledCodeVerify.run { registerAllVerifyTasks() }
allprojects {

View File

@ -1,108 +0,0 @@
/*
* 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
*/
import org.gradle.api.Project
import java.io.File
open class GpgSigner(private val workdir: File) {
private val workdirParent by lazy { workdir.parentFile ?: error("Assertion error: No parent file of $workdir") }
private val workdirName by lazy { workdir.name }
fun verbose(msg: String) {
println("[GPG SIGN] [Verbose] $msg")
}
@Suppress("RemoveExplicitTypeArguments")
private val verbosePrintOnce by lazy<Unit> {
verbose("GPG Signer working dir: $workdir")
verbose("GPG command working dir: $workdirParent")
}
constructor(workdir: String) : this(File(workdir))
object NoopSigner : GpgSigner("build/gpg-noop") {
override fun processGpg(vararg cmds: String) {
}
override fun importKey(file: File) {
}
override fun doSign(file: File) {
}
}
companion object {
private var initialized: Boolean = false
var signer: GpgSigner = NoopSigner
fun setup(project: Project) {
if (initialized) return
initialized = true
val rootProject = project.rootProject
val gpg = rootProject.projectDir.resolve("build-gpg-sign")
gpg.mkdirs()
val keyFile = gpg.resolve("keys.gpg")
val keyFilePub = gpg.resolve("keys.gpg.pub")
if (keyFile.isFile) {
val homedir = gpg.resolve("homedir")
signer = GpgSigner(homedir.absolutePath)
if (!homedir.resolve("pubring.kbx").isFile) {
signer.importKey(keyFile)
if (keyFilePub.isFile) {
signer.importKey(keyFilePub)
} else {
println("[GPG SIGN] Missing public key storage")
println("[GPG SIGN] GPG Sign 2nd verity may failed.")
}
}
} else {
println("[GPG SIGN] GPG Key not found.")
println("[GPG SIGN] GPG Signer will not setup")
println("[GPG SIGN] Key file location: $keyFile")
}
}
}
open fun processGpg(
vararg cmds: String
) {
workdir.mkdirs()
verbosePrintOnce
val response = ProcessBuilder().command(ArrayList<String>().apply {
add("gpg")
add("--homedir"); add(workdirName)
addAll(cmds)
}.also {
verbose("Processing " + it.joinToString(" "))
}).directory(workdirParent)
.inheritIO()
.start()
.waitFor()
if (response != 0) {
error("Exit Response $response")
}
}
open fun importKey(file: File) {
processGpg("--batch", "--import", file.toString())
}
open fun doSign(file: File) {
if (!file.isFile) {
println("[GPG SIGN] $file not a file")
return
}
println("[GPG SIGN] Signing $file")
File("${file.path}.asc").delete()
processGpg("-a", "--batch", "--no-tty", "--detach-sig", "--sign", file.toString())
processGpg("--verify", "$file.asc", file.toString())
}
}

View File

@ -25,9 +25,6 @@ import java.io.File
fun Project.configureRemoteRepos() {
tasks.register("ensureMavenCentralAvailable") {
doLast {
if (GpgSigner.signer == GpgSigner.NoopSigner) {
error("GPG Signer isn't available.")
}
val keys = SecretKeys.getCache(project)
if (!keys.loadKey("sonatype").isValid) {
error("Maven Central isn't available.")
@ -86,7 +83,6 @@ inline fun Project.configurePublishing(
artifactId: String,
vcs: String = "https://github.com/mamoe/mirai",
addProjectComponents: Boolean = true,
setupGpg: Boolean = true,
skipPublicationSetup: Boolean = false,
addShadowJar: Boolean = true
) {
@ -132,8 +128,5 @@ inline fun Project.configurePublishing(
shadowJar?.get()?.let { artifact(it) }
}
}
if (setupGpg) {
configGpgSign(this@configurePublishing)
}
}
}

View File

@ -80,7 +80,6 @@ fun Project.configureMppPublishing() {
}
}
}
configGpgSign(this@configureMppPublishing)
}
}
}

View File

@ -1,103 +0,0 @@
/*
* 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
*/
import org.gradle.api.Project
import org.gradle.api.internal.tasks.DefaultTaskDependency
import org.gradle.api.internal.tasks.TaskDependencyInternal
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenArtifact
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.internal.artifact.AbstractMavenArtifact
import org.gradle.api.publish.maven.internal.publication.DefaultMavenPublication
import java.io.File
open class GPGSignMavenArtifact(
private val delegate: MavenArtifact,
private val tasks: TaskDependencyInternal = TaskDependencyInternal.EMPTY
) : AbstractMavenArtifact() {
override fun getFile(): File {
return File(delegate.file.path + ".asc")
}
override fun shouldBePublished(): Boolean = (delegate as? AbstractMavenArtifact)?.shouldBePublished() ?: true
override fun getDefaultExtension(): String = delegate.extension + ".asc"
override fun getDefaultClassifier(): String = delegate.classifier ?: ""
override fun getDefaultBuildDependencies(): TaskDependencyInternal = tasks
}
class NameCounter(val name: String) {
private var counter = 0
val nextName: String
get() = name + if (counter == 0) {
counter = 1; ""
} else {
counter++; counter
}
}
object PublishingAccess {
fun getMetadataArtifacts(publication: MavenPublication): Collection<MavenArtifact> {
if (publication is DefaultMavenPublication) {
@Suppress("UNCHECKED_CAST")
return DefaultMavenPublication::class.java.getDeclaredField("metadataArtifacts")
.also { it.isAccessible = true }
.get(publication) as Collection<MavenArtifact>
}
return emptyList()
}
}
fun PublishingExtension.configGpgSign(project: Project) {
if (GpgSigner.signer === GpgSigner.NoopSigner) {
return
}
val tasks = DefaultTaskDependency()
val signArtifactsGPG = NameCounter("signArtifactsGPG")
publications.forEach { publication ->
if (publication is MavenPublication) {
val artifacts0: Collection<Pair<Collection<MavenArtifact>, (MavenArtifact) -> Unit>> = listOf(
publication.artifacts to { publication.artifact(it) }, // main artifacts
PublishingAccess.getMetadataArtifacts(publication).let { artifacts -> // pom files
if (artifacts is MutableCollection<MavenArtifact>) {
artifacts to { artifacts.add(it) }
} else {
artifacts to { publication.artifact(it) }
}
}
)
val allArtifacts = artifacts0.flatMap { it.first }.toList()
if (allArtifacts.isNotEmpty()) {
tasks.add(project.tasks.create(signArtifactsGPG.nextName) {
group = "publishing"
doLast {
allArtifacts.forEach { artifact ->
if ((artifact as? AbstractMavenArtifact)?.shouldBePublished() != false) {
GpgSigner.signer.doSign(artifact.file)
}
}
}
allArtifacts.forEach {
dependsOn(it.buildDependencies)
}
})
artifacts0.forEach { (artifacts, artifactsRegister) ->
artifacts.toList().forEach { artifact ->
logPublishing("gpg sign for artifact ${artifact.smartToString()}")
artifactsRegister(GPGSignMavenArtifact(artifact, tasks))
}
}
}
}
}
}

View File

@ -36,13 +36,8 @@ dependencies {
configurePublishing(
"mirai-bom",
addProjectComponents = false,
setupGpg = false,
)
publishing.publications.getByName<MavenPublication>("mavenJava") {
from(components["javaPlatform"])
}
publishing {
configGpgSign(project)
}

View File

@ -32,11 +32,10 @@ version = Versions.consoleTerminal
description = "Console Terminal CLI frontend for mirai"
configurePublishing("mirai-console-terminal", setupGpg = false, addShadowJar = false)
configurePublishing("mirai-console-terminal", addShadowJar = false)
val shadowJar = registerRegularShadowTaskForJvmProject(listOf(shadow))
publishing {
publications.getByName("mavenJava", MavenPublication::class) {
artifacts.artifact(shadowJar)
}
configGpgSign(project)
}

View File

@ -34,16 +34,13 @@ val shadow = configureRelocatedShadowJarForJvmProject(kotlin)
if (System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING")?.toBoolean() != true) {
// Do not publish -all jars to snapshot server since they are too large.
configurePublishing("mirai-core-all", addShadowJar = false, setupGpg = false)
configurePublishing("mirai-core-all", addShadowJar = false)
publications {
getByName("mavenJava", MavenPublication::class) {
artifact(shadow)
}
}
publishing {
configGpgSign(project)
}
tasks.getByName("publishMavenJavaPublicationToMavenLocal").dependsOn(shadow)
tasks.findByName("publishMavenJavaPublicationToMavenCentralRepository")?.dependsOn(shadow)