Skip to content

Commit abb2251

Browse files
update to support central sonatype
1 parent 87f7754 commit abb2251

File tree

7 files changed

+114
-33
lines changed

7 files changed

+114
-33
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package dev.inmo.kmppscriptbuilder.core.export
2+
3+
import dev.inmo.kmppscriptbuilder.core.models.MavenConfig
4+
5+
const val generateCentralSonatypeUploadingPartImports = """import java.nio.charset.StandardCharsets
6+
import java.net.http.HttpClient
7+
import java.net.http.HttpRequest
8+
import java.net.http.HttpResponse"""
9+
const val generateCentralSonatypeUploadingPart = """if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) {
10+
def taskName = "uploadSonatypePublication"
11+
if (rootProject.tasks.names.contains(taskName) == false) {
12+
rootProject.tasks.register(taskName) {
13+
doLast {
14+
def username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
15+
def password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
16+
def bearer = Base64.getEncoder().encodeToString("${"$"}username:${"$"}password".getBytes(StandardCharsets.UTF_8))
17+
18+
def client = HttpClient.newHttpClient()
19+
def request = HttpRequest.newBuilder()
20+
.uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/search/repositories?state=open"))
21+
.GET()
22+
.header("Content-Type", "application/json")
23+
.header("Authorization", "Bearer ${"$"}bearer")
24+
.build()
25+
26+
def response = client.send(request, HttpResponse.BodyHandlers.ofString())
27+
def keys = new ArrayList<String>()
28+
response.body().findAll("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").forEach {
29+
def key = it.find("[^\"]+\"\$").find("[^\"]+")
30+
keys.add(key)
31+
}
32+
keys.forEach {
33+
println("Start uploading ${"$"}it")
34+
def uploadRequest = HttpRequest.newBuilder()
35+
.uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/${"$"}it?publishing_type=user_managed"))
36+
.POST(HttpRequest.BodyPublishers.ofString(""))
37+
.header("Content-Type", "application/json")
38+
.header("Authorization", "Bearer ${"$"}bearer")
39+
.build()
40+
def uploadResponse = client.send(uploadRequest, HttpResponse.BodyHandlers.ofString())
41+
if (uploadResponse.statusCode() != 200) {
42+
throw IllegalStateException("Faced error of uploading for repo with key ${"$"}it. Response: ${"$"}uploadResponse")
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}"""

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/export/GpgSignMavenConfig.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fun GpgSigning.generateMavenConfig() = when (this) {
88
"""
99
if (project.hasProperty("signing.gnupg.keyName")) {
1010
apply plugin: 'signing'
11-
11+
1212
signing {
1313
useGpgCmd()
14-
14+
1515
sign publishing.publications
1616
}
17-
17+
1818
task signAll {
1919
tasks.withType(Sign).forEach {
2020
dependsOn(it)

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/export/js_only/MavenTemplater.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package dev.inmo.kmppscriptbuilder.core.export.js_only
22

3+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPart
4+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPartImports
35
import dev.inmo.kmppscriptbuilder.core.export.generateMavenConfig
46
import dev.inmo.kmppscriptbuilder.core.models.*
57

68
fun MavenConfig.buildJsOnlyMavenConfig(licenses: List<License>): String = """
9+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPartImports\n" else ""}
710
apply plugin: 'maven-publish'
11+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPart\n" else ""}
812
913
task javadocJar(type: Jar) {
1014
archiveClassifier = 'javadoc'

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/export/jvm_only/MavenTemplater.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package dev.inmo.kmppscriptbuilder.core.export.jvm_only
22

3+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPart
4+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPartImports
35
import dev.inmo.kmppscriptbuilder.core.export.generateMavenConfig
46
import dev.inmo.kmppscriptbuilder.core.models.*
57

68
fun MavenConfig.buildJvmOnlyMavenConfig(licenses: List<License>): String = """
9+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPartImports\n" else ""}
710
apply plugin: 'maven-publish'
11+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPart\n" else ""}
812
913
task javadocJar(type: Jar) {
1014
from javadoc

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/export/mpp/MavenTemplater.kt

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package dev.inmo.kmppscriptbuilder.core.export.mpp
22

3+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPart
4+
import dev.inmo.kmppscriptbuilder.core.export.generateCentralSonatypeUploadingPartImports
35
import dev.inmo.kmppscriptbuilder.core.export.generateMavenConfig
46
import dev.inmo.kmppscriptbuilder.core.models.*
57

68
fun MavenConfig.buildMultiplatformMavenConfig(licenses: List<License>): String = """
9+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPartImports\n" else ""}
710
apply plugin: 'maven-publish'
8-
11+
${if (includeCentralSonatypeUploadingScript) "$generateCentralSonatypeUploadingPart\n" else ""}
912
task javadocsJar(type: Jar) {
1013
archiveClassifier = 'javadoc'
1114
}
@@ -24,23 +27,19 @@ publishing {
2427
url = "$vcsUrl"
2528
}
2629
27-
developers {
28-
${developers.joinToString("\n") { """
29-
developer {
30-
id = "${it.id}"
31-
name = "${it.name}"
32-
email = "${it.eMail}"
33-
}
34-
""" }}
30+
developers {${developers.joinToString("\n") { """
31+
developer {
32+
id = "${it.id}"
33+
name = "${it.name}"
34+
email = "${it.eMail}"
35+
}""" }}
3536
}
3637
37-
licenses {
38-
${licenses.joinToString("\n") { """
39-
license {
40-
name = "${it.title}"
41-
url = "${it.url}"
42-
}
43-
""" }}
38+
licenses {${licenses.joinToString("\n") { """
39+
license {
40+
name = "${it.title}"
41+
url = "${it.url}"
42+
}""" }}
4443
}
4544
}
4645
repositories {

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/models/MavenConfig.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ data class MavenConfig(
2626
val gpgSigning: GpgSigning = GpgSigning.Disabled,
2727
@Deprecated("Replaced with gpgSigning")
2828
val includeGpgSigning: Boolean = false,
29+
val includeCentralSonatypeUploadingScript: Boolean = false,
2930
)
3031

3132
@Serializable
@@ -61,8 +62,7 @@ return """
6162
credentials {
6263
username = project.hasProperty('${usernameProperty}') ? project.property('${usernameProperty}') : System.getenv('${usernameProperty}')
6364
password = project.hasProperty('${passwordProperty}') ? project.property('${passwordProperty}') : System.getenv('${passwordProperty}')
64-
}
65-
"""
65+
}"""
6666
}
6767

6868
companion object {
@@ -89,8 +89,7 @@ return """
8989
9090
authentication {
9191
header(HttpHeaderAuthentication)
92-
}
93-
"""
92+
}"""
9493
}
9594

9695
companion object {
@@ -119,3 +118,4 @@ ${credsType.buildCredsPart()}
119118
}
120119

121120
val SonatypeRepository = MavenPublishingRepository("sonatype", "https://oss.sonatype.org/service/local/staging/deploy/maven2/")
121+
val CentralSonatypeRepository = MavenPublishingRepository("sonatype", "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/")

core/src/commonMain/kotlin/dev/inmo/kmppscriptbuilder/core/ui/MavenInfoView.kt

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.inmo.kmppscriptbuilder.core.ui
22

33
import androidx.compose.runtime.*
4+
import dev.inmo.kmppscriptbuilder.core.models.CentralSonatypeRepository
45
import dev.inmo.kmppscriptbuilder.core.models.GpgSigning
56
import dev.inmo.kmppscriptbuilder.core.models.MavenConfig
67
import dev.inmo.kmppscriptbuilder.core.models.SonatypeRepository
@@ -23,22 +24,31 @@ class MavenInfoView : VerticalView("Project information") {
2324
internal var projectVcsUrlProperty by mutableStateOf("")
2425
internal var gpgSignProperty by mutableStateOf<GpgSigning>(GpgSigning.Disabled)
2526
internal var publishToMavenCentralProperty by mutableStateOf(false)
27+
internal var publishToCentralSonatypeProperty by mutableStateOf(false)
28+
internal var includeCentralSonatypeUploadingScriptProperty by mutableStateOf(false)
2629
internal val developersView = DevelopersView()
2730
internal val repositoriesView = RepositoriesView()
2831

2932
var mavenConfig: MavenConfig
3033
get() = MavenConfig(
31-
projectNameProperty.ifBlank { defaultProjectName },
32-
projectDescriptionProperty.ifBlank { defaultProjectDescription },
33-
projectUrlProperty,
34-
projectVcsUrlProperty,
35-
developersView.developers,
36-
repositoriesView.repositories + if (publishToMavenCentralProperty) {
37-
listOf(SonatypeRepository)
34+
name = projectNameProperty.ifBlank { defaultProjectName },
35+
description = projectDescriptionProperty.ifBlank { defaultProjectDescription },
36+
url = projectUrlProperty,
37+
vcsUrl = projectVcsUrlProperty,
38+
developers = developersView.developers,
39+
repositories = repositoriesView.repositories + if (publishToMavenCentralProperty) {
40+
listOf(
41+
if (publishToCentralSonatypeProperty) {
42+
CentralSonatypeRepository
43+
} else {
44+
SonatypeRepository
45+
}
46+
)
3847
} else {
3948
emptyList()
4049
},
41-
gpgSignProperty
50+
gpgSigning = gpgSignProperty,
51+
includeCentralSonatypeUploadingScript = includeCentralSonatypeUploadingScriptProperty
4252
)
4353
set(value) {
4454
projectNameProperty = value.name
@@ -50,9 +60,11 @@ class MavenInfoView : VerticalView("Project information") {
5060
} else {
5161
value.gpgSigning
5262
}
53-
publishToMavenCentralProperty = value.repositories.any { it == SonatypeRepository }
63+
publishToMavenCentralProperty = value.repositories.any { it == SonatypeRepository || it == CentralSonatypeRepository }
5464
developersView.developers = value.developers
55-
repositoriesView.repositories = value.repositories.filter { it != SonatypeRepository }
65+
repositoriesView.repositories = value.repositories.filter { it != SonatypeRepository && it != CentralSonatypeRepository }
66+
publishToCentralSonatypeProperty = value.repositories.any { it == CentralSonatypeRepository }
67+
includeCentralSonatypeUploadingScriptProperty = value.includeCentralSonatypeUploadingScript
5668
}
5769

5870
private val gpgSigningDrawer = GpgSigningOptionDrawerWithView(this)
@@ -100,6 +112,20 @@ class MavenInfoView : VerticalView("Project information") {
100112
publishToMavenCentralProperty,
101113
placeSwitchAtTheStart = true
102114
) { publishToMavenCentralProperty = it }
115+
if (publishToMavenCentralProperty) {
116+
SwitchWithLabel(
117+
"Use Central Sonatype instead of OSSRH (OSSRH has been deprecated)",
118+
publishToCentralSonatypeProperty,
119+
placeSwitchAtTheStart = true
120+
) { publishToCentralSonatypeProperty = it }
121+
if (publishToCentralSonatypeProperty) {
122+
SwitchWithLabel(
123+
"Add 'uploadSonatypePublication' root project task (required for Central Sonatype publishing)",
124+
includeCentralSonatypeUploadingScriptProperty,
125+
placeSwitchAtTheStart = true
126+
) { includeCentralSonatypeUploadingScriptProperty = it }
127+
}
128+
}
103129
developersView.build()
104130
repositoriesView.build()
105131
}

0 commit comments

Comments
 (0)