-
Notifications
You must be signed in to change notification settings - Fork 822
/
Copy pathProject.swift
298 lines (257 loc) · 11.4 KB
/
Project.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import Foundation
import JSONUtilities
import PathKit
import Yams
public struct Project: BuildSettingsContainer {
public var basePath: Path
public var name: String
public var targets: [Target] {
didSet {
targetsMap = Dictionary(uniqueKeysWithValues: targets.map { ($0.name, $0) })
}
}
public var aggregateTargets: [AggregateTarget] {
didSet {
aggregateTargetsMap = Dictionary(uniqueKeysWithValues: aggregateTargets.map { ($0.name, $0) })
}
}
public var packages: [String: SwiftPackage]
public var localPackages: [String]
public var settings: Settings
public var settingGroups: [String: Settings]
public var configs: [Config]
public var schemes: [Scheme]
public var options: SpecOptions
public var attributes: [String: Any]
public var fileGroups: [String]
public var configFiles: [String: String]
public var include: [String] = []
public var projectReferences: [ProjectReference] = [] {
didSet {
projectReferencesMap = Dictionary(uniqueKeysWithValues: projectReferences.map { ($0.name, $0) })
}
}
private var targetsMap: [String: Target]
private var aggregateTargetsMap: [String: AggregateTarget]
private var projectReferencesMap: [String: ProjectReference]
public init(
basePath: Path = "",
name: String,
configs: [Config] = Config.defaultConfigs,
targets: [Target] = [],
aggregateTargets: [AggregateTarget] = [],
settings: Settings = .empty,
settingGroups: [String: Settings] = [:],
schemes: [Scheme] = [],
packages: [String: SwiftPackage] = [:],
localPackages: [String] = [],
options: SpecOptions = SpecOptions(),
fileGroups: [String] = [],
configFiles: [String: String] = [:],
attributes: [String: Any] = [:],
projectReferences: [ProjectReference] = []
) {
self.basePath = basePath
self.name = name
self.targets = targets
targetsMap = Dictionary(uniqueKeysWithValues: self.targets.map { ($0.name, $0) })
self.aggregateTargets = aggregateTargets
aggregateTargetsMap = Dictionary(uniqueKeysWithValues: self.aggregateTargets.map { ($0.name, $0) })
self.configs = configs
self.settings = settings
self.settingGroups = settingGroups
self.schemes = schemes
self.packages = packages
self.localPackages = localPackages
self.options = options
self.fileGroups = fileGroups
self.configFiles = configFiles
self.attributes = attributes
self.projectReferences = projectReferences
projectReferencesMap = Dictionary(uniqueKeysWithValues: self.projectReferences.map { ($0.name, $0) })
}
public func getProjectReference(_ projectName: String) -> ProjectReference? {
projectReferencesMap[projectName]
}
public func getTarget(_ targetName: String) -> Target? {
targetsMap[targetName]
}
public func getAggregateTarget(_ targetName: String) -> AggregateTarget? {
aggregateTargetsMap[targetName]
}
public func getProjectTarget(_ targetName: String) -> ProjectTarget? {
targetsMap[targetName] ?? aggregateTargetsMap[targetName]
}
public func getConfig(_ configName: String) -> Config? {
configs.first { $0.name == configName }
}
public var defaultProjectPath: Path {
basePath + "\(name).xcodeproj"
}
}
extension Project: CustomDebugStringConvertible {
public var debugDescription: String {
var string = "Name: \(name)"
let indent = " "
if !include.isEmpty {
string += "\nInclude:\n\(indent)" + include.map { $0.description }.joined(separator: "\n\(indent)")
}
if !settingGroups.isEmpty {
string += "\nSetting Groups:\n\(indent)" + settingGroups.keys
.sorted()
.joined(separator: "\n\(indent)")
}
if !targets.isEmpty {
string += "\nTargets:\n\(indent)" + targets.map { $0.description }.joined(separator: "\n\(indent)")
}
if !aggregateTargets.isEmpty {
string += "\nAggregate Targets:\n\(indent)" + aggregateTargets.map { $0.description }.joined(separator: "\n\(indent)")
}
if !schemes.isEmpty {
let allSchemes = targets.filter { $0.scheme != nil }.map { $0.name } + schemes.map { $0.name }
string += "\nSchemes:\n\(indent)" + allSchemes.joined(separator: "\n\(indent)")
}
return string
}
}
extension Project: Equatable {
public static func == (lhs: Project, rhs: Project) -> Bool {
lhs.name == rhs.name &&
lhs.targets == rhs.targets &&
lhs.aggregateTargets == rhs.aggregateTargets &&
lhs.settings == rhs.settings &&
lhs.settingGroups == rhs.settingGroups &&
lhs.configs == rhs.configs &&
lhs.schemes == rhs.schemes &&
lhs.fileGroups == rhs.fileGroups &&
lhs.configFiles == rhs.configFiles &&
lhs.options == rhs.options &&
lhs.packages == rhs.packages &&
lhs.localPackages == rhs.localPackages &&
NSDictionary(dictionary: lhs.attributes).isEqual(to: rhs.attributes)
}
}
extension Project {
public init(path: Path) throws {
let spec = try SpecFile(path: path)
try self.init(spec: spec)
}
public init(spec: SpecFile) throws {
try self.init(basePath: spec.basePath, jsonDictionary: spec.resolvedDictionary())
}
public init(basePath: Path = "", jsonDictionary: JSONDictionary) throws {
self.basePath = basePath
let jsonDictionary = Project.resolveProject(jsonDictionary: jsonDictionary)
name = try jsonDictionary.json(atKeyPath: "name")
settings = jsonDictionary.json(atKeyPath: "settings") ?? .empty
settingGroups = jsonDictionary.json(atKeyPath: "settingGroups")
?? jsonDictionary.json(atKeyPath: "settingPresets") ?? [:]
let configs: [String: String] = jsonDictionary.json(atKeyPath: "configs") ?? [:]
self.configs = configs.isEmpty ? Config.defaultConfigs :
configs.map { Config(name: $0, type: ConfigType(rawValue: $1)) }.sorted { $0.name < $1.name }
targets = try jsonDictionary.json(atKeyPath: "targets", parallel: true).sorted { $0.name < $1.name }
aggregateTargets = try jsonDictionary.json(atKeyPath: "aggregateTargets").sorted { $0.name < $1.name }
projectReferences = try jsonDictionary.json(atKeyPath: "projectReferences").sorted { $0.name < $1.name }
schemes = try jsonDictionary.json(atKeyPath: "schemes")
fileGroups = jsonDictionary.json(atKeyPath: "fileGroups") ?? []
configFiles = jsonDictionary.json(atKeyPath: "configFiles") ?? [:]
attributes = jsonDictionary.json(atKeyPath: "attributes") ?? [:]
include = jsonDictionary.json(atKeyPath: "include") ?? []
if jsonDictionary["packages"] != nil {
packages = try jsonDictionary.json(atKeyPath: "packages", invalidItemBehaviour: .fail)
} else {
packages = [:]
}
localPackages = jsonDictionary.json(atKeyPath: "localPackages") ?? []
if jsonDictionary["options"] != nil {
options = try jsonDictionary.json(atKeyPath: "options")
} else {
options = SpecOptions()
}
targetsMap = Dictionary(uniqueKeysWithValues: targets.map { ($0.name, $0) })
aggregateTargetsMap = Dictionary(uniqueKeysWithValues: aggregateTargets.map { ($0.name, $0) })
projectReferencesMap = Dictionary(uniqueKeysWithValues: projectReferences.map { ($0.name, $0) })
}
static func resolveProject(jsonDictionary: JSONDictionary) -> JSONDictionary {
var jsonDictionary = jsonDictionary
// resolve multiple times so that we support both multi-platform templates,
// as well as platform specific templates in multi-platform targets
jsonDictionary = Target.resolveMultiplatformTargets(jsonDictionary: jsonDictionary)
jsonDictionary = Target.resolveTargetTemplates(jsonDictionary: jsonDictionary)
jsonDictionary = Scheme.resolveSchemeTemplates(jsonDictionary: jsonDictionary)
jsonDictionary = Target.resolveMultiplatformTargets(jsonDictionary: jsonDictionary)
return jsonDictionary
}
}
extension Project: PathContainer {
static var pathProperties: [PathProperty] {
[
.string("configFiles"),
.string("localPackages"),
.object("options", SpecOptions.pathProperties),
.object("targets", Target.pathProperties),
.object("targetTemplates", Target.pathProperties),
.object("aggregateTargets", AggregateTarget.pathProperties),
.object("projectReferences", ProjectReference.pathProperties),
]
}
}
extension Project {
public var allFiles: [Path] {
var files: [Path] = []
files.append(contentsOf: configFilePaths)
for fileGroup in fileGroups {
let fileGroupPath = basePath + fileGroup
let fileGroupChildren = (try? fileGroupPath.recursiveChildren()) ?? []
files.append(contentsOf: fileGroupChildren)
files.append(fileGroupPath)
}
for target in aggregateTargets {
files.append(contentsOf: target.configFilePaths)
}
for target in targets {
files.append(contentsOf: target.configFilePaths)
for source in target.sources {
let sourcePath = basePath + source.path
let sourceChildren = (try? sourcePath.recursiveChildren()) ?? []
files.append(contentsOf: sourceChildren)
files.append(sourcePath)
}
}
return files
}
}
extension BuildSettingsContainer {
fileprivate var configFilePaths: [Path] {
configFiles.values.map { Path($0) }
}
}
extension Project: JSONEncodable {
public func toJSONValue() -> Any {
toJSONDictionary()
}
public func toJSONDictionary() -> JSONDictionary {
let targetPairs = targets.map { ($0.name, $0.toJSONValue()) }
let configsPairs = configs.map { ($0.name, $0.type?.rawValue) }
let aggregateTargetsPairs = aggregateTargets.map { ($0.name, $0.toJSONValue()) }
let schemesPairs = schemes.map { ($0.name, $0.toJSONValue()) }
let projectReferencesPairs = projectReferences.map { ($0.name, $0.toJSONValue()) }
var dictionary: JSONDictionary = [:]
dictionary["name"] = name
dictionary["options"] = options.toJSONValue()
dictionary["settings"] = settings.toJSONValue()
dictionary["fileGroups"] = fileGroups
dictionary["configFiles"] = configFiles
dictionary["include"] = include
dictionary["attributes"] = attributes
dictionary["packages"] = packages.mapValues { $0.toJSONValue() }
dictionary["localPackages"] = localPackages
dictionary["targets"] = Dictionary(uniqueKeysWithValues: targetPairs)
dictionary["configs"] = Dictionary(uniqueKeysWithValues: configsPairs)
dictionary["aggregateTargets"] = Dictionary(uniqueKeysWithValues: aggregateTargetsPairs)
dictionary["schemes"] = Dictionary(uniqueKeysWithValues: schemesPairs)
dictionary["settingGroups"] = settingGroups.mapValues { $0.toJSONValue() }
dictionary["projectReferences"] = projectReferencesPairs
return dictionary
}
}