Publish Local Package from Xcode as Package
Starting with Xcode11, you can create and use
Swift Package Manager packages on Xcode. We can use the published package as a library, or you can modularize a part of the project code as a
Local Package by separating it from the project. When developing, I also cut out common extensions and components into a
Local Package in consideration of code organization and reusability. However, as it is, when creating a new application, I have to copy the code, so I uploaded the
Local Package to the project on GitHub and published it as a package, so I summarized the procedure. In addition, it became possible to operate Git such as Commit and Push from the project of Xcode, and this was the first time I operated it, but it was easier to operate than I expected, so this Git operation uses Xcode.
- Mac: Big Sur v11.3.1
- Xcode: 12.5
Publish Local Package
First, create a
Local Package in your project. In my case, I created four Local Packages as shown below. It is a collection of common components, extensions, network processing and Utils.
SwiftComponents depends on
SwiftExtension, so publish only those that don’t depend on that package either.
As a preparation, copy the relevant
Local Package and move it to another directory. And if you click
Package.swift in it, you can open only that package in Xcode. Before working with Git, let’s build once to see if the build works! Also, if you want to include information about the package in the README, write it to README! (I didn’t write it because it’s my own package lol) Once the build is successful, I will finally upload the package to GitHub. Select Source Control → New Git Repositories from the top menu.
Then create it as it is. Then the existing implementation will be committed as an
Initial Commit as follows:
To see the commit history in Xcode, tap the second icon from the left in the menu on the left side of Xcode, then select the package name.
Next, set the tag. In order for the published package to be available, you need to set a tag like
1.0.0. Open the menu at the package name and select
Tag" main" as shown in the capture below.
Then create a tag by setting, for example,
1.0.0 as shown below.
Then add the remote repository. This time we will create a new repository. If you are not logged in to GitHub, log in to GitHub. In my case, I logged in using my GitHub ID and
GitHub Personal Access Token. You can check how to issue tokens here. After that, describe the repository name and details of the repository. This time, we will make it a package that anyone can view and use, so set
Public and create it.
And finally push to the remote repository. Select Source Control → Push from the menu at the top of Xcode. When the following screen is displayed, check
Include tags and push.
Now you can publish your
Local Package on GitHub and use it from any app. Remove the package from the app that uses the existing Local Package, and add the
Remote Package using the URL of the newly created package. In my case, SwiftUtils etc. depend on SwiftExtensions added above, so I will replace the existing
Local Package with the newly created
Remote Package. Originally it is the following
And Replace it with the URL of the
Remote Package as shown below.
Now you can publish this package as well by doing the same thing as above. The final
Package.swift for SwiftUtils looks like this:
This time, I published the locally created package on GitHub so that it can be used in common by other apps. It is convenient to share extensions and components that are often used in personal development. As an actual usage, I think that we will continue development as a
Local Package at first, and later use it as a
Remote Package when the implementation settles down. If you put all the code and processing together in one project, the dependencies may be messed up, or it may be difficult to reuse some processing when you want to use it in another application. At that time, if you cut out as a module for each function or component, the visibility and reusability of the code will be improved. This
Swift Package Manager is genuine Apple and officially supported by Xcode, so it is easy to use, and it is expected that more functions will be added in the future, so I think it is very useful for modularizing some functions.