Swift packages created by Xcode (File > New > Swift Package
) show up in the root of the project or the workspace. I have a number of packages under development in my project, so after creating a few swift packages, I end up with a workspace root that looks like this
Root(Workspace)
- SwiftPackage1
- SwiftPackage2
...
- SwiftPackage18
- Project1
- Project2
I would like to be able to store all my swift packages in a location that doesn’t pollute the root of the workspace, something like this:
Root(Workspace)
- Libs // Collapsable!
- SwiftPackage1
- SwiftPackage2
...
- SwiftPackage18
- Project1
- Project2
However, after moving the package folder to another location (drag & dropping in Xcode), the folder is copied to the new location, but it’s a plain folder – and the original package is still under the root.
Is there a way to move Swift Package away from the root to keep the workspace organized?
2
Answers
After spending hours, I figured out a solution. The key is to create the Swift Package from the command line directly under the
Libs
folder.If you are creating a brand new Swift Package, thats' all you have to do.
In my case I had to restore the content of all my packages using this technique. In case that's useful, these are the steps I used (Note: this is with Xcode12):
cd
to the filesystem folder that matches theLib
folder under the workspace's rootLib
) so Xcode will acknowledge the resourceLib
folder (This will overwrite the folders created in step #3 - and that's OK)Lib
...6 steps to just relocate a folder is pretty clunky. I would welcome any suggestions to improve!
The quickest and easiest way to do this is:
That’s it!
Here’s an example in more detail, of moving a Swift Package called
UserInterfaces
from the root of a git repository (alongside the.xcworkspace
directory) into a sub-directory calledModules/
.In Terminal, from the repository root directory:
git mv UserInterfaces ./Modules
In Xcode, select the package in the Project Navigator on the left. The package name will be red because the package is referenced from the workspace but Xcode can’t find it at the referenced location.
This method can be used to re-locate any files or directories in an Xcode project or workspace, and is often much quicker and easier than using the UI.