Swift Packages: exclude developments resources/assets from package used in archived app

One workaround is to rely on environment variables in your Package.swift

You can then do something like this

// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import Foundation
import PackageDescription

let mocksEnabled = ProcessInfo.processInfo.environment["MOCKS"] != "NO"

let package = Package(
    name: "MyPackage",
    defaultLocalization: "en",
    dependencies: [
    ],
    targets: [
      // your targets
    ]
)


package.targets.forEach { target in
    guard target.type == .regular else { return }

    var settings = target.swiftSettings ?? []

    if mocksEnabled || target.type == .test {
        settings.append(.define("MOCKS"))
    } else {
        target.exclude.append("Mocks")
    }

    target.swiftSettings = settings
}

We are using this to include mocks by default
They are by convention required to be located in a folder named Mocks at the root of each target.

On CI we then define a env var MOCKS=NO to exclude them.

One caveat though when using Xcode is you need to have the environment variable set BEFORE you launch Xcode.

So when you have to quit xcode (command + Q) and then launch it with the env vars set

For instance MOCKS=NO xed .

But as i said this is more of a workaround and i would really like a proper way to do these configurations

2 Likes