Skip to content

Commit 608a29b

Browse files
committed
Add some missing references. Add referrers to allow a remove an object from them
1 parent 19a06ab commit 608a29b

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

Sources/PBXBuildFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public class PBXBuildFile: PBXProjectItem {
1717
}
1818

1919
#if LAZY
20-
public lazy var fileRef: PBXReference? = self.object(PBXKeys.fileRef)
20+
public lazy var fileRef: PBXFileReference? = self.object(PBXKeys.fileRef)
2121
public lazy var productRef: XCSwiftPackageProductDependency? = self.object(PBXKeys.productRef)
2222
public lazy var settings: [String: Any]? = self.dictionary(PBXKeys.settings)
2323
#else
24-
public var fileRef: PBXReference? { self.object(PBXKeys.fileRef) }
24+
public var fileRef: PBXFileReference? { self.object(PBXKeys.fileRef) }
2525
public var productRef: XCSwiftPackageProductDependency? { self.object(PBXKeys.productRef) }
2626
public var settings: [String: Any]? { self.dictionary(PBXKeys.settings) }
2727
#endif

Sources/PBXObject.swift

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public /* abstract */ class PBXObject {
1818
public var fields: PBXObject.Fields
1919
let objects: PBXObjectFactory
2020

21+
public var referrers: [PBXObject] = []
22+
2123
public enum PBXKeys: PBXKey {
2224
case isa
2325
}
@@ -148,12 +150,19 @@ extension PBXObject {
148150
guard let objectKeys = fields[key] as? [String] else {
149151
return []
150152
}
151-
152-
return objectKeys.compactMap(objects.object)
153+
let objects: [T] = objectKeys.compactMap(objects.object)
154+
for object in objects {
155+
object.addReferrers(self)
156+
}
157+
return objects
153158
}
154159

155160
func object<T: PBXObject, R: RawRepresentable>(_ key: R) -> T? where R.RawValue == String {
156-
return object(key.rawValue)
161+
if let object: T = object(key.rawValue) {
162+
object.addReferrers(self)
163+
return object
164+
}
165+
return nil
157166
}
158167

159168
func objects<T: PBXObject, R: RawRepresentable>(_ key: R) -> [T] where R.RawValue == String {
@@ -227,6 +236,37 @@ extension PBXObject {
227236
public func attach() {
228237
objects.add(self)
229238
}
239+
240+
public func addReferrers(_ object: PBXObject) {
241+
self.referrers.append(object)
242+
}
243+
244+
public func removeReferrers(_ object: PBXObject) {
245+
self.referrers.removeAll(where: { $0.ref == object.ref})
246+
}
247+
248+
public func removeFromReferrers() {
249+
for referrer in self.referrers {
250+
for (key, value) in referrer.fields {
251+
if let value = value as? String {
252+
if value == self.ref {
253+
referrer.fields[key] = nil
254+
}
255+
} else if var values = value as? [String] {
256+
for (index, value) in values.enumerated() where value == self.ref {
257+
values.remove(at: index)
258+
referrer.fields[key] = values
259+
break
260+
}
261+
}
262+
}
263+
}
264+
}
265+
266+
public func remove() {
267+
unattach()
268+
removeFromReferrers()
269+
}
230270
}
231271

232272
extension PBXObject: CustomStringConvertible {

Sources/PBXTarget.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
1313
public enum PBXKeys: PBXKey {
1414
case name
1515
case productName
16+
case productReference
17+
case productType
1618
case buildPhases
1719
case buildRules
1820
case buildConfigurationList
@@ -23,6 +25,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
2325
#if LAZY
2426
public lazy var name: String = self.string(PBXKeys.name)
2527
public lazy var productName: String? = self.string(PBXKeys.productName)
28+
public lazy var productReference: PBXFileReference? = self.object(PBXKeys.productReference)
29+
public lazy var productType: String? = self.string(PBXKeys.productName)
2630
public lazy var buildPhases: [PBXBuildPhase] = self.objects(PBXKeys.buildPhases)
2731
public lazy var buildRules: [PBXBuildRule] = self.objects(PBXKeys.buildRules)
2832
public lazy var buildConfigurationList: XCConfigurationList? = self.object(PBXKeys.buildConfigurationList)
@@ -31,6 +35,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
3135
#else
3236
public var name: String { self.string(PBXKeys.name) }
3337
public var productName: String? { self.string(PBXKeys.productName) }
38+
public var productReference: PBXFileReference? { self.object(PBXKeys.productReference) }
39+
public var productType: String? { self.string(PBXKeys.productName) }
3440
public var buildPhases: [PBXBuildPhase] { self.objects(PBXKeys.buildPhases) }
3541
public var buildRules: [PBXBuildRule] { self.objects(PBXKeys.buildRules) }
3642
public var buildConfigurationList: XCConfigurationList? { self.object(PBXKeys.buildConfigurationList) }

Sources/XCBuildConfiguration.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ public class XCBuildConfiguration: PBXBuildStyle {
1212

1313
public enum PBXKeys: PBXKey {
1414
case name
15+
case baseConfigurationReference
1516
}
1617

1718
#if LAZY
1819
public lazy var name: String? = self.string(PBXKeys.name)
20+
public var baseConfigurationReference: PBXFileReference? = self.object(PBXKeys.baseConfigurationReference)
1921
#else
2022
public var name: String? { self.string(PBXKeys.name) }
23+
public var baseConfigurationReference: PBXFileReference? { self.object(PBXKeys.baseConfigurationReference) }
2124
#endif
2225

2326
public override var comment: String? {

0 commit comments

Comments
 (0)