From 15bf493ff4f96d616de50bf131201ef1dda8e1e1 Mon Sep 17 00:00:00 2001 From: Geoffrey Foster Date: Wed, 17 Jan 2018 15:29:30 -0500 Subject: [PATCH] removing unneeded code, cleanup some todo items (#1) --- .../Archiving/PBXPListArchiver.swift | 56 ++++--------------- .../Archiving/PListArchivable.swift | 4 +- .../PBXGroup+FolderSync.swift | 2 +- .../PBXProject+Helpers.swift | 6 +- .../PBXReference+Extensions.swift | 2 +- .../XcodeProject/Objects/PBXObject+ID.swift | 6 ++ Sources/XcodeProject/ProjectFile.swift | 6 +- 7 files changed, 30 insertions(+), 52 deletions(-) diff --git a/Sources/XcodeProject/Archiving/PBXPListArchiver.swift b/Sources/XcodeProject/Archiving/PBXPListArchiver.swift index 9760a06..224c9a6 100644 --- a/Sources/XcodeProject/Archiving/PBXPListArchiver.swift +++ b/Sources/XcodeProject/Archiving/PBXPListArchiver.swift @@ -9,7 +9,7 @@ import Foundation public protocol StreamWritable: class { - func write(_ string: String) + func write(_ string: String) throws } public class ConsoleStreamWriter: StreamWritable { @@ -80,41 +80,17 @@ struct Format { var indentation: Indentation = Indentation() } -struct PBXObjectType: Hashable, CustomStringConvertible { - let type: PBXObject.Type - - init(type: PBXObject.Type) { - self.type = type - } - - static func ==(lhs: PBXObjectType, rhs: PBXObjectType) -> Bool { - return lhs.type == rhs.type - } - - var hashValue: Int { - return String(describing: type).hashValue - } - - var description: String { - return String(describing: type) - } -} - struct ObjectMap { - let objectMap: [PBXObjectType: [PBXObject]] + let objectMap: [String: [PBXObject]] init(project: PBXProject) { - var buckets: [PBXObjectType: [PBXObject]] = [:] + var buckets: [String: [PBXObject]] = [:] let visitor = ObjectVisitor() visitor.visit(object: project) - visitor.types.forEach { - buckets[$0] = [] - } - visitor.allObjects.forEach { object in - buckets[PBXObjectType(type: type(of: object))]!.append(object) + buckets[String(describing: type(of: object)), default: []].append(object) } self.objectMap = buckets @@ -134,23 +110,23 @@ public final class PBXPListArchiver { archiveDictionary[ProjectFile.RootKey.rootObject] = projectFile.project.plistID } - public func write(stream: StreamWritable) { + public func write(stream: StreamWritable) throws { var format = Format() - stream.write("// !$*UTF8*$!\(format.endOfLine)") - stream.write("{\(format.endOfLine)") + try stream.write("// !$*UTF8*$!\(format.endOfLine)") + try stream.write("{\(format.endOfLine)") format.indentation.increase() - archiveDictionary.sorted { (obj1, obj2) in + try archiveDictionary.sorted { (obj1, obj2) in return obj1.key < obj2.key }.forEach { (key, value) in guard let value = value else { return } - stream.write("\(format.indentation)\(key) = ") - stream.write(value.plistRepresentation(format: format)) - stream.write(";\(format.endOfLine)") + try stream.write("\(format.indentation)\(key) = ") + try stream.write(value.plistRepresentation(format: format)) + try stream.write(";\(format.endOfLine)") } format.indentation.decrease() - stream.write("}\(format.endOfLine)") + try stream.write("}\(format.endOfLine)") } } @@ -168,10 +144,6 @@ final class ObjectVisitor { object.visit(self) } - var types: [PBXObjectType] { - return Array(Set(objectMap.map { PBXObjectType(type: type(of: $0.value)) })) - } - var allObjects: [PBXObject] { return objects() } @@ -179,10 +151,6 @@ final class ObjectVisitor { func objects() -> [T] { return objectMap.flatMap { return $0.value as? T } } - - var keys: Set { - return Set(objectMap.keys) - } } extension PBXObject { diff --git a/Sources/XcodeProject/Archiving/PListArchivable.swift b/Sources/XcodeProject/Archiving/PListArchivable.swift index cff0520..3fa6fcd 100644 --- a/Sources/XcodeProject/Archiving/PListArchivable.swift +++ b/Sources/XcodeProject/Archiving/PListArchivable.swift @@ -24,7 +24,7 @@ extension ObjectMap: PListArchivable { func plist(objects: [PBXObject], format: Format) -> String { var str = "" objects.sorted { (obj1, obj2) -> Bool in - obj1.globalID.rawValue < obj2.globalID.rawValue + obj1.globalID < obj2.globalID }.forEach { str += $0.plistRepresentation(format: format) } @@ -32,7 +32,7 @@ extension ObjectMap: PListArchivable { } var str = "{\n" objectMap.sorted { (obj1, obj2) -> Bool in - return obj1.key.description < obj2.key.description + return obj1.key < obj2.key }.forEach { (key, value) in str += "\n/* Begin \(key) section */\n" var format = format diff --git a/Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift b/Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift index 9976b7c..b5ae2bf 100644 --- a/Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift +++ b/Sources/XcodeProject/Objects+Extensions/PBXGroup+FolderSync.swift @@ -11,7 +11,7 @@ import Foundation public extension PBXGroup { @discardableResult func add(file: URL, createGroupsRecursively: Bool = true) -> PBXFileReference? { - guard let url = url else { return nil } // TODO: return an absolute reference? or maybe a project one? + guard let url = url else { return nil } var matchingComponentsCount = 0 var filePathComponents = file.pathComponents for components in zip(url.pathComponents, filePathComponents) { diff --git a/Sources/XcodeProject/Objects+Extensions/PBXProject+Helpers.swift b/Sources/XcodeProject/Objects+Extensions/PBXProject+Helpers.swift index d73d163..57da328 100644 --- a/Sources/XcodeProject/Objects+Extensions/PBXProject+Helpers.swift +++ b/Sources/XcodeProject/Objects+Extensions/PBXProject+Helpers.swift @@ -8,7 +8,11 @@ public extension PBXProject { func generateGlobalId() -> PBXObject.ID { - return PBXObject.ID() // TODO: PBXProject should keep track of its already used ID's and prevent duplicates + var objectId = PBXObject.ID() + while objects[objectId] != nil { + objectId = PBXObject.ID() + } + return objectId } } public extension PBXProject { diff --git a/Sources/XcodeProject/Objects+Extensions/PBXReference+Extensions.swift b/Sources/XcodeProject/Objects+Extensions/PBXReference+Extensions.swift index 4c2b831..a70eff2 100644 --- a/Sources/XcodeProject/Objects+Extensions/PBXReference+Extensions.swift +++ b/Sources/XcodeProject/Objects+Extensions/PBXReference+Extensions.swift @@ -12,7 +12,7 @@ public extension PBXReference { var url: URL? { guard let sourceTree = sourceTree else { return nil } guard let project = parentProject else { return nil } - let filePath = path ?? "" // TODO: this is probably not quite right + let filePath = path ?? "" switch sourceTree { case .absolute: return URL(fileURLWithPath: filePath) diff --git a/Sources/XcodeProject/Objects/PBXObject+ID.swift b/Sources/XcodeProject/Objects/PBXObject+ID.swift index 0ee81ea..a391f43 100644 --- a/Sources/XcodeProject/Objects/PBXObject+ID.swift +++ b/Sources/XcodeProject/Objects/PBXObject+ID.swift @@ -124,6 +124,12 @@ extension PBXObject.ID: Hashable { } } +extension PBXObject.ID: Comparable { + public static func <(lhs: PBXObject.ID, rhs: PBXObject.ID) -> Bool { + return lhs.rawValue < rhs.rawValue + } +} + extension PropertyList { var globalID: PBXObject.ID? { return PBXObject.ID(rawValue: self.string) diff --git a/Sources/XcodeProject/ProjectFile.swift b/Sources/XcodeProject/ProjectFile.swift index 7fab799..0da8b47 100644 --- a/Sources/XcodeProject/ProjectFile.swift +++ b/Sources/XcodeProject/ProjectFile.swift @@ -81,14 +81,14 @@ public final class ProjectFile { } } - public func currentFileWrapper() -> FileWrapper { + public func currentFileWrapper() throws -> FileWrapper { let currentFileWrapper = fileWrapper let oldPbxproj = currentFileWrapper.fileWrappers!["project.pbxproj"]! let dataStream = DataStreamWriter() let archiver = PBXPListArchiver(projectFile: self) - archiver.write(stream: dataStream) + try archiver.write(stream: dataStream) let newPbxproj = FileWrapper(regularFileWithContents: dataStream.data) newPbxproj.preferredFilename = "project.pbxproj" @@ -113,7 +113,7 @@ extension ProjectFile { let dataStream = DataStreamWriter() let archiver = PBXPListArchiver(projectFile: self) - archiver.write(stream: dataStream) + try archiver.write(stream: dataStream) let newPbxproj = FileWrapper(regularFileWithContents: dataStream.data) newPbxproj.preferredFilename = "project.pbxproj"