/
PBXGroup.swift
228 lines (205 loc) Β· 8.68 KB
/
PBXGroup.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
import Foundation
import PathKit
public class PBXGroup: PBXFileElement {
// MARK: - Attributes
/// Group children references.
var childrenReferences: [PBXObjectReference]
/// Group children.
public var children: [PBXFileElement] {
set {
childrenReferences = newValue.references()
}
get {
return childrenReferences.objects()
}
}
// MARK: - Init
/// Initializes the group with its attributes.
///
/// - Parameters:
/// - children: group children.
/// - sourceTree: group source tree.
/// - name: group name.
/// - path: group relative path from `sourceTree`, if different than `name`.
/// - includeInIndex: should the IDE index the files in the group?
/// - wrapsLines: should the IDE wrap lines for files in the group?
/// - usesTabs: group uses tabs.
/// - indentWidth: the number of positions to indent blocks of code
/// - tabWidth: the visual width of tab characters
public init(children: [PBXFileElement] = [],
sourceTree: PBXSourceTree? = nil,
name: String? = nil,
path: String? = nil,
includeInIndex: Bool? = nil,
wrapsLines: Bool? = nil,
usesTabs: Bool? = nil,
indentWidth: UInt? = nil,
tabWidth: UInt? = nil) {
childrenReferences = children.references()
super.init(sourceTree: sourceTree,
path: path,
name: name,
includeInIndex: includeInIndex,
usesTabs: usesTabs,
indentWidth: indentWidth,
tabWidth: tabWidth,
wrapsLines: wrapsLines)
}
// MARK: - Decodable
fileprivate enum CodingKeys: String, CodingKey {
case children
}
public required init(from decoder: Decoder) throws {
let objects = decoder.context.objects
let objectReferenceRepository = decoder.context.objectReferenceRepository
let container = try decoder.container(keyedBy: CodingKeys.self)
let childrenReferences: [String] = (try container.decodeIfPresent(.children)) ?? []
self.childrenReferences = childrenReferences.map { objectReferenceRepository.getOrCreate(reference: $0, objects: objects) }
try super.init(from: decoder)
}
// MARK: - PlistSerializable
override func plistKeyAndValue(proj: PBXProj, reference: String) throws -> (key: CommentedString, value: PlistValue) {
var dictionary: [CommentedString: PlistValue] = try super.plistKeyAndValue(proj: proj, reference: reference).value.dictionary ?? [:]
dictionary["isa"] = .string(CommentedString(type(of: self).isa))
dictionary["children"] = .array(childrenReferences.map { (fileReference) -> PlistValue in
let fileElement: PBXFileElement? = fileReference.getObject()
return .string(CommentedString(fileReference.value, comment: fileElement?.fileName()))
})
return (key: CommentedString(reference,
comment: name ?? path),
value: .dictionary(dictionary))
}
}
// MARK: - Helpers
/// Options passed when adding new groups.
public struct GroupAddingOptions: OptionSet {
/// Raw value.
public let rawValue: Int
/// Initializes the options with the raw value.
///
/// - Parameter rawValue: raw value.
public init(rawValue: Int) {
self.rawValue = rawValue
}
/// Create group without reference to folder
public static let withoutFolder = GroupAddingOptions(rawValue: 1 << 0)
}
public extension PBXGroup {
/// Returns group with the given name contained in the given parent group.
///
/// - Parameter groupName: group name.
/// - Returns: group with the given name contained in the given parent group.
func group(named name: String) -> PBXGroup? {
return childrenReferences
.objects()
.first(where: { $0.name == name })
}
/// Returns the file in the group with the given name.
///
/// - Parameter name: file name.
/// - Returns: file with the given name contained in the given parent group.
func file(named name: String) -> PBXFileReference? {
return childrenReferences
.objects()
.first(where: { $0.name == name })
}
/// Creates a group with the given name and returns it.
///
/// - Parameters:
/// - groupName: group name.
/// - options: creation options.
/// - Returns: created groups.
@discardableResult
func addGroup(named groupName: String, options: GroupAddingOptions = []) throws -> [PBXGroup] {
let objects = try self.objects()
return groupName.components(separatedBy: "/").reduce(into: [PBXGroup]()) { groups, name in
let group = groups.last ?? self
let newGroup = PBXGroup(children: [], sourceTree: .group, name: name, path: options.contains(.withoutFolder) ? nil : name)
newGroup.parent = self
group.childrenReferences.append(newGroup.reference)
objects.add(object: newGroup)
groups.append(newGroup)
}
}
/// Creates a variant group with the given name and returns it.
///
/// - Parameters:
/// - groupName: group name.
/// - Returns: created groups.
@discardableResult
func addVariantGroup(named groupName: String) throws -> [PBXVariantGroup] {
let objects = try self.objects()
return groupName.components(separatedBy: "/").reduce(into: [PBXVariantGroup]()) { groups, name in
let group = groups.last ?? self
let newGroup = PBXVariantGroup(children: [], sourceTree: .group, name: name)
newGroup.parent = self
group.childrenReferences.append(newGroup.reference)
objects.add(object: newGroup)
groups.append(newGroup)
}
}
/// Adds file at the give path to the project or returns existing file and its reference.
///
/// - Parameters:
/// - filePath: path to the file.
/// - sourceTree: file sourceTree, default is `.group`.
/// - sourceRoot: path to project's source root.
/// - override: flag to enable overriding of existing file references, default is `true`.
/// - validatePresence: flag to validate the existence of the file in the file system, default is `true`.
/// - Returns: new or existing file and its reference.
@discardableResult
func addFile(
at filePath: Path,
sourceTree: PBXSourceTree = .group,
sourceRoot: Path,
override: Bool = true,
validatePresence: Bool = true
) throws -> PBXFileReference {
let projectObjects = try objects()
if validatePresence, !filePath.exists {
throw XcodeprojEditingError.unexistingFile(filePath)
}
let groupPath = try fullPath(sourceRoot: sourceRoot)
if override, let existingFileReference = try projectObjects.fileReferences.first(where: {
// Optimization: compare lastComponent before fullPath compare
guard let fileRefPath = $0.value.path else {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
let fileRefLastPathComponent = fileRefPath.split(separator: "/").last!
if filePath.lastComponent == fileRefLastPathComponent {
return try filePath == $0.value.fullPath(sourceRoot: sourceRoot)
}
return false
}) {
if !childrenReferences.contains(existingFileReference.key) {
existingFileReference.value.path = groupPath.flatMap { filePath.relative(to: $0) }?.string
childrenReferences.append(existingFileReference.key)
}
return existingFileReference.value
}
let path: String?
switch sourceTree {
case .group:
path = groupPath.map { filePath.relative(to: $0) }?.string
case .sourceRoot:
path = filePath.relative(to: sourceRoot).string
case .absolute:
path = filePath.string
default:
path = nil
}
let fileReference = PBXFileReference(
sourceTree: sourceTree,
name: filePath.lastComponent,
explicitFileType: filePath.extension.flatMap(Xcode.filetype),
lastKnownFileType: filePath.extension.flatMap(Xcode.filetype),
path: path
)
projectObjects.add(object: fileReference)
fileReference.parent = self
if !childrenReferences.contains(fileReference.reference) {
childrenReferences.append(fileReference.reference)
}
return fileReference
}
}