From 802dfa868fb95f2d246c574c02557dc4984cb9bf Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Wed, 17 Jul 2024 22:14:07 +0200 Subject: [PATCH] refactor(conan): Port the remaining Jackson based code to KxS Signed-off-by: Frank Viernau --- .../package-managers/conan/build.gradle.kts | 6 +-- .../conan/src/main/kotlin/Conan.kt | 50 +++++++++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/plugins/package-managers/conan/build.gradle.kts b/plugins/package-managers/conan/build.gradle.kts index af71c27846c9a..46caa6c615519 100644 --- a/plugins/package-managers/conan/build.gradle.kts +++ b/plugins/package-managers/conan/build.gradle.kts @@ -40,13 +40,9 @@ dependencies { implementation(projects.utils.ortUtils) implementation(projects.utils.spdxUtils) - implementation(libs.jackson.core) - implementation(libs.jackson.databind) - implementation(libs.jackson.dataformat.yaml) - implementation(libs.jackson.module.kotlin) - implementation(libs.kotlinx.serialization.core) implementation(libs.kotlinx.serialization.json) + implementation(libs.kotlinx.serialization.yaml) funTestImplementation(testFixtures(projects.analyzer)) } diff --git a/plugins/package-managers/conan/src/main/kotlin/Conan.kt b/plugins/package-managers/conan/src/main/kotlin/Conan.kt index 01c0ccbc57270..923b4590aa7e1 100644 --- a/plugins/package-managers/conan/src/main/kotlin/Conan.kt +++ b/plugins/package-managers/conan/src/main/kotlin/Conan.kt @@ -19,11 +19,20 @@ package org.ossreviewtoolkit.plugins.packagemanagers.conan -import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.module.kotlin.readValue +import com.charleskorn.kaml.Yaml +import com.charleskorn.kaml.YamlNode +import com.charleskorn.kaml.YamlScalar +import com.charleskorn.kaml.yamlList +import com.charleskorn.kaml.yamlMap +import com.charleskorn.kaml.yamlScalar import java.io.File +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive + import org.apache.logging.log4j.kotlin.logger import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory @@ -44,14 +53,11 @@ import org.ossreviewtoolkit.model.VcsInfo import org.ossreviewtoolkit.model.config.AnalyzerConfiguration import org.ossreviewtoolkit.model.config.PackageManagerConfiguration import org.ossreviewtoolkit.model.config.RepositoryConfiguration -import org.ossreviewtoolkit.model.jsonMapper -import org.ossreviewtoolkit.model.yamlMapper import org.ossreviewtoolkit.utils.common.CommandLineTool import org.ossreviewtoolkit.utils.common.Os import org.ossreviewtoolkit.utils.common.masked import org.ossreviewtoolkit.utils.common.safeDeleteRecursively import org.ossreviewtoolkit.utils.common.stashDirectories -import org.ossreviewtoolkit.utils.common.textValueOrEmpty import org.ossreviewtoolkit.utils.common.toUri import org.ossreviewtoolkit.utils.ort.ORT_CONFIG_FILENAME import org.ossreviewtoolkit.utils.ort.createOrtTempDir @@ -112,7 +118,7 @@ class Conan( // [2]: https://docs.conan.io/en/latest/configuration/download_cache.html#download-cache private val conanStoragePath = conanHome.resolve("data") - private val pkgInspectResults = mutableMapOf() + private val pkgInspectResults = mutableMapOf() override fun command(workingDir: File?) = "conan" @@ -319,8 +325,10 @@ class Conan( pkgInspectResults.getOrPut(pkgName) { val jsonFile = createOrtTempDir().resolve("inspect.json") run(workingDir, "inspect", pkgName, "--json", jsonFile.absolutePath) - jsonMapper.readTree(jsonFile).also { jsonFile.parentFile.safeDeleteRecursively(force = true) } - }.get(field).textValueOrEmpty() + Json.parseToJsonElement(jsonFile.readText()).jsonObject.also { + jsonFile.parentFile.safeDeleteRecursively(force = true) + } + }.get(field)?.jsonPrimitive?.content.orEmpty() /** * Find the [PackageInfo] that represents the project defined in the definition file. @@ -367,19 +375,21 @@ class Conan( /** * Return the generic map of Conan data for the [id]. */ - private fun readConanData(id: Identifier): Map { + private fun readConanData(id: Identifier): Map { val conanDataFile = conanStoragePath.resolve("${id.name}/${id.version}/_/_/export/conandata.yml") return runCatching { - val conanData = yamlMapper.readValue>(conanDataFile) + val conanData = Yaml.default.parseToYamlNode(conanDataFile.readText()).yamlMap.entries.mapKeys { + it.key.content + } // Replace metadata for all version with metadata for this specific version for convenient access. - conanData.mapValues { (key, value) -> - when (key) { - "patches", "sources" -> value[id.version] + conanData.mapNotNull { (key, value) -> + key to when (key) { + "patches", "sources" -> value.yamlMap[id.version] ?: return@mapNotNull null else -> value } - } + }.toMap() }.getOrDefault(emptyMap()) } @@ -387,15 +397,15 @@ class Conan( * Return the source artifact contained in [conanData], or [RemoteArtifact.EMPTY] if no source artifact is * available. */ - private fun parseSourceArtifact(conanData: Map): RemoteArtifact = + private fun parseSourceArtifact(conanData: Map): RemoteArtifact = runCatching { - val artifactEntry = conanData.getValue("sources") + val artifactEntry = conanData.getValue("sources").yamlMap - val url = artifactEntry["url"].let { urlNode -> - urlNode.takeIf { it.isTextual } ?: urlNode.first() - }.textValueOrEmpty() + val url = checkNotNull(artifactEntry.get("url")).let { urlNode -> + urlNode.takeIf { it is YamlScalar } ?: urlNode.yamlList.items.first() + }.yamlScalar.content - val hashValue = artifactEntry["sha256"].textValueOrEmpty() + val hashValue = artifactEntry.get("sha256")?.content.orEmpty() val hash = Hash.create(hashValue, HashAlgorithm.SHA256.name) RemoteArtifact(url, hash)