From 8135771fc0852cdea4361674d964d1c2e048db05 Mon Sep 17 00:00:00 2001 From: david-swift Date: Wed, 3 Apr 2024 05:43:27 +0200 Subject: [PATCH] Remove deprecated Swift macro --- Package.swift | 25 +-------- README.md | 25 --------- Sources/Localized/Localized.swift | 13 ----- Sources/LocalizedMacros/LocalizedMacro.swift | 51 ------------------- Sources/LocalizedMacros/LocalizedPlugin.swift | 20 -------- Tests/MacroTests/Tests.swift | 43 ---------------- 6 files changed, 2 insertions(+), 175 deletions(-) delete mode 100644 Sources/Localized/Localized.swift delete mode 100644 Sources/LocalizedMacros/LocalizedMacro.swift delete mode 100644 Sources/LocalizedMacros/LocalizedPlugin.swift delete mode 100644 Tests/MacroTests/Tests.swift diff --git a/Package.swift b/Package.swift index 5c0869d..30078a8 100644 --- a/Package.swift +++ b/Package.swift @@ -24,9 +24,7 @@ let package = Package( ) ], dependencies: [ - .package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"), - .package(url: "https://github.com/jpsim/Yams", from: "5.0.6"), - .package(url: "https://github.com/stackotter/swift-macro-toolkit", from: "0.3.1") + .package(url: "https://github.com/jpsim/Yams", from: "5.0.6") ], targets: [ .target( @@ -41,15 +39,6 @@ let package = Package( "GenerationLibrary" ] ), - .macro( - name: "LocalizedMacros", - dependencies: [ - .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), - .product(name: "MacroToolkit", package: "swift-macro-toolkit"), - .product(name: "SwiftCompilerPlugin", package: "swift-syntax"), - "GenerationLibrary" - ] - ), .plugin( name: "GenerateLocalized", capability: .buildTool(), @@ -58,17 +47,7 @@ let package = Package( ] ), .target( - name: "Localized", - dependencies: [ - "LocalizedMacros" - ] - ), - .executableTarget( - name: "MacroTests", - dependencies: [ - "Localized" - ], - path: "Tests/MacroTests" + name: "Localized" ), .executableTarget( name: "PluginTests", diff --git a/README.md b/README.md index c26ec33..139d331 100644 --- a/README.md +++ b/README.md @@ -99,31 +99,6 @@ to the target in the `Package.swift` file. ) ``` -
- - Use the Swift macro alternatively - -If you don't want to have a separate `Localized.yml` resource, you can use the -YML syntax directly in your Swift code using a Swift macro. -Leave out the `resources` and `plugins` lines in the target definition, and -instead of creating a `Localized.yml` file, use the following macro in a Swift file. - -```swift -#localized(default: "en", yml: """ -export: - en: Export Document - de: Exportiere das Dokument - -send(message, name): - en: Send (message) to (name). - de: Sende (message) to (name). -""") -``` - -You cannot have a `defaultLanguage` set in the YML, instead, use the macro parameter. - -
- ### Usage In most cases, you want to get the translated string in the system language. diff --git a/Sources/Localized/Localized.swift b/Sources/Localized/Localized.swift deleted file mode 100644 index 2ceb04b..0000000 --- a/Sources/Localized/Localized.swift +++ /dev/null @@ -1,13 +0,0 @@ -// -// Localized.swift -// Localized -// -// Created by david-swift on 27.02.2024. -// - -/// A macro that takes the YML syntax and converts it into enumerations. -@freestanding(declaration, names: named(Localized), named(Loc)) -public macro localized(default defaultLanguage: String, yml: String) = #externalMacro( - module: "LocalizedMacros", - type: "LocalizedMacro" -) diff --git a/Sources/LocalizedMacros/LocalizedMacro.swift b/Sources/LocalizedMacros/LocalizedMacro.swift deleted file mode 100644 index 941dbaf..0000000 --- a/Sources/LocalizedMacros/LocalizedMacro.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// LocalizedMacro.swift -// Localized -// -// Created by david-swift on 27.02.2024. -// - -import GenerationLibrary -import MacroToolkit -import SwiftSyntax -import SwiftSyntaxMacros - -/// Implementation of the `localized` macro, which takes YML -/// as a string and converts it into two enumerations. -/// Access a specific language using `Localized.key.language`, or use `Localized.key.string` -/// which automatically uses the system language on Linux, macOS and Windows. -/// Use `Loc.key` for a quick access to the automatically localized value. -public struct LocalizedMacro: DeclarationMacro { - - /// The errors the expansion can throw. - public enum LocalizedError: Error { - - /// The string literal syntax is invalid. - case invalidStringLiteral - /// The default language syntax is invalid. - case invalidDefaultLanguage - - } - - /// Expand the `localized` macro. - /// - Parameters: - /// - node: Information about the macro call. - /// - context: The expansion context. - /// - Returns: The enumerations `Localized` and `Loc`. - public static func expansion( - of node: some SwiftSyntax.FreestandingMacroExpansionSyntax, - in context: some SwiftSyntaxMacros.MacroExpansionContext - ) throws -> [SwiftSyntax.DeclSyntax] { - guard let `default` = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self), - let defaultLanguage = StringLiteral(`default`).value?.description else { - throw LocalizedError.invalidDefaultLanguage - } - guard let syntax = node.argumentList.last?.expression.as(StringLiteralExprSyntax.self), - var yml = StringLiteral(syntax).value else { - throw LocalizedError.invalidStringLiteral - } - yml.append("\n\ndefault: \"\(defaultLanguage)\"") - return try Generation.getCode(yml: yml).map { "\(raw: $0)" } - } - -} diff --git a/Sources/LocalizedMacros/LocalizedPlugin.swift b/Sources/LocalizedMacros/LocalizedPlugin.swift deleted file mode 100644 index b286b13..0000000 --- a/Sources/LocalizedMacros/LocalizedPlugin.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// LocalizedPlugin.swift -// Localized -// -// Created by david-swift on 27.02.2024. -// - -import SwiftCompilerPlugin -import SwiftSyntaxMacros - -/// The compiler plugin offering the `localized` macro. -@main -struct LocalizedPlugin: CompilerPlugin { - - /// The macros. - let providingMacros: [Macro.Type] = [ - LocalizedMacro.self - ] - -} diff --git a/Tests/MacroTests/Tests.swift b/Tests/MacroTests/Tests.swift deleted file mode 100644 index d0e6a37..0000000 --- a/Tests/MacroTests/Tests.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// Tests.swift -// Localized -// -// Created by david-swift on 27.02.2024. -// - -import Foundation -import Localized - -#localized(default: "en", yml: """ -hello(name): - en: Hello, (name)! - de: Hallo, (name)! - fr: Salut, (name)! - -house: - en: House - de: Haus - fr: Maison - -helloPair(name1, name2): - en: Hello, (name1) and (name2)! - de: Hallo, (name1) und (name2)! - fr: Salut, (name1) et (name2)! -""") - -/// Test cases for the `localized` macro. -@main -enum Tests { - - /// Test the `localized` macro. - static func main() { - print("EN: \(Localized.hello(name: "Peter").en)") - print("DE: \(Localized.hello(name: "Ruedi").de)") - print("SYSTEM: \(Loc.hello(name: "Sams"))") - print("FR: \(Localized.house.fr)") - print("DE_CH: \(Localized.house.string(for: "de_CH"))") - print("SYSTEM: \(Localized.house.string)") - print("EN: \(Localized.helloPair(name1: "Max", name2: "Ruedi").en)") - } - -}