28 lines
887 B
Swift
28 lines
887 B
Swift
import SwiftSyntax
|
|
import SwiftSyntaxMacros
|
|
import SwiftCompilerPlugin
|
|
|
|
public struct ReadEnv: ExpressionMacro {
|
|
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
|
|
guard let argument = node.arguments.first?.expression,
|
|
let segments = argument.as(StringLiteralExprSyntax.self)?.segments,
|
|
segments.count == 1,
|
|
case .stringSegment(let literalSegment)? = segments.first
|
|
else {
|
|
throw CustomError.message("Need a static string")
|
|
}
|
|
|
|
let name = literalSegment.content.text
|
|
return """
|
|
ProcessInfo.processInfo.environment["\(raw: name)"]
|
|
"""
|
|
}
|
|
}
|
|
|
|
enum CustomError: Error { case message(String) }
|
|
|
|
@main
|
|
struct NahiMacros: CompilerPlugin {
|
|
var providingMacros: [Macro.Type] = [ReadEnv.self]
|
|
}
|