Skip to content

Commit de90da1

Browse files
authored
Merge pull request #2 from apple/update
Sync with SwiftPM trunk
2 parents fcaa2ce + 9013a5f commit de90da1

20 files changed

+170
-233
lines changed

Sources/TSCBasic/Await.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// - Returns: The value wrapped by the async method's result.
1616
/// - Throws: The error wrapped by the async method's result
1717
public func await<T, ErrorType>(_ body: (@escaping (Result<T, ErrorType>) -> Void) -> Void) throws -> T {
18-
return try await(body).dematerialize()
18+
return try await(body).get()
1919
}
2020

2121
public func await<T>(_ body: (@escaping (T) -> Void) -> Void) -> T {

Sources/TSCBasic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(TSCBasic
1010
Await.swift
1111
ByteString.swift
1212
CacheableSequence.swift
13+
CodableResult.swift
1314
CollectionAlgorithms.swift
1415
CollectionExtensions.swift
1516
Condition.swift
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2019 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
/// Codable wrapper for Result
12+
public struct CodableResult<Success, Failure>: Codable where Success: Codable, Failure: Codable & Error {
13+
private enum CodingKeys: String, CodingKey {
14+
case success, failure
15+
}
16+
17+
public let result: Result<Success, Failure>
18+
public init(result: Result<Success, Failure>) {
19+
self.result = result
20+
}
21+
22+
public func encode(to encoder: Encoder) throws {
23+
var container = encoder.container(keyedBy: CodingKeys.self)
24+
switch self.result {
25+
case .success(let value):
26+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .success)
27+
try unkeyedContainer.encode(value)
28+
case .failure(let error):
29+
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .failure)
30+
try unkeyedContainer.encode(error)
31+
}
32+
}
33+
34+
public init(from decoder: Decoder) throws {
35+
let values = try decoder.container(keyedBy: CodingKeys.self)
36+
guard let key = values.allKeys.first(where: values.contains) else {
37+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
38+
}
39+
switch key {
40+
case .success:
41+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
42+
let value = try unkeyedValues.decode(Success.self)
43+
self.init(result: .success(value))
44+
case .failure:
45+
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
46+
let error = try unkeyedValues.decode(Failure.self)
47+
self.init(result: .failure(error))
48+
}
49+
}
50+
}
51+
52+
extension CodableResult where Failure == StringError {
53+
public init(body: () throws -> Success) {
54+
do {
55+
self.init(result: .success(try body()))
56+
} catch let error as StringError {
57+
self.init(result: .failure(error))
58+
} catch {
59+
self.init(result: .failure(StringError(String(describing: error))))
60+
}
61+
}
62+
}

Sources/TSCBasic/Process.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public struct ProcessResult: CustomStringConvertible {
8989

9090
/// Converts stdout output bytes to string, assuming they're UTF8.
9191
public func utf8Output() throws -> String {
92-
return String(decoding: try output.dematerialize(), as: Unicode.UTF8.self)
92+
return String(decoding: try output.get(), as: Unicode.UTF8.self)
9393
}
9494

9595
/// Converts stderr output bytes to string, assuming they're UTF8.
9696
public func utf8stderrOutput() throws -> String {
97-
return String(decoding: try stderrOutput.dematerialize(), as: Unicode.UTF8.self)
97+
return String(decoding: try stderrOutput.get(), as: Unicode.UTF8.self)
9898
}
9999

100100
public var description: String {
@@ -201,10 +201,10 @@ public final class Process: ObjectIdentifierProtocol {
201201
#endif
202202

203203
/// If redirected, stdout result and reference to the thread reading the output.
204-
private var stdout: (result: Result<[UInt8], AnyError>, thread: Thread?) = (Result([]), nil)
204+
private var stdout: (result: Result<[UInt8], AnyError>, thread: Thread?) = (.success([]), nil)
205205

206206
/// If redirected, stderr result and reference to the thread reading the output.
207-
private var stderr: (result: Result<[UInt8], AnyError>, thread: Thread?) = (Result([]), nil)
207+
private var stderr: (result: Result<[UInt8], AnyError>, thread: Thread?) = (.success([]), nil)
208208

209209
/// Queue to protect concurrent reads.
210210
private let serialQueue = DispatchQueue(label: "org.swift.swiftpm.process")
@@ -516,7 +516,7 @@ public final class Process: ObjectIdentifierProtocol {
516516
// Close the read end of the output pipe.
517517
close(fd)
518518
// Construct the output result.
519-
return error.map(Result.init) ?? Result(out)
519+
return error.map(Result.init) ?? .success(out)
520520
}
521521
#endif
522522

Sources/TSCBasic/Result.swift

Lines changed: 7 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,13 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
/// An simple enum which is either a value or an error.
12-
/// It can be used for error handling in situations where try catch is
13-
/// problematic to use, for eg: asynchronous APIs.
14-
public enum Result<Value, ErrorType: Swift.Error> {
15-
/// Indicates success with value in the associated object.
16-
case success(Value)
17-
18-
/// Indicates failure with error inside the associated object.
19-
case failure(ErrorType)
20-
21-
/// Initialiser for value.
22-
public init(_ value: Value) {
23-
self = .success(value)
24-
}
25-
26-
/// Initialiser for error.
27-
public init(_ error: ErrorType) {
28-
self = .failure(error)
29-
}
30-
31-
/// Initialise with something that can throw ErrorType.
32-
public init(_ body: () throws -> Value) throws {
33-
do {
34-
self = .success(try body())
35-
} catch let error as ErrorType {
36-
self = .failure(error)
37-
}
38-
}
39-
40-
/// Get the value if success else throw the saved error.
41-
public func dematerialize() throws -> Value {
42-
switch self {
43-
case .success(let value):
44-
return value
45-
case .failure(let error):
46-
throw error
47-
}
48-
}
49-
50-
/// Evaluates the given closure when this Result instance has a value.
51-
public func map<U>(_ transform: (Value) throws -> U) rethrows -> Result<U, ErrorType> {
52-
switch self {
53-
case .success(let value):
54-
return Result<U, ErrorType>(try transform(value))
55-
case .failure(let error):
56-
return Result<U, ErrorType>(error)
57-
}
58-
}
59-
60-
/// Evaluates the given closure when this Result instance has a value, passing the unwrapped value as a parameter.
61-
///
62-
/// The closure returns a Result instance itself which can have value or not.
63-
public func flatMap<U>(_ transform: (Value) -> Result<U, ErrorType>) -> Result<U, ErrorType> {
64-
switch self {
65-
case .success(let value):
66-
return transform(value)
67-
case .failure(let error):
68-
return Result<U, ErrorType>(error)
69-
}
70-
}
71-
72-
}
73-
74-
extension Result: CustomStringConvertible {
75-
public var description: String {
76-
switch self {
77-
case .success(let value):
78-
return "Result(\(value))"
79-
case .failure(let error):
80-
return "Result(\(error))"
81-
}
82-
}
83-
}
84-
8511
/// A type erased error enum.
8612
public struct AnyError: Swift.Error, CustomStringConvertible {
8713
/// The underlying error.
@@ -114,9 +40,9 @@ public struct StringError: Equatable, Codable, CustomStringConvertible, Error {
11440
}
11541

11642
// AnyError specific helpers.
117-
extension Result where ErrorType == AnyError {
43+
extension Result where Failure == AnyError {
11844
/// Initialise with something that throws AnyError.
119-
public init(anyError body: () throws -> Value) {
45+
public init(anyError body: () throws -> Success) {
12046
do {
12147
self = .success(try body())
12248
} catch {
@@ -132,11 +58,11 @@ extension Result where ErrorType == AnyError {
13258
/// Evaluates the given throwing closure when this Result instance has a value.
13359
///
13460
/// The final result will either be the transformed value or any error thrown by the closure.
135-
public func mapAny<U>(_ transform: (Value) throws -> U) -> Result<U, AnyError> {
61+
public func mapAny<U>(_ transform: (Success) throws -> U) -> Result<U, AnyError> {
13662
switch self {
13763
case .success(let value):
13864
do {
139-
return Result<U, AnyError>(try transform(value))
65+
return Result<U, AnyError>.success(try transform(value))
14066
} catch {
14167
return Result<U, AnyError>(error)
14268
}
@@ -146,11 +72,11 @@ extension Result where ErrorType == AnyError {
14672
}
14773
}
14874

149-
extension Result where ErrorType == StringError {
75+
extension Result where Failure == StringError {
15076
/// Create an instance of Result<Value, StringError>.
15177
///
15278
/// Errors will be encoded as StringError using their description.
153-
public init(string body: () throws -> Value) {
79+
public init(string body: () throws -> Success) {
15480
do {
15581
self = .success(try body())
15682
} catch let error as StringError {
@@ -160,40 +86,3 @@ extension Result where ErrorType == StringError {
16086
}
16187
}
16288
}
163-
164-
extension Result: Equatable where Value: Equatable, ErrorType: Equatable {}
165-
166-
extension Result: Codable where Value: Codable, ErrorType: Codable {
167-
private enum CodingKeys: String, CodingKey {
168-
case success, failure
169-
}
170-
171-
public func encode(to encoder: Encoder) throws {
172-
var container = encoder.container(keyedBy: CodingKeys.self)
173-
switch self {
174-
case .success(let value):
175-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .success)
176-
try unkeyedContainer.encode(value)
177-
case .failure(let error):
178-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .failure)
179-
try unkeyedContainer.encode(error)
180-
}
181-
}
182-
183-
public init(from decoder: Decoder) throws {
184-
let values = try decoder.container(keyedBy: CodingKeys.self)
185-
guard let key = values.allKeys.first(where: values.contains) else {
186-
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
187-
}
188-
switch key {
189-
case .success:
190-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
191-
let value = try unkeyedValues.decode(Value.self)
192-
self = .success(value)
193-
case .failure:
194-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
195-
let error = try unkeyedValues.decode(ErrorType.self)
196-
self = .failure(error)
197-
}
198-
}
199-
}

Sources/TSCBasic/TemporaryFile.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,23 @@ private extension TempFileError {
4545
///
4646
/// - Returns: Path to directory in which temporary file should be created.
4747
public func determineTempDirectory(_ dir: AbsolutePath? = nil) throws -> AbsolutePath {
48-
// FIXME: Add other platform specific locations.
49-
let tmpDir = dir ?? AbsolutePath(NSTemporaryDirectory())
48+
let tmpDir = dir ?? cachedTempDirectory
5049
guard localFileSystem.isDirectory(tmpDir) else {
5150
throw TempFileError.couldNotFindTmpDir
5251
}
5352
return tmpDir
5453
}
5554

55+
/// Returns temporary directory location by searching relevant env variables.
56+
///
57+
/// Evaluates once per execution.
58+
private var cachedTempDirectory: AbsolutePath = {
59+
let override = ProcessEnv.vars["TMPDIR"] ?? ProcessEnv.vars["TEMP"] ?? ProcessEnv.vars["TMP"]
60+
if let path = override.flatMap({ try? AbsolutePath(validating: $0) }) {
61+
return path
62+
}
63+
return AbsolutePath(NSTemporaryDirectory())
64+
}()
5665

5766
/// The closure argument of the `body` closue of `withTemporaryFile`.
5867
public struct TemporaryFile {
@@ -86,7 +95,7 @@ public struct TemporaryFile {
8695
// Convert path to a C style string terminating with null char to be an valid input
8796
// to mkstemps method. The XXXXXX in this string will be replaced by a random string
8897
// which will be the actual path to the temporary file.
89-
var template = [UInt8](path.pathString.utf8).map({ Int8($0) }) + [Int8(0)]
98+
var template = Array(path.pathString.utf8CString)
9099

91100
fd = TSCLibc.mkstemps(&template, Int32(suffix.utf8.count))
92101
// If mkstemps failed then throw error.

Sources/TSCBasic/TerminalController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010

1111
import TSCLibc
12+
#if os(Windows)
13+
import MSVCRT
14+
#endif
1215

1316
/// A class to have better control on tty output streams: standard output and standard error.
1417
/// Allows operations like cursor movement and colored text output on tty.
@@ -84,6 +87,18 @@ public final class TerminalController {
8487
} else {
8588
width = 80
8689
}
90+
91+
#if os(Windows)
92+
// Enable VT100 interpretation
93+
let hOut = GetStdHandle(STD_OUTPUT_HANDLE)
94+
var dwMode: DWORD = 0
95+
96+
guard hOut != INVALID_HANDLE_VALUE else { return nil }
97+
guard GetConsoleMode(hOut, &dwMode) else { return nil }
98+
99+
dwMode |= DWORD(ENABLE_VIRTUAL_TERMINAL_PROCESSING)
100+
guard SetConsoleMode(hOut, dwMode) else { return nil }
101+
#endif
87102
self.stream = stream
88103
}
89104

Sources/TSCLibc/libc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
@_exported import Darwin.C
1818
#endif
1919

20-
@_exported import clibc
20+
@_exported import TSCclibc

Sources/TSCTestSupport/Product.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ extension Product {
9797
environment["SWIFTPM_TESTS_MODULECACHE"] = self.path.parentDirectory.pathString
9898
environment["SDKROOT"] = nil
9999

100+
// Unset the internal env variable that allows skipping certain tests.
101+
environment["_SWIFTPM_SKIP_TESTS_LIST"] = nil
102+
100103
var completeArgs = [path.pathString]
101104
if let packagePath = packagePath {
102105
completeArgs += ["--package-path", packagePath.pathString]

Sources/TSCUtility/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_library(TSCUtility
1818
IndexStore.swift
1919
InterruptHandler.swift
2020
misc.swift
21+
OSLog.swift
2122
PkgConfig.swift
2223
Platform.swift
2324
ProgressAnimation.swift

0 commit comments

Comments
 (0)