I'm trying to learn SPM by making a simple standalone Swift Package with Xcode 11. So far so good
I'm now trying to add a Build Phase script to execute SwiftLint when I run my Package's unit-tests locally in Xcode.
While reading the SPM documentation, i found that many of the Xcode concepts I'm familiar with can be described in the Package.swift manifest (e.g. targets, products, build-settings, build-configurations...).
I found nothing about Schemes in the documentation, but I noticed that Xcode automatically creates a hidden .swiftpm directory which can then contain my Schemes .
But I still haven't found an easy way to run a Build-Phase script, like in my other Xcode projects.
I tried creating a dummy Xcode project inside my Package's root directory, which would then reference the Package itself and add a Build-Phase script here. But with this kind of circular reference, it feels like I'm doing it wrong. Also my dummy project would get included in every App that depends on my Package.
I also thought about creating a dummy project inside another Git repository that then depends on my Package. But having to create 2 repositories for every Package also feels wrong.
Some tutorials suggest running scripts as a Git pre-commit hook, or maybe as a Scheme pre-action, but I would lose the nice integration of SwiftLint with Xcode's source code editor.
Has anyone found an elegant solution to this problem? Ideally a solution that would also work nicely if I decide to add my Package to a CI system. Am I missing something obvious?
(Disclaimer: This information relates to Xcode 11. I havenât tried any of this in the Xcode 12 betas to know if any of it is out of date.)
The Xcode project will be completely ignored by any package clients.
SwiftPM has no equivalent of custom schemes. There is --configuration debug and --configuration release and that is all. (Well, you can add stuff to .swiftpm for some development time tweaks, but clients will not inherit it and you cannot inject build steps.)
External scripts are not supported. The feature you would need to do this ârightâ (known usually as âextensible build toolsâ been much talked about but not implemented so far.
What I do for static analysis tools providing inline warnings is to script swift package generate-xcodeproj and the insertion of a build phase. Then I quickly run that when I want to find a bunch of warnings quickly. But I donât keep the Xcode project around all the time.
If you have CI, just add it as a separate step outside of swift test.
Since SwiftLint is itself a package, an alternative workaround would be to add it as a dependency of your test target. Then you can use #file or other shenanigans to locate the produced executable and launch it with Process inside the tests. (But to be honest I donât think static analysis belongs inside unit tests in the first place, although that is just my opinion.)
Thanks for your help Jeremy.
Can you please explain how you script "the insertion of a build phase"?
I assume I will have to edit the .pbxproj file generated by swift package generate-xcodeproj.
Is there a command line tool that can help me do that, or some reference documentation for pbxproj files?
My own use case is simple and always the same, so it is hardcoded into a tool that also does a lot of other things. You could reverse engineer it from here. As a clumsy way of âjust getting it workingâ, you could clone the linked package, replace these lines with your own bash script, then build and run that package with this:
swift run workspace refresh xcode â˘project /path/to/your/own/package
Iâm pretty sure that would get you what you want. You could then cut out all the stuff you donât care about, convert it into a standalone shell script or change it however else you want.
No, there is still no way to add a script build phase to a package.
However, the script you posted does not require Xcode to have any knowledge of the project whatsoever. You can create an empty Xcode project and add that build phase to an empty target. Xcode will still parse the error messages and display them inline, even if no reference to the relevant file exists in Xcodeâs file navigator.
I have not tried it. A preâaction belongs to a scheme though, not a build, so my instinct is that it would probably work. However, because it is disassociated from the build, it would be easy to circumvent by accident.
I tried it as both in the run pre-actions, and the build pre-actions, and the result was nothing. It might be that errors/ outputs of the swiftlint running in the scheme don't do anything (because its not integrated well with Xcode warnings/ errors)
Yeah, I did not really expect Xcode to parse the warnings. But I thought it would still halt the build if the script exited with an error code. Does that not even happen? (I mostly ask because other readers might only be trying to do things for which the output does not matter.)
However, there are some circumstances that will cause it to abort early, normally if a can't progress any further. Not finding the build script file might be one of them.