XcodeGen icon indicating copy to clipboard operation
XcodeGen copied to clipboard

Re-Order "Copy Bundle Resources" before "Compile Sources"

Open ck2shine opened this issue 4 years ago • 1 comments

In order to execute third party framework I had to move "Copy Bundle Resources" before "Compile Sources" in "Build Phases".

截圖 2021-03-25 下午5 09 45

Is there any way to change Build Phases order of those two setting in the project.yml file?

ck2shine avatar Mar 25 '21 09:03 ck2shine

I do face the same problem. I also want to move the Copy bundle Resources phase before the Compile Sources. I do not see a way of doing it. I now need to patch in a custom script in thepostGenCommand command.

There are multiple possible solutions:

  1. Always place the Copy bundle Resources before Compile Sources (but this is non default for an new Xcode project.
  2. Add a property to a target where you can toggle if you want this behaviour. (something like:
Target.shouldCopyResourcesBeforeSourceCompile 

And here -> https://github.com/yonaskolb/XcodeGen/blob/master/Sources/XcodeGenKit/PBXProjGenerator.swift#L1080 the following code:

  func addresourcesBuildPhase() {
            let resourcesBuildPhaseFiles = getBuildFilesForPhase(.resources) + copyResourcesReferences
            if !resourcesBuildPhaseFiles.isEmpty {
                let resourcesBuildPhase = addObject(PBXResourcesBuildPhase(files: resourcesBuildPhaseFiles))
                buildPhases.append(resourcesBuildPhase)
            }
        }

        if target.shouldCopyResourcesBeforeSourceCompile {
            addresourcesBuildPhase()
        }

        let sourcesBuildPhaseFiles = getBuildFilesForPhase(.sources)
        let shouldSkipSourcesBuildPhase = sourcesBuildPhaseFiles.isEmpty && target.type.canSkipCompileSourcesBuildPhase
        if !shouldSkipSourcesBuildPhase {
            let sourcesBuildPhase = addObject(PBXSourcesBuildPhase(files: sourcesBuildPhaseFiles))
            buildPhases.append(sourcesBuildPhase)
        }

        buildPhases += try target.postCompileScripts.map { try generateBuildScript(targetName: target.name, buildScript: $0) }

        if !target.shouldCopyResourcesBeforeSourceCompile {
            addresourcesBuildPhase()
        }
  1. Add an property to a target to manually order all build phases. (The PBXProjGenerator.swift needs some refactoring to support his. Because the data for an phase is now coupled to the order.
  2. Add an property to a target to order all build phases and filter the phases at the end to have your wanted ordering.
  3. Add support to place files before the compile sources phase with the buildPhase.copyFiles option.

@yonaskolb witch direction do you suggest?

mat1th avatar Sep 22 '21 13:09 mat1th

We're facing the same issue with our app. @yonaskolb any thoughts on this?

teameh avatar Mar 01 '23 07:03 teameh

So I understand the problem better, how is this failing? What step or framework has this requirement and why does it break?

yonaskolb avatar Mar 02 '23 00:03 yonaskolb

In the project I'm using it it validates the assets during the build phase. Thus the assets should be already located in the app otherwise I'm not able to validate the assets.

mat1th avatar Mar 20 '23 20:03 mat1th

In the project I'm using it it validates the assets during the build phase. Thus the assets should be already located in the app otherwise I'm not able to validate the assets.

The compile sources step validates the resources, or how does it do that? Or is it a custom run script?

In any case maybe a property on target resourcesBeforeSourcesBuildPhase could be the way to go. A list of phase types for total order control might be powerful, but perhaps overkill for the complexity it would add

yonaskolb avatar Apr 04 '23 07:04 yonaskolb

Happy to accept a PR

yonaskolb avatar Apr 04 '23 07:04 yonaskolb

Indeed the validation is done in a custom build script.

Thank you for pointing me out for the direction to go. I've created a pr to add this to the project. See https://github.com/yonaskolb/XcodeGen/pull/1351

mat1th avatar Apr 21 '23 20:04 mat1th

This issue can now be closed because it is resolved by https://github.com/yonaskolb/XcodeGen/pull/1351 and is released in https://github.com/yonaskolb/XcodeGen/releases/tag/2.35.0

mat1th avatar May 05 '23 18:05 mat1th