Skip to content

Commit

Permalink
fix(CocoaPods): Correctly parse secondary dependencies with versions
Browse files Browse the repository at this point in the history
Until now, for secondary lines in the "PODS" section any present version
was ignored. However, if a version is present there, then there is no
top-level entry for the same package anymore to declare the version,
which resulted in a `NoSuchElementException` for the `versionForName`
map. Solve that by also parsing the version for such secondary entries,
if present.

Also, allow the lookup in `dependenciesForName` to fail, which simply
means that named package has no dependencies.

Fixes #7523.

Signed-off-by: Sebastian Schuberth <sschuberth@gmail.com>
  • Loading branch information
sschuberth committed Sep 22, 2023
1 parent cf295f6 commit 18f9be2
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions plugins/package-managers/cocoapods/src/main/kotlin/CocoaPods.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ private const val SCOPE_NAME = "dependencies"

private fun parseNameAndVersion(entry: String): Pair<String, String?> {
val info = entry.split(' ', limit = 2)
return info[0] to info.getOrNull(1)?.removeSurrounding("(", ")")
val name = info[0]

// A version entry could look something like "(6.3.0)", "(= 2021.06.28.00-v2)", "(~> 8.15.0)", etc. Also see
// https://guides.cocoapods.org/syntax/podfile.html#pod.
val version = info.getOrNull(1)?.removeSurrounding("(", ")")?.substringAfterLast(' ')

return name to version
}

private fun getPackageReferences(podfileLock: File): Set<PackageReference> {
Expand All @@ -251,14 +257,18 @@ private fun getPackageReferences(podfileLock: File): Set<PackageReference> {
val (name, version) = parseNameAndVersion(entry)
versionForName[name] = checkNotNull(version)

val dependencies = node[entry]?.map { it.textValue().substringBefore(" ") }.orEmpty()
val dependencies = node[entry]?.map { depNode ->
val (depName, depVersion) = parseNameAndVersion(depNode.textValue())
depName.also { if (depVersion != null) versionForName[it] = depVersion }
}.orEmpty()

dependenciesForName.getOrPut(name) { mutableSetOf() } += dependencies
}

fun createPackageReference(name: String): PackageReference =
PackageReference(
id = Identifier("Pod", "", name, versionForName.getValue(name)),
dependencies = dependenciesForName.getValue(name).mapTo(mutableSetOf()) { createPackageReference(it) }
dependencies = dependenciesForName[name].orEmpty().mapTo(mutableSetOf()) { createPackageReference(it) }
)

return root.get("DEPENDENCIES").mapTo(mutableSetOf()) { node ->
Expand Down

0 comments on commit 18f9be2

Please sign in to comment.