|
1 | 1 | @preconcurrency import func Foundation.exit |
2 | 2 | @preconcurrency import func Foundation.fputs |
3 | | -@preconcurrency import func Foundation.kill |
4 | 3 | @preconcurrency import var Foundation.stderr |
5 | | -@preconcurrency import var Foundation.SIGINT |
6 | | -@preconcurrency import var Foundation.SIGTERM |
7 | 4 | @preconcurrency import struct Foundation.URL |
8 | 5 | @preconcurrency import struct Foundation.Data |
9 | 6 | @preconcurrency import class Foundation.JSONEncoder |
10 | 7 | @preconcurrency import class Foundation.FileManager |
11 | 8 | @preconcurrency import class Foundation.JSONDecoder |
12 | | -@preconcurrency import class Foundation.ProcessInfo |
13 | | -@preconcurrency import class Foundation.Process |
14 | | -@preconcurrency import class Foundation.Pipe |
15 | | -import protocol Dispatch.DispatchSourceSignal |
16 | | -import class Dispatch.DispatchSource |
17 | 9 | import SwiftParser |
18 | 10 |
|
19 | 11 | /// BridgeJS Tool |
@@ -227,101 +219,6 @@ import SwiftParser |
227 | 219 | } |
228 | 220 | } |
229 | 221 |
|
230 | | -internal func which(_ executable: String) throws -> URL { |
231 | | - do { |
232 | | - // Check overriding environment variable |
233 | | - let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH" |
234 | | - if let path = ProcessInfo.processInfo.environment[envVariable] { |
235 | | - let url = URL(fileURLWithPath: path).appendingPathComponent(executable) |
236 | | - if FileManager.default.isExecutableFile(atPath: url.path) { |
237 | | - return url |
238 | | - } |
239 | | - } |
240 | | - } |
241 | | - let pathSeparator: Character |
242 | | - #if os(Windows) |
243 | | - pathSeparator = ";" |
244 | | - #else |
245 | | - pathSeparator = ":" |
246 | | - #endif |
247 | | - let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator) |
248 | | - for path in paths { |
249 | | - let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable) |
250 | | - if FileManager.default.isExecutableFile(atPath: url.path) { |
251 | | - return url |
252 | | - } |
253 | | - } |
254 | | - throw BridgeJSToolError("Executable \(executable) not found in PATH") |
255 | | -} |
256 | | - |
257 | | -extension ImportTS { |
258 | | - /// Processes a TypeScript definition file and extracts its API information |
259 | | - mutating func addSourceFile(_ sourceFile: String, tsconfigPath: String) throws { |
260 | | - let nodePath = try which("node") |
261 | | - let ts2skeletonPath = URL(fileURLWithPath: #filePath) |
262 | | - .deletingLastPathComponent() |
263 | | - .deletingLastPathComponent() |
264 | | - .appendingPathComponent("JavaScript") |
265 | | - .appendingPathComponent("bin") |
266 | | - .appendingPathComponent("ts2skeleton.js") |
267 | | - let arguments = [ts2skeletonPath.path, sourceFile, "--project", tsconfigPath] |
268 | | - |
269 | | - progress.print("Running ts2skeleton...") |
270 | | - progress.print(" \(([nodePath.path] + arguments).joined(separator: " "))") |
271 | | - |
272 | | - let process = Process() |
273 | | - let stdoutPipe = Pipe() |
274 | | - nonisolated(unsafe) var stdoutData = Data() |
275 | | - |
276 | | - process.executableURL = nodePath |
277 | | - process.arguments = arguments |
278 | | - process.standardOutput = stdoutPipe |
279 | | - |
280 | | - stdoutPipe.fileHandleForReading.readabilityHandler = { handle in |
281 | | - let data = handle.availableData |
282 | | - if data.count > 0 { |
283 | | - stdoutData.append(data) |
284 | | - } |
285 | | - } |
286 | | - try process.forwardTerminationSignals { |
287 | | - try process.run() |
288 | | - process.waitUntilExit() |
289 | | - } |
290 | | - |
291 | | - if process.terminationStatus != 0 { |
292 | | - throw BridgeJSCoreError("ts2skeleton returned \(process.terminationStatus)") |
293 | | - } |
294 | | - let skeleton = try JSONDecoder().decode(ImportedFileSkeleton.self, from: stdoutData) |
295 | | - self.addSkeleton(skeleton) |
296 | | - } |
297 | | -} |
298 | | - |
299 | | -extension Foundation.Process { |
300 | | - // Monitor termination/interrruption signals to forward them to child process |
301 | | - func setSignalForwarding(_ signalNo: Int32) -> DispatchSourceSignal { |
302 | | - let signalSource = DispatchSource.makeSignalSource(signal: signalNo) |
303 | | - signalSource.setEventHandler { [self] in |
304 | | - signalSource.cancel() |
305 | | - kill(processIdentifier, signalNo) |
306 | | - } |
307 | | - signalSource.resume() |
308 | | - return signalSource |
309 | | - } |
310 | | - |
311 | | - func forwardTerminationSignals(_ body: () throws -> Void) rethrows { |
312 | | - let sources = [ |
313 | | - setSignalForwarding(SIGINT), |
314 | | - setSignalForwarding(SIGTERM), |
315 | | - ] |
316 | | - defer { |
317 | | - for source in sources { |
318 | | - source.cancel() |
319 | | - } |
320 | | - } |
321 | | - try body() |
322 | | - } |
323 | | -} |
324 | | - |
325 | 222 | struct BridgeJSToolError: Swift.Error, CustomStringConvertible { |
326 | 223 | let description: String |
327 | 224 |
|
|
0 commit comments