forked from tuist/XcodeProj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXcodeProj.swift
More file actions
243 lines (211 loc) Β· 9.57 KB
/
XcodeProj.swift
File metadata and controls
243 lines (211 loc) Β· 9.57 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import Foundation
import PathKit
/// Model that represents a .xcodeproj project.
public final class XcodeProj: Equatable {
// MARK: - Properties
/// Project workspace
public var workspace: XCWorkspace
/// .pbxproj representation
public var pbxproj: PBXProj
/// Shared data.
public var sharedData: XCSharedData?
/// User data.
public var userData: [XCUserData]
// MARK: - Init
public init(path: Path) throws {
var pbxproj: PBXProj!
var workspace: XCWorkspace!
var sharedData: XCSharedData?
var userData: [XCUserData]
if !path.exists { throw XCodeProjError.notFound(path: path) }
guard let pbxprojPath = path.glob("*.pbxproj").first else {
throw XCodeProjError.pbxprojNotFound(path: path)
}
pbxproj = try PBXProj(path: pbxprojPath)
let xcworkspacePaths = path.glob("*.xcworkspace")
if xcworkspacePaths.isEmpty {
workspace = XCWorkspace()
} else {
workspace = try XCWorkspace(path: xcworkspacePaths.first!)
}
let sharedDataPath = path + "xcshareddata"
sharedData = try? XCSharedData(path: sharedDataPath)
userData = XCUserData.path(path)
.glob("*.xcuserdatad")
.compactMap { try? XCUserData(path: $0) }
self.pbxproj = pbxproj
self.workspace = workspace
self.sharedData = sharedData
self.userData = userData
}
public convenience init(pathString: String) throws {
try self.init(path: Path(pathString))
}
/// Initializes the XCodeProj
///
/// - Parameters:
/// - workspace: project internal workspace.
/// - pbxproj: project .pbxproj.
/// - sharedData: shared data
/// - userData: user data
public init(workspace: XCWorkspace,
pbxproj: PBXProj,
sharedData: XCSharedData? = nil,
userData: [XCUserData] = []) {
self.workspace = workspace
self.pbxproj = pbxproj
self.sharedData = sharedData
self.userData = userData
}
// MARK: - Equatable
public static func == (lhs: XcodeProj, rhs: XcodeProj) -> Bool {
lhs.workspace == rhs.workspace &&
lhs.pbxproj == rhs.pbxproj &&
lhs.sharedData == rhs.sharedData &&
lhs.userData == rhs.userData
}
}
// MARK: - <Writable>
extension XcodeProj: Writable {
/// Writes project to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if project should be overridden. Default is true.
/// If false will throw error if project already exists at the given path.
public func write(path: Path, override: Bool = true) throws {
try write(path: path, override: override, outputSettings: PBXOutputSettings())
}
/// Writes project to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if project should be overridden. Default is true.
/// - Parameter outputSettings: Controls the writing of various files.
/// If false will throw error if project already exists at the given path.
public func write(path: Path, override: Bool = true, outputSettings: PBXOutputSettings) throws {
try path.mkpath()
try writeWorkspace(path: path, override: override)
try writePBXProj(path: path, override: override, outputSettings: outputSettings)
try writeSharedData(path: path, override: override)
try writeUserData(path: path, override: override)
}
/// Returns workspace file path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Returns: workspace file path relative to the given path.
public static func workspacePath(_ path: Path) -> Path {
path + "project.xcworkspace"
}
/// Writes workspace to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if workspace should be overridden. Default is true.
/// If false will throw error if workspace already exists at the given path.
public func writeWorkspace(path: Path, override: Bool = true) throws {
try workspace.write(path: XcodeProj.workspacePath(path), override: override)
}
/// Returns project file path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Returns: project file path relative to the given path.
public static func pbxprojPath(_ path: Path) -> Path {
path + "project.pbxproj"
}
/// Writes project to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if project should be overridden. Default is true.
/// - Parameter outputSettings: Controls the writing of various files.
/// If false will throw error if project already exists at the given path.
public func writePBXProj(path: Path, override: Bool = true, outputSettings: PBXOutputSettings) throws {
try pbxproj.write(path: XcodeProj.pbxprojPath(path), override: override, outputSettings: outputSettings)
}
/// Returns shared data path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Returns: shared data path relative to the given path.
public static func sharedDataPath(_ path: Path) -> Path {
XCSharedData.path(path)
}
/// Writes shared data to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if shared data should be overridden. Default is true.
/// - Parameter outputSettings: Controls the writing of various files.
/// If false will throw error if shared data already exists at the given path.
public func writeSharedData(path: Path, override: Bool = true) throws {
try sharedData?.write(path: XCSharedData.path(path), override: override)
}
/// Writes user data to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if user data should be overridden. Default is true.
/// - Parameter outputSettings: Controls the writing of various files.
/// If false will throw error if user data already exists at the given path.
public func writeUserData(path: Path, override: Bool = true) throws {
for userData in userData {
try userData.write(path: XCUserData.path(path, userName: userData.userName), override: override)
}
}
/// Returns shared schemes folder path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Returns: schemes folder path relative to the given path.
@available(*, deprecated, message: "use XCScheme.schemesPath(path:)")
public static func schemesPath(_ path: Path) -> Path {
XCScheme.schemesPath(sharedDataPath(path))
}
/// Returns shared scheme file path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Parameter schemeName: scheme name
/// - Returns: scheme file path relative to the given path.
@available(*, deprecated, message: "use XCScheme.path(path:schemeName)")
public static func schemePath(_ path: Path, schemeName: String) -> Path {
XCScheme.path(schemesPath(path), schemeName: schemeName)
}
/// Writes all project schemes to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if project should be overridden. Default is true.
/// If true will remove all existing schemes before writing.
/// If false will throw error if scheme already exists at the given path.
public func writeSchemes(path: Path, override: Bool = true) throws {
try sharedData?.writeSchemes(path: XCSharedData.path(path), override: override)
for userData in userData {
let userDataPath = XCUserData.path(path, userName: userData.userName)
try userData.writeSchemes(path: userDataPath, override: override)
}
}
/// Returns shared debugger folder path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Parameter schemeName: scheme name
/// - Returns: debugger folder path relative to the given path.
@available(*, deprecated, message: "use XCDebugger.path(path:)")
public static func debuggerPath(_ path: Path) -> Path {
XCDebugger.path(XCSharedData.path(path))
}
/// Returns shared breakpoints plist path relative to the given path.
///
/// - Parameter path: `.xcodeproj` file path
/// - Parameter schemeName: scheme name
/// - Returns: breakpoints plist path relative to the given path.
@available(*, deprecated, message: "use XCBreakpointList.path(path:)")
public static func breakPointsPath(_ path: Path) -> Path {
XCBreakpointList.path(debuggerPath(path))
}
/// Writes all project breakpoints to the given path.
///
/// - Parameter path: path to `.xcodeproj` file.
/// - Parameter override: if project should be overridden. Default is true.
/// If true will remove all existing debugger data before writing.
/// If false will throw error if breakpoints file exists at the given path.
public func writeBreakPoints(path: Path, override: Bool = true) throws {
let sharedDataPath = XcodeProj.sharedDataPath(path)
try sharedData?.writeBreakpoints(path: sharedDataPath, override: override)
for userData in userData {
let userDataPath = XCUserData.path(path, userName: userData.userName)
try userData.writeBreakpoints(path: userDataPath, override: override)
}
}
}