This commit is contained in:
2022-11-10 23:48:59 +06:00
parent 26a5d20e26
commit 283bc5acb4
12 changed files with 71 additions and 98 deletions

View File

@@ -1,7 +1,6 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@@ -4,6 +4,7 @@ import dev.inmo.kmppscriptbuilder.core.utils.serialFormat
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.url
import io.ktor.client.statement.bodyAsText
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer
@@ -20,9 +21,9 @@ private val commonLicensesListDeserializer = MapSerializer(String.serializer(),
private var licenses: Map<String, License>? = null
suspend fun HttpClient.getLicenses(): Map<String, License> {
val answer = get<String> {
val answer = get {
url("https://licenses.opendefinition.org/licenses/groups/all.json")
}
}.bodyAsText()
return serialFormat.decodeFromString(commonLicensesListDeserializer, answer).also { gotLicenses ->
licenses = gotLicenses
}

View File

@@ -31,23 +31,67 @@ data class MavenConfig(
@Serializable
data class MavenPublishingRepository(
val name: String,
val url: String
val url: String,
val credsType: CredentialsType = CredentialsType.UsernameAndPassword(
"${name.uppercase()}_USER",
"${name.uppercase()}_PASSWORD"
)
) {
private val nameCapitalized by lazy {
name.toUpperCase()
}
val defaultUsernameProperty = "${name.uppercase()}_USER"
val defaultPasswordProperty = "${name.uppercase()}_PASSWORD"
val defaultHeaderValueProperty = "${name.uppercase()}_TOKEN"
fun build(indent: String): String {
val usernameProperty = "${nameCapitalized}_USER"
val passwordProperty = "${nameCapitalized}_PASSWORD"
return """if ((project.hasProperty('${usernameProperty}') || System.getenv('${usernameProperty}') != null) && (project.hasProperty('${passwordProperty}') || System.getenv('${passwordProperty}') != null)) {
maven {
name = "$name"
url = uri("$url")
@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 {
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}')
}
"""
}
}
@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}')
}
"""
}
}
fun buildCheckPart(): String
fun buildCredsPart(): String
}
private val nameCapitalized by lazy {
name.uppercase()
}
fun build(indent: String): String {
return """if (${credsType.buildCheckPart()}) {
maven {
name = "$name"
url = uri("$url")
${credsType.buildCredsPart()}
}
}""".replace("\n", "\n$indent")
}

View File

@@ -1 +0,0 @@
<manifest package="dev.inmo.KotlinPublicationScriptsBuilder.core"/>