forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeJSBuildPlugin.swift
More file actions
78 lines (67 loc) · 2.94 KB
/
BridgeJSBuildPlugin.swift
File metadata and controls
78 lines (67 loc) · 2.94 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
67
68
69
70
71
72
73
74
75
76
77
78
#if canImport(PackagePlugin)
import PackagePlugin
import Foundation
/// Build plugin for runtime code generation with BridgeJS.
/// This plugin automatically generates bridge code between Swift and JavaScript
/// during each build process.
@main
struct BridgeJSBuildPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
guard let swiftSourceModuleTarget = target as? SwiftSourceModuleTarget else {
return []
}
return [try createGenerateCommand(context: context, target: swiftSourceModuleTarget)]
}
private func pathToConfigFile(target: SwiftSourceModuleTarget) -> URL {
return target.directoryURL.appending(path: "bridge-js.config.json")
}
private func createGenerateCommand(context: PluginContext, target: SwiftSourceModuleTarget) throws -> Command {
let outputSwiftPath = context.pluginWorkDirectoryURL.appending(path: "BridgeJS.swift")
let inputSwiftFiles = target.sourceFiles.filter {
!$0.url.path.hasPrefix(context.pluginWorkDirectoryURL.path + "/")
}
.map(\.url)
let configFile = pathToConfigFile(target: target)
var inputFiles: [URL] = inputSwiftFiles
if FileManager.default.fileExists(atPath: configFile.path) {
inputFiles.append(configFile)
}
// Include Swift files generated by other plugins applied to this
// target (available in tools-version 6.0+). This lets BridgeJS
// process @JS annotations in files produced by earlier plugins
// without requiring any extra configuration.
let pluginGeneratedSwiftFiles = target.pluginGeneratedSources.filter {
$0.pathExtension == "swift"
}
inputFiles.append(contentsOf: pluginGeneratedSwiftFiles)
let inputTSFile = target.directoryURL.appending(path: "bridge-js.d.ts")
let tsconfigPath = context.package.directoryURL.appending(path: "tsconfig.json")
var arguments: [String] = [
"generate",
"--module-name",
target.name,
"--target-dir",
target.directoryURL.path,
"--output-dir",
context.pluginWorkDirectoryURL.path,
"--always-write", "true",
]
if FileManager.default.fileExists(atPath: inputTSFile.path) {
inputFiles.append(contentsOf: [inputTSFile, tsconfigPath])
arguments.append(contentsOf: [
"--project",
tsconfigPath.path,
])
}
let allSwiftFiles = inputSwiftFiles + pluginGeneratedSwiftFiles
arguments.append(contentsOf: allSwiftFiles.map(\.path))
return .buildCommand(
displayName: "Generate BridgeJS code",
executable: try context.tool(named: "BridgeJSTool").url,
arguments: arguments,
inputFiles: inputFiles,
outputFiles: [outputSwiftPath]
)
}
}
#endif