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

Add --output option to lint and analyze commands #4148

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ accordingly._
[JP Simard](https://github.com/jpsim)
[#2495](https://github.com/realm/SwiftLint/issues/2495)

* Add `--output` option to lint and analyze commands to write to a file instead
of to stdout.
[JP Simard](https://github.com/jpsim)
[#4048](https://github.com/realm/SwiftLint/issues/4048)

#### Bug Fixes

* Migrate `empty_xctest_method` rule to SwiftSyntax fixing some false positives.
Expand All @@ -36,6 +41,10 @@ accordingly._
[Martin Hosna](https://github.com/mhosna)
[#4142](https://github.com/realm/SwiftLint/issues/4142)

* Consistently print error/info messages to stderr instead of stdout,
which wasn't being done for errors regarding remote configurations.
[JP Simard](https://github.com/jpsim)

## 0.49.0: Asynchronous Defuzzer

_Note: The default branch for the SwiftLint git repository will be renamed from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal extension Configuration.FileGraph.FilePath {

private mutating func handleMissingNetwork(urlString: String, cachedFilePath: String?) throws -> String {
if let cachedFilePath = cachedFilePath {
queuedPrint(
queuedPrintError(
"warning: No internet connectivity: Unable to load remote config from \"\(urlString)\". "
+ "Using cached version as a fallback."
)
Expand All @@ -141,11 +141,11 @@ internal extension Configuration.FileGraph.FilePath {
) throws -> String {
if let cachedFilePath = cachedFilePath {
if taskDone {
queuedPrint(
queuedPrintError(
jpsim marked this conversation as resolved.
Show resolved Hide resolved
"warning: Unable to load remote config from \"\(urlString)\". Using cached version as a fallback."
)
} else {
queuedPrint(
queuedPrintError(
"warning: Timeout (\(timeout) sec): Unable to load remote config from \"\(urlString)\". "
+ "Using cached version as a fallback."
)
Expand All @@ -170,7 +170,7 @@ internal extension Configuration.FileGraph.FilePath {

private mutating func handleFileWriteFailure(urlString: String, cachedFilePath: String?) throws -> String {
if let cachedFilePath = cachedFilePath {
queuedPrint("Unable to cache remote config from \"\(urlString)\". Using cached version as a fallback.")
queuedPrintError("Unable to cache remote config from \"\(urlString)\". Using cached version as a fallback.")
self = .existing(path: cachedFilePath)
return cachedFilePath
} else {
Expand Down
1 change: 1 addition & 0 deletions Source/swiftlint/Commands/Analyze.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extension SwiftLint {
benchmark: common.benchmark,
reporter: common.reporter,
quiet: quiet,
output: common.output,
cachePath: nil,
ignoreCache: true,
enableAllRules: false,
Expand Down
1 change: 1 addition & 0 deletions Source/swiftlint/Commands/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extension SwiftLint {
benchmark: common.benchmark,
reporter: common.reporter,
quiet: quiet,
output: common.output,
cachePath: cachePath,
ignoreCache: noCache,
enableAllRules: enableAllRules,
Expand Down
16 changes: 0 additions & 16 deletions Source/swiftlint/Extensions/Reporter+CommandLine.swift

This file was deleted.

2 changes: 2 additions & 0 deletions Source/swiftlint/Helpers/LintOrAnalyzeArguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ struct LintOrAnalyzeArguments: ParsableArguments {
var reporter: String?
@Flag(help: "Use the in-process version of SourceKit.")
var inProcessSourcekit = false
@Option(help: "The file where violations should be saved. Prints to stdout by default.")
var output: String?
}

// MARK: - Common Argument Help
Expand Down
49 changes: 43 additions & 6 deletions Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct LintOrAnalyzeCommand {
}
}
linter.file.invalidateCache()
builder.reporter.report(violations: currentViolations, realtimeCondition: true)
builder.report(violations: currentViolations, realtimeCondition: true)
}
}

Expand All @@ -89,9 +89,9 @@ struct LintOrAnalyzeCommand {
builder.violations.append(
createThresholdViolation(threshold: configuration.warningThreshold!)
)
builder.reporter.report(violations: [builder.violations.last!], realtimeCondition: true)
builder.report(violations: [builder.violations.last!], realtimeCondition: true)
}
builder.reporter.report(violations: builder.violations, realtimeCondition: false)
builder.report(violations: builder.violations, realtimeCondition: false)
let numberOfSeriousViolations = builder.violations.filter({ $0.severity == .error }).count
if !options.quiet {
printStatus(violations: builder.violations, files: files, serious: numberOfSeriousViolations,
Expand Down Expand Up @@ -183,8 +183,8 @@ struct LintOrAnalyzeCommand {

let corrections = linter.correct(using: storage)
if !corrections.isEmpty && !options.quiet && !options.useSTDIN {
let correctionLogs = corrections.map({ $0.consoleDescription })
queuedPrint(correctionLogs.joined(separator: "\n"))
let correctionLogs = corrections.map(\.consoleDescription)
options.writeToOutput(correctionLogs.joined(separator: "\n"))
}
}

Expand All @@ -210,6 +210,7 @@ struct LintOrAnalyzeOptions {
let benchmark: Bool
let reporter: String?
let quiet: Bool
let output: String?
let cachePath: String?
let ignoreCache: Bool
let enableAllRules: Bool
Expand Down Expand Up @@ -243,8 +244,44 @@ private class LintOrAnalyzeResultBuilder {
Configuration(options: options)
}
configuration = config
reporter = reporterFrom(optionsReporter: options.reporter, configuration: config)
reporter = reporterFrom(identifier: options.reporter ?? config.reporter)
cache = options.ignoreCache ? nil : LinterCache(configuration: config)
self.options = options

if let outFile = options.output {
do {
try Data().write(to: URL(fileURLWithPath: outFile))
} catch {
queuedPrintError("Could not write to file at path \(outFile)")
}
}
}

func report(violations: [StyleViolation], realtimeCondition: Bool) {
if reporter.isRealtime == realtimeCondition {
let report = reporter.generateReport(violations)
if !report.isEmpty {
options.writeToOutput(report)
}
}
}
}

private extension LintOrAnalyzeOptions {
func writeToOutput(_ string: String) {
guard let outFile = output else {
queuedPrint(string)
return
}

do {
let outFileURL = URL(fileURLWithPath: outFile)
let fileUpdater = try FileHandle(forUpdating: outFileURL)
fileUpdater.seekToEndOfFile()
fileUpdater.write(Data((string + "\n").utf8))
fileUpdater.closeFile()
} catch {
queuedPrintError("Could not write to file at path \(outFile)")
}
}
}