Swift 4: Local Dependencies with Swift Package Manager? - TagMerge
6Swift 4: Local Dependencies with Swift Package Manager?Swift 4: Local Dependencies with Swift Package Manager?

Swift 4: Local Dependencies with Swift Package Manager?

Asked 7 months ago
4
6 answers

Your URL can be relative, so ../MySwiftLib is perfectly valid. However, the source directory must be a git repository. It is recommended that this repo be tagged, but you can also do:

.package(url: "../MySwiftLib", .branch("master"))

if you want to use whatever code is on the master branch.

Hope this helps!

Source: link

2

Set the url value to file:///path/to/swiftlib:

let package = Package(
name: "MySwiftExe",
dependencies: [
    .package(url: "file:///path/to/MySwiftLib", from: "1.0.0"),

...

Source: link

0

According to docs there's the package(path: String) function that can be used to define a local dependency.

One thing to mention is that in case the package provides multiple targets and your project is configured to only use some of those targets:

...
dependencies: [
    .package(name: "MyPackage", url: "git@github.com:path/to/package.git", .exact("ver.si.on")),
]
...
targets: [
    .target(
        name: "MyTarget",
        dependencies: [
            .product(name: "SpecificTargetName", package: "MyPackage"),
        ],
    ),
]

Then for that to work locally the package definition deeds to declare the name as well (the path is not enough):

.package(name: "MyPackage", path: "/local/path/to/package"),

Source: link

0

import PackageDescription

let package = Package(
  name: "EmojiMapper",
  products: [
    .library( name: "EmojiMapper", targets: ["EmojiMapper"]) ],
  dependencies: [
    .package(url: "https://github.com/Alamofire/Alamofire.git", 
    .upToNextMajor(from: "5.4.0")),
  ],
  targets: [
    .target(
      name: "EmojiMapper",
      dependencies: [ .product(name: "Alamofire", package: "Alamofire") ]),
    .testTarget(
      name: "EmojiMapperTests", 
      dependencies: ["EmojiMapper"])
  ]
)
let package = Package(
  name: "EmojiMapper",
  products: [
    .library(
      name: "EmojiMapper",
      targets: ["EmojiMapper"])
  ],
  dependencies: [
    .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.4.0")),
    .package(path: "../ResourcesUI"),  // 👈  Reference to a Local Package
  ],
  targets: [
    .target(
      name: "EmojiMapper",
      dependencies: [
        .product(name: "Alamofire", package: "Alamofire"),
        .product(name: "ResourcesUI", package: "ResourcesUI"), // 👈  Reference to a Local Package
      ]),
    .testTarget(
      name: "EmojiMapperTests",
      dependencies: ["EmojiMapper"])
  ]
)
public struct LoggerModel {
  let caller: String
  let someValue: Bool
  
  // 🚫 Default init I'ts not accessible from another package
}

public struct LoggerModel {
  let caller: String
  let someValue: Bool

  // ✅ This init should be declared public
  public init(caller: String, someValue: Bool) {
    self.caller = caller
    self.someValue = someValue
  }
}
let image = UIImage(named: "home", in: .module, compatibleWith: nil)
public protocol LoggerProtocol {
  func log(caller: String, message: String)
}

public class ConcreteLogger: LoggerProtocol {
  public func log(caller: String, message: String) {
    // Do some stuff
  }
}

Source: link

0

You can also use
.package(path: "path to folder")
With the Swift 4 version tools, there's different means to do this-- where you don't have to provide a tagged version:
.package(url: "<path to repo>", .branch("master")),

Source: link

0

As an example, let’s say that we’re working on a todo list app, and that we want to create a TodoKit package for all of our core logic that’s shared across the app — including things like our database layer, our model code, and so on. To get started, we’ll create a new folder (with a name that matches what we want our package to be called), and we’ll then run swift package init within it to create our package:
$ mkdir TodoKit
$ cd TodoKit
$ swift package init
By doing the above, the Swift Package Manager will now have created an initial structure for our new package — which includes a Package.swift manifest file that looks something like this:
// swift-tools-version:5.1

import PackageDescription

let package = Package(
    name: "TodoKit",
    products: [
        // The external product of our package is an importable
        // library that has the same name as the package itself:
        .library(
            name: "TodoKit",
            targets: ["TodoKit"]
        )
    ],
    targets: [
        // Our package contains two targets, one for our library
        // code, and one for our tests:
        .target(name: "TodoKit"),
        .testTarget(
            name: "TodoKitTests",
            dependencies: ["TodoKit"]
        )
    ]
)
Besides facilitating the creation of packages, one of the Swift Package Manager’s core use cases is enabling remote dependencies — such as third party libraries — to be added to a project. Any package that can be fetched through Git can be added simply by specifying its URL, as well as the version constraint that we wish to apply to it:
let package = Package(
    ...
    dependencies: [
        // Here we define our package's external dependencies
        // and from where they can be fetched:
        .package(
            url: "https://github.com/johnsundell/files.git", 
            from: "4.0.0"
        )
    ],
    targets: [
        .target(
            name: "TodoKit",
            // Here we add our new dependency to our main target,
            // which lets us import it within that target's code:
            dependencies: ["Files"]
        ),
        .testTarget(
            name: "TodoKitTests",
            dependencies: ["TodoKit"]
        )
    ]
)
However, sometimes we might want to lock onto a specific version of one of our dependencies — perhaps to avoid a regression that was introduced in a later version, or to be able to keep using an API that was later removed. To do that, we can replace the above from: parameter with the .exact version requirement — like this:
.package(
    url: "https://github.com/johnsundell/files.git",
    .exact("4.0.0")
)
The first option is to point our dependency to a specific Git branch (which can be quite risky if that branch is rapidly changing), or to lock onto a specific commit hash (which is less risky, but also less flexible, since we’ll have to manually change that hash each time we want to update that dependency):
// Depending on a branch (master in this case):
.package(
    url: "https://github.com/johnsundell/files.git",
    .branch("master")
)

// Depending on an exact commit:
.package(
    url: "https://github.com/johnsundell/files.git",
    .revision("0e0c6aca147add5d5750ecb7810837ef4fd10fc2")
)

Source: link

Recent Questions on swift

    Programming Languages