Skip to content

Commit

Permalink
Fix building on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Zollerboy1 committed Aug 17, 2022
1 parent cb1bfad commit b4ea08c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
@@ -1,6 +1,7 @@
# SwiftCommand

![Platform: macOS/Linux](https://img.shields.io/badge/platform-macOS%20%7C%20Linux-orange) ![Swift versions: 5.6](https://img.shields.io/badge/swift-5.6-blue)
![Platform: macOS/Linux/Windows](https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-orange)
![Swift versions: 5.6](https://img.shields.io/badge/swift-5.6-blue)

A wrapper around `Foundation.Process`, inspired by Rust's
`std::process::Command`. This package makes it easy to call command line
Expand Down
38 changes: 32 additions & 6 deletions Sources/SwiftCommand/Command.swift
Expand Up @@ -74,7 +74,24 @@ where Stdin: InputSource, Stdout: OutputDestination, Stderr: OutputDestination {
}
}



#if os(Windows)
@inline(__always)
private static var pathVariable: String { "Path" }
@inline(__always)
private static var pathSeparator: Character { ";" }
@inline(__always)
private static var executableExtension: String { ".exe" }
#else
@inline(__always)
private static var pathVariable: String { "PATH" }
@inline(__always)
private static var pathSeparator: Character { ":" }
@inline(__always)
private static var executableExtension: String { "" }
#endif


/// The path of the executable file that will be invoked when this command
/// is spawned.
///
Expand Down Expand Up @@ -206,15 +223,24 @@ where Stdin: InputSource, Stdout: OutputDestination, Stderr: OutputDestination {
where Stdin == UnspecifiedInputSource,
Stdout == UnspecifiedOutputDestination,
Stderr == UnspecifiedOutputDestination {
guard let environmentPath = ProcessInfo.processInfo
.environment["PATH"] else {
let nameWithExtension: String
if !Self.executableExtension.isEmpty
&& !name.hasSuffix(Self.executableExtension) {
nameWithExtension = name + Self.executableExtension
} else {
nameWithExtension = name
}

guard let environmentPath =
ProcessInfo.processInfo.environment[Self.pathVariable] else {
return nil
}

guard let executablePath = environmentPath.split(separator: ":").lazy
guard let executablePath =
environmentPath.split(separator: Self.pathSeparator).lazy
.compactMap(FilePath.init(substring:))
.map({ $0.appending(name) })
.first(where: {
.map({ $0.appending(nameWithExtension) })
.first(where: {
FileManager.default.isExecutableFile(atPath: $0.string)
}) else {
return nil
Expand Down
14 changes: 11 additions & 3 deletions Sources/SwiftCommand/FileHandle+Async.swift
@@ -1,6 +1,7 @@
import Foundation

fileprivate final actor IOActor {
#if !os(Windows)
fileprivate func read(
from fd: Int32,
into buffer: UnsafeMutableRawBufferPointer
Expand All @@ -10,9 +11,6 @@ fileprivate final actor IOActor {
let read = Darwin.read
#elseif canImport(Glibc)
let read = Glibc.read
#elseif canImport(CRT)
let read = CRT._read
#else
#error("Unsupported platform!")
#endif
let amount = read(fd, buffer.baseAddress, buffer.count)
Expand All @@ -29,6 +27,7 @@ fileprivate final actor IOActor {
}
}
}
#endif

fileprivate func read(
from handle: FileHandle,
Expand Down Expand Up @@ -174,7 +173,9 @@ extension FileHandle {
fileprivate init(file: FileHandle) {
self._buffer = _AsyncBytesBuffer(_capacity: Self.bufferSize)

#if !os(Windows)
let fileDescriptor = file.fileDescriptor
#endif

self._buffer.readFunction = { buf in
buf._nextPointer = buf.baseAddress
Expand All @@ -186,6 +187,12 @@ extension FileHandle {
count: capacity
)

#if os(Windows)
let readSize = try await IOActor.default.read(
from: file,
into: bufPtr
)
#else
let readSize: Int
if fileDescriptor >= 0 {
readSize = try await IOActor.default.read(
Expand All @@ -198,6 +205,7 @@ extension FileHandle {
into: bufPtr
)
}
#endif

buf._endPointer = buf._nextPointer + readSize
return readSize
Expand Down

0 comments on commit b4ea08c

Please sign in to comment.