migrate to .env for tokens and implement chats
This commit is contained in:
27
Sources/NahiMacros/ReadEnv.swift
Normal file
27
Sources/NahiMacros/ReadEnv.swift
Normal file
@@ -0,0 +1,27 @@
|
||||
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]
|
||||
}
|
Reference in New Issue
Block a user