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

122 lines
4.1 KiB
Kotlin
Raw Normal View History

2021-03-01 14:17:06 +00:00
package dev.inmo.kmppscriptbuilder.core.models
import kotlinx.serialization.Serializable
2021-05-01 14:12:06 +00:00
const val defaultProjectName = "\${project.name}"
const val defaultProjectDescription = "\${project.name}"
2022-01-04 15:49:16 +00:00
@Serializable
2022-01-04 16:47:37 +00:00
sealed class GpgSigning(val name: String) {
2022-01-04 15:49:16 +00:00
@Serializable
2022-01-04 16:47:37 +00:00
object Disabled : GpgSigning("Disabled")
2022-01-04 15:49:16 +00:00
@Serializable
2022-01-04 16:47:37 +00:00
object Optional : GpgSigning("Optional")
2022-01-04 15:49:16 +00:00
@Serializable
2022-01-04 16:47:37 +00:00
object Enabled : GpgSigning("Enabled")
2022-01-04 15:49:16 +00:00
}
@Serializable
data class MavenConfig(
val name: String,
val description: String,
val url: String,
val vcsUrl: String,
val developers: List<Developer>,
2022-01-04 15:49:16 +00:00
val repositories: List<MavenPublishingRepository> = emptyList(),
val gpgSigning: GpgSigning = GpgSigning.Disabled,
@Deprecated("Replaced with gpgSigning")
val includeGpgSigning: Boolean = false,
)
@Serializable
data class MavenPublishingRepository(
val name: String,
2022-11-10 17:48:59 +00:00
val url: String,
val credsType: CredentialsType = CredentialsType.UsernameAndPassword(
2022-11-16 11:54:16 +00:00
CredentialsType.UsernameAndPassword.defaultUsernameProperty(name),
CredentialsType.UsernameAndPassword.defaultPasswordProperty(name),
2022-11-10 17:48:59 +00:00
)
) {
2022-11-10 17:48:59 +00:00
@Serializable
sealed interface CredentialsType {
@Serializable
object Nothing: CredentialsType {
override fun buildCheckPart(): String = "true"
override fun buildCredsPart(): String = ""
}
@Serializable
data class UsernameAndPassword(
val usernameProperty: String,
val passwordProperty: String
): CredentialsType {
constructor(baseParameter: String) : this(
2022-11-16 11:54:16 +00:00
defaultUsernameProperty(baseParameter),
defaultPasswordProperty(baseParameter)
)
2022-11-10 17:48:59 +00:00
override fun buildCheckPart(): String = "(project.hasProperty('${usernameProperty}') || System.getenv('${usernameProperty}') != null) && (project.hasProperty('${passwordProperty}') || System.getenv('${passwordProperty}') != null)"
override fun buildCredsPart(): String {
return """
credentials {
username = project.hasProperty('${usernameProperty}') ? project.property('${usernameProperty}') : System.getenv('${usernameProperty}')
password = project.hasProperty('${passwordProperty}') ? project.property('${passwordProperty}') : System.getenv('${passwordProperty}')
}
"""
}
2022-11-16 11:54:16 +00:00
companion object {
fun defaultUsernameProperty(name: String): String {
return "${name.uppercase()}_USER"
}
fun defaultPasswordProperty(name: String): String {
return "${name.uppercase()}_PASSWORD"
}
}
2022-11-10 17:48:59 +00:00
}
@Serializable
data class HttpHeaderCredentials(
val headerName: String,
val headerValueProperty: String
): CredentialsType {
override fun buildCheckPart(): String = "project.hasProperty('${headerValueProperty}') || System.getenv('${headerValueProperty}') != null"
override fun buildCredsPart(): String {
return """
credentials(HttpHeaderCredentials) {
name = "$headerName"
value = project.hasProperty('${headerValueProperty}') ? project.property('${headerValueProperty}') : System.getenv('${headerValueProperty}')
}
2022-11-16 15:19:01 +00:00
authentication {
header(HttpHeaderAuthentication)
}
2022-11-10 17:48:59 +00:00
"""
}
2022-11-16 11:54:16 +00:00
companion object {
fun defaultValueProperty(name: String): String {
return "${name.uppercase()}_TOKEN"
}
}
2022-11-10 17:48:59 +00:00
}
fun buildCheckPart(): String
fun buildCredsPart(): String
}
private val nameCapitalized by lazy {
2022-11-10 17:48:59 +00:00
name.uppercase()
}
2021-03-02 10:45:46 +00:00
fun build(indent: String): String {
2022-11-10 17:48:59 +00:00
return """if (${credsType.buildCheckPart()}) {
2021-03-02 10:45:46 +00:00
maven {
name = "$name"
url = uri("$url")
2022-11-10 17:48:59 +00:00
${credsType.buildCredsPart()}
}
}""".replace("\n", "\n$indent")
2021-03-02 10:45:46 +00:00
}
}
val SonatypeRepository = MavenPublishingRepository("sonatype", "https://oss.sonatype.org/service/local/staging/deploy/maven2/")