Improve PluginDependency.parseFromString

This commit is contained in:
Karlatemp 2020-11-24 12:52:03 +08:00
parent cd2ee20961
commit debe5baae4
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -66,7 +66,7 @@ public data class PluginDependency @JvmOverloads constructor(
public override fun toString(): String = buildString {
append(id)
versionRequirement?.let {
append(":")
append(':')
append(it)
}
if (isOptional) {
@ -83,10 +83,14 @@ public data class PluginDependency @JvmOverloads constructor(
public fun parseFromString(string: String): PluginDependency {
require(string.isNotEmpty()) { "string is empty." }
val optional = string.endsWith('?')
val (id, version) = string.removeSuffix("?").let {
it.substringBeforeLast(':') to it.substringAfterLast(':', "")
val (id, version) = string.removeSuffix("?").let { rule ->
if (rule.contains(':')) {
rule.substringBeforeLast(':') to rule.substringAfterLast(':')
} else {
rule to null
}
}
return PluginDependency(id, version.takeIf(String::isNotBlank), optional)
return PluginDependency(id, version, optional)
}
}