Build SwiftPM with resources through CLI

Hi,

I've been trying to build SwiftPM package that I have through command line, but looks like the command line doesn't respect newly resources that was added in Swift 5.3. The sample project is a simple one, just included xcdatamodeld as .process like below:

let package = Package(
    name: "CoreDataSwiftPM",
    platforms: [.iOS(.v14), .macOS(.v10_12)],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "CoreDataSwiftPM",
            targets: ["CoreDataSwiftPM"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "CoreDataSwiftPM",
            dependencies: [],
            resources: [
                .process("Resources/Model.xcdatamodeld")
            ]),
        .testTarget(
            name: "CoreDataSwiftPMTests",
            dependencies: ["CoreDataSwiftPM"],
            resources: [
                .process("Resources/Model.xcdatamodeld")
            ]),
    ]
)

Using Xcode, everything works fine and I can see momd file in my generated bundle, However, building it in command line with swift bundle it just copies xcdatamodeld file into the bundle without properly compiling it.

Is it an expected behavior? Any workaround for it?

1 Like

As far as I know, this is expected behaviour. SPM doesn't support compiling resources like this, so you have to resort to using xcodebuild.

1 Like

Yep, it is correct that you need to use xcodebuild for Xcode specific resources such as CoreData.

2 Likes

@NeoNacho Thanks for the response, appreciate it. Just a follow up on that, is it safe to assume that I can rely on using ./swiftpm/Package.xcworkspace for now? Or is there any better way to use xcodebuild and SwiftPM structure?

You can simply run xcodebuild -scheme ... in the package's directory. Use xcodebuild -list to find eligible schemes.

The workspace in .swiftpm is a so-called wrapping workspace that shouldn't be used explicitly.

3 Likes

Thanks a lot @NeoNacho! I wasn't aware that xcodebuild can be used without defining -project or -workspace. It's great : )

1 Like