Skip to content

Commit 9b1f56b

Browse files
committed
swiftlint: add a Windows port
This enables building the swiftlint command on Windows. There is no system ioctl for terminal access, instead, we can use the Win32 Console API surface to query the console size. In the case of a failure, assume the width to be 80-columns (as the standard VGA console is 80x25).
1 parent 33f7857 commit 9b1f56b

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

Source/swiftlint/Commands/Rules.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import ArgumentParser
33
import Darwin
44
#elseif canImport(Glibc)
55
import Glibc
6+
#elseif canImport(ucrt)
7+
import ucrt
68
#else
79
#error("Unsupported platform")
810
#endif
911
import Foundation
1012
import SwiftLintFramework
1113
import SwiftyTextTable
14+
#if os(Windows)
15+
import WinSDK
16+
#endif
1217

1318
extension SwiftLint {
1419
struct Rules: ParsableCommand {
@@ -111,12 +116,20 @@ private extension TextTable {
111116

112117
private struct Terminal {
113118
static func currentWidth() -> Int {
119+
#if os(Windows)
120+
var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO()
121+
guard GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) else {
122+
return 80
123+
}
124+
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
125+
#else
114126
var size = winsize()
115127
#if os(Linux)
116128
_ = ioctl(CInt(STDOUT_FILENO), UInt(TIOCGWINSZ), &size)
117129
#else
118130
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)
119131
#endif
120132
return Int(size.ws_col)
133+
#endif
121134
}
122135
}

Source/swiftlint/Extensions/Configuration+CommandLine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private func scriptInputFiles() throws -> [SwiftLintFile] {
4040
}
4141
}
4242

43-
#if os(Linux)
43+
#if os(Linux) || os(Windows)
4444
private func autoreleasepool<T>(block: () -> T) -> T { return block() }
4545
#endif
4646

Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private actor CorrectionsBuilder {
316316
}
317317

318318
private func memoryUsage() -> String? {
319-
#if os(Linux)
319+
#if os(Linux) || os(Windows)
320320
return nil
321321
#else
322322
var info = mach_task_basic_info()

Source/swiftlint/Helpers/ProgressBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ actor ProgressBar {
5353
}
5454
}
5555

56-
#if os(Linux)
56+
#if os(Linux) || os(Windows)
5757
// swiftlint:disable:next identifier_name
5858
private let NSEC_PER_SEC = 1_000_000_000
5959
#endif

0 commit comments

Comments
 (0)