forked from tuist/tuist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Graph+TestData.swift
92 lines (79 loc) · 3.88 KB
/
Graph+TestData.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Basic
import Foundation
@testable import TuistGenerator
extension Graph {
static func test(name: String = "test",
entryPath: AbsolutePath = AbsolutePath("/test/graph"),
cache: GraphLoaderCaching = GraphLoaderCache(),
entryNodes: [GraphNode] = []) -> Graph {
return Graph(name: name,
entryPath: entryPath,
cache: cache,
entryNodes: entryNodes)
}
/// Creates a test dependency graph for targets within a single project
///
/// Note: For the purposes of testing, to reduce complexity of resolving dependencies
/// The `dependencies` property is used to define the dependencies explicitly.
/// All targets need to be listed even if they don't have any dependencies.
static func create(project: Project,
dependencies: [(target: Target, dependencies: [Target])]) -> Graph {
let depenenciesWithProject = dependencies.map { (
project: project,
target: $0.target,
dependencies: $0.dependencies
) }
let targetNodes = createTargetNodes(dependencies: depenenciesWithProject)
let cache = GraphLoaderCache()
let graph = Graph.test(name: project.name,
entryPath: project.path,
cache: cache,
entryNodes: targetNodes)
targetNodes.forEach { cache.add(targetNode: $0) }
cache.add(project: project)
return graph
}
/// Creates a test dependency graph for targets within a multiple projects
///
/// Note: For the purposes of testing, to reduce complexity of resolving dependencies
/// The `dependencies` property is used to define the dependencies explicitly.
/// All targets need to be listed even if they don't have any dependencies.
static func create(projects: [Project] = [],
entryNodes: [Target]? = nil,
dependencies: [(project: Project, target: Target, dependencies: [Target])]) -> Graph {
let targetNodes = createTargetNodes(dependencies: dependencies)
let entryNodes = entryNodes.map { entryNodes in
targetNodes.filter { entryNodes.contains($0.target) }
}
let cache = GraphLoaderCache()
let graph = Graph.test(name: projects.first?.name ?? "Test",
entryPath: projects.first?.path ?? "/test/path",
cache: cache,
entryNodes: entryNodes ?? targetNodes)
targetNodes.forEach { cache.add(targetNode: $0) }
projects.forEach { cache.add(project: $0) }
return graph
}
private static func createTargetNodes(dependencies: [(project: Project, target: Target, dependencies: [Target])]) -> [TargetNode] {
let nodesCache = Dictionary(uniqueKeysWithValues: dependencies.map {
($0.target.name, TargetNode(project: $0.project,
target: $0.target,
dependencies: []))
})
dependencies.forEach {
let node = nodesCache[$0.target.name]!
let platform = $0.target.platform
node.dependencies = $0.dependencies.map { nodesCache[$0.name]! }
let sdkDependencies: [(name: String, status: SDKStatus)] = $0.target.dependencies.compactMap {
if case let .sdk(name: name, status: status) = $0 {
return (name: name, status: status)
}
return nil
}
node.dependencies.append(contentsOf: sdkDependencies.compactMap {
try? SDKNode(name: $0.name, platform: platform, status: $0.status)
})
}
return dependencies.map { nodesCache[$0.target.name]! }
}
}