tuist / XcodeProj

šŸ“ Read, update and write your Xcode projects
https://xcodeproj.tuist.io
MIT License
2.03k stars 309 forks source link

Get Data representation for `Writable` #787

Closed Ibrahimhass closed 1 year ago

Ibrahimhass commented 1 year ago

Resolves https://github.com/tuist/XcodeProj/issues/786

Short description šŸ“

Added an option to get the pbxproj's String representation. I want to modify a pbxproj in memory to add it to an in-memory archive later. This is needed because we are making a server-side Swift application. Since it is a server-side application, it would be great to get the modified pbxproj without any extra disk I/O operations. The current APIs can only achieve this by first writing the pbxproj to disk.

Solution šŸ“¦

Added a function inside the PBXProj extension, which can be used to get the String representation of the pbxproj file. Also refactored the code to use the above function for better code reuse.

Implementation šŸ‘©ā€šŸ’»šŸ‘Øā€šŸ’»

extension PBXProj: Writable {
    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 output = try encodeAsString(outputSettings: outputSettings)
        if override, path.exists {
            try path.delete()
        }
        try path.write(output)
    }

    public func encodeAsString(outputSettings: PBXOutputSettings) throws -> String {
        let encoder = PBXProjEncoder(outputSettings: outputSettings)
        return try encoder.encode(proj: self)
    }
}
netlify[bot] commented 1 year ago

Deploy Preview for xcodeproj ready!

Name Link
Latest commit f91f4247046bcf31a61ea4a859968025e61fb5ad
Latest deploy log https://app.netlify.com/sites/xcodeproj/deploys/64db7abee8cb840007561e78
Deploy Preview https://deploy-preview-787--xcodeproj.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Ibrahimhass commented 1 year ago

@kwridan Thanks for the suggestion :) Looking further, I found that the underlying type for Writable here is Data. So shall we have stringRepresentation() as an Optional requirement for Writable?

Something similar to this:

/// Protocol that defines how an entity can be written to disk
public protocol Writable {
    /// Writes the object that conforms the protocol.
    ///
    /// - Parameter path: The path to write to
    /// - Parameter override: True if the content should be overridden if it already exists.
    /// - Throws: writing error if something goes wrong.
    func write(path: Path, override: Bool) throws

    /// Writes the object that conforms the protocol.
    ///
    /// - Parameter pathString: The path string to write to
    /// - 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 string representation of the object  that conforms the protocol.
    ///
    /// - Throws: error if encoding to String goes wrong.
    func stringRepresentation() throws -> String
}

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

    public func stringRepresentation() throws -> String { "" }
}
kwridan commented 1 year ago

@kwridan Thanks for the suggestion :) Looking further, I found that the underlying type for Writable here is Data. So shall we have stringRepresentation() as an Optional requirement for Writable?

Thanks for looking further into this would making dataRepresentation() be a better fit then?

Ibrahimhass commented 1 year ago

@kwridan

Thanks for looking further into this. Would making dataRepresentation() be a better fit then?

Thanks for the suggestion. Yes, this could work, we modify Writable to something like:

/// Protocol that defines how an entity can be written to disk
public protocol Writable {
    /// Writes the object that conforms the protocol.
    ///
    /// - Parameter path: The path to write to
    /// - Parameter override: True if the content should be overridden if it already exists.
    /// - Throws: writing error if something goes wrong.
    func write(path: Path, override: Bool) throws

    /// Writes the object that conforms the protocol.
    ///
    /// - Parameter pathString: The path string to write to
    /// - 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 goes wrong.
    func dataRepresentation() throws -> Data
}

The representation can be used to get the string representation of any of the file if required.

Ibrahimhass commented 1 year ago

@kwridan Please review the changes here.

kwridan commented 1 year ago

@all-contributors add @Ibrahimhass for code

allcontributors[bot] commented 1 year ago

@kwridan

I've put up a pull request to add @Ibrahimhass! :tada: