Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 68e3f10

Browse files
committed
Fix article 🚀
1 parent 910126b commit 68e3f10

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

_posts/2019-01-03-swift-package-manager-linux-macos-create-library-executable.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ The library had a simple `Package.swift`, but honestly I never tested it with th
2727
Soooo I though: "It's time to add full support for the Swift Package Manager to ID3TagEditor and port it
2828
also on Linux!!!!" :sparkling_heart:
2929
In this post I will describe how you can create a Swift library package for the Swift Package Manager compatible with
30-
macOS and Linux for an existing project. Obviously, I will show you the entire process using my [ID3TagEditor](https://github.com/chicio/ID3TagEditor) as example.
30+
macOS and Linux for an existing project. Obviously, I will show you the entire process using my [ID3TagEditor](https://github.com/chicio/ID3TagEditor) as example.
3131
First of all, if you are starting with a new library project, you will use the following SPM `init` command:
3232

3333
`swift package init --type library`
3434

3535
This command will create all the files and folders you need to develop your library. But in my case, I was working
36-
on an existing project. This is why I will create all the needed files manually and I will describe them in details
37-
so we can understand better the meaning of each one of them.
38-
The first file we need is the `Package.swift`. This file must be created in the root folder of your project. This
39-
file contains some swift code that defines the properties of the project using the `PackageDescription` module API. At
40-
the moment of this writing there are 3 API version of the PackageDescription API:
36+
on an existing project. This is why I created all the needed files manually and I will describe them in details
37+
so you can understand better the meaning of each one of them.
38+
The first file needed is the `Package.swift`. This file must be created in the root folder of your project. This
39+
file contains some Swift code that defines the properties of the project using the `PackageDescription` module API. At
40+
the moment of this writing there are 3 API versions of the PackageDescription API:
4141

4242
* [Version 3](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV3.md)
4343
* [Version 4](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md)
4444
* [Version 4.2](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4_2.md)
4545

46-
For my [ID3TagEditor](https://github.com/chicio/ID3TagEditor) I will use the [Version 4.2](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4_2.md).
46+
For my [ID3TagEditor](https://github.com/chicio/ID3TagEditor) I used the [Version 4.2](https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4_2.md).
4747

4848
```swift
4949
// swift-tools-version:4.2
@@ -87,21 +87,21 @@ In this post I will describe how you can create a Swift library package for the
8787
Let's see in details the meaning of each option:
8888

8989
* `name`, the name of the package
90-
* `products`, the list of all products in the package. You can have `executable` or `library` products. In our case
91-
we have `library` and for that we have to specify:
90+
* `products`, the list of all products in the package. You can have `executable` or `library` products. In my case
91+
I have `library` product and for that I have to specify:
9292
* `name`, the name of the product
9393
* `targets`, the targets which are supposed to be used by other packages, i.e. the public API of a library package
9494
* `dependencies`, a list of `package` dependencies for our package. At the moment ID3TagEditor doesn't have any
95-
dependencies so we can just pass an empty array.
96-
* `targets`, the list of targets in the package. In our case we have two target:
95+
dependencies so I declared an empty array.
96+
* `targets`, the list of targets in the package. In my case I have two target:
9797
* `ID3TagEditor`, that is the main target of the library and is a classic `.target`. For this target you specify its
98-
name, its dependencies and the path to the source files. In our case we have everything inside the `Source` folder.
99-
* `ID3TagEditorTests`, that is the `.testTarget` of the library. For this target we need to specify an additional
98+
name, its dependencies and the path to the source files. In my case I have everything inside the `Source` folder.
99+
* `ID3TagEditorTests`, that is the `.testTarget` of the library. For this target I had to specify an additional
100100
`exclude` option. The tests excluded contains some references to bundle resources, **and at the moment of this
101101
writing the SPM doesn't support resource bundles**.
102102
* `swiftLanguageVersions`, that contains the set of supported Swift language versions.
103103

104-
Next we need to create a `XCTestManifests.swift` file inside our `Tests` folder. This file contains an extension for each `XCTestCase` subclass we included in our test target. This extension contains an array `__allTest` that exposes a list of all our test methods inside our `XCTestCase` subclasses. At the end of this file you can find a `__allTests()` function that pass all the test methods to the `testCase()` utility function. `__allTests()` and `testCase` are available on Linux platform (and in fact the `__allTests()` function is wrapped in a conditional check `#if !os(macOS)`). Below you can see a part of the `XCTestManifests.swift` file for the `ID3TagEditor` library.
104+
Next I had to create a `XCTestManifests.swift` file inside the `Tests` folder. This file contains an extension for each `XCTestCase` subclass I included in my test target. This extension contains an array `__allTest` that exposes a list of all my test methods inside my `XCTestCase` subclasses. At the end of this file you can find a `__allTests()` function that pass all the test methods to the `testCase()` utility function. `__allTests()` and `testCase` are available only on Linux platform (and in fact the `__allTests()` function is wrapped in a conditional check `#if !os(macOS)`). Below you can see a part of the `XCTestManifests.swift` file for the `ID3TagEditor` library.
105105

106106
```swift
107107
import XCTest
@@ -142,7 +142,7 @@ public func __allTests() -> [XCTestCaseEntry] {
142142
#endif
143143
```
144144

145-
Next we need `LinuxMain.swift` file in the root folder of our project. This file loads all the test on Linux (using the functions and extensions defined in the previous file `XCTestManifests.swift`).
145+
Next I created a `LinuxMain.swift` file in the root folder of my ID3TagEditor project. This file loads all the test on Linux (using the functions and extensions defined in the previous `XCTestManifests.swift` file).
146146

147147
```swift
148148
import XCTest
@@ -155,14 +155,14 @@ tests += ID3TagEditorTests.__allTests()
155155
XCTMain(tests)
156156
```
157157

158-
We are now ready to test our `ID3TagEditor` using the SPM on macOS and Linux. To do this we will use Ubuntu distro. The version used is the 18.04 LTS.
159-
First of all, how do we install Swift on linux? First of all we need to download the Swift release for linux from the [Swift download page](Ubuntu 18.04 "swift download page"). The version we are going to use is the one you can find at [this link](https://swift.org/builds/swift-4.2.1-release/ubuntu1804/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-ubuntu18.04.tar.gz). Then we need to install some additional packages.
158+
Now I was ready to test `ID3TagEditor` using the SPM on macOS and Linux. To do this I used Ubuntu as distro. The distro version used at the moment of this writing is the 18.04 LTS.
159+
First of all, how do I install Swift on linux? I downloaded the Swift release for Linux from the [Swift download page](https://swift.org/download/ "swift download page"). The version I used is the one you can find at [this link](https://swift.org/builds/swift-4.2.1-release/ubuntu1804/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-ubuntu18.04.tar.gz). Then I installed the additional packages `clang` and `libicu-dev` with the following shell command.
160160

161161
```shell
162162
sudo apt-get install clang libicu-dev
163163
```
164164

165-
Then we can extract from the archive we downloaded before the Swift release folder and we need to add to our shell enviroment `PATH` variable the path to /usr/bin folder contained inside the Swift release folder extracted from the archive.
165+
Then I extracted from the archive previously downloaded the Swift release folder and I added to my shell enviroment variable `PATH` the path to /usr/bin folder contained inside the this release folder.
166166

167167
```shell
168168
tar xzf swift-<VERSION>-<PLATFORM>.tar.gz
@@ -171,16 +171,16 @@ tar xzf swift-<VERSION>-<PLATFORM>.tar.gz
171171
export PATH=/<path to the Swift release folder>/usr/bin:"${PATH}"
172172
```
173173

174-
Now we are ready to test our `ID3TagEditor` as a SPM library. To do this I created a new simple project indise the demo folder called `Demo Ubuntu`. This is a simple executable SPM project that has the `ID3TagEditor` as package dependecies. The executable is a command line application that opens a mp3 file, parses its ID3 tag and print it to the standard output. To test our work we can just clone the `ID3TagEditor` on Linux or macOS and launch the following commands inside in the root of the `Demo Ubuntu` project:
174+
The setup was done. Now I was able to test `ID3TagEditor` as a SPM library on Linux. To do this I created a new project inside the demo folder of the ID3TagEditor project called `Demo Ubuntu`. This is an executable SPM project that has `ID3TagEditor` as package dependecies. The executable is a command line application that opens a mp3 file, parses its ID3 tag and print it to the standard output. To test my work I just cloned the `ID3TagEditor` on Linux (and also on macOS :stuck_out_tongue_winking_eye:) and launched the following commands in the root folder of the `Demo Ubuntu` project:
175175

176176
```shell
177177
swift build
178178
swift run
179179
```
180180

181-
Below you can see some screenshot taken from both macOS and Linux that shows the final output of the demo `Demo Ubuntu` after you execute the `swift run` command.
181+
Below you can see some screenshot taken from both Linux and macOS that shows the final output of the demo `Demo Ubuntu` after you execute the `swift run` command.
182182

183183
![id3tageditor SPM demo ubuntu](/assets/images/posts/spm-id3tageditor-demo-linux.jpg "id3tageditor SPM demo ubuntu")
184184
![id3tageditor SPM demo macOS](/assets/images/posts/spm-id3tageditor-demo-macos.jpg "id3tageditor SPM demo macOS")
185185

186-
Coool!! Now the ID3TagEditor is fully compatible with the SPM and could be used in Swift application for macOS and Linux. You can see the entire codebase of the `ID3TagEditor` in [this github repository](https://github.com/chicio/ID3TagEditor). Now you can start to port your libraries and applications on Linux with the Swift Package Manager :sparkles:.
186+
Coool!! Now the ID3TagEditor is fully compatible with the SPM and could be used in Swift applications for both macOS and Linux. You can see the entire codebase of the `ID3TagEditor` in [this github repository](https://github.com/chicio/ID3TagEditor). Now you can start to port your libraries and applications on Linux with the Swift Package Manager :sparkles:.

0 commit comments

Comments
 (0)