Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger Xcode's 'Update to Latest Package Versions' from command line?

Goal:

I want to programmatically update the versions of Swift Packages consumed by a sample project.

Problem:

Xcode > 11 offers a menu option:

File > Swift Packages > Update to Latest Package Versions

This will update the Package.resolved file to point to a specific revision.

This file is located at:

MyProject.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

How can I trigger this action from the command line for a CI build?

like image 576
Joe Susnick Avatar asked Nov 07 '19 18:11

Joe Susnick


2 Answers

Try out xcodebuild -resolvePackageDependencies

like image 157
joshbillions Avatar answered Nov 15 '22 22:11

joshbillions


xcodebuild -resolvePackageDependencies does not seem to work reliably. It will often stubbornly refuse to update to the latest version, particularly when an SPM dependency is pointed to a branch rather than a version tag. Obviously Xcode is caching this information somewhere, but I could not figure out exactly which file it is. Even nuking the SourcePackages folder in the derived data did not do the trick.

The only way I have found to do this reliably is to nuke the entire DerivedData subfolder for the Xcode project.

derived=$(xcodebuild -showBuildSettings | grep -m 1 BUILD_DIR | grep -oE "\/.*" | sed 's|/Build/Products||')
# The above will return a folder with a name like /Users/you/Library/Developer/Xcode/DerivedData/MyProject-ehdkocavaenextdcrsaszjdmyssx
rm -rf "$derived"
xcodebuild -resolvePackageDependencies

It is completely safe to delete the DerivedData subfolder for a project. Xcode will simply regenerate it on demand.

like image 5
Gregory Higley Avatar answered Nov 15 '22 23:11

Gregory Higley