Skip to content

Commit

Permalink
removing unneeded code, cleanup some todo items (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-Off authored Jan 17, 2018
1 parent 905dde9 commit 15bf493
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 52 deletions.
56 changes: 12 additions & 44 deletions Sources/XcodeProject/Archiving/PBXPListArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

public protocol StreamWritable: class {
func write(_ string: String)
func write(_ string: String) throws
}

public class ConsoleStreamWriter: StreamWritable {
Expand Down Expand Up @@ -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
Expand All @@ -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)")
}
}

Expand All @@ -168,21 +144,13 @@ 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()
}

func objects<T: PBXObject>() -> [T] {
return objectMap.flatMap { return $0.value as? T }
}

var keys: Set<PBXObject.ID> {
return Set(objectMap.keys)
}
}

extension PBXObject {
Expand Down
4 changes: 2 additions & 2 deletions Sources/XcodeProject/Archiving/PListArchivable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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)
}
return str
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions Sources/XcodeProject/Objects/PBXObject+ID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Sources/XcodeProject/ProjectFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand Down

0 comments on commit 15bf493

Please sign in to comment.