Skip to content

Commit

Permalink
Add some missing references. Add referrers to allow a remove an objec…
Browse files Browse the repository at this point in the history
…t from them
  • Loading branch information
e-marchand committed Nov 11, 2022
1 parent 19a06ab commit 608a29b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/PBXBuildFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class PBXBuildFile: PBXProjectItem {
}

#if LAZY
public lazy var fileRef: PBXReference? = self.object(PBXKeys.fileRef)
public lazy var fileRef: PBXFileReference? = self.object(PBXKeys.fileRef)
public lazy var productRef: XCSwiftPackageProductDependency? = self.object(PBXKeys.productRef)
public lazy var settings: [String: Any]? = self.dictionary(PBXKeys.settings)
#else
public var fileRef: PBXReference? { self.object(PBXKeys.fileRef) }
public var fileRef: PBXFileReference? { self.object(PBXKeys.fileRef) }
public var productRef: XCSwiftPackageProductDependency? { self.object(PBXKeys.productRef) }
public var settings: [String: Any]? { self.dictionary(PBXKeys.settings) }
#endif
Expand Down
46 changes: 43 additions & 3 deletions Sources/PBXObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public /* abstract */ class PBXObject {
public var fields: PBXObject.Fields
let objects: PBXObjectFactory

public var referrers: [PBXObject] = []

public enum PBXKeys: PBXKey {
case isa
}
Expand Down Expand Up @@ -148,12 +150,19 @@ extension PBXObject {
guard let objectKeys = fields[key] as? [String] else {
return []
}

return objectKeys.compactMap(objects.object)
let objects: [T] = objectKeys.compactMap(objects.object)
for object in objects {
object.addReferrers(self)
}
return objects
}

func object<T: PBXObject, R: RawRepresentable>(_ key: R) -> T? where R.RawValue == String {
return object(key.rawValue)
if let object: T = object(key.rawValue) {
object.addReferrers(self)
return object
}
return nil
}

func objects<T: PBXObject, R: RawRepresentable>(_ key: R) -> [T] where R.RawValue == String {
Expand Down Expand Up @@ -227,6 +236,37 @@ extension PBXObject {
public func attach() {
objects.add(self)
}

public func addReferrers(_ object: PBXObject) {
self.referrers.append(object)
}

public func removeReferrers(_ object: PBXObject) {
self.referrers.removeAll(where: { $0.ref == object.ref})
}

public func removeFromReferrers() {
for referrer in self.referrers {
for (key, value) in referrer.fields {
if let value = value as? String {
if value == self.ref {
referrer.fields[key] = nil
}
} else if var values = value as? [String] {
for (index, value) in values.enumerated() where value == self.ref {
values.remove(at: index)
referrer.fields[key] = values
break
}
}
}
}
}

public func remove() {
unattach()
removeFromReferrers()
}
}

extension PBXObject: CustomStringConvertible {
Expand Down
6 changes: 6 additions & 0 deletions Sources/PBXTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
public enum PBXKeys: PBXKey {
case name
case productName
case productReference
case productType
case buildPhases
case buildRules
case buildConfigurationList
Expand All @@ -23,6 +25,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
#if LAZY
public lazy var name: String = self.string(PBXKeys.name)
public lazy var productName: String? = self.string(PBXKeys.productName)
public lazy var productReference: PBXFileReference? = self.object(PBXKeys.productReference)
public lazy var productType: String? = self.string(PBXKeys.productName)
public lazy var buildPhases: [PBXBuildPhase] = self.objects(PBXKeys.buildPhases)
public lazy var buildRules: [PBXBuildRule] = self.objects(PBXKeys.buildRules)
public lazy var buildConfigurationList: XCConfigurationList? = self.object(PBXKeys.buildConfigurationList)
Expand All @@ -31,6 +35,8 @@ public /* abstract */ class PBXTarget: PBXProjectItem, PBXBuildConfigurationList
#else
public var name: String { self.string(PBXKeys.name) }
public var productName: String? { self.string(PBXKeys.productName) }
public var productReference: PBXFileReference? { self.object(PBXKeys.productReference) }
public var productType: String? { self.string(PBXKeys.productName) }
public var buildPhases: [PBXBuildPhase] { self.objects(PBXKeys.buildPhases) }
public var buildRules: [PBXBuildRule] { self.objects(PBXKeys.buildRules) }
public var buildConfigurationList: XCConfigurationList? { self.object(PBXKeys.buildConfigurationList) }
Expand Down
3 changes: 3 additions & 0 deletions Sources/XCBuildConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public class XCBuildConfiguration: PBXBuildStyle {

public enum PBXKeys: PBXKey {
case name
case baseConfigurationReference
}

#if LAZY
public lazy var name: String? = self.string(PBXKeys.name)
public var baseConfigurationReference: PBXFileReference? = self.object(PBXKeys.baseConfigurationReference)
#else
public var name: String? { self.string(PBXKeys.name) }
public var baseConfigurationReference: PBXFileReference? { self.object(PBXKeys.baseConfigurationReference) }
#endif

public override var comment: String? {
Expand Down

0 comments on commit 608a29b

Please sign in to comment.