@@ -21,20 +21,18 @@ import Foundation
21
21
import TSCLibc
22
22
import Dispatch
23
23
24
- import _Concurrency
25
-
26
24
/// Process result data which is available after process termination.
27
- public struct ProcessResult : CustomStringConvertible , Sendable {
25
+ public struct ProcessResult : CustomStringConvertible {
28
26
29
- public enum Error : Swift . Error , Sendable {
27
+ public enum Error : Swift . Error {
30
28
/// The output is not a valid UTF8 sequence.
31
29
case illegalUTF8Sequence
32
30
33
31
/// The process had a non zero exit.
34
32
case nonZeroExit( ProcessResult )
35
33
}
36
34
37
- public enum ExitStatus : Equatable , Sendable {
35
+ public enum ExitStatus : Equatable {
38
36
/// The process was terminated normally with a exit code.
39
37
case terminated( code: Int32 )
40
38
#if os(Windows)
@@ -127,18 +125,12 @@ public struct ProcessResult: CustomStringConvertible, Sendable {
127
125
}
128
126
}
129
127
130
- #if swift(<5.6)
131
- extension Process : UnsafeSendable { }
132
- #else
133
- extension Process : @unchecked Sendable { }
134
- #endif
135
-
136
128
/// Process allows spawning new subprocesses and working with them.
137
129
///
138
130
/// Note: This class is thread safe.
139
131
public final class Process {
140
132
/// Errors when attempting to invoke a process
141
- public enum Error : Swift . Error , Sendable {
133
+ public enum Error : Swift . Error {
142
134
/// The program requested to be executed cannot be found on the existing search paths, or is not executable.
143
135
case missingExecutableProgram( program: String )
144
136
@@ -815,29 +807,7 @@ public final class Process {
815
807
#endif // POSIX implementation
816
808
}
817
809
818
- /// Executes the process I/O state machine, returning the result when finished.
819
- @available ( macOS 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , watchOS 6 . 0 , * )
820
- @discardableResult
821
- public func waitUntilExit( ) async throws -> ProcessResult {
822
- #if compiler(>=5.6)
823
- return try await withCheckedThrowingContinuation { continuation in
824
- waitUntilExit ( continuation. resume ( with: ) )
825
- }
826
- #else
827
- if #available( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * ) {
828
- return try await withCheckedThrowingContinuation { continuation in
829
- waitUntilExit ( continuation. resume ( with: ) )
830
- }
831
- } else {
832
- preconditionFailure ( " Unsupported with Swift 5.5 on this OS version " )
833
- }
834
- #endif
835
- }
836
-
837
810
/// Blocks the calling process until the subprocess finishes execution.
838
- #if compiler(>=5.8)
839
- @available ( * , noasync)
840
- #endif
841
811
@discardableResult
842
812
public func waitUntilExit( ) throws -> ProcessResult {
843
813
let group = DispatchGroup ( )
@@ -968,88 +938,6 @@ public final class Process {
968
938
}
969
939
}
970
940
971
- extension Process {
972
- /// Execute a subprocess and returns the result when it finishes execution
973
- ///
974
- /// - Parameters:
975
- /// - arguments: The arguments for the subprocess.
976
- /// - environment: The environment to pass to subprocess. By default the current process environment
977
- /// will be inherited.
978
- /// - loggingHandler: Handler for logging messages
979
- @available ( macOS 10 . 15 , * )
980
- static public func popen(
981
- arguments: [ String ] ,
982
- environment: [ String : String ] = ProcessEnv . vars,
983
- loggingHandler: LoggingHandler ? = . none
984
- ) async throws -> ProcessResult {
985
- let process = Process (
986
- arguments: arguments,
987
- environment: environment,
988
- outputRedirection: . collect,
989
- loggingHandler: loggingHandler
990
- )
991
- try process. launch ( )
992
- return try await process. waitUntilExit ( )
993
- }
994
-
995
- /// Execute a subprocess and returns the result when it finishes execution
996
- ///
997
- /// - Parameters:
998
- /// - args: The arguments for the subprocess.
999
- /// - environment: The environment to pass to subprocess. By default the current process environment
1000
- /// will be inherited.
1001
- /// - loggingHandler: Handler for logging messages
1002
- @available ( macOS 10 . 15 , * )
1003
- static public func popen(
1004
- args: String ... ,
1005
- environment: [ String : String ] = ProcessEnv . vars,
1006
- loggingHandler: LoggingHandler ? = . none
1007
- ) async throws -> ProcessResult {
1008
- try await popen ( arguments: args, environment: environment, loggingHandler: loggingHandler)
1009
- }
1010
-
1011
- /// Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
1012
- ///
1013
- /// - Parameters:
1014
- /// - arguments: The arguments for the subprocess.
1015
- /// - environment: The environment to pass to subprocess. By default the current process environment
1016
- /// will be inherited.
1017
- /// - loggingHandler: Handler for logging messages
1018
- /// - Returns: The process output (stdout + stderr).
1019
- @available ( macOS 10 . 15 , * )
1020
- @discardableResult
1021
- static public func checkNonZeroExit(
1022
- arguments: [ String ] ,
1023
- environment: [ String : String ] = ProcessEnv . vars,
1024
- loggingHandler: LoggingHandler ? = . none
1025
- ) async throws -> String {
1026
- let result = try await popen ( arguments: arguments, environment: environment, loggingHandler: loggingHandler)
1027
- // Throw if there was a non zero termination.
1028
- guard result. exitStatus == . terminated( code: 0 ) else {
1029
- throw ProcessResult . Error. nonZeroExit ( result)
1030
- }
1031
- return try result. utf8Output ( )
1032
- }
1033
-
1034
- /// Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
1035
- ///
1036
- /// - Parameters:
1037
- /// - args: The arguments for the subprocess.
1038
- /// - environment: The environment to pass to subprocess. By default the current process environment
1039
- /// will be inherited.
1040
- /// - loggingHandler: Handler for logging messages
1041
- /// - Returns: The process output (stdout + stderr).
1042
- @available ( macOS 10 . 15 , * )
1043
- @discardableResult
1044
- static public func checkNonZeroExit(
1045
- args: String ... ,
1046
- environment: [ String : String ] = ProcessEnv . vars,
1047
- loggingHandler: LoggingHandler ? = . none
1048
- ) async throws -> String {
1049
- try await checkNonZeroExit ( arguments: args, environment: environment, loggingHandler: loggingHandler)
1050
- }
1051
- }
1052
-
1053
941
extension Process {
1054
942
/// Execute a subprocess and calls completion block when it finishes execution
1055
943
///
@@ -1060,9 +948,6 @@ extension Process {
1060
948
/// - loggingHandler: Handler for logging messages
1061
949
/// - queue: Queue to use for callbacks
1062
950
/// - completion: A completion handler to return the process result
1063
- #if compiler(>=5.8)
1064
- @available ( * , noasync)
1065
- #endif
1066
951
static public func popen(
1067
952
arguments: [ String ] ,
1068
953
environment: [ String : String ] = ProcessEnv . vars,
@@ -1097,9 +982,6 @@ extension Process {
1097
982
/// will be inherited.
1098
983
/// - loggingHandler: Handler for logging messages
1099
984
/// - Returns: The process result.
1100
- #if compiler(>=5.8)
1101
- @available ( * , noasync)
1102
- #endif
1103
985
@discardableResult
1104
986
static public func popen(
1105
987
arguments: [ String ] ,
@@ -1124,9 +1006,6 @@ extension Process {
1124
1006
/// will be inherited.
1125
1007
/// - loggingHandler: Handler for logging messages
1126
1008
/// - Returns: The process result.
1127
- #if compiler(>=5.8)
1128
- @available ( * , noasync)
1129
- #endif
1130
1009
@discardableResult
1131
1010
static public func popen(
1132
1011
args: String ... ,
@@ -1144,9 +1023,6 @@ extension Process {
1144
1023
/// will be inherited.
1145
1024
/// - loggingHandler: Handler for logging messages
1146
1025
/// - Returns: The process output (stdout + stderr).
1147
- #if compiler(>=5.8)
1148
- @available ( * , noasync)
1149
- #endif
1150
1026
@discardableResult
1151
1027
static public func checkNonZeroExit(
1152
1028
arguments: [ String ] ,
@@ -1176,9 +1052,6 @@ extension Process {
1176
1052
/// will be inherited.
1177
1053
/// - loggingHandler: Handler for logging messages
1178
1054
/// - Returns: The process output (stdout + stderr).
1179
- #if compiler(>=5.8)
1180
- @available ( * , noasync)
1181
- #endif
1182
1055
@discardableResult
1183
1056
static public func checkNonZeroExit(
1184
1057
args: String ... ,
0 commit comments