forked from tomlokhorst/XcodeEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXCProjectFile.swift
More file actions
161 lines (132 loc) · 4.65 KB
/
XCProjectFile.swift
File metadata and controls
161 lines (132 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// XCProjectFile.swift
// Xcode
//
// Created by Tom Lokhorst on 2015-08-12.
// Copyright (c) 2015 nonstrict. All rights reserved.
//
import Foundation
enum ProjectFileError : ErrorType, CustomStringConvertible {
case InvalidData
case NotXcodeproj
case MissingPbxproj
var description: String {
switch self {
case .InvalidData:
return "Data in .pbxproj file not in expected format"
case .NotXcodeproj:
return "Path is not a .xcodeproj package"
case .MissingPbxproj:
return "project.pbxproj file missing"
}
}
}
public class AllObjects {
var dict: [String: PBXObject] = [:]
var fullFilePaths: [String: Path] = [:]
func object<T : PBXObject>(key: String) -> T {
let obj = dict[key]!
if let t = obj as? T {
return t
}
return T(id: key, dict: obj.dict, allObjects: self)
}
}
public class XCProjectFile {
public let project: PBXProject
let dict: JsonObject
var format: NSPropertyListFormat
let allObjects = AllObjects()
public convenience init(xcodeprojURL: NSURL) throws {
let pbxprojURL = xcodeprojURL.URLByAppendingPathComponent("project.pbxproj")
guard let data = NSData(contentsOfURL: pbxprojURL) else {
throw ProjectFileError.MissingPbxproj
}
try self.init(propertyListData: data)
}
public convenience init(propertyListData data: NSData) throws {
let options = NSPropertyListReadOptions.Immutable
var format: NSPropertyListFormat = NSPropertyListFormat.BinaryFormat_v1_0
let obj = try NSPropertyListSerialization.propertyListWithData(data, options: options, format: &format)
guard let dict = obj as? JsonObject else {
throw ProjectFileError.InvalidData
}
self.init(dict: dict, format: format)
}
init(dict: JsonObject, format: NSPropertyListFormat) {
self.dict = dict
self.format = format
let objects = dict["objects"] as! [String: JsonObject]
for (key, obj) in objects {
allObjects.dict[key] = XCProjectFile.createObject(key, dict: obj, allObjects: allObjects)
}
let rootObjectId = dict["rootObject"]! as! String
let projDict = objects[rootObjectId]!
self.project = PBXProject(id: rootObjectId, dict: projDict, allObjects: allObjects)
self.allObjects.fullFilePaths = paths(self.project.mainGroup, prefix: "")
}
static func projectName(url: NSURL) throws -> String {
guard let subpaths = url.pathComponents,
let last = subpaths.last,
let range = last.rangeOfString(".xcodeproj")
else {
throw ProjectFileError.NotXcodeproj
}
return last.substringToIndex(range.startIndex)
}
static func createObject(id: String, dict: JsonObject, allObjects: AllObjects) -> PBXObject {
let isa = dict["isa"] as? String
if let isa = isa,
let type = types[isa] {
return type.init(id: id, dict: dict, allObjects: allObjects)
}
// Fallback
assertionFailure("Unknown PBXObject subclass isa=\(isa)")
return PBXObject(id: id, dict: dict, allObjects: allObjects)
}
func paths(current: PBXGroup, prefix: String) -> [String: Path] {
var ps: [String: Path] = [:]
for file in current.fileRefs {
switch file.sourceTree {
case .Group:
ps[file.id] = .RelativeTo(.SourceRoot, prefix + "/" + file.path!)
case .Absolute:
ps[file.id] = .Absolute(file.path!)
case let .RelativeTo(sourceTreeFolder):
ps[file.id] = .RelativeTo(sourceTreeFolder, file.path!)
}
}
for group in current.subGroups {
if let path = group.path {
ps += paths(group, prefix: prefix + "/" + path)
}
else {
ps += paths(group, prefix: prefix)
}
}
return ps
}
}
let types: [String: PBXObject.Type] = [
"PBXProject": PBXProject.self,
"PBXContainerItemProxy": PBXContainerItemProxy.self,
"PBXBuildFile": PBXBuildFile.self,
"PBXCopyFilesBuildPhase": PBXCopyFilesBuildPhase.self,
"PBXFrameworksBuildPhase": PBXFrameworksBuildPhase.self,
"PBXHeadersBuildPhase": PBXHeadersBuildPhase.self,
"PBXResourcesBuildPhase": PBXResourcesBuildPhase.self,
"PBXShellScriptBuildPhase": PBXShellScriptBuildPhase.self,
"PBXSourcesBuildPhase": PBXSourcesBuildPhase.self,
"PBXBuildStyle": PBXBuildStyle.self,
"XCBuildConfiguration": XCBuildConfiguration.self,
"PBXAggregateTarget": PBXAggregateTarget.self,
"PBXNativeTarget": PBXNativeTarget.self,
"PBXTargetDependency": PBXTargetDependency.self,
"XCConfigurationList": XCConfigurationList.self,
"PBXReference": PBXReference.self,
"PBXReferenceProxy": PBXReferenceProxy.self,
"PBXFileReference": PBXFileReference.self,
"PBXGroup": PBXGroup.self,
"PBXVariantGroup": PBXVariantGroup.self,
"XCVersionGroup": XCVersionGroup.self
]