Skip to content

Commit

Permalink
refactor(conan): Make the return type of inspectField() nullable
Browse files Browse the repository at this point in the history
Let the caller decide how to handle a non-existing field, for which null
is a more natural value than an empty string. This now also better fits
the `inspectPyFile()` use-case which already is nullable.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Jul 18, 2024
1 parent af632f0 commit 056ce4c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions plugins/package-managers/conan/src/main/kotlin/Conan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class Conan(
id = id,
authors = parseAuthors(pkgInfo),
declaredLicenses = pkgInfo.license.toSet(),
description = inspectField(pkgInfo.displayName, workingDir, "description"),
description = inspectField(pkgInfo.displayName, workingDir, "description").orEmpty(),
homepageUrl = homepageUrl,
binaryArtifact = RemoteArtifact.EMPTY, // TODO: implement me!
sourceArtifact = parseSourceArtifact(conanData),
Expand All @@ -321,18 +321,21 @@ class Conan(
}

/**
* Return the value `conan inspect` reports for the given [field].
* Return the value `conan inspect` reports for the given [field], or null if the field does not exist.
*/
private fun inspectField(pkgName: String, workingDir: File, field: String): String =
pkgInspectResults.getOrPut(pkgName) {
private fun inspectField(pkgName: String, workingDir: File, field: String): String? {
val results = pkgInspectResults.getOrPut(pkgName) {
// Note: While Conan 2 supports inspect output to stdout, Conan 1 does not and a temporary file is required,
// see https://github.com/conan-io/conan/issues/6972.
val jsonFile = createOrtTempDir().resolve("inspect.json")
run(workingDir, "inspect", pkgName, "--json", jsonFile.absolutePath)
Json.parseToJsonElement(jsonFile.readText()).jsonObject.also {
jsonFile.parentFile.safeDeleteRecursively(force = true)
}
}[field]?.jsonPrimitive?.content.orEmpty()
}

return results[field]?.jsonPrimitive?.content
}

/**
* Find the [PackageInfo] that represents the project defined in the definition file.
Expand All @@ -356,8 +359,8 @@ class Conan(
Identifier(
type = "Conan",
namespace = "",
name = inspectField(pkgInfo.displayName, workingDir, "name"),
version = inspectField(pkgInfo.displayName, workingDir, "version")
name = inspectField(pkgInfo.displayName, workingDir, "name").orEmpty(),
version = inspectField(pkgInfo.displayName, workingDir, "version").orEmpty()
)

/**
Expand Down

0 comments on commit 056ce4c

Please sign in to comment.