Skip to content

Commit 51e7155

Browse files
committed
Add an API to get all the targets in a build description / modules graph in a topological order
We need this for background preparation for indexing so that we can prepare and index lower-level targets first.
1 parent da5d854 commit 51e7155

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ public struct ModulesGraph {
7474
/// Returns all the targets in the graph, regardless if they are reachable from the root targets or not.
7575
public let allTargets: IdentifiableSet<ResolvedTarget>
7676

77+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
78+
/// dependencies).
79+
package var allTargetsInTopologicalOrder: [ResolvedTarget] {
80+
get throws {
81+
try topologicalSort(Array(allTargets)) { $0.dependencies.compactMap { $0.target } }.reversed()
82+
}
83+
}
84+
7785
/// Returns all the products in the graph, regardless if they are reachable from the root targets or not.
7886

7987
public let allProducts: IdentifiableSet<ResolvedProduct>

Sources/SourceKitLSPAPI/BuildDescription.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,12 @@ public struct BuildDescription {
121121
return nil
122122
}
123123
}
124+
125+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
126+
/// dependencies).
127+
public func allTargetsInTopologicalOrder(in modulesGraph: ModulesGraph) throws -> [BuildTarget] {
128+
try modulesGraph.allTargetsInTopologicalOrder.compactMap {
129+
getBuildTarget(for: $0, in: modulesGraph)
130+
}
131+
}
124132
}

0 commit comments

Comments
 (0)