cpisciotta / xcbeautify

A little beautifier tool for xcodebuild
MIT License
947 stars 67 forks source link

Allow usage in custom Swift Package #304

Closed raptorxcz closed 1 month ago

raptorxcz commented 1 month ago

Hello, great work!

I wanted to use this Swift package in my custom Swift Package, but I ran into issue with Parser access level. The current access level of Parser is package. If I wanted to use Parser in my custom Swift package I had to change it to public.

Parser: https://github.com/cpisciotta/xcbeautify/blob/main/Sources/XcbeautifyLib/Parser.swift

Here is change: https://github.com/raptorxcz/xcbeautify/commit/1bd38b6a64bb447c264fbdfc1b453bf8065b8ec6

I can make a pull request If you allow this change?

My usage is something like this:

        let printStream = WriteStream.stdout
        let parser = XcbeautifyLib.Parser(
            colored: true,
            renderer: .terminal,
            preserveUnbeautifiedLines: true,
            additionalLines: { nil }
        )
        let outputStream = makeBeautifyStream(outputStream: printStream, parser: parser)

        let command = arguments.joined(separator: " ")
        let task = Task(executable: "/bin/bash", arguments: ["-c", command], stdout: outputStream)
        ...
    }

    private func makeBeautifyStream(outputStream: WritableStream, parser: XcbeautifyLib.Parser) -> ProcessingStream {
        return LineStream { line in
            if let formatted = parser.parse(line: line) {
                outputStream.write(formatted)
            } else {
                outputStream.write(line)
            }
        }
    }
cpisciotta commented 1 month ago

Hey @raptorxcz! Thanks for reaching out and for using xcbeautify. In version 2.0.0, I internalized Parser due to numerous breaking and structural changes. As part of this rewrite, I introduced a new type, XCBeautifier. It's the type you should use in a Swift package. It more properly encapsulates many internal implementation details, specifically parsing and formatting.

Currently, there are some differences between it and Parser. For example, XCBeautifier returns an optional, formatted String if possible. Unlike Parser, XCBeautifier doesn't provide context around the recognized CaptureGroup instance or the OutputType. Based on your example, I believe XCBeautifier's current implementation offers what you're looking for. Let me know if you have any questions or suggestions!

raptorxcz commented 1 month ago

Awesome, that is exactly what I needed. Thank you.