Swiftgen: Adding Xcode build phase using SPM

Created on 14 Apr 2020  路  7Comments  路  Source: SwiftGen/SwiftGen

Hello 馃憢

I'm attempting to add SwiftGen using Swift Package Manager and following the integration instructions here: https://github.com/SwiftGen/SwiftGen/blob/master/Documentation/Articles/Xcode-Integration.md

I notice that there isn't documentation on how to access the build script from a SPM installation and I was wondering if there is a preferred path format for locating the installation and calling the script.

I've attempted to use "$SOURCE_ROOT/SwiftGen/bin/swiftgen" and am getting warnings about an invalid file location.

Thanks for your help!

question

Most helpful comment

For anyone interested, I'm currently using SwiftGen with SPM following this article

Couple of things I would like to add:

  1. swift package update is no longer necessary
  2. Automatic package resolution causes issues for some reason, command i'm currently using is
swift run --disable-automatic-resolution -c release swiftgen config run --config "$SRCROOT/Configs/.swiftgen.yml"
  1. Xcode input-ouput file system is very smart, and allows to not run a tool if files were not changed, but you need to add all .stencil files you are using, resource files used in generation and .swiftgen.yml config to input files, and all generated files in output files.
  2. SwiftGen from SPM is slightly slower than from CocoaPods, but not a big deal, when resource file change, it takes about 5-7 seconds to generate code. When they don't change, build phase ends in milliseconds.
  3. If you use templates from SwiftGen, you need to specify path manually(templatePath), it will not be able to automatically find it using templateName parameter. All templates are in BuildTools/.build/checkouts/SwiftGen/templates
  4. I'm using develop branch of SwiftGen, master branch and current release version do not seem to work.

All 7 comments

@IanHoar

From my previous experience with SPM, the previous syntax changed from:

swift run swiftgen

To the new one (and starting Xcode 11.4), you need to specify macOS: (it may not be exactly this, so I'll ask you to test):

/usr/bin/xcrun --sdk macosx swift run swiftgen

I thought SPM didn't support binary dependencies yet. (it's coming in 5.3)

How are you adding it to your project now? Is there something we should add to the documentation?

Maybe a question to @IanHoar , as I'm using Cocoapods for Swiftgen here.

I was just explaining how I call SPM compiled binaries on another projects since Xcode 11.4

For anyone interested, I'm currently using SwiftGen with SPM following this article

Couple of things I would like to add:

  1. swift package update is no longer necessary
  2. Automatic package resolution causes issues for some reason, command i'm currently using is
swift run --disable-automatic-resolution -c release swiftgen config run --config "$SRCROOT/Configs/.swiftgen.yml"
  1. Xcode input-ouput file system is very smart, and allows to not run a tool if files were not changed, but you need to add all .stencil files you are using, resource files used in generation and .swiftgen.yml config to input files, and all generated files in output files.
  2. SwiftGen from SPM is slightly slower than from CocoaPods, but not a big deal, when resource file change, it takes about 5-7 seconds to generate code. When they don't change, build phase ends in milliseconds.
  3. If you use templates from SwiftGen, you need to specify path manually(templatePath), it will not be able to automatically find it using templateName parameter. All templates are in BuildTools/.build/checkouts/SwiftGen/templates
  4. I'm using develop branch of SwiftGen, master branch and current release version do not seem to work.

@DenTelezhkin what issues does automatic package resolution cause? I was trying the --disable-automatic-resolution option in my GitHub Action that installs swift based tools (either downloads a cached version or builds it from source) and it made the build fail for SwiftLint at the latest version tag (0.40.3) on Linux (it didn't make any difference on macOS).

@Cyberbeni I don't remember exactly, but i think dependencies were not actually resolving without this flag on Xcode 11(super weird, but it is what it is).

BTW since then there were couple of adjustments in my flow, specifically:

I now use SwiftGen generation for input output files, that shipped in 6.4.0, and also 6.4.0 release instead of develop branch.

I've been working on switching our project structure to fully use SPM (see #793), it'll solve some of the workarounds that @DenTelezhkin mentioned. For example, step 5 won't be needed anymore, as SPM now supports resources (and we use it to our advantage).

The problem is that there's no easy solution to "add a build phase from SPM in your project", as SPM does not have this feature yet. SPM doesn't have the concept of build "tools" yet, and until it does we'll be stuck with these manual steps.

Was this page helpful?
0 / 5 - 0 ratings