Code Monkey home page Code Monkey logo

Comments (4)

kwridan avatar kwridan commented on June 21, 2024 1

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]
}

from xcodeproj.

kwridan avatar kwridan commented on June 21, 2024

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.

https://github.com/yonaskolb/XcodeGen/blob/5a34c489e16eed6b7d43ee0064b3525b0d11f1c0/Sources/XcodeGenKit/PBXProjGenerator.swift#L1192

https://github.com/tuist/tuist/blob/4fdcf9d2b75fc16a4426a3f9dd7f30ca9d0dc3b6/Sources/TuistGenerator/Generator/BuildPhaseGenerator.swift#L541

Hope this helps

from xcodeproj.

swwol avatar swwol commented on June 21, 2024

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]
}

from xcodeproj.

swwol avatar swwol commented on June 21, 2024

Thank you so much! Just tried that and its working perfectly now

from xcodeproj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.