diff --git a/Source/swiftlint/Commands/Configure.swift b/Source/swiftlint/Commands/Configure.swift index e69f6abb556..615c37491fd 100644 --- a/Source/swiftlint/Commands/Configure.swift +++ b/Source/swiftlint/Commands/Configure.swift @@ -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 ) } @@ -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 { @@ -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 { @@ -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 + } + } + } + } } } diff --git a/Source/swiftlint/Commands/Reporters.swift b/Source/swiftlint/Commands/Reporters.swift index cbce394df7e..c4e673a91a9 100644 --- a/Source/swiftlint/Commands/Reporters.swift +++ b/Source/swiftlint/Commands/Reporters.swift @@ -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() + } } }