mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
30 lines
1.0 KiB
Swift
30 lines
1.0 KiB
Swift
import SwiftSyntax
|
|
import SwiftSyntaxBuilder
|
|
import SwiftSyntaxMacros
|
|
|
|
#if os(macOS)
|
|
private let pathPrefix = "/private"
|
|
#else
|
|
private let pathPrefix = ""
|
|
#endif
|
|
|
|
enum TemporaryDirectory: BodyMacro {
|
|
static func expansion(of _: AttributeSyntax,
|
|
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
|
|
in context: some MacroExpansionContext) -> [CodeBlockItemSyntax] {
|
|
guard let body = declaration.body else {
|
|
context.diagnose(SwiftLintCoreMacroError.noBody.diagnose(at: declaration))
|
|
return []
|
|
}
|
|
return [
|
|
"""
|
|
let _currentDirectory = FileManager.default.currentDirectoryPath
|
|
FileManager.default.changeCurrentDirectoryPath(
|
|
"\(raw: pathPrefix)" + FileManager.default.temporaryDirectory.path
|
|
)
|
|
defer { FileManager.default.changeCurrentDirectoryPath(_currentDirectory) }
|
|
""",
|
|
] + Array(body.statements)
|
|
}
|
|
}
|