You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 19, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: _posts/2019-01-03-swift-package-manager-linux-macos-create-library-executable.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,23 +27,23 @@ The library had a simple `Package.swift`, but honestly I never tested it with th
27
27
Soooo I though: "It's time to add full support for the Swift Package Manager to ID3TagEditor and port it
28
28
also on Linux!!!!" :sparkling_heart:
29
29
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.
31
31
First of all, if you are starting with a new library project, you will use the following SPM `init` command:
32
32
33
33
`swift package init --type library`
34
34
35
35
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:
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).
47
47
48
48
```swift
49
49
// 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
87
87
Let's see in details the meaning of each option:
88
88
89
89
*`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:
92
92
*`name`, the name of the product
93
93
*`targets`, the targets which are supposed to be used by other packages, i.e. the public API of a library package
94
94
*`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:
97
97
*`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
100
100
`exclude` option. The tests excluded contains some references to bundle resources, **and at the moment of this
101
101
writing the SPM doesn't support resource bundles**.
102
102
*`swiftLanguageVersions`, that contains the set of supported Swift language versions.
103
103
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.
105
105
106
106
```swift
107
107
importXCTest
@@ -142,7 +142,7 @@ public func __allTests() -> [XCTestCaseEntry] {
142
142
#endif
143
143
```
144
144
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).
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.
160
160
161
161
```shell
162
162
sudo apt-get install clang libicu-dev
163
163
```
164
164
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.
166
166
167
167
```shell
168
168
tar xzf swift-<VERSION>-<PLATFORM>.tar.gz
@@ -171,16 +171,16 @@ tar xzf swift-<VERSION>-<PLATFORM>.tar.gz
171
171
export PATH=/<path to the Swift release folder>/usr/bin:"${PATH}"
172
172
```
173
173
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:
175
175
176
176
```shell
177
177
swift build
178
178
swift run
179
179
```
180
180
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.
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