Xcode 15 - generating assets for use in internal SPM modules

1.5k views Asked by At

Currently in our project we have an internal SPM module that contains our asset catalog and an Image and Color extension to provide easy access to the different colors using static vars instead of strings.

With Xcode 15 it now generates those static vars for us automagically.

So now instead of having to define something like...

extension Color {
  public static let fogmeisterRed: Color = Color("FogmeisterRed", bundle: .module)
}

You get that for free.

However, that only seems to work if the asset catalog is inside the target and has target membership to the target being built. When our asset catalog was inside the SPM module it did not (and could not) have target membership.

I've moved the asset catalog out to a shared folder now and given it target membership and now the static vars are generated. Except they're not accessible from inside the SPM modules.

So now I can do Color(.fogmeisterRed) in the target. But in the SPM modules that doesn't exist.

Is there a way to get this to work together nicely so that I can access the auto generated colors from our internal SPM modules?

1

There are 1 answers

1
Statistics Tutorial On

In your project, you're using an internal SPM module containing an asset catalog and extensions for easy color access. With Xcode 15's automatic static variable generation for asset catalogs, you're facing issues accessing these variables from inside the SPM modules. Below is a potential solution and workaround for this issue.

Solution:

  1. Use a Resource Bundle:
    • Create a separate bundle for resources in your SPM package.
    • Move the asset catalog to this resource bundle.
    • Reference this resource bundle from both your SPM modules and your application.

Steps:

  1. Modify your Package.swift file:

    • Define a resource bundle by adding a .resources parameter to the .target method.
    .target(
        name: "YourSPMModule",
        resources: [.process("Resources")]
    )
    
    • Place your asset catalog inside a folder named "Resources" at the root of your target's directory.
  2. Accessing Colors in your Code:

    • When accessing colors or images from the asset catalog, specify the bundle.
    extension Color {
        public static let fogmeisterRed: Color = 
            Color("FogmeisterRed", bundle: Bundle.module)
    }
    
    • Bundle.module will automatically refer to the resource bundle in the SPM package.

Additional Considerations:

  • Ensure the resource bundle is included in the build target of your application and any other targets that need to access the asset catalog.
  • This solution may affect the file organization and build settings of your project.

Workaround:

If the above solution does not work for your setup, a manual workaround is to continue creating the static variables as before:

extension Color {
    public static let fogmeisterRed: Color = 
        Color("FogmeisterRed", bundle: Bundle.module)
}

This involves more manual work but ensures the colors are accessible from both your application and your SPM modules.