Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Formatting Methods Requiring Raw Input #233

Merged
merged 10 commits into from
Jan 8, 2024
89 changes: 58 additions & 31 deletions Sources/XcbeautifyLib/CaptureGroups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,18 +513,21 @@ struct RestartingTestCaptureGroup: CaptureGroup {
static let outputType: OutputType = .test

/// Regular expression captured groups:
/// $1 = test suite + test case
/// $2 = test suite
/// $3 = test case
static let regex = Regex(pattern: #"^Restarting after unexpected exit, crash, or test timeout in (-\[(\w+)\s(\w+)\]|(\w+)\.(\w+)\(\));"#)
/// $1 = whole message
/// $2 = test suite + test case
/// $3 = test suite
/// $4 = test case
static let regex = Regex(pattern: #"^(Restarting after unexpected exit, crash, or test timeout in (-\[(\w+)\s(\w+)\]|(\w+)\.(\w+)\(\));.*)"#)

let wholeMessage: String
let testSuiteAndTestCase: String
let testSuite: String
let testCase: String

init?(groups: [String]) {
assert(groups.count >= 3)
guard let testSuiteAndTestCase = groups[safe: 0], let testSuite = groups[safe: 1], let testCase = groups[safe: 2] else { return nil }
assert(groups.count >= 4)
guard let wholeMessage = groups[safe: 0], let testSuiteAndTestCase = groups[safe: 1], let testSuite = groups[safe: 2], let testCase = groups[safe: 3] else { return nil }
self.wholeMessage = wholeMessage
self.testSuiteAndTestCase = testSuiteAndTestCase
self.testSuite = testSuite
self.testCase = testCase
Expand Down Expand Up @@ -805,14 +808,17 @@ struct ParallelTestingStartedCaptureGroup: CaptureGroup {
static let outputType: OutputType = .test

/// Regular expression captured groups:
/// $1 = device
static let regex = Regex(pattern: #"^Testing\s+started\s+on\s+'(.*)'"#)
/// $1 = whole message
/// $2 = device
static let regex = Regex(pattern: #"^(Testing\s+started\s+on\s+'(.*)'.*)$"#)

let wholeMessage: String
let device: String

init?(groups: [String]) {
assert(groups.count >= 1)
guard let device = groups[safe: 0] else { return nil }
assert(groups.count >= 2)
guard let wholeMessage = groups[safe: 0], let device = groups[safe: 1] else { return nil }
self.wholeMessage = wholeMessage
self.device = device
}
}
Expand All @@ -821,14 +827,17 @@ struct ParallelTestingPassedCaptureGroup: CaptureGroup {
static let outputType: OutputType = .test

/// Regular expression captured groups:
/// $1 = device
static let regex = Regex(pattern: #"^Testing\s+passed\s+on\s+'(.*)'"#)
/// $1 = whole message
/// $2 = device
static let regex = Regex(pattern: #"^(Testing\s+passed\s+on\s+'(.*)'.*)$"#)

let wholeMessage: String
let device: String

init?(groups: [String]) {
assert(groups.count >= 1)
guard let device = groups[safe: 0] else { return nil }
assert(groups.count >= 2)
guard let wholeMessage = groups[safe: 0], let device = groups[safe: 1] else { return nil }
self.wholeMessage = wholeMessage
self.device = device
}
}
Expand All @@ -837,14 +846,17 @@ struct ParallelTestingFailedCaptureGroup: CaptureGroup {
static let outputType: OutputType = .nonContextualError

/// Regular expression captured groups:
/// $1 = device
static let regex = Regex(pattern: #"^Testing\s+failed\s+on\s+'(.*)'"#)
/// $1 = whole error
/// $2 = device
static let regex = Regex(pattern: #"^(Testing\s+failed\s+on\s+'(.*)'.*)$"#)

let wholeError: String
let device: String

init?(groups: [String]) {
assert(groups.count >= 1)
guard let device = groups[safe: 0] else { return nil }
assert(groups.count >= 2)
guard let wholeError = groups[safe: 0], let device = groups[safe: 1] else { return nil }
self.wholeError = wholeError
self.device = device
}
}
Expand Down Expand Up @@ -939,15 +951,24 @@ struct PreprocessCaptureGroup: CaptureGroup {
static let outputType: OutputType = .task

/// Regular expression captured groups:
/// $1 = file
static let regex = Regex(pattern: #"^Preprocess\s(?:(?:\ |[^ ])*)\s((?:\ |[^ ])*)$"#)
/// $1 = file path
/// $2 = file
/// $3 = target
/// $4 = project
static let regex = Regex(pattern: #"^Preprocess\s(.*\/(.*\.(?:m|mm|cc|cpp|c|cxx)))\s.*\(in target '(.*)' from project '(.*)'\)"#)

let filePath: String
let file: String
let target: String
let project: String

init?(groups: [String]) {
assert(groups.count >= 1)
guard let file = groups[safe: 0] else { return nil }
assert(groups.count >= 4)
guard let filePath = groups[safe: 0], let file = groups[safe: 1], let target = groups[safe: 2], let project = groups[safe: 3] else { return nil }
self.filePath = filePath
self.file = file
self.target = target
self.project = project
}
}

Expand Down Expand Up @@ -1485,14 +1506,17 @@ struct SymbolReferencedFromCaptureGroup: CaptureGroup {
static let outputType: OutputType = .error

/// Regular expression captured groups:
/// $1 = reference
static let regex = Regex(pattern: #"\s+\"(.*)\", referenced from:$"#)
/// $1 = wholeError
/// $2 = reference
static let regex = Regex(pattern: #"(\s+\"(.*)\", referenced from:)$"#)

let wholeError: String
let reference: String

init?(groups: [String]) {
assert(groups.count >= 1)
guard let reference = groups[safe: 0] else { return nil }
assert(groups.count >= 2)
guard let wholeError = groups[safe: 0], let reference = groups[safe: 1] else { return nil }
self.wholeError = wholeError
self.reference = reference
}
}
Expand All @@ -1516,16 +1540,19 @@ struct ModuleIncludesErrorCaptureGroup: ErrorCaptureGroup {
struct UndefinedSymbolLocationCaptureGroup: CaptureGroup {
static let outputType: OutputType = .warning
/// Regular expression captured groups:
/// $1 = target
/// $2 = filename
static let regex = Regex(pattern: #".+ in (.+)\((.+)\.o\)$"#)
/// $1 = whole warning
/// $2 = target
/// $3 = filename
static let regex = Regex(pattern: #"(.+ in (.+)\((.+)\.o\))$"#)

let wholeWarning: String
let target: String
let filename: String

init?(groups: [String]) {
assert(groups.count >= 2)
guard let target = groups[safe: 0], let filename = groups[safe: 1] else { return nil }
assert(groups.count >= 3)
guard let wholeWarning = groups[safe: 0], let target = groups[safe: 1], let filename = groups[safe: 2] else { return nil }
self.wholeWarning = wholeWarning
self.target = target
self.filename = filename
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/XcbeautifyLib/Renderers/GitHubActionsRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ struct GitHubActionsRenderer: OutputRendering {
)
}

func formatCompleteError(line: String) -> String {
func formatSymbolReferencedFrom(group: SymbolReferencedFromCaptureGroup) -> String {
outputGitHubActionsLog(
annotationType: .error,
message: line
message: group.wholeError
)
}

func formatCompleteWarning(line: String) -> String {
func formatUndefinedSymbolLocation(group: UndefinedSymbolLocationCaptureGroup) -> String {
outputGitHubActionsLog(
annotationType: .warning,
message: line
message: group.wholeWarning
)
}

Expand Down Expand Up @@ -160,15 +160,15 @@ struct GitHubActionsRenderer: OutputRendering {
return Format.indent + testCase + " on '\(device)' (\(time) seconds)"
}

func formatParallelTestingFailed(line: String, group: ParallelTestingFailedCaptureGroup) -> String {
func formatParallelTestingFailed(group: ParallelTestingFailedCaptureGroup) -> String {
outputGitHubActionsLog(
annotationType: .error,
message: line
message: group.wholeError
)
}

func formatRestartingTest(line: String, group: RestartingTestCaptureGroup) -> String {
let message = Format.indent + line
func formatRestartingTest(group: RestartingTestCaptureGroup) -> String {
let message = Format.indent + group.wholeMessage
return outputGitHubActionsLog(
annotationType: .error,
message: message
Expand Down
71 changes: 33 additions & 38 deletions Sources/XcbeautifyLib/Renderers/OutputRendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ protocol OutputRendering {

func format(testSummary: TestSummary) -> String

func format(line: String, command: String, pattern: String, arguments: String) -> String?
func formatAnalyze(group: AnalyzeCaptureGroup) -> String
func formatCheckDependencies() -> String
func formatCleanRemove(group: CleanRemoveCaptureGroup) -> String
func formatCodeSign(group: CodesignCaptureGroup) -> String
func formatCodeSignFramework(group: CodesignFrameworkCaptureGroup) -> String
func formatCompile(group: CompileFileCaptureGroup) -> String
func formatCompileCommand(group: CompileCommandCaptureGroup) -> String?
func formatCompileError(group: CompileErrorCaptureGroup, additionalLines: @escaping () -> (String?)) -> String
func formatCompileWarning(group: CompileWarningCaptureGroup, additionalLines: @escaping () -> (String?)) -> String
func formatCompleteError(line: String) -> String
func formatCompleteWarning(line: String) -> String
func formatCopy(group: CopyCaptureGroup) -> String
func formatCoverageReport(group: GeneratedCoverageReportCaptureGroup) -> String
func formatCursor(group: CursorCaptureGroup) -> String?
Expand Down Expand Up @@ -45,17 +43,19 @@ protocol OutputRendering {
func formatParallelTestCaseAppKitPassed(group: ParallelTestCaseAppKitPassedCaptureGroup) -> String
func formatParallelTestCaseFailed(group: ParallelTestCaseFailedCaptureGroup) -> String
func formatParallelTestCasePassed(group: ParallelTestCasePassedCaptureGroup) -> String
func formatParallelTestingFailed(line: String, group: ParallelTestingFailedCaptureGroup) -> String
func formatParallelTestingPassed(line: String, group: ParallelTestingPassedCaptureGroup) -> String
func formatParallelTestingStarted(line: String, group: ParallelTestingStartedCaptureGroup) -> String
func formatParallelTestingFailed(group: ParallelTestingFailedCaptureGroup) -> String
func formatParallelTestingPassed(group: ParallelTestingPassedCaptureGroup) -> String
func formatParallelTestingStarted(group: ParallelTestingStartedCaptureGroup) -> String
func formatParallelTestSuiteStarted(group: ParallelTestSuiteStartedCaptureGroup) -> String
func formatPhaseScriptExecution(group: PhaseScriptExecutionCaptureGroup) -> String
func formatPhaseSuccess(group: PhaseSuccessCaptureGroup) -> String
func formatPreprocess(group: PreprocessCaptureGroup) -> String
func formatProcessInfoPlist(group: ProcessInfoPlistCaptureGroup) -> String
func formatProcessPch(group: ProcessPchCaptureGroup) -> String
func formatProcessPchCommand(group: ProcessPchCommandCaptureGroup) -> String
func formatRestartingTest(line: String, group: RestartingTestCaptureGroup) -> String
func formatRestartingTest(group: RestartingTestCaptureGroup) -> String
func formatShellCommand(group: ShellCommandCaptureGroup) -> String?
func formatSymbolReferencedFrom(group: SymbolReferencedFromCaptureGroup) -> String
func formatTargetCommand(command: String, group: TargetCaptureGroup) -> String
func formatTestCaseMeasured(group: TestCaseMeasuredCaptureGroup) -> String
func formatTestCasePassed(group: TestCasePassedCaptureGroup) -> String
Expand All @@ -69,6 +69,7 @@ protocol OutputRendering {
func formatTIFFUtil(group: TIFFutilCaptureGroup) -> String?
func formatTouch(group: TouchCaptureGroup) -> String
func formatUIFailingTest(group: UIFailingTestCaptureGroup) -> String
func formatUndefinedSymbolLocation(group: UndefinedSymbolLocationCaptureGroup) -> String
func formatWarning(group: GenericWarningCaptureGroup) -> String
func formatWillNotBeCodesignWarning(group: WillNotBeCodeSignedCaptureGroup) -> String
func formatWriteAuxiliaryFiles(group: WriteAuxiliaryFilesCaptureGroup) -> String?
Expand Down Expand Up @@ -98,7 +99,7 @@ extension OutputRendering {
case let group as BuildTargetCaptureGroup:
return formatTargetCommand(command: "Build", group: group)
case is CheckDependenciesCaptureGroup:
return format(line: line, command: "Check Dependencies", pattern: CheckDependenciesCaptureGroup.pattern, arguments: "")
return formatCheckDependencies()
case let group as CheckDependenciesErrorsCaptureGroup:
return formatError(group: group)
case let group as ClangErrorCaptureGroup:
Expand Down Expand Up @@ -192,11 +193,11 @@ extension OutputRendering {
case let group as ParallelTestCasePassedCaptureGroup:
return formatParallelTestCasePassed(group: group)
case let group as ParallelTestingFailedCaptureGroup:
return formatParallelTestingFailed(line: line, group: group)
return formatParallelTestingFailed(group: group)
case let group as ParallelTestingPassedCaptureGroup:
return formatParallelTestingPassed(line: line, group: group)
return formatParallelTestingPassed(group: group)
case let group as ParallelTestingStartedCaptureGroup:
return formatParallelTestingStarted(line: line, group: group)
return formatParallelTestingStarted(group: group)
case let group as ParallelTestSuiteStartedCaptureGroup:
return formatParallelTestSuiteStarted(group: group)
case let group as PbxcpCaptureGroup:
Expand All @@ -207,8 +208,8 @@ extension OutputRendering {
return formatPhaseSuccess(group: group)
case let group as PodsErrorCaptureGroup:
return formatError(group: group)
case is PreprocessCaptureGroup:
return format(line: line, command: "Preprocessing", pattern: pattern, arguments: "$1")
case let group as PreprocessCaptureGroup:
return formatPreprocess(group: group)
case let group as ProcessInfoPlistCaptureGroup:
return formatProcessInfoPlist(group: group)
case let group as ProcessPchCaptureGroup:
Expand All @@ -218,11 +219,11 @@ extension OutputRendering {
case let group as ProvisioningProfileRequiredCaptureGroup:
return formatError(group: group)
case let group as RestartingTestCaptureGroup:
return formatRestartingTest(line: line, group: group)
return formatRestartingTest(group: group)
case let group as ShellCommandCaptureGroup:
return formatShellCommand(group: group)
case is SymbolReferencedFromCaptureGroup:
return formatCompleteError(line: line)
case let group as SymbolReferencedFromCaptureGroup:
return formatSymbolReferencedFrom(group: group)
case let group as TestCaseMeasuredCaptureGroup:
return formatTestCaseMeasured(group: group)
case let group as TestCasePassedCaptureGroup:
Expand All @@ -247,8 +248,8 @@ extension OutputRendering {
return formatTouch(group: group)
case let group as UIFailingTestCaptureGroup:
return formatUIFailingTest(group: group)
case is UndefinedSymbolLocationCaptureGroup:
return formatCompleteWarning(line: line)
case let group as UndefinedSymbolLocationCaptureGroup:
return formatUndefinedSymbolLocation(group: group)
case let group as WillNotBeCodeSignedCaptureGroup:
return formatWillNotBeCodesignWarning(group: group)
case let group as WriteAuxiliaryFilesCaptureGroup:
Expand All @@ -265,28 +266,16 @@ extension OutputRendering {
}

extension OutputRendering {
func format(line: String, command: String, pattern: String, arguments: String) -> String? {
let template = command.style.Bold + " " + arguments

guard let formatted = try? NSRegularExpression(pattern: pattern)
.stringByReplacingMatches(
in: line,
range: NSRange(location: 0, length: line.count),
withTemplate: template
)
else {
return nil
}

return formatted
}

func formatAnalyze(group: AnalyzeCaptureGroup) -> String {
let filename = group.filename
let target = group.target
return colored ? "[\(target.f.Cyan)] \("Analyzing".s.Bold) \(filename)" : "[\(target)] Analyzing \(filename)"
}

func formatCheckDependencies() -> String {
colored ? "Check Dependencies".style.Bold : "Check Dependencies"
}

func formatCleanRemove(group: CleanRemoveCaptureGroup) -> String {
let directory = group.directory
return colored ? "\("Cleaning".s.Bold) \(directory)" : "Cleaning \(directory)"
Expand Down Expand Up @@ -401,8 +390,8 @@ extension OutputRendering {
return "Updating " + source
}

func formatParallelTestingPassed(line: String, group: ParallelTestingPassedCaptureGroup) -> String {
colored ? line.s.Bold.f.Green : line
func formatParallelTestingPassed(group: ParallelTestingPassedCaptureGroup) -> String {
colored ? group.wholeMessage.s.Bold.f.Green : group.wholeMessage
}

func formatParallelTestSuiteStarted(group: ParallelTestSuiteStartedCaptureGroup) -> String {
Expand All @@ -412,8 +401,8 @@ extension OutputRendering {
return colored ? heading.s.Bold.f.Cyan : heading
}

func formatParallelTestingStarted(line: String, group: ParallelTestingStartedCaptureGroup) -> String {
colored ? line.s.Bold.f.Cyan : line
func formatParallelTestingStarted(group: ParallelTestingStartedCaptureGroup) -> String {
colored ? group.wholeMessage.s.Bold.f.Cyan : group.wholeMessage
}

func formatPhaseScriptExecution(group: PhaseScriptExecutionCaptureGroup) -> String {
Expand All @@ -429,6 +418,12 @@ extension OutputRendering {
return colored ? "\(phase) Succeeded".s.Bold.f.Green : "\(phase) Succeeded"
}

func formatPreprocess(group: PreprocessCaptureGroup) -> String {
let target = group.target
let file = group.file
return colored ? "[\(target.f.Cyan)] \("Preprocess".s.Bold) \(file)" : "[\(target)] Preprocess \(file)"
}

func formatProcessInfoPlist(group: ProcessInfoPlistCaptureGroup) -> String {
let plist = group.filename

Expand Down
Loading