Skip to content

Commit

Permalink
refactor(model): Introduce a function to add dependencies to the graph
Browse files Browse the repository at this point in the history
This reduces a bit of code duplication and avoids mistakes where the
qualified scope is generated on each dependency iteration.

Signed-off-by: Sebastian Schuberth <sebastian@doubleopen.org>
  • Loading branch information
sschuberth committed Nov 14, 2024
1 parent aee88f2 commit dcc97a0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 36 deletions.
9 changes: 9 additions & 0 deletions model/src/main/kotlin/utils/DependencyGraphBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ class DependencyGraphBuilder<D>(
fun addDependency(scopeName: String, dependency: D): DependencyGraphBuilder<D> =
apply { addDependencyToGraph(scopeName, dependency, transitive = false, emptySet()) }

/**
* Add all [dependencies] to the qualified scope name generated from the [projectId] and unqualified [scopeName].
*/
fun addDependencies(projectId: Identifier, scopeName: String, dependencies: Collection<D>) =
apply {
val qualifiedScopeName = DependencyGraph.qualifyScope(projectId, scopeName)
dependencies.forEach { addDependency(qualifiedScopeName, it) }
}

/**
* Add the given [packages] to this builder. They are stored internally and also returned when querying the set of
* known packages. This function can be used by package managers that have to deal with packages not part of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
Expand Down Expand Up @@ -254,12 +253,7 @@ class GradleInspector(
dependencyTreeModel.configurations.filterNot {
excludes.isScopeExcluded(it.name)
}.forEach { configuration ->
configuration.dependencies.forEach { dependency ->
graphBuilder.addDependency(
DependencyGraph.qualifyScope(projectId, configuration.name),
dependency
)
}
graphBuilder.addDependencies(projectId, configuration.name, configuration.dependencies)
}

val project = Project(
Expand Down
16 changes: 5 additions & 11 deletions plugins/package-managers/gradle/src/main/kotlin/Gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
Expand Down Expand Up @@ -300,16 +299,11 @@ class Gradle(
version = dependencyTreeModel.version
)

dependencyTreeModel.configurations
.filterNot { excludes.isScopeExcluded(it.name) }
.forEach { configuration ->
configuration.dependencies.forEach { dependency ->
graphBuilder.addDependency(
DependencyGraph.qualifyScope(projectId, configuration.name),
dependency
)
}
}
dependencyTreeModel.configurations.filterNot {
excludes.isScopeExcluded(it.name)
}.forEach { configuration ->
graphBuilder.addDependencies(projectId, configuration.name, configuration.dependencies)
}

val project = Project(
id = projectId,
Expand Down
6 changes: 1 addition & 5 deletions plugins/package-managers/node/src/main/kotlin/npm/Npm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import org.apache.logging.log4j.kotlin.logger
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
Expand Down Expand Up @@ -115,11 +114,8 @@ class Npm(
.filterNot { excludes.isScopeExcluded(it.descriptor) }
.mapTo(mutableSetOf()) { scope ->
val scopeName = scope.descriptor
val qualifiedScopeName = DependencyGraph.qualifyScope(project, scopeName)

projectModuleInfo.getScopeDependencies(scope).forEach { dependency ->
graphBuilder.addDependency(qualifiedScopeName, dependency)
}
graphBuilder.addDependencies(project.id, scopeName, projectModuleInfo.getScopeDependencies(scope))

scopeName
}
Expand Down
6 changes: 1 addition & 5 deletions plugins/package-managers/node/src/main/kotlin/pnpm/Pnpm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.apache.logging.log4j.kotlin.logger
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.ProjectAnalyzerResult
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
Expand Down Expand Up @@ -85,12 +84,9 @@ class Pnpm(

val scopeNames = Scope.entries.mapTo(mutableSetOf()) { scope ->
val scopeName = scope.descriptor
val qualifiedScopeName = DependencyGraph.qualifyScope(project, scopeName)
val moduleInfo = moduleInfosForScope.getValue(scope).single { it.path == projectDir.absolutePath }

moduleInfo.getScopeDependencies(scope).forEach { dependency ->
graphBuilder.addDependency(qualifiedScopeName, dependency)
}
graphBuilder.addDependencies(project.id, scopeName, moduleInfo.getScopeDependencies(scope))

scopeName
}
Expand Down
4 changes: 1 addition & 3 deletions plugins/package-managers/node/src/main/kotlin/yarn/Yarn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import org.apache.logging.log4j.kotlin.loggerOf
import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory
import org.ossreviewtoolkit.analyzer.PackageManager
import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Project
Expand Down Expand Up @@ -292,10 +291,9 @@ open class Yarn(
): String? {
if (excludes.isScopeExcluded(targetScope)) return null

val qualifiedScopeName = DependencyGraph.qualifyScope(project, targetScope)
val moduleInfo = checkNotNull(getModuleInfo(workingDir, scopes, projectDirs, listOfNotNull(workspaceDir)))

moduleInfo.dependencies.forEach { graphBuilder.addDependency(qualifiedScopeName, it) }
graphBuilder.addDependencies(project.id, targetScope, moduleInfo.dependencies)

return targetScope.takeUnless { moduleInfo.dependencies.isEmpty() }
}
Expand Down
6 changes: 1 addition & 5 deletions plugins/package-managers/node/src/main/kotlin/yarn2/Yarn2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import org.ossreviewtoolkit.analyzer.PackageManagerResult
import org.ossreviewtoolkit.analyzer.parseAuthorString
import org.ossreviewtoolkit.downloader.VcsHost
import org.ossreviewtoolkit.downloader.VersionControlSystem
import org.ossreviewtoolkit.model.DependencyGraph
import org.ossreviewtoolkit.model.Hash
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Issue
Expand Down Expand Up @@ -322,7 +321,6 @@ class Yarn2(
allDependencies.filterNot { excludes.isScopeExcluded(it.key.type) }
.forEach { (dependencyType, allScopedDependencies) ->
allProjects.values.forEach { project ->
val qualifiedScopeName = DependencyGraph.qualifyScope(project.id, dependencyType.type)
val dependencies = allScopedDependencies[project.id]
val dependenciesInfo = dependencies?.mapNotNull { dependency ->
if ("Yarn2" in dependency.type) {
Expand Down Expand Up @@ -364,9 +362,7 @@ class Yarn2(
}
}?.toSet().orEmpty()

dependenciesInfo.forEach {
graphBuilder.addDependency(qualifiedScopeName, it)
}
graphBuilder.addDependencies(project.id, dependencyType.type, dependenciesInfo)
}
}

Expand Down

0 comments on commit dcc97a0

Please sign in to comment.