Skip to content

Commit

Permalink
Merge pull request #445 from tuist/fix/remote_id
Browse files Browse the repository at this point in the history
Fix PBXContainerItemProxy.remoteGlobalID
  • Loading branch information
Pedro Piñera Buendía authored Jun 10, 2019
2 parents 72657d8 + 2f45b83 commit fa30960
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
70 changes: 63 additions & 7 deletions Sources/xcodeproj/Objects/Files/PBXContainerItemProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,49 @@ public final class PBXContainerItemProxy: PBXObject {
case unknownObject(PBXObject?) /// This is used only for reading from corrupted projects. Don't use it.
}

enum RemoteGlobalIDReference: Equatable {
case reference(PBXObjectReference)
case string(String)

var uuid: String {
switch self {
case .reference(let reference): return reference.value
case .string(let string): return string
}
}

var id: RemoteGlobalID {
switch self {
case .reference(let reference):
if let object = reference.getObject() {
return .object(object)
} else {
return .string(reference.value)
}
case .string(let string): return .string(string)
}
}
}

public enum RemoteGlobalID: Equatable {
case object(PBXObject)
case string(String)

var uuid: String {
switch self {
case .object(let object): return object.uuid
case .string(let string): return string
}
}

var reference: RemoteGlobalIDReference {
switch self {
case .object(let object): return .reference(object.reference)
case .string(let string): return .string(string)
}
}
}

/// The object is a reference to a PBXProject element if proxy is for the object located in current .xcodeproj, otherwise PBXFileReference.
var containerPortalReference: PBXObjectReference

Expand All @@ -32,7 +75,16 @@ public final class PBXContainerItemProxy: PBXObject {
public var proxyType: ProxyType?

/// Element remote global ID reference. ID of the proxied object.
public var remoteGlobalIDString: String?
public var remoteGlobalID: RemoteGlobalID? {
get {
return remoteGlobalIDReference?.id
} set {
remoteGlobalIDReference = newValue?.reference
}
}

/// Element remote global ID reference. ID of the proxied object.
var remoteGlobalIDReference: RemoteGlobalIDReference?

/// Element remote info.
public var remoteInfo: String?
Expand All @@ -42,16 +94,16 @@ public final class PBXContainerItemProxy: PBXObject {
///
/// - Parameters:
/// - containerPortal: container portal. For proxied object located in the same .xcodeproj use .project. For remote object use .fileReference with PBXFileRefence of remote .xcodeproj
/// - remoteGlobalIDString: ID of the proxied object. Can be ID from remote .xcodeproj referenced if containerPortal is .fileReference
/// - remoteGlobalID: ID of the proxied object. Can be ID from remote .xcodeproj referenced if containerPortal is .fileReference
/// - proxyType: proxy type.
/// - remoteInfo: remote info.
public init(containerPortal: ContainerPortal,
remoteGlobalIDString: String? = nil,
remoteGlobalID: RemoteGlobalID? = nil,
proxyType: ProxyType? = nil,
remoteInfo: String? = nil) {
guard let containerPortalReference = containerPortal.reference else { fatalError("Container portal is mandatory field that has to be set to a known value instead of: \(containerPortal)") }
self.containerPortalReference = containerPortalReference
self.remoteGlobalIDString = remoteGlobalIDString
self.remoteGlobalIDReference = remoteGlobalID?.reference
self.remoteInfo = remoteInfo
self.proxyType = proxyType
super.init()
Expand All @@ -75,7 +127,11 @@ public final class PBXContainerItemProxy: PBXObject {
objects: objects)

proxyType = try container.decodeIntIfPresent(.proxyType).flatMap(ProxyType.init)
remoteGlobalIDString = try container.decodeIfPresent(.remoteGlobalIDString)
if let remoteGlobalIDString: String = try container.decodeIfPresent(.remoteGlobalIDString) {
let remoteGlobalReference = objectReferenceRepository.getOrCreate(reference: remoteGlobalIDString,
objects: objects)
remoteGlobalIDReference = .reference(remoteGlobalReference)
}
remoteInfo = try container.decodeIfPresent(.remoteInfo)
try super.init(from: decoder)
}
Expand All @@ -91,8 +147,8 @@ extension PBXContainerItemProxy: PlistSerializable {
if let proxyType = proxyType {
dictionary["proxyType"] = .string(CommentedString("\(proxyType.rawValue)"))
}
if let remoteGlobalIDString = remoteGlobalIDString {
dictionary["remoteGlobalIDString"] = .string(CommentedString(remoteGlobalIDString))
if let remoteGlobalID = remoteGlobalID {
dictionary["remoteGlobalIDString"] = .string(CommentedString(remoteGlobalID.uuid))
}
if let remoteInfo = remoteInfo {
dictionary["remoteInfo"] = .string(CommentedString(remoteInfo))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension PBXContainerItemProxy {
guard let rhs = object as? PBXContainerItemProxy else { return false }
if containerPortalReference != rhs.containerPortalReference { return false }
if proxyType != rhs.proxyType { return false }
if remoteGlobalIDString != rhs.remoteGlobalIDString { return false }
if remoteGlobalIDReference != rhs.remoteGlobalIDReference { return false }
if remoteInfo != rhs.remoteInfo { return false }
return super.isEqual(to: rhs)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/xcodeproj/Objects/Targets/PBXNativeTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public extension PBXNativeTarget {
return nil
}
let proxy = PBXContainerItemProxy(containerPortal: .project(project),
remoteGlobalIDString: target.uuid,
remoteGlobalID: .object(target),
proxyType: .nativeTarget,
remoteInfo: target.name)
objects.add(object: proxy)
Expand Down
2 changes: 1 addition & 1 deletion Sources/xcodeproj/Utils/ReferenceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ final class ReferenceGenerator: ReferenceGenerating {
if let targetProxyReference = targetDependency.targetProxyReference,
targetProxyReference.temporary,
let targetProxy = targetDependency.targetProxy,
let remoteGlobalIDString = targetProxy.remoteGlobalIDString {
let remoteGlobalIDString = targetProxy.remoteGlobalID?.uuid {
var identifiers = identifiers
identifiers.append(remoteGlobalIDString)
fixReference(for: targetProxy, identifiers: identifiers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ final class PBXContainerItemProxyTests: XCTestCase {
} catch {}
}

func test_maintains_remoteID() {
let target = PBXNativeTarget(name: "")
let project = PBXProject(name: "", buildConfigurationList: XCConfigurationList(), compatibilityVersion: "", mainGroup: PBXGroup())
let containerProxy = PBXContainerItemProxy(containerPortal: .project(project), remoteGlobalID: .object(target))

XCTAssertEqual(target.uuid, containerProxy.remoteGlobalID?.uuid)
}

private func testDictionary() -> [String: Any] {
return [
"containerPortal": "containerPortal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class PBXNativeTargetTests: XCTestCase {
XCTAssertEqual(targetDependency?.targetReference, dependency.reference)
let containerItemProxy: PBXContainerItemProxy? = targetDependency?.targetProxyReference?.getObject()
XCTAssertEqual(containerItemProxy?.containerPortalReference, project.reference)
XCTAssertEqual(containerItemProxy?.remoteGlobalIDString, dependency.reference.value)
XCTAssertEqual(containerItemProxy?.remoteGlobalID?.uuid, dependency.reference.value)
XCTAssertEqual(containerItemProxy?.proxyType, .nativeTarget)
XCTAssertEqual(containerItemProxy?.remoteInfo, "Dependency")
}
Expand Down

0 comments on commit fa30960

Please sign in to comment.