mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-12-21 03:10:06 +01:00
62 lines
1.8 KiB
Swift
62 lines
1.8 KiB
Swift
import SwiftCompilerPlugin
|
|
import SwiftSyntax
|
|
import SwiftSyntaxBuilder
|
|
import SwiftSyntaxMacros
|
|
|
|
public struct ContainerAccessMacro: MemberMacro {
|
|
public static func expansion(
|
|
of node: AttributeSyntax,
|
|
providingMembersOf declaration: some DeclGroupSyntax,
|
|
in context: some MacroExpansionContext
|
|
) throws -> [DeclSyntax] {
|
|
// Map of ALL Container properties to their types
|
|
// This should be kept in sync with the Container class
|
|
let allContainerServices: [(name: String, type: String)] = [
|
|
("shell", "ShellProtocol"),
|
|
("filesystem", "FileSystemProtocol"),
|
|
("command", "CommandProtocol"),
|
|
("paths", "Paths"),
|
|
("phpEnvs", "PhpEnvironments"),
|
|
("favorites", "Favorites"),
|
|
("warningManager", "WarningManager")
|
|
]
|
|
|
|
// Check if the class already has an initializer
|
|
let hasExistingInit = declaration.memberBlock.members.contains { member in
|
|
if let initDecl = member.decl.as(InitializerDeclSyntax.self) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var members: [DeclSyntax] = []
|
|
|
|
// Add the container property
|
|
members.append(
|
|
"""
|
|
public let container: Container
|
|
"""
|
|
)
|
|
|
|
// Only add the initializer if one doesn't already exist
|
|
if !hasExistingInit {
|
|
members.append(
|
|
"""
|
|
init(_ container: Container) {
|
|
self.container = container
|
|
}
|
|
"""
|
|
)
|
|
}
|
|
|
|
return members
|
|
}
|
|
}
|
|
|
|
@main
|
|
struct ContainerMacroPlugin: CompilerPlugin {
|
|
let providingMacros: [Macro.Type] = [
|
|
ContainerAccessMacro.self,
|
|
]
|
|
}
|