-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Keep log file in root of volume for controlling installed versions. * Add several new resources * Integrate macros. Fix test plan.
- Loading branch information
Showing
12 changed files
with
189 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
import CompilerPluginSupport | ||
|
||
let package = Package( | ||
name: "EmpusaMacros", | ||
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)], | ||
products: [ | ||
// Products define the executables and libraries a package produces, making them visible to other packages. | ||
.library( | ||
name: "EmpusaMacros", | ||
targets: ["EmpusaMacros"] | ||
), | ||
], | ||
dependencies: [ | ||
// Depend on the Swift 5.9 release of SwiftSyntax | ||
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package, defining a module or a test suite. | ||
// Targets can depend on other targets in this package and products from dependencies. | ||
// Macro implementation that performs the source transformation of a macro. | ||
.macro( | ||
name: "EmpusaMacrosImpl", | ||
dependencies: [ | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
] | ||
), | ||
|
||
// Library that exposes a macro as part of its API, which is used in client programs. | ||
.target(name: "EmpusaMacros", dependencies: ["EmpusaMacrosImpl"]), | ||
|
||
// A test target used to develop the macro implementation. | ||
.testTarget( | ||
name: "EmpusaMacrosTests", | ||
dependencies: [ | ||
"EmpusaMacrosImpl", | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), | ||
] | ||
), | ||
] | ||
) |
4 changes: 4 additions & 0 deletions
4
Packages/EmpusaMacros/Sources/EmpusaMacros/EmpusaMacros.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Foundation | ||
|
||
@freestanding(expression) | ||
public macro URL(_ stringLiteral: String) -> URL = #externalMacro(module: "EmpusaMacrosImpl", type: "URLMacro") |
11 changes: 11 additions & 0 deletions
11
Packages/EmpusaMacros/Sources/EmpusaMacrosImpl/EmpusaMacrosPlugin.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
struct EmpusaMacrosPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
URLMacro.self | ||
] | ||
} |
41 changes: 41 additions & 0 deletions
41
Packages/EmpusaMacros/Sources/EmpusaMacrosImpl/URLMacro.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
enum URLMacroError: Error, CustomStringConvertible { | ||
case requiresStaticStringLiteral | ||
case malformedURL(urlString: String) | ||
|
||
var description: String { | ||
switch self { | ||
case .requiresStaticStringLiteral: | ||
return "#URL requires a static string literal" | ||
case .malformedURL(let urlString): | ||
return "The input URL is malformed: \(urlString)" | ||
} | ||
} | ||
} | ||
|
||
public struct URLMacro: ExpressionMacro { | ||
public static func expansion( | ||
of node: some FreestandingMacroExpansionSyntax, | ||
in context: some MacroExpansionContext | ||
) throws -> ExprSyntax { | ||
guard | ||
let argument = node.argumentList.first?.expression, | ||
let segments = argument.as(StringLiteralExprSyntax.self)?.segments, | ||
segments.count == 1, | ||
case .stringSegment(let literalSegment)? = segments.first | ||
else { | ||
throw URLMacroError.requiresStaticStringLiteral | ||
} | ||
|
||
guard let _ = URL(string: literalSegment.content.text) else { | ||
throw URLMacroError.malformedURL(urlString: "\(argument)") | ||
} | ||
|
||
return "URL(string: \(argument))!" | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Packages/EmpusaMacros/Tests/EmpusaMacrosTests/EmpusaMacrosTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import XCTest | ||
|
||
// Macro implementations build for the host, so the corresponding module is not available when cross-compiling. Cross-compiled tests may still make use of the macro itself in end-to-end tests. | ||
#if canImport(EmpusaMacrosMacros) | ||
import EmpusaMacrosMacros | ||
|
||
let testMacros: [String: Macro.Type] = [ | ||
"stringify": StringifyMacro.self, | ||
] | ||
#endif | ||
|
||
final class EmpusaMacrosTests: XCTestCase { | ||
func testMacro() throws { | ||
#if canImport(EmpusaMacrosMacros) | ||
assertMacroExpansion( | ||
""" | ||
#stringify(a + b) | ||
""", | ||
expandedSource: """ | ||
(a + b, "a + b") | ||
""", | ||
macros: testMacros | ||
) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
#endif | ||
} | ||
|
||
func testMacroWithStringLiteral() throws { | ||
#if canImport(EmpusaMacrosMacros) | ||
assertMacroExpansion( | ||
#""" | ||
#stringify("Hello, \(name)") | ||
"""#, | ||
expandedSource: #""" | ||
("Hello, \(name)", #""Hello, \(name)""#) | ||
"""#, | ||
macros: testMacros | ||
) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
#endif | ||
} | ||
} |
File renamed without changes.