Skip to content

Implementation of #bundle #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Sources/FoundationMacros/BundleMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxMacros

public struct BundleMacro: SwiftSyntaxMacros.ExpressionMacro, Sendable {
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
"""
{
#if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
return Bundle.module
#elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
#error("No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually.")
#else
return Bundle(_dsoHandle: #dsohandle) ?? .main
#endif
}()
"""
}
}
1 change: 1 addition & 0 deletions Sources/FoundationMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ target_compile_options(FoundationMacros PRIVATE -parse-as-library)

target_sources(FoundationMacros PRIVATE
FoundationMacros.swift
BundleMacro.swift
PredicateMacro.swift)

target_compile_options(FoundationMacros PRIVATE
Expand Down
6 changes: 5 additions & 1 deletion Sources/FoundationMacros/FoundationMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import SwiftCompilerPlugin

@main
struct FoundationMacros: CompilerPlugin {
var providingMacros: [Macro.Type] = [PredicateMacro.self, ExpressionMacro.self]
var providingMacros: [Macro.Type] = [
PredicateMacro.self,
ExpressionMacro.self,
BundleMacro.self
]
}

#endif
57 changes: 57 additions & 0 deletions Tests/FoundationMacrosTests/BundleMacroTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import XCTest
import FoundationMacros

final class BundleMacroTests: XCTestCase {

func testSimple() {
AssertMacroExpansion(
macros: ["bundle": BundleMacro.self],
"""
#bundle
""",
"""
{
#if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
return Bundle.module
#elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
#error("No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually.")
#else
return Bundle(_dsoHandle: #dsohandle) ?? .main
#endif
}()
"""
)
}

func testUsingParenthesis() {
AssertMacroExpansion(
macros: ["bundle": BundleMacro.self],
"""
#bundle()
""",
"""
{
#if SWIFT_MODULE_RESOURCE_BUNDLE_AVAILABLE
return Bundle.module
#elseif SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE
#error("No resource bundle is available for this module. If resources are included elsewhere, specify the bundle manually.")
#else
return Bundle(_dsoHandle: #dsohandle) ?? .main
#endif
}()
"""
)
}
}