Skip to content

Commit 3f2aa2c

Browse files
nnobelissschuberth
authored andcommitted
refactor(conan): Change the model to introduce an interface PackageInfo
This interface contains the properties common to all [PackageInfo], regardless of their Conan version. This is preliminary work for the support of Conan 2. Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent 5bbafe5 commit 3f2aa2c

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

plugins/package-managers/conan/src/main/kotlin/ConanV1Handler.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
6868
).requireSuccess()
6969
}
7070

71-
val pkgInfos = parsePackageInfos(jsonFile).also { jsonFile.parentFile.safeDeleteRecursively() }
71+
val pkgInfos = parsePackageInfosV1(jsonFile).also { jsonFile.parentFile.safeDeleteRecursively() }
7272

7373
val packageList = removeProjectPackage(pkgInfos, definitionFile.name)
7474
val packages = parsePackages(packageList, workingDir)
@@ -122,7 +122,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
122122
/**
123123
* Return the map of packages and their identifiers which are contained in [pkgInfos].
124124
*/
125-
private fun parsePackages(pkgInfos: List<PackageInfo>, workingDir: File): Map<String, Package> =
125+
private fun parsePackages(pkgInfos: List<PackageInfoV1>, workingDir: File): Map<String, Package> =
126126
pkgInfos.associate { pkgInfo ->
127127
val pkg = parsePackage(pkgInfo, workingDir)
128128
"${pkg.id.name}:${pkg.id.version}" to pkg
@@ -131,7 +131,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
131131
/**
132132
* Return the [Package] parsed from the given [pkgInfo].
133133
*/
134-
private fun parsePackage(pkgInfo: PackageInfo, workingDir: File): Package {
134+
private fun parsePackage(pkgInfo: PackageInfoV1, workingDir: File): Package {
135135
val homepageUrl = pkgInfo.homepage.orEmpty()
136136

137137
val id = parsePackageId(pkgInfo, workingDir)
@@ -153,7 +153,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
153153
/**
154154
* Return the [Identifier] for the package contained in [pkgInfo].
155155
*/
156-
private fun parsePackageId(pkgInfo: PackageInfo, workingDir: File) =
156+
private fun parsePackageId(pkgInfo: PackageInfoV1, workingDir: File) =
157157
Identifier(
158158
type = "Conan",
159159
namespace = "",
@@ -170,7 +170,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
170170
* TODO: The format of `conan info` output for a conanfile.txt file may be such that we can get project metadata
171171
* from the `requires` field. Need to investigate whether this is a sure thing before implementing.
172172
*/
173-
private fun generateProjectPackage(pkgInfo: PackageInfo, definitionFile: File, workingDir: File): Package {
173+
private fun generateProjectPackage(pkgInfo: PackageInfoV1, definitionFile: File, workingDir: File): Package {
174174
fun inspectPyFile(field: String) =
175175
definitionFile.name.takeIf { it == "conanfile.py" }?.let { conan.inspectField(it, workingDir, field) }
176176

@@ -195,7 +195,7 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
195195
* Return the dependency tree for the given [direct scope dependencies][requires].
196196
*/
197197
private fun parseDependencyTree(
198-
pkgInfos: List<PackageInfo>,
198+
pkgInfos: List<PackageInfoV1>,
199199
requires: List<String>,
200200
workingDir: File
201201
): Set<PackageReference> =
@@ -217,13 +217,13 @@ internal class ConanV1Handler(private val conan: Conan) : ConanVersionHandler {
217217
/**
218218
* Return the full list of packages, excluding the project level information.
219219
*/
220-
private fun removeProjectPackage(pkgInfos: List<PackageInfo>, definitionFileName: String): List<PackageInfo> =
220+
private fun removeProjectPackage(pkgInfos: List<PackageInfoV1>, definitionFileName: String): List<PackageInfoV1> =
221221
pkgInfos.minusElement(findProjectPackageInfo(pkgInfos, definitionFileName))
222222

223223
/**
224224
* Find the [PackageInfo] that represents the project defined in the definition file.
225225
*/
226-
private fun findProjectPackageInfo(pkgInfos: List<PackageInfo>, definitionFileName: String): PackageInfo =
226+
private fun findProjectPackageInfo(pkgInfos: List<PackageInfoV1>, definitionFileName: String): PackageInfoV1 =
227227
pkgInfos.first {
228228
// Use "in" because conanfile.py's reference string often includes other data.
229229
definitionFileName in it.reference.orEmpty()

plugins/package-managers/conan/src/main/kotlin/PackageInfo.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,29 @@ private val JSON = Json {
3030
namingStrategy = JsonNamingStrategy.SnakeCase
3131
}
3232

33-
internal fun parsePackageInfos(file: File): List<PackageInfo> = JSON.decodeFromString(file.readText())
33+
internal fun parsePackageInfosV1(file: File): List<PackageInfoV1> =
34+
JSON.decodeFromString<List<PackageInfoV1>>(file.readText())
35+
36+
/**
37+
* A class containing the properties common to all [PackageInfo], regardless of their Conan version. Used for
38+
* abstracting some functions and keeping them in [Conan].
39+
*/
40+
@Serializable
41+
sealed interface PackageInfo {
42+
val author: String?
43+
val revision: String?
44+
val url: String?
45+
}
3446

3547
@Serializable
36-
internal data class PackageInfo(
48+
internal data class PackageInfoV1(
49+
override val author: String? = null,
50+
override val revision: String? = null,
51+
override val url: String? = null,
3752
val reference: String? = null,
38-
val author: String? = null,
3953
val license: List<String> = emptyList(),
4054
val homepage: String? = null,
41-
val revision: String? = null,
42-
val url: String? = null,
4355
val displayName: String,
4456
val requires: List<String> = emptyList(),
4557
val buildRequires: List<String> = emptyList()
46-
)
58+
) : PackageInfo

0 commit comments

Comments
 (0)