Skip to content

Commit f020fb6

Browse files
phimagee-marchand
authored andcommitted
Same signature for plist serialization
1 parent 00fe58d commit f020fb6

File tree

4 files changed

+59
-83
lines changed

4 files changed

+59
-83
lines changed

Sources/PropertyList.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ open class PropertyList {
6363
return nil
6464
}
6565
}
66-
66+
6767
}
6868

6969
public let dict: PBXObject.Fields
@@ -107,10 +107,17 @@ open class PropertyList {
107107

108108
// MARK: - Write
109109
public func write(to url: URL,
110-
format: Format,
110+
format: Format? = nil,
111+
projectName: String? = nil,
112+
lineEnding: String? = nil,
111113
atomic: Bool = true) throws {
114+
let format = format ?? .xml
112115
if format == .openStep {
113-
throw XcodeProjError.notSupported
116+
try XcodeProj(dict: self.dict, format: Format.openStep).write(to: url,
117+
format: format,
118+
projectName: projectName,
119+
lineEnding: lineEnding,
120+
atomic: atomic)
114121
} else if let propertyListformat = format.toPropertyListformat() {
115122
let data = try PropertyListSerialization.data(
116123
fromPropertyList: dict,
@@ -131,9 +138,9 @@ open class PropertyList {
131138
}
132139
}
133140

134-
public func data() throws -> Data {
141+
public func data(projectName: String? = nil) throws -> Data {
135142
if format == .openStep {
136-
throw XcodeProjError.notSupported
143+
return try XcodeProj(dict: self.dict, format: Format.openStep).data(projectName: projectName)
137144
} else if let propertyListformat = format.toPropertyListformat() {
138145
return try PropertyListSerialization.data(fromPropertyList: dict, format: propertyListformat, options: 0)
139146
} else if format == .json {

Sources/XcodeProj+Serialization.swift

Lines changed: 0 additions & 75 deletions
This file was deleted.

Sources/XcodeProj.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,53 @@ public class XcodeProj: PropertyList {
158158
return PBXObject(ref: ref, fields: fields, objects: objects)
159159
}
160160

161+
public override func write(to url: URL,
162+
format: Format? = nil,
163+
projectName: String? = nil,
164+
lineEnding: String? = nil,
165+
atomic: Bool = true) throws {
166+
let pbxprojURL: URL
167+
let name: String
168+
if url.isDirectoryURL {
169+
pbxprojURL = url.appendingPathComponent(XcodeProj.pbxprojFileName, isDirectory: false)
170+
let subpaths = url.pathComponents
171+
if let projectName = projectName {
172+
name = projectName
173+
} else {
174+
// Find in project
175+
if let last = subpaths.last, let range = last.range(of: ".xcodeproj") {
176+
name = String(last[...range.lowerBound])
177+
} else {
178+
name = self.projectName // default
179+
}
180+
}
181+
} else {
182+
pbxprojURL = url
183+
name = projectName ?? self.projectName
184+
}
185+
let lineEnding = lineEnding ?? self.lineEnding
186+
let format = format ?? self.format
187+
if format == .openStep {
188+
let serializer = OpenStepSerializer(projectName: name, lineEnding: lineEnding, projectFile: self)
189+
try serializer.serialize().write(to: pbxprojURL, atomically: atomic, encoding: .utf8)
190+
} else {
191+
try super.write(to: pbxprojURL,
192+
format: format,
193+
projectName: projectName,
194+
lineEnding: lineEnding,
195+
atomic: atomic)
196+
}
197+
}
198+
199+
public override func data(projectName: String? = nil) throws -> Data {
200+
if format == .openStep {
201+
let projectName = projectName ?? self.projectName
202+
let serializer = OpenStepSerializer(projectName: projectName, projectFile: self)
203+
return try serializer.serialize().data(using: .utf8) ?? Data()
204+
}
205+
return try super.data(projectName: projectName)
206+
}
207+
161208
}
162209

163210
let extractProjetName = "Build configuration list for PBXProject \""

Sources/XcodeProjError.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,4 @@ public enum XcodeProjError: Error {
2222
// object missing
2323
case objectMissing(key: String, expectedType: Isa?)
2424

25-
// not supported
26-
case notSupported
27-
2825
}

0 commit comments

Comments
 (0)