- 
                Notifications
    
You must be signed in to change notification settings  - Fork 207
 
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
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
          
            22 changes: 22 additions & 0 deletions
          
          22 
        
  Sources/FoundationInternationalization/CurrentBundle.swift
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,22 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| 
     | 
||
| #if FOUNDATION_FRAMEWORK | ||
| /// Returns the bundle most likely to contain resources for the calling code. | ||
| /// | ||
| /// Code in an app, app extension, framework, etc. will return the bundle associated with that target. | ||
| /// Code in a Swift Package target will return the resource bundle associated with that target. | ||
| @available(macOS 10.0, iOS 2.0, tvOS 9.0, watchOS 2.0, *) | ||
| @freestanding(expression) | ||
| public macro bundle() -> Bundle = #externalMacro(module: "FoundationMacros", type: "BundleMacro") | ||
| #endif | ||
| 
     | 
||
  
    
      This file contains hidden or 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,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 | ||
| }() | ||
| """ | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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,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 | ||
| }() | ||
| """ | ||
| ) | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tantalum73 @jmschonfeld should this go somewhere other than
FoundationInternationalizationgiven that bundles are not just for internationalization? For example someone could easily use this to lookup an asset or some other resource.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now,
Bundledoes not exist inFoundationInternationalizationand this is just aFOUNDATION_FRAMEWORKpiece that will be picked up by theFoundation.frameworkbuild. For this to be fully usable on non-Darwin, thismacrowould currently need to be declared in theFoundationmodule in the swift-corelibs-foundation repo as well / separately (which we can do next). Eventually, if we sinkBundledown to swift-foundation this would remove theFOUNDATION_FRAMEWORKifdef and would go alongside whereverBundleis defined, but given thatBundledoes not currently exist in swift-foundation I'm ok leaving this here for now as just a piece for the Foundation.framework build to pickup and moving it whenever we decide whereBundlewill go laterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same question as @matthewseaman when I read this. IMO
Bundlewill very likely be in FoundationEssentials if/when we move it, so we'll likely move this there, though like Jeremy said it doesn't really matter now for swift-foundation when building as a packageThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Removed the declaration and now only the implementation in FoundationMacros remains.