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