Skip to content

Commit 5537b4e

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 3f79c42 commit 5537b4e

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
private typealias SortedRules = [(String, any Rule.Type)]
1419

@@ -149,12 +154,20 @@ private extension TextTable {
149154

150155
private struct Terminal {
151156
static func currentWidth() -> Int {
157+
#if os(Windows)
158+
var csbi: CONSOLE_SCREEN_BUFFER_INFO = CONSOLE_SCREEN_BUFFER_INFO()
159+
guard GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) else {
160+
return 80
161+
}
162+
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
163+
#else
152164
var size = winsize()
153165
#if os(Linux)
154166
_ = ioctl(CInt(STDOUT_FILENO), UInt(TIOCGWINSZ), &size)
155167
#else
156168
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)
157169
#endif
158170
return Int(size.ws_col)
171+
#endif
159172
}
160173
}

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
@@ -330,7 +330,7 @@ private actor CorrectionsBuilder {
330330
}
331331

332332
private func memoryUsage() -> String? {
333-
#if os(Linux)
333+
#if os(Linux) || os(Windows)
334334
return nil
335335
#else
336336
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)