Skip to content

Commit c695fa7

Browse files
committed
Refactor Parser to return CaptureGroup
1 parent 18dcfb3 commit c695fa7

File tree

9 files changed

+50
-49
lines changed

9 files changed

+50
-49
lines changed

Sources/XcbeautifyLib/CaptureGroups.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Foundation
22

3-
protocol CaptureGroup {
3+
package protocol CaptureGroup {
44
static var outputType: OutputType { get }
55
static var regex: XcbeautifyLib.Regex { get }
66
init?(groups: [String])
77
}
88

99
extension CaptureGroup {
10+
var outputType: OutputType { Self.outputType }
11+
1012
static var pattern: String { regex.pattern }
1113
var pattern: String { Self.regex.pattern }
1214
}

Sources/XcbeautifyLib/Formatter.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import Foundation
22

3-
class Formatter {
3+
package class Formatter {
44

55
private let colored: Bool
66
private let renderer: OutputRendering
77
private let additionalLines: () -> String?
8-
private(set) var preserveUnbeautifiedLines = false
98

10-
init(
9+
package init(
1110
colored: Bool = true,
1211
renderer: Renderer,
13-
preserveUnbeautifiedLines: Bool = false,
1412
additionalLines: @escaping () -> (String?)
1513
) {
1614
self.colored = colored
@@ -22,11 +20,10 @@ class Formatter {
2220
self.renderer = GitHubActionsRenderer()
2321
}
2422

25-
self.preserveUnbeautifiedLines = preserveUnbeautifiedLines
2623
self.additionalLines = additionalLines
2724
}
2825

29-
func format(captureGroup: CaptureGroup) -> String? {
26+
package func format(captureGroup: CaptureGroup) -> String? {
3027
return renderer.beautify(
3128
group: captureGroup,
3229
additionalLines: additionalLines

Sources/XcbeautifyLib/Parser.swift

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import Foundation
22

33
package class Parser {
4-
private let formatter: XcbeautifyLib.Formatter
5-
64
package private(set) var outputType = OutputType.undefined
75

86
private lazy var captureGroupTypes: [CaptureGroup.Type] = [
@@ -94,28 +92,16 @@ package class Parser {
9492

9593
// MARK: - Init
9694

97-
package init(
98-
colored: Bool = true,
99-
renderer: Renderer,
100-
preserveUnbeautifiedLines: Bool = false,
101-
additionalLines: @escaping () -> (String?)
102-
) {
103-
self.formatter = Formatter(
104-
colored: colored,
105-
renderer: renderer,
106-
preserveUnbeautifiedLines: preserveUnbeautifiedLines,
107-
additionalLines: additionalLines
108-
)
109-
}
95+
public init() { }
11096

111-
package func parse(line: String) -> String? {
97+
public func parse(line: String) -> CaptureGroup? {
11298
// Find first parser that can parse specified string
11399
guard let idx = captureGroupTypes.firstIndex(where: { $0.regex.match(string: line) }) else {
114100
// Some uncommon cases, which have additional logic and don't follow default flow
115101

116102
// Nothing found?
117103
outputType = OutputType.undefined
118-
return formatter.preserveUnbeautifiedLines ? line : nil
104+
return nil
119105
}
120106

121107
guard let captureGroupType = captureGroupTypes[safe: idx] else {
@@ -134,7 +120,7 @@ package class Parser {
134120
// Move found parser to the top, so next time it will be checked first
135121
captureGroupTypes.insert(captureGroupTypes.remove(at: idx), at: 0)
136122

137-
return formatter.format(captureGroup: captureGroup)
123+
return captureGroup
138124
}
139125
}
140126

Sources/XcbeautifyLib/Regex.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
class Regex {
3+
public final class Regex {
44
let pattern: String
55

66
private lazy var regex: NSRegularExpression? = try? NSRegularExpression(pattern: "^" + pattern, options: [.caseInsensitive])

Sources/XcbeautifyLib/XCBeautifier.swift

+16-3
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@ import Foundation
22

33
public struct XCBeautifier {
44
private let parser: Parser
5+
private let formatter: Formatter
6+
private let preserveUnbeautifiedLines: Bool
57

68
public init(
79
colored: Bool,
810
renderer: Renderer,
911
preserveUnbeautifiedLines: Bool,
1012
additionalLines: @escaping () -> String?
1113
) {
12-
parser = Parser(
14+
parser = Parser()
15+
16+
formatter = Formatter(
1317
colored: colored,
1418
renderer: renderer,
15-
preserveUnbeautifiedLines: preserveUnbeautifiedLines,
1619
additionalLines: additionalLines
1720
)
21+
22+
self.preserveUnbeautifiedLines = preserveUnbeautifiedLines
1823
}
1924

2025
public func format(line: String) -> String? {
21-
parser.parse(line: line)
26+
guard let captureGroup = parser.parse(line: line) else {
27+
if preserveUnbeautifiedLines {
28+
return line
29+
} else {
30+
return nil
31+
}
32+
}
33+
34+
return formatter.format(captureGroup: captureGroup)
2235
}
2336
}

Sources/xcbeautify/Xcbeautify.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,23 @@ struct Xcbeautify: ParsableCommand {
4949
return line
5050
}
5151

52-
let parser = Parser(
52+
let parser = Parser()
53+
54+
let formatter = XcbeautifyLib.Formatter(
5355
colored: !disableColoredOutput,
5456
renderer: renderer,
55-
preserveUnbeautifiedLines: preserveUnbeautified,
5657
additionalLines: { readLine() }
5758
)
5859

5960
while let line = readLine() {
60-
guard let formatted = parser.parse(line: line) else { continue }
61+
guard let captureGroup = parser.parse(line: line) else {
62+
if preserveUnbeautified {
63+
output.write(.undefined, line)
64+
}
65+
66+
continue
67+
}
68+
guard let formatted = formatter.format(captureGroup: captureGroup) else { continue }
6169
output.write(parser.outputType, formatted)
6270
}
6371

Tests/XcbeautifyLibTests/ParsingTests/ParsingTests.swift

+2-13
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@ final class ParsingTests: XCTestCase {
88
var buildLog: [String] = try String(contentsOf: url)
99
.components(separatedBy: .newlines)
1010

11-
let parser = Parser(
12-
colored: false,
13-
renderer: .terminal,
14-
preserveUnbeautifiedLines: false,
15-
additionalLines: {
16-
guard !buildLog.isEmpty else {
17-
XCTFail("The build log should never be empty when fetching additional lines.")
18-
return nil
19-
}
20-
return buildLog.removeFirst()
21-
}
22-
)
11+
let parser = Parser()
2312

2413
var uncapturedOutput = 0
2514

@@ -36,6 +25,6 @@ final class ParsingTests: XCTestCase {
3625
// It uses `XCTAssertEqual` instead of `XCTAssertLessThanOrEqual` as a reminder.
3726
// Update this magic number whenever `uncapturedOutput` is less than the current magic number.
3827
// There's a regression whenever `uncapturedOutput` is greater than the current magic number.
39-
XCTAssertEqual(uncapturedOutput, 2218)
28+
XCTAssertEqual(uncapturedOutput, 833)
4029
}
4130
}

Tests/XcbeautifyLibTests/RendererTests/GitHubActionsRendererTests.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import XCTest
33

44
final class GitHubActionsRendererTests: XCTestCase {
55
var parser: Parser!
6+
var formatter: XcbeautifyLib.Formatter!
67

78
override func setUpWithError() throws {
89
try super.setUpWithError()
9-
parser = Parser(colored: false, renderer: .gitHubActions, additionalLines: { nil })
10+
parser = Parser()
11+
formatter = Formatter(colored: false, renderer: .gitHubActions, additionalLines: { nil })
1012
}
1113

1214
private func logFormatted(_ string: String) -> String? {
13-
parser.parse(line: string)
15+
guard let captureGroup = parser.parse(line: string) else { return nil }
16+
return formatter.format(captureGroup: captureGroup)
1417
}
1518

1619
func testAggregateTarget() {

Tests/XcbeautifyLibTests/RendererTests/TerminalRendererTests.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ import XCTest
33

44
final class TerminalRendererTests: XCTestCase {
55
var parser: Parser!
6+
var formatter: XcbeautifyLib.Formatter!
67

78
override func setUpWithError() throws {
89
try super.setUpWithError()
9-
parser = Parser(colored: false, renderer: .terminal, additionalLines: { nil })
10+
parser = Parser()
11+
formatter = Formatter(colored: false, renderer: .terminal, additionalLines: { nil })
1012
}
1113

1214
private func noColoredFormatted(_ string: String) -> String? {
13-
parser.parse(line: string)
15+
guard let captureGroup = parser.parse(line: string) else { return nil }
16+
return formatter.format(captureGroup: captureGroup)
1417
}
1518

1619
func testAggregateTarget() {

0 commit comments

Comments
 (0)