Skip to content

Commit 72522a0

Browse files
committed
[ModulesGraph] NFC: Handle module->package and product->package link using package set
Since `packages` is now an identifiable set no additional storage is needed to find package from a module or a product it contains.
1 parent db1a26f commit 72522a0

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,19 @@ public struct ModulesGraph {
138138
return self.rootPackages.contains(id: package.id)
139139
}
140140

141-
private var modulesToPackages: [ResolvedModule.ID: ResolvedPackage]
142-
143141
/// Returns the package based on the given identity, or nil if the package isn't in the graph.
144142
public func package(for identity: PackageIdentity) -> ResolvedPackage? {
145143
packages[identity]
146144
}
147145

148146
/// Returns the package that contains the module, or nil if the module isn't in the graph.
149147
public func package(for module: ResolvedModule) -> ResolvedPackage? {
150-
return self.modulesToPackages[module.id]
148+
self.package(for: module.packageIdentity)
151149
}
152150

153-
154-
private var productsToPackages: [ResolvedProduct.ID: ResolvedPackage]
155151
/// Returns the package that contains the product, or nil if the product isn't in the graph.
156152
public func package(for product: ResolvedProduct) -> ResolvedPackage? {
157-
return self.productsToPackages[product.id]
153+
self.package(for: product.packageIdentity)
158154
}
159155

160156
/// Returns all of the packages that the given package depends on directly.
@@ -182,20 +178,6 @@ public struct ModulesGraph {
182178
self.binaryArtifacts = binaryArtifacts
183179
self.packages = packages
184180

185-
// Create a mapping from targets to the packages that define them. Here
186-
// we include all targets, including tests in non-root packages, since
187-
// this is intended for lookup and not traversal.
188-
var modulesToPackages = self.packages.reduce(into: [:], { partial, package in
189-
package.targets.forEach { partial[$0.id] = package }
190-
})
191-
192-
// Create a mapping from products to the packages that define them. Here
193-
// we include all products, including tests in non-root packages, since
194-
// this is intended for lookup and not traversal.
195-
var productsToPackages = packages.reduce(into: [:], { partial, package in
196-
package.products.forEach { partial[$0.id] = package }
197-
})
198-
199181
var allTargets = IdentifiableSet<ResolvedModule>()
200182
var allProducts = IdentifiableSet<ResolvedProduct>()
201183
for package in self.packages {
@@ -217,12 +199,8 @@ public struct ModulesGraph {
217199
switch dependency {
218200
case .target(let targetDependency, _):
219201
allTargets.insert(targetDependency)
220-
modulesToPackages[targetDependency.id] =
221-
self.packages[targetDependency.packageIdentity]
222202
case .product(let productDependency, _):
223203
allProducts.insert(productDependency)
224-
productsToPackages[productDependency.id] =
225-
self.packages[productDependency.packageIdentity]
226204
}
227205
}
228206
}
@@ -237,9 +215,6 @@ public struct ModulesGraph {
237215
}
238216
}
239217

240-
self.modulesToPackages = modulesToPackages
241-
self.productsToPackages = productsToPackages
242-
243218
// Compute the reachable targets and products.
244219
let inputTargets = self.inputPackages.flatMap { $0.targets }
245220
let inputProducts = self.inputPackages.flatMap { $0.products }
@@ -274,17 +249,6 @@ public struct ModulesGraph {
274249
product.buildTriple = buildTriple
275250
return product
276251
})
277-
278-
self.modulesToPackages = .init(self.modulesToPackages.map {
279-
var target = $0
280-
target.buildTriple = buildTriple
281-
return (target, $1)
282-
}, uniquingKeysWith: { $1 })
283-
self.productsToPackages = .init(self.productsToPackages.map {
284-
var product = $0
285-
product.buildTriple = buildTriple
286-
return (product, $1)
287-
}, uniquingKeysWith: { $1 })
288252
}
289253

290254
/// Computes a map from each executable target in any of the root packages to the corresponding test targets.

0 commit comments

Comments
 (0)