Skip to content

Commit

Permalink
Get Data representation for Writable (#787)
Browse files Browse the repository at this point in the history
* get string representation of pbxproj

* made the outputSettings with `default` value

* reverted change to parameter

* Refactored code

* Added dataRepresentation() to Writable

* fixed issue with code
  • Loading branch information
Ibrahimhass authored Aug 30, 2023
1 parent f134171 commit 8206939
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 18 deletions.
15 changes: 13 additions & 2 deletions Sources/XcodeProj/Objects/Project/PBXProj.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,24 @@ extension PBXProj: Equatable {
// MARK: - Writable

extension PBXProj: Writable {
public func dataRepresentation(outputSettings: PBXOutputSettings) throws -> Data? {
let encoder = PBXProjEncoder(outputSettings: outputSettings)
return try encoder.encode(proj: self).data(using: .utf8)
}

public func dataRepresentation() throws -> Data? {
let encoder = PBXProjEncoder(outputSettings: PBXOutputSettings())
return try encoder.encode(proj: self).data(using: .utf8)
}

public func write(path: Path, override: Bool) throws {
try write(path: path, override: override, outputSettings: PBXOutputSettings())
}

public func write(path: Path, override: Bool, outputSettings: PBXOutputSettings) throws {
let encoder = PBXProjEncoder(outputSettings: outputSettings)
let output = try encoder.encode(proj: self)
guard let output = try dataRepresentation(outputSettings: outputSettings) else {
return
}
if override, path.exists {
try path.delete()
}
Expand Down
16 changes: 13 additions & 3 deletions Sources/XcodeProj/Project/WorkspaceSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,24 @@ public class WorkspaceSettings: Codable, Equatable, Writable {
/// - Parameter override: True if the content should be overriden if it already exists.
/// - Throws: writing error if something goes wrong.
public func write(path: Path, override: Bool) throws {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(self)
guard let data = try dataRepresentation() else {
return
}
if override, path.exists {
try path.delete()
}
try path.write(data)
}

/// Get the workspace settings.
///
/// - Throws: reading error if something goes wrong.
public func dataRepresentation() throws -> Data? {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
let data = try encoder.encode(self)
return data
}
}

extension WorkspaceSettings {
Expand Down
17 changes: 13 additions & 4 deletions Sources/XcodeProj/Project/XCBreakpointList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ public final class XCBreakpointList: Equatable, Writable {
// MARK: - Writable

public func write(path: Path, override: Bool) throws {
let document = getAEXMLDocument()

if override, path.exists {
try path.delete()
}
try path.write(document.xmlXcodeFormat)
}

private func getAEXMLDocument() -> AEXMLDocument {
let document = AEXMLDocument()
var schemeAttributes: [String: String] = [:]
schemeAttributes["type"] = type
Expand All @@ -412,11 +421,11 @@ public final class XCBreakpointList: Equatable, Writable {
let breakpoints = AEXMLElement(name: "Breakpoints", value: nil, attributes: [:])
self.breakpoints.map { $0.xmlElement() }.forEach { breakpoints.addChild($0) }
bucket.addChild(breakpoints)
return document
}

if override, path.exists {
try path.delete()
}
try path.write(document.xmlXcodeFormat)
public func dataRepresentation() throws -> Data? {
getAEXMLDocument().xmlXcodeFormat.data(using: .utf8)
}

// MARK: - Equatable
Expand Down
7 changes: 7 additions & 0 deletions Sources/XcodeProj/Protocols/Writable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ public protocol Writable {
/// - Parameter override: True if the content should be overridden if it already exists.
/// - Throws: writing error if something goes wrong.
func write(pathString: String, override: Bool) throws

/// Gets the data representation of the object that conforms to the protocol.
///
/// - Throws: error if encoding to Data fails.
func dataRepresentation() throws -> Data?
}

extension Writable {
public func write(pathString: String, override: Bool) throws {
let path = Path(pathString)
try write(path: path, override: override)
}

public func dataRepresentation() throws -> Data? { nil }
}
17 changes: 13 additions & 4 deletions Sources/XcodeProj/Scheme/XCScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public final class XCScheme: Writable, Equatable {
// MARK: - Writable

public func write(path: Path, override: Bool) throws {
let document = getAEXMLDocument()
if override, path.exists {
try path.delete()
}
try path.write(document.xmlXcodeFormat)
}

public func dataRepresentation() throws -> Data? {
getAEXMLDocument().xmlXcodeFormat.data(using: .utf8)
}

private func getAEXMLDocument() -> AEXMLDocument {
let document = AEXMLDocument()
var schemeAttributes: [String: String] = [:]
schemeAttributes["LastUpgradeVersion"] = lastUpgradeVersion
Expand All @@ -109,10 +121,7 @@ public final class XCScheme: Writable, Equatable {
if let wasCreatedForAppExtension = wasCreatedForAppExtension {
scheme.attributes["wasCreatedForAppExtension"] = wasCreatedForAppExtension.xmlString
}
if override, path.exists {
try path.delete()
}
try path.write(document.xmlXcodeFormat)
return document
}

// MARK: - Equatable
Expand Down
14 changes: 13 additions & 1 deletion Sources/XcodeProj/Scheme/XCSchemeManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,21 @@ public struct XCSchemeManagement: Codable, Equatable, Writable {
try path.delete()
}

let encoder = getEncoder()
try encoder.encode(self).write(to: path.url)
}

/// Gets the data representation of the property list representation of the object.
///
/// - Throws: Error if encoding fails.
public func dataRepresentation() throws -> Data? {
return try getEncoder().encode(self)
}

private func getEncoder() -> PropertyListEncoder {
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
try encoder.encode(self).write(to: path.url)
return encoder
}

// MARK: - Codable
Expand Down
17 changes: 13 additions & 4 deletions Sources/XcodeProj/Utils/XCConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,24 @@ extension XCConfig {

extension XCConfig: Writable {
public func write(path: Path, override: Bool) throws {
var content = ""
content.append(writeIncludes())
content.append("\n")
content.append(writeBuildSettings())
let content = getContent()
if override, path.exists {
try path.delete()
}
try path.write(content)
}

public func dataRepresentation() throws -> Data? {
getContent().data(using: .utf8)
}

private func getContent() -> String {
var content = ""
content.append(writeIncludes())
content.append("\n")
content.append(writeBuildSettings())
return content
}

private func writeIncludes() -> String {
var content = ""
Expand Down
4 changes: 4 additions & 0 deletions Sources/XcodeProj/Workspace/XCWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public final class XCWorkspace: Writable, Equatable {
try dataPath.mkpath()
try data.write(path: dataPath)
}

public func dataRepresentation() throws -> Data? {
self.data.rawContents().data(using: .utf8)
}

// MARK: - Equatable

Expand Down
4 changes: 4 additions & 0 deletions Sources/XcodeProj/Workspace/XCWorkspaceData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ extension XCWorkspaceData: Writable {
}
try path.write(rawXml)
}

public func dataRepresentation() throws -> Data? {
rawContents().data(using: .utf8)
}
}

// MARK: - XCWorkspaceDataElement AEXMLElement decoding and encoding
Expand Down

0 comments on commit 8206939

Please sign in to comment.