Closed swwol closed 1 year ago
I think the missing bit is adding the file reference and build file objects to the project.
project.add(object: fileReference)
project.add(object: buildFile)
XcodeGen and Tuist can be useful references as they both leverage XcodeProj.
Hope this helps
Thanks for this - those are helpful references. I am still having an issue however. The problem seems to be with the PBXFileReference. And it's not clear from those code excerpts how this should be created. Without adding the file reference I can create an empty buildphase, but as soon as I try to add the fileReference to the buildphase I get a corrupted Xcode project with the error message: "Exception: -[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x60009b278930".
I am creating the file reference and adding it to the project. I have also tried adding it to the mainGroup.children array as without this step it gets an identifier prefixed with 'TEMP'. However both with and without that step, I am getting the corrupted project issue. Current iteration of code below - any thoughts much appreciated!
func addAppClipEmbedPhase(pbxProj: PBXProj, target: PBXTarget) throws {
guard !target.buildPhases.contains(where: { $0.name() == "Embed App Clip" }) else {
throw ProjectEditError.error("Target already has a build phase called Embed App Clip")
}
let mainGroup = pbxProj.projects.first!.mainGroup
let embedAppClipsBuildPhase = PBXCopyFilesBuildPhase(
dstPath: "$(CONTENTS_FOLDER_PATH)/AppClips",
dstSubfolderSpec: .productsDirectory,
name: "Embed App Clip"
)
pbxProj.add(object: embedAppClipsBuildPhase)
target.buildPhases.append(embedAppClipsBuildPhase)
let fileReference = PBXFileReference(
sourceTree: .buildProductsDir,
explicitFileType: "wrapper.application",
path: "AppClip.app",
includeInIndex: false
)
pbxProj.add(object: fileReference)
mainGroup!.children.append(fileReference)
let buildFile = PBXBuildFile(file: fileReference, settings: ["ATTRIBUTES": "RemoveHeadersOnCopy"])
pbxProj.add(object: buildFile)
embedAppClipsBuildPhase.files = [buildFile]
}
I've tried out the snippet in a sample and hit the same error, looks like there's a small type error in the build file attributes, where it should be an array of string flags rather than a single string/
let buildFile = PBXBuildFile(file: fileReference, settings: ["ATTRIBUTES": ["RemoveHeadersOnCopy"]])
Note, if you are modifying a project that already has both targets within it, existing file references can be used rather than creating new ones:
func addAppClipEmbedPhase(pbxProj: PBXProj, appTarget: PBXTarget, appClipTarget: PBXTarget) throws {
guard !appTarget.buildPhases.contains(where: { $0.name() == "Embed App Clip" }) else {
throw ProjectEditError.error("Target already has a build phase called Embed App Clip")
}
let embedAppClipsBuildPhase = PBXCopyFilesBuildPhase(
dstPath: "$(CONTENTS_FOLDER_PATH)/AppClips",
dstSubfolderSpec: .productsDirectory,
name: "Embed App Clip"
)
pbxProj.add(object: embedAppClipsBuildPhase)
appTarget.buildPhases.append(embedAppClipsBuildPhase)
guard let appClipFileReference = appClipTarget.product else {
throw ProjectEditError.error("AppClip file reference is not available")
}
let buildFile = PBXBuildFile(file: appClipFileReference, settings: ["ATTRIBUTES": ["RemoveHeadersOnCopy"]])
pbxProj.add(object: buildFile)
embedAppClipsBuildPhase.files = [buildFile]
}
Thank you so much! Just tried that and its working perfectly now
I'm trying to write a script to embed an AppClip into a project. There are 2 steps.. Adding the AppClip as a dependency of the main app target, and adding a Copy Files build phase that embeds the AppClip.
Step 1 appears to be working ok, but step 2 - The Copy Files phase is created but doesn't have any content.
This is what I have so far:
I suspect I may be missing a step and the buildFile needs to be added somewhere else? But not sure where? Thanks!