-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAppleMacros.swift
More file actions
42 lines (36 loc) · 1.43 KB
/
OpenAppleMacros.swift
File metadata and controls
42 lines (36 loc) · 1.43 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
@_spi(PluginMessage) import SwiftCompilerPluginMessageHandling
import OpenAppleMacrosBase
@main enum Server {
static func main() throws {
let connection = try StandardIOMessageConnection()
let listener = CompilerPluginMessageListener(
connection: connection,
messageHandler: PluginProviderMessageHandler(provider: Provider())
)
listener.main()
}
}
private struct Provider: PluginProvider {
private let macrosByName: [String: Macro.Type]
private let modules: Set<String>
init() {
let macros = allMacros.flatMap { $0 }
macrosByName = Dictionary(macros.map { (String(reflecting: $0), $0) }) { $1 }
modules = Set(macrosByName.keys.compactMap { $0.split(separator: ".", maxSplits: 1).first }.map { String($0) })
}
var features: [PluginFeature] {
[.loadPluginLibrary]
}
func loadPluginLibrary(libraryPath: String, moduleName: String) throws {
guard modules.contains(moduleName) else {
throw MacroError("OpenAppleMacros: Could not find macros for module '\(moduleName)'")
}
}
func resolveMacro(moduleName: String, typeName: String) throws -> Macro.Type {
let key = "\(moduleName).\(typeName)"
guard let macro = macrosByName[key] else {
throw MacroError("OpenAppleMacros: Could not find macro '\(typeName)' in module '\(moduleName)'")
}
return macro
}
}