Skip to content

Commit

Permalink
Added reporter support
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered committed Oct 3, 2023
1 parent 4a359f9 commit 2b39c62
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
42 changes: 40 additions & 2 deletions Source/swiftlint/Commands/Configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ extension SwiftLint {
let allowZeroLintableFiles = topLevelDirectories.isEmpty ? allowZeroLintableFiles() : false
let rulesIdentifiersToDisable = try await rulesToDisable(topLevelDirectories)
let analyzerRuleIdentifiers = analyzerRulesToEnable()
let reporterIdentifier = reporterIdentifier()
return try writeConfiguration(
topLevelDirectories,
allowZeroLintableFiles,
rulesIdentifiersToDisable,
analyzerRuleIdentifiers
analyzerRuleIdentifiers,
reporterIdentifier
)
}

Expand Down Expand Up @@ -178,11 +180,33 @@ extension SwiftLint {
}
}

private func reporterIdentifier() -> String {
var reporterIdentifier = XcodeReporter.identifier
if askUser("Do you want to use the default (\(reporterIdentifier) reporter?") {
return reporterIdentifier
}
reporterIdentifier = ""
while !isValidReporterIdentifier(reporterIdentifier) {
if reporterIdentifier.isNotEmpty {
print("'\(reporterIdentifier)' is not a valid reporter identifier")
}
print("Available reporters:")
print(Reporters.reportersTable())
reporterIdentifier = askUserWhichReporter()
}
return reporterIdentifier
}

private func isValidReporterIdentifier(_ reporterIdentifier: String) -> Bool {
reportersList.contains { $0.identifier == reporterIdentifier }
}

private func writeConfiguration(
_ topLevelDirectories: [String],
_ allowZeroLintableFiles: Bool,
_ ruleIdentifiersToDisable: [String],
_ analyzerRuleIdentifiers: [String]
_ analyzerRuleIdentifiers: [String],
_ reporterIdentifier: String
) throws -> Bool {
var configuration = configuration(forTopLevelDirectories: topLevelDirectories)
if allowZeroLintableFiles {
Expand All @@ -194,6 +218,7 @@ extension SwiftLint {
configuration += "analyzer_rules:\n"
analyzerRuleIdentifiers.forEach { configuration += " - \($0)\n" }
}
configuration += "reporter: \(reporterIdentifier)"
print("Proposed configuration\n")
print(configuration)
if askUser("Does that look good?") == false {
Expand Down Expand Up @@ -266,6 +291,19 @@ extension SwiftLint {
ExitHelper.successfullyExit()
}
}

private func askUserWhichReporter() -> String {
let message = "Which reporter would you like to use?"
let colorizedMessage = shouldColorizeOutput ? message.boldify : message
while true {
print(colorizedMessage, terminator: " ")
if let reporterIdentifier = readLine() {
if reporterIdentifier.isNotEmpty {
return reporterIdentifier
}
}
}
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Source/swiftlint/Commands/Reporters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ extension SwiftLint {
static let configuration = CommandConfiguration(abstract: "Display the list of reporters and their identifiers")

func run() throws {
print(TextTable(reporters: reportersList).render())
print(Self.reportersTable())
ExitHelper.successfullyExit()
}

static func reportersTable() -> String {
TextTable(reporters: reportersList).render()
}
}
}

Expand Down

0 comments on commit 2b39c62

Please sign in to comment.