Categories
iOS Xcode

Swift Package Manager.4

This is going to be the last post of the Swift Package Manager Series. After we create a package, then make it automated, and include it in our project, we would like to make a second package. And we would like the package A to depend on packageB.

What we want

Package A

Package B, depends on A

Our app, needs to use A and B

How to do this?

Both packages can be created as mentioned in the previous posts without problem.

If we want to make package B depend on A, we need to:

  • In Xcode of Package B, “Add package dependency” to Package A.This is help to make the package B compile in Xcode, so we can use it, make unit tests and so on.
  • in code of Package B, we can use code of package A when importing correctly “import FluentCoding.A”
  • In the Manifest “Package.swift”, we need to define the dependency, like this
import PackageDescription
 

let package = Package(
     name: "FluentCoding.B",
     products: [
         // Products define the executables and libraries a package produces, and make them visible to other packages.
         .library(
             name: "FluentCoding.B",
             targets: ["FluentCoding.B"])
     ],
     dependencies: [
         // Dependencies declare other packages that this package depends on.
         .package(
             url: "git@github.com:thomalexandre/FluentCoding.A.git",
             .branch("main")
         )
     ],
     targets: [
         // Targets are the basic building blocks of a package. A target can define a module or a test suite.
         // Targets can depend on other targets in this package, and on products in packages this package depends on.
         .target(
             name: "FluentCoding.B",
             dependencies: ["FluentCoding.A"]),
         .testTarget(
             name: "FluentCoding.BTests",
             dependencies: ["FluentCoding.B"])
     ]
 ) 
  • Add the dependency package (package with branch of version)
  • Add the dependency package name in the target part

When doing this, the package A will be automatically imported in a project when trying to import package B.

Linking in your project

If you have a direct dependency and an indirect dependency to the same package, you may get a linking error in your project. If this happen and you get “Swift package product ‘x’ is linked as a static library by ‘y’ and ‘z’. This will result in duplication of library code.”, you can try adding this settings in your build settings.

DISABLE_DIAMOND_PROBLEM_DIAGNOSTIC = YES

Compiling with Github Actions

When you have this dependency from one package to another, Github Actions needs to be able to check the dependent package. If this is a public package, it’s fine, it should work automatically.

If it’s a private package, mostly yours I guess, you need to specify the SSH key in order to access the private package..

  • In your repository, open Settings > Secret > New Repository Secret. Call it “SSH_PRIVATE_KEY” and past your private key
  • In the git hub action script which automates the build, add the following Step after the checkout:
 - uses: webfactory/ssh-agent@v0.5.1
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

More Info: https://github.com/marketplace/actions/setup-ssh-action

Thank you for reading until here…  👊 Have a good week end…  🍺