|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import LSPLogging |
| 14 | + |
| 15 | +import struct TSCBasic.AbsolutePath |
| 16 | +import class TSCBasic.Process |
| 17 | +import enum TSCBasic.ProcessEnv |
| 18 | +import struct TSCBasic.ProcessEnvironmentBlock |
| 19 | + |
| 20 | +extension Process { |
| 21 | + /// Launches a new process with the given parameters. |
| 22 | + /// |
| 23 | + /// - Important: If `workingDirectory` is not supported on this platform, this logs an error and falls back to launching the |
| 24 | + /// process without the working directory set. |
| 25 | + public static func launch( |
| 26 | + arguments: [String], |
| 27 | + environmentBlock: ProcessEnvironmentBlock = ProcessEnv.block, |
| 28 | + workingDirectory: AbsolutePath?, |
| 29 | + outputRedirection: OutputRedirection = .collect, |
| 30 | + startNewProcessGroup: Bool = true, |
| 31 | + loggingHandler: LoggingHandler? = .none |
| 32 | + ) throws -> Process { |
| 33 | + let process = |
| 34 | + if let workingDirectory { |
| 35 | + Process( |
| 36 | + arguments: arguments, |
| 37 | + environmentBlock: environmentBlock, |
| 38 | + workingDirectory: workingDirectory, |
| 39 | + outputRedirection: outputRedirection, |
| 40 | + startNewProcessGroup: startNewProcessGroup, |
| 41 | + loggingHandler: loggingHandler |
| 42 | + ) |
| 43 | + } else { |
| 44 | + self.init( |
| 45 | + arguments: arguments, |
| 46 | + environmentBlock: environmentBlock, |
| 47 | + outputRedirection: outputRedirection, |
| 48 | + startNewProcessGroup: startNewProcessGroup, |
| 49 | + loggingHandler: loggingHandler |
| 50 | + ) |
| 51 | + } |
| 52 | + do { |
| 53 | + try process.launch() |
| 54 | + } catch Process.Error.workingDirectoryNotSupported where workingDirectory != nil { |
| 55 | + // TODO (indexing): We need to figure out how to set the working directory on all platforms. |
| 56 | + logger.error( |
| 57 | + "Working directory not supported on the platform. Launching process without working directory \(workingDirectory!.pathString)" |
| 58 | + ) |
| 59 | + return try Process.launch( |
| 60 | + arguments: arguments, |
| 61 | + environmentBlock: environmentBlock, |
| 62 | + workingDirectory: nil, |
| 63 | + outputRedirection: outputRedirection, |
| 64 | + startNewProcessGroup: startNewProcessGroup, |
| 65 | + loggingHandler: loggingHandler |
| 66 | + ) |
| 67 | + } |
| 68 | + return process |
| 69 | + } |
| 70 | +} |
0 commit comments