Skip to content

Commit 1702e73

Browse files
Re-organize BridgeJS plugin to fix BridgeJS tests build
1 parent 5d757fb commit 1702e73

File tree

18 files changed

+121
-118
lines changed

18 files changed

+121
-118
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../../../Plugins/BridgeJS/Sources/JavaScript/src/processor.js
1+
../../../../Plugins/BridgeJS/Sources/TS2Skeleton/JavaScript/src/processor.js

Plugins/BridgeJS/Package.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
import PackageDescription
44

5+
let coreDependencies: [Target.Dependency] = [
6+
.product(name: "SwiftParser", package: "swift-syntax"),
7+
.product(name: "SwiftSyntax", package: "swift-syntax"),
8+
.product(name: "SwiftBasicFormat", package: "swift-syntax"),
9+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
10+
]
11+
512
let package = Package(
613
name: "BridgeJS",
714
platforms: [.macOS(.v13)],
@@ -10,19 +17,13 @@ let package = Package(
1017
],
1118
targets: [
1219
.target(name: "BridgeJSBuildPlugin"),
13-
.target(name: "BridgeJSLink"),
1420
.executableTarget(
1521
name: "BridgeJSTool",
16-
dependencies: [
17-
.product(name: "SwiftParser", package: "swift-syntax"),
18-
.product(name: "SwiftSyntax", package: "swift-syntax"),
19-
.product(name: "SwiftBasicFormat", package: "swift-syntax"),
20-
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
21-
]
22+
dependencies: coreDependencies
2223
),
2324
.testTarget(
2425
name: "BridgeJSToolTests",
25-
dependencies: ["BridgeJSTool", "BridgeJSLink"],
26+
dependencies: coreDependencies,
2627
exclude: ["__Snapshots__", "Inputs"]
2728
),
2829
]

Plugins/BridgeJS/Sources/BridgeJSTool/BridgeJSTool.swift

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
@preconcurrency import func Foundation.exit
22
@preconcurrency import func Foundation.fputs
3-
@preconcurrency import func Foundation.kill
43
@preconcurrency import var Foundation.stderr
5-
@preconcurrency import var Foundation.SIGINT
6-
@preconcurrency import var Foundation.SIGTERM
74
@preconcurrency import struct Foundation.URL
85
@preconcurrency import struct Foundation.Data
96
@preconcurrency import class Foundation.JSONEncoder
107
@preconcurrency import class Foundation.FileManager
118
@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
179
import SwiftParser
1810

1911
/// BridgeJS Tool
@@ -227,101 +219,6 @@ import SwiftParser
227219
}
228220
}
229221

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-
325222
struct BridgeJSToolError: Swift.Error, CustomStringConvertible {
326223
let description: String
327224

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../TS2Skeleton

0 commit comments

Comments
 (0)