Skip to content

Commit

Permalink
refactor(swiftpm): Remove code redundancy for converting Pin to Package
Browse files Browse the repository at this point in the history
The SwiftPM implementation has three data models, `PinV1`, `PinV2` and
`Pin`. That implementation defines a mapping `PinV1 -> Pin` and
`PinV2 -> Pin` see [1]. Within ORT introducing a third class seems not
needed, so simply map `PinV1` to `PinV2` to unify the handling.

[1]: https://github.com/apple/swift-package-manager/blob/3ef830dddff459e569d6e49c186c3ded33c39bcc/Sources/PackageGraph/PinsStore.swift#L486-L553

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Feb 2, 2024
1 parent d6f6a06 commit 34bd991
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
3 changes: 2 additions & 1 deletion plugins/package-managers/swiftpm/src/main/kotlin/SwiftPm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ private fun parseLockfile(packageResolvedFile: File): Result<Set<Package>> =

when (val version = root.getValue("version").jsonPrimitive.content) {
"1" -> {
val projectDir = packageResolvedFile.parentFile
val pinsJson = root["object"]?.jsonObject?.get("pins")
val pins = pinsJson?.let { json.decodeFromJsonElement<List<PinV1>>(it) }.orEmpty()
pins.mapTo(mutableSetOf()) { it.toPackage() }
pins.mapTo(mutableSetOf()) { it.toPinV2(projectDir).toPackage() }
}

"2" -> {
Expand Down
41 changes: 14 additions & 27 deletions plugins/package-managers/swiftpm/src/main/kotlin/SwiftPmModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.ossreviewtoolkit.plugins.packagemanagers.swiftpm

import java.io.File

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -110,34 +112,19 @@ internal fun SwiftPackage.Dependency.toPackage(): Package {
return createPackage(id, vcsInfo)
}

internal fun PinV1.toPackage(): Package {
val id = Identifier(
type = PACKAGE_TYPE,
namespace = "",
name = getCanonicalName(repositoryUrl),
version = state?.run {
when {
!version.isNullOrBlank() -> version
!revision.isNullOrBlank() -> "revision-$revision"
!branch.isNullOrBlank() -> "branch-$branch"
else -> ""
}
}.orEmpty()
)

val vcsInfoFromUrl = VcsHost.parseUrl(repositoryUrl)
val vcsInfo = if (vcsInfoFromUrl.revision.isBlank() && state != null) {
when {
!state.revision.isNullOrBlank() -> vcsInfoFromUrl.copy(revision = state.revision)
!state.version.isNullOrBlank() -> vcsInfoFromUrl.copy(revision = state.version)
else -> vcsInfoFromUrl
internal fun PinV1.toPinV2(projectDir: File): PinV2 =
PinV2(
identity = packageName,
state = state,
location = repositoryUrl,
// Map the 'kind' analog to
// https://github.com/apple/swift-package-manager/blob/3ef830dddff459e569d6e49c186c3ded33c39bcc/Sources/PackageGraph/PinsStore.swift#L492-L496:
kind = if (projectDir.resolve(repositoryUrl).isDirectory) {
PinV2.Kind.LOCAL_SOURCE_CONTROL
} else {
PinV2.Kind.REMOTE_SOURCE_CONTROL
}
} else {
vcsInfoFromUrl
}

return createPackage(id, vcsInfo)
}
)

internal fun PinV2.toPackage(): Package {
val id = Identifier(
Expand Down

0 comments on commit 34bd991

Please sign in to comment.