Skip to content

Commit

Permalink
refactor(cocoapods): Port the Podspec parsing to KxS
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Jul 15, 2024
1 parent b3f6311 commit ebc4b63
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
6 changes: 6 additions & 0 deletions plugins/package-managers/cocoapods/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
plugins {
// Apply precompiled plugins.
id("ort-library-conventions")

// Apply third-party plugins.
alias(libs.plugins.kotlinSerialization)
}

dependencies {
Expand All @@ -37,6 +40,9 @@ dependencies {
implementation(projects.utils.ortUtils)
implementation(projects.utils.spdxUtils)

implementation(libs.kotlinx.serialization.core)
implementation(libs.kotlinx.serialization.json)

implementation(libs.jackson.annotations)
implementation(libs.jackson.core)
implementation(libs.jackson.databind)
Expand Down
41 changes: 19 additions & 22 deletions plugins/package-managers/cocoapods/src/main/kotlin/Podspec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,27 @@

package org.ossreviewtoolkit.plugins.packagemanagers.cocoapods

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.JsonTransformingSerializer
import kotlinx.serialization.json.jsonPrimitive

import org.ossreviewtoolkit.model.jsonMapper
import org.ossreviewtoolkit.utils.common.textValueOrEmpty

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
internal data class Podspec(
val name: String = "",
val version: String = "",
@JsonDeserialize(using = LicenseDeserializer::class)
@Serializable(with = LicenseSerializer::class)
val license: String = "",
val summary: String = "",
val homepage: String = "",
val source: Source? = null,
private val subspecs: List<Podspec> = emptyList()
) {
@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class Source(
val git: String? = null,
val tag: String? = null,
Expand All @@ -63,22 +61,21 @@ internal data class Podspec(
}
}

internal fun String.parsePodspec(): Podspec = jsonMapper.readValue<Podspec>(this)
private val JSON = Json { ignoreUnknownKeys = true }

internal fun String.parsePodspec(): Podspec = JSON.decodeFromString<Podspec>(this)

/**
* Handle deserialization of the following two possible representations:
*
* 1. https://github.com/CocoaPods/Specs/blob/f75c24e7e9df1dac6ffa410a6fb30f01e026d4d6/Specs/8/5/e/SocketIOKit/2.0.1/SocketIOKit.podspec.json#L6-L9
* 2. https://github.com/CocoaPods/Specs/blob/f75c24e7e9df1dac6ffa410a6fb30f01e026d4d6/Specs/8/5/e/FirebaseObjects/0.0.1/FirebaseObjects.podspec.json#L6
*/
private class LicenseDeserializer : StdDeserializer<String>(String::class.java) {
override fun deserialize(parser: JsonParser, context: DeserializationContext): String {
val node = parser.codec.readTree<JsonNode>(parser)

return if (node.isTextual) {
node.textValue()
private object LicenseSerializer : JsonTransformingSerializer<String>(String.serializer()) {
override fun transformDeserialize(element: JsonElement): JsonElement =
if (element is JsonObject) {
element["type"]?.jsonPrimitive ?: JsonPrimitive("")
} else {
node["type"].textValueOrEmpty()
element
}
}
}

0 comments on commit ebc4b63

Please sign in to comment.