Skip to content

Commit

Permalink
refactor(cargo): Do not require parsing the manifest
Browse files Browse the repository at this point in the history
The generated metadata already contains the relenvant data from the
manifest, so there is no need to also parse the manifest.

Note that virtual workspaces [1] also were not supported before as
`CargoManifest.pkg` was not nullable.

[1]: https://doc.rust-lang.org/cargo/reference/workspaces.html#virtual-workspace

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Apr 5, 2024
1 parent 4e95065 commit 90f190d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 56 deletions.
19 changes: 6 additions & 13 deletions plugins/package-managers/cargo/src/main/kotlin/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ class Cargo(
}

override fun resolveDependencies(definitionFile: File, labels: Map<String, String>): List<ProjectAnalyzerResult> {
// Get the project name and version. If one of them is missing return null, because this is a workspace
// definition file that does not contain a project.
val pkgDefinition = definitionFile.reader().use { toml.decodeFromNativeReader<CargoManifest>(it) }

val workingDir = definitionFile.parentFile
val metadataProcess = run(workingDir, "metadata", "--format-version=1")
val metadata = json.decodeFromString<CargoMetadata>(metadataProcess.stdout)
Expand All @@ -178,8 +174,8 @@ class Cargo(
{ parsePackage(it, hashes) }
)

val projectId = metadata.workspaceMembers.single {
it.startsWith("${pkgDefinition.pkg.name} ${pkgDefinition.pkg.version}")
val projectId = requireNotNull(metadata.resolve.root) {
"Virtual workspaces are not supported."
}

val projectNode = metadata.packages.single { it.id == projectId }
Expand All @@ -190,8 +186,7 @@ class Cargo(

val transitiveDependencies = directDependencies
.mapNotNull { dependency ->
val version =
getResolvedVersion(pkgDefinition.pkg.name, pkgDefinition.pkg.version, dependency.name, metadata)
val version = getResolvedVersion(projectNode.name, projectNode.version, dependency.name, metadata)
version?.let { Pair(dependency.name, it) }
}
.mapTo(mutableSetOf()) {
Expand All @@ -207,12 +202,10 @@ class Cargo(
getTransitiveDependencies(groupedDependencies["build"], "build-dependencies")
)

val projectPkg = packages.values.single { pkg ->
pkg.id.name == pkgDefinition.pkg.name && pkg.id.version == pkgDefinition.pkg.version
}.let { it.copy(id = it.id.copy(type = managerName)) }
val projectPkg = packages.getValue(projectId).let { it.copy(id = it.id.copy(type = managerName)) }

val homepageUrl = pkgDefinition.pkg.homepage.orEmpty()
val authors = pkgDefinition.pkg.authors.mapNotNullTo(mutableSetOf(), ::parseAuthorString)
val homepageUrl = projectNode.homepage.orEmpty()
val authors = projectNode.authors.mapNotNullTo(mutableSetOf(), ::parseAuthorString)

val project = Project(
id = projectPkg.id,
Expand Down
43 changes: 0 additions & 43 deletions plugins/package-managers/cargo/src/main/kotlin/CargoManifest.kt

This file was deleted.

0 comments on commit 90f190d

Please sign in to comment.