iOS

Publish Local Package from Xcode as Package

xcode-publish-package-en

Publish Local Package from Xcode as Package

Xcode11からXcode上でSwift Package Managerのパッケージを作成・利用できるようになりました。公開されているパッケージをライブラリとして利用したり、プロジェクトのコードの一部をLocal Packageとしてプロジェクトから切り離してモジュール化することができます。 私も開発をする際に、コード整理や再利用性も考慮して、共通のextensionやcomponentsをLocal Packageに切り出しています。しかしこのままでは新規にアプリを作成するときに、そのコードをコピーしないといけないので、Local PackageをGitHubにプロジェクトにアップロードし、パッケージとして公開したので、その手順をまとめました。 またプロジェクトのCommit, PushなどのGitの操作をXcodeから操作できるようになり、今回初めて操作したのですが、思ったよりも操作しやすかったので、今回のGitの操作は、Xcodeを利用しています。

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.

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.

xcode-publish-package0
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.

xcode-publish-package1
Then create it as it is. Then the existing implementation will be committed as an Initial Commit as follows:

xcode-publish-package6
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.

xcode-publish-package10 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.

xcode-publish-package3

Then create a tag by setting, for example, 1.0.0 as shown below.
xcode-publish-package4
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 Visibility to Public and create it.
xcode-publish-package2
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.
xcode-publish-package5
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 Package.swift.

xcode-publish-package7

And Replace it with the URL of the Remote Package as shown below.

xcode-publish-package8
Now you can publish this package as well by doing the same thing as above. The final Package.swift for SwiftUtils looks like this:
xcode-publish-package9 

Wrapping up

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.

0

COMMENT

Your email address will not be published. Required fields are marked *

CAPTCHA