Xcodegen: Adding a sources entry for a non-existent path

Created on 17 Sep 2019  路  4Comments  路  Source: yonaskolb/XcodeGen

I have the following entry in my config file:

targetTemplates:
  DynamicFrameworkWithResources:
    platform: iOS
    type: framework
    sources: 
      - Sources
      - Resources

The particular issue is that the Resources directory for a project does not exist. The reason why it's included is that a generated (via a run script phase), git ignored file is supposed to be eventually placed there and copied into our bundle. It's the only file that gets copied. I worry this will become an even bigger issue down the road when we start utilizing code generation tools.

The hacky way I've worked around this for now is to create the directory and commit in a dummy, empty file. Unfortunately, it's

  1. A hack. I'd like to do things correctly.
  2. It gets included into the final build products unless I add an excluded file exception.

Are there proper ways to solve this?

Most helpful comment

Luckily, optional still solves the problem. It is a requirement that you list all of the output files that need to be included though. Here's the syntax I use in my own projects when using R.swift. Even though R.generated.swift doesn't exist when I run xcodegen, it will be included in the project structure, and then exist as a real file once Xcode builds the project.

    sources:
      - path: Resources/R.generated.swift
        optional: true

All 4 comments

Are you looking for optional?

https://github.com/yonaskolb/XcodeGen/blob/master/Docs/ProjectSpec.md#sources

It allows you to specify a directory or file, but _not_ check if it exists at project generation time.

targetTemplates:
  DynamicFrameworkWithResources:
    platform: iOS
    type: framework
    sources: 
      - Sources
      - path: Resources
        optional: true

I'm not sure that's it. There's an X+1 problem here. If you run Xcodegen from a fresh checkout, the generated file is not included in the list of files under Xcode's file browser. Thus, that initial build will not bundle the generated file. It'll take a build and a subsequent run of Xcodegen for that file to be picked up by Xcode. For most day to day development this mostly isn't a problem as this file will persist on disk between checkouts but ignored by git. However, it is problematic from a CI perspective as such builds are always from a clean checkout. So, a CI process would need to checkout -> run Xcodegen -> build -> Run Xcodegen again (to pick up this generated file) -> Build again.

Without Xcodegen, the process would normally involve just adding that file to the project. Fresh checkouts would have that file appear red (indicating not present), but that's ok as it will be generated before being packaged.

Would it be possible to parse through the list of output files/output files lists from run script build phases to determine if there are additional references that need to be made when generating the project?

Luckily, optional still solves the problem. It is a requirement that you list all of the output files that need to be included though. Here's the syntax I use in my own projects when using R.swift. Even though R.generated.swift doesn't exist when I run xcodegen, it will be included in the project structure, and then exist as a real file once Xcode builds the project.

    sources:
      - path: Resources/R.generated.swift
        optional: true

Thanks, this appeared to solve my issues!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohammdsss1 picture mohammdsss1  路  6Comments

lukewakeford picture lukewakeford  路  6Comments

brentleyjones picture brentleyjones  路  6Comments

pvinis picture pvinis  路  6Comments

ksulliva picture ksulliva  路  4Comments