Skip to content

Commit

Permalink
Ensure that content is complete using a queue
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Feb 1, 2025
1 parent a4893f6 commit 49eee3e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ swift_library(
":SourceKittenFramework.wrapper",
"@sourcekitten_com_github_jpsim_yams//:Yams",
"@swiftlint_com_github_scottrhoyt_swifty_text_table//:SwiftyTextTable",
"@com_github_mattmassicotte_queue//:Queue",
] + select({
"@platforms//os:linux": ["@com_github_krzyzanowskim_cryptoswift//:CryptoSwift"],
"//conditions:default": [":DyldWarningWorkaround"],
Expand Down
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ swiftlint_repos = use_extension("//bazel:repos.bzl", "swiftlint_repos_bzlmod")
use_repo(
swiftlint_repos,
"com_github_johnsundell_collectionconcurrencykit",
"com_github_mattmassicotte_queue",
"com_github_krzyzanowskim_cryptoswift",
"swiftlint_com_github_scottrhoyt_swifty_text_table",
)
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"version" : "1.8.4"
}
},
{
"identity" : "queue",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattmassicotte/Queue",
"state" : {
"revision" : "6adf359a705e3252742905b413bb8f56401043ca",
"version" : "0.2.0"
}
},
{
"identity" : "sourcekitten",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let package = Package(
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.9.0"),
.package(url: "https://github.com/JohnSundell/CollectionConcurrencyKit.git", from: "0.2.0"),
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.8.4")),
.package(url: "https://github.com/mattmassicotte/Queue", from: "0.2.0"),
],
targets: [
.executableTarget(
Expand Down Expand Up @@ -91,6 +92,7 @@ let package = Package(
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftyTextTable", package: "SwiftyTextTable"),
.product(name: "Yams", package: "Yams"),
.product(name: "Queue", package: "Queue"),
"SwiftLintCoreMacros",
],
swiftSettings: swiftFeatures + strictConcurrency
Expand Down
28 changes: 17 additions & 11 deletions Source/SwiftLintCore/Models/Issue.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Queue

/// All possible SwiftLint issues which are printed as warnings by default.
public enum Issue: LocalizedError, Equatable {
Expand Down Expand Up @@ -85,6 +86,7 @@ public enum Issue: LocalizedError, Equatable {
package nonisolated(unsafe) static var printDeprecationWarnings = true

@TaskLocal private static var messageConsumer: (@Sendable (String) -> Void)?
@TaskLocal private static var printQueue: AsyncQueue?

/// Hook used to capture all messages normally printed to stdout and return them back to the caller.
///
Expand All @@ -101,12 +103,19 @@ public enum Issue: LocalizedError, Equatable {
defer {
Console.content = ""
}
try $messageConsumer.withValue(
{ Console.content += (Console.content.isEmpty ? "" : "\n") + $0 },
operation: runner
let queue = AsyncQueue()
try $printQueue.withValue(
queue,
operation: {
try $messageConsumer.withValue(
{ Console.content += (Console.content.isEmpty ? "" : "\n") + $0 },
operation: runner
)
}
)
await Task.yield()
return Console.content
return await queue.addBarrierOperation {
Console.content
}.value
}

/// Wraps any `Error` into a `SwiftLintError.genericWarning` if it is not already a `SwiftLintError`.
Expand Down Expand Up @@ -140,13 +149,10 @@ public enum Issue: LocalizedError, Equatable {
if case .ruleDeprecated = self, !Self.printDeprecationWarnings {
return
}
Task(priority: .high) { @MainActor in
if let consumer = Self.messageConsumer {
consumer(errorDescription)
} else {
queuedPrintError(errorDescription)
}
Self.printQueue?.addOperation {
Self.messageConsumer?(errorDescription)
}
queuedPrintError(errorDescription)
}

private var message: String {
Expand Down
11 changes: 11 additions & 0 deletions bazel/Queue.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(
"@build_bazel_rules_swift//swift:swift.bzl",
"swift_library",
)

swift_library(
name = "Queue",
srcs = glob(["Sources/Queue/*.swift"]),
module_name = "Queue",
visibility = ["//visibility:public"],
)
8 changes: 8 additions & 0 deletions bazel/repos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def swiftlint_repos(bzlmod = False):
url = "https://github.com/JohnSundell/CollectionConcurrencyKit/archive/refs/tags/0.2.0.tar.gz",
)

http_archive(
name = "com_github_mattmassicotte_queue",
sha256 = "2c00cde22406d868b2341432c119c73d0c1a8ce40a663469b1b8cc5c39007737",
build_file = "@SwiftLint//bazel:Queue.BUILD",
strip_prefix = "Queue-0.2.0",
url = "https://github.com/mattmassicotte/Queue/archive/refs/tags/0.2.0.tar.gz",
)

http_archive(
name = "com_github_krzyzanowskim_cryptoswift",
sha256 = "69b23102ff453990d03aff4d3fabd172d0667b2b3ed95730021d60a0f8d50d14",
Expand Down

0 comments on commit 49eee3e

Please sign in to comment.