Skip to content
Open
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let package = Package(
.target(
name: "SwiftLintCore",
dependencies: [
.product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: [.linux])),
.product(name: "CryptoSwift", package: "CryptoSwift", condition: .when(platforms: [.linux, .windows])),
.target(name: "DyldWarningWorkaround", condition: .when(platforms: [.macOS])),
.product(name: "SourceKittenFramework", package: "SourceKitten"),
.product(name: "SwiftIDEUtils", package: "swift-syntax"),
Expand Down
8 changes: 8 additions & 0 deletions Plugins/SwiftLintPlugin/Path+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PackagePlugin

#if os(Linux)
import Glibc
#elseif os(Windows)
import ucrt
#else
import Darwin
#endif
Expand Down Expand Up @@ -33,9 +35,15 @@ extension Path {

/// Safe way to check if the file is accessible from within the current process sandbox.
private func isAccessible() -> Bool {
#if os(Windows)
let result = string.withCString(encodedAs: UTF16.self) {
_waccess($0, 04)
}
#else
let result = string.withCString { pointer in
access(pointer, R_OK)
}
#endif

return result == 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private extension String {
var strippingURLs: String {
let range = fullNSRange
// Workaround for Linux until NSDataDetector is available
#if os(Linux)
#if os(Linux) || os(Windows)
// Regex pattern from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
let pattern = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)" +
"(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*" +
Expand Down
8 changes: 8 additions & 0 deletions Source/SwiftLintCore/Extensions/Configuration+Remote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Foundation // swiftlint:disable:this file_name
import FoundationNetworking
#endif

#if os(Windows)
import func WinSDK.Sleep
#endif

internal extension Configuration.FileGraph.FilePath {
// MARK: - Properties: Remote Cache
/// This should never be touched.
Expand Down Expand Up @@ -87,7 +91,11 @@ internal extension Configuration.FileGraph.FilePath {
while true {
if taskDone { break }
if Date().timeIntervalSince(startDate) > timeout { task.cancel(); break }
#if os(Windows)
Sleep(50)
#else
usleep(50_000) // Sleep for 50 ms
#endif
}

// Handle wrong data
Expand Down
31 changes: 31 additions & 0 deletions Source/SwiftLintCore/Helpers/Glob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ private let globFunction = Darwin.glob
import Glibc

private let globFunction = Glibc.glob
#elseif canImport(ucrt)
import ucrt
#else
#error("Unsupported platform")
#endif

#if os(Windows)
import WinSDK
#endif

// Adapted from https://gist.github.com/efirestone/ce01ae109e08772647eb061b3bb387c3

struct Glob {
Expand All @@ -23,12 +29,35 @@ struct Glob {

return expandGlobstar(pattern: pattern)
.reduce(into: [String]()) { paths, pattern in
#if os(Windows)
URL(fileURLWithPath: pattern).withUnsafeFileSystemRepresentation {
var ffd: WIN32_FIND_DATAW = WIN32_FIND_DATAW()

let hDirectory: HANDLE = String(cString: $0!).withCString(encodedAs: UTF16.self) {
FindFirstFileW($0, &ffd)
}
if hDirectory == INVALID_HANDLE_VALUE { return }
defer { FindClose(hDirectory) }

repeat {
let path: String = withUnsafePointer(to: &ffd.cFileName) {
$0.withMemoryRebound(to: UInt16.self, capacity: MemoryLayout.size(ofValue: $0) / MemoryLayout<WCHAR>.size) {
String(decodingCString: $0, as: UTF16.self)
}
}
if path != "." && path != ".." {
paths.append(path)
}
} while FindNextFileW(hDirectory, &ffd)
}
#else
var globResult = glob_t()
defer { globfree(&globResult) }

if globFunction(pattern, GLOB_TILDE | GLOB_BRACE | GLOB_MARK, nil, &globResult) == 0 {
paths.append(contentsOf: populateFiles(globResult: globResult))
}
#endif
}
.unique
.sorted()
Expand Down Expand Up @@ -90,6 +119,7 @@ struct Glob {
return isDirectory && isDirectoryBool.boolValue
}

#if !os(Windows)
private static func populateFiles(globResult: glob_t) -> [String] {
#if os(Linux)
let matchCount = globResult.gl_pathc
Expand All @@ -100,4 +130,5 @@ struct Glob {
globResult.gl_pathv[index].flatMap { String(validatingUTF8: $0) }
}
}
#endif
}
6 changes: 3 additions & 3 deletions Source/SwiftLintCore/Helpers/Reachability.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !os(Linux)
#if os(macOS)
import SystemConfiguration
#endif

Expand All @@ -9,9 +9,9 @@ enum Reachability {
}

/// Returns whether the device is connected to a network, if known.
/// On Linux, this always evaluates to `nil`.
/// On Linux and Windows, this always evaluates to `nil`.
static var connectivityStatus: ConnectivityStatus {
#if os(Linux)
#if os(Linux) || os(Windows)
return .unknown
#else
var zeroAddress = sockaddr_in()
Expand Down
13 changes: 13 additions & 0 deletions Source/swiftlint/Commands/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import ArgumentParser
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(ucrt)
import ucrt
#else
#error("Unsupported platform")
#endif
import Foundation
import SwiftLintFramework
import SwiftyTextTable
#if os(Windows)
import WinSDK
#endif

private typealias SortedRules = [(String, any Rule.Type)]

Expand Down Expand Up @@ -149,12 +154,20 @@ private extension TextTable {

private struct Terminal {
static func currentWidth() -> Int {
#if os(Windows)
var csbi = CONSOLE_SCREEN_BUFFER_INFO()
guard GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi) else {
return 80
}
return Int(csbi.srWindow.Right - csbi.srWindow.Left) + 1
#else
var size = winsize()
#if os(Linux)
_ = ioctl(CInt(STDOUT_FILENO), UInt(TIOCGWINSZ), &size)
#else
_ = ioctl(STDOUT_FILENO, TIOCGWINSZ, &size)
#endif
return Int(size.ws_col)
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private func scriptInputFiles() throws -> [SwiftLintFile] {
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Helpers/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private actor CorrectionsBuilder {
}

private func memoryUsage() -> String? {
#if os(Linux)
#if os(Linux) || os(Windows)
return nil
#else
var info = mach_task_basic_info()
Expand Down
2 changes: 1 addition & 1 deletion Source/swiftlint/Helpers/ProgressBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ actor ProgressBar {
}
}

#if os(Linux)
#if os(Linux) || os(Windows)
// swiftlint:disable:next identifier_name
private let NSEC_PER_SEC = 1_000_000_000
#endif
Expand Down