Add PostgreSQL to vapor project - vapor

I added .package(url: "https://github.com/vapor-community/postgresql-provider.git", .upToNextMajor(from: "2.1.0")) to my Package.swift file, ran vapor update and let it regenerate the Xcode project. When I then add the import of PostgreSQLProvider Xcode can't find it. I already have postgresql and pkg-config installed via homebrew.
I'm using Xcode 9 and Swift 4

Looks like you are using the Swift 4 package manager. The API for it was updated so you have to explicitly add the dependency to the target:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "Project",
products: [
.library(name: "App", targets: ["App"]),
.executable(name: "Run", targets: ["Run"])
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.1.0")),
.package(url: "https://github.com/vapor/fluent-provider.git", .upToNextMajor(from: "1.2.0")),
.package(url: "https://github.com/vapor-community/postgresql-provider.git", .exact("2.1.0"))
],
targets: [
.target(name: "App", dependencies: ["Vapor", "FluentProvider", "PostgreSQLProvider"],
exclude: [
"Config",
"Public",
"Resources",
]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App", "Testing"])
]
)
See above, I added the PostgreSQLProvider package to dependencies array for App target.

I had this problem too and wasn't sufficient with vapor xcode because it only regenerate the project and don't fetch the new dependencies like postgree you're adding, to solve this use vapor fetch and later vapor xcode, both commands with the project closed

Related

Excluding SPM packages from building in specific targets

Say I have some debugging library, and I want it to not be included in the production versions of my iOS app, how can I achieve this with Swift Package Manager? so in short, I want it to be only part of development builds.
You can conditionally include a package in a target, reference.
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "BestPackage",
dependencies: [
.package(url: "https://github.com/pureswift/bluetooth", .branch("master")),
.package(url: "https://github.com/pureswift/bluetoothlinux", .branch("master")),
],
targets: [
.target(
name: "BestExecutable",
dependencies: [
.product(name: "Bluetooth", condition: .when(platforms: [.macOS])),
.product(name: "BluetoothLinux", condition: .when(platforms: [.linux])),
.target(name: "DebugHelpers", condition: .when(configuration: .debug)),
]
),
.target(name: "DebugHelpers")
]
)

Swift Package Manager: two similar targets different only with the dependencies

I'm migrating a library built and tested as an Xcode Project to Swift Package Manager. The project contains 3 targets: the library itself, the same library but with different dependency and the test (application) target.
So far, porting the library was easy.
Now I'm interested in building the same library with a different dependency (mocks) which could be tested.
In practice, the Package.swift file looks like this:
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "TargetLibrary",
platforms: [
.iOS(.v14), .macOS(.v10_15)
],
products: [
.library(
name: "TargetLibrary",
targets: ["TargetLibrary"]
),
],
dependencies: [
.package(
url: "ssh://git#github.com/DependentLib",
.upToNextMinor(from: "1.3.18")
),
],
targets: [
.target(
name: "TargetLibrary",
dependencies: [
// Using the "main" dependency for the "TargetLibrary"
.product(name: "DependentLib", package: "DependentLib"),
],
path: "src",
sources: [
"sourcefiles"
],
publicHeadersPath: "SwiftPackage/include",
cxxSettings: [
.headerSearchPath("lib"),
]
),
// This target is virtually identical, to the previous one,
// the only difference is that it is using a different dependency library
// from the same package
.target(
name: "TargetLibraryWithMock",
dependencies: [
// Using the "mock" dependency for this library
.product(name: "DependentLibMock", package: "DependentLib"),
],
path: "src",
sources: [
"sourcefiles"
],
publicHeadersPath: "SwiftPackage/include",
cxxSettings: [
.headerSearchPath("lib"),
]
),
// Now testing the "TargetLibraryWithMock"
.testTarget()....
],
cxxLanguageStandard: .cxx14
)
Now, of course, if I just duplicate two targets, I'll get a target overlapping sources error
So, the question is:
How can I build two targets different only by the dependencies they include?
It looks like target dependency condition could have helped here
dependencies: [
.target(name: "DependentLib", condition: .when(configuration: .build)),
.target(name: "DependentLibMock", condition: .when(configuration: .test)),
]
but only platform-specific conditionals exist now. What could be solutions to this problem?
Graphical representation of the desired result:

Firebase [Swift Package Manager]: no such module FirebaseRemoteConfig

After I read the instructions listed here for how to add Firebase as a dependency to a Swift package, I couldn't get it to work, here's my Package.swift manifest:
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "Firebase",
url: "https://github.com/firebase/firebase-ios-sdk.git",
.upToNextMajor(from: "8.0.0"))
],
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: "MyPackage",
dependencies: [.product(name: "FirebaseRemoteConfig", package: "Firebase")]),
.testTarget(
name: "MyPackageTests",
dependencies: ["MyPackage"]),
]
)
the package graph resolves without problems, but when I try to add my own code in Sources/MyPackage.swift starting with import FirebaseRemoteConfig the compiler complains with:
No such model 'FirebaseRemoteConfig'.
what's went wrong with my setup ?
I figured out the solution, but unfortunately it's not documented on Firebase Docs, I have to add the .platforms array in the Package.swift manifest specifying a version that supports FirebaseRemoteConfig, for example:
platforms: [
.iOS(.v13)
]

How do I declare the Siesta Swift package as a dependency of another Swift package?

I'm trying to use the Swift package Siesta as a dependency for the package I'm building and reference it in my package code. I've identified how to import the package into my project in my Package.swift file which is simple enough:
dependencies: [
.package(url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
This causes the package to be copied into my package just fine. The problem I'm having is actually linking it up to my package so I can import it and reference it in code. I know I need to actually link it up to my target
I've read some other package files and because the package name for Siesta is like this
let package = Package(
name: "Siesta",
And the products it declares are like this
products: [
.library(name: "Siesta", targets: ["Siesta"]),
.library(name: "SiestaUI", targets: ["SiestaUI"]),
.library(name: "Siesta_Alamofire", targets: ["Siesta_Alamofire"]),
],
I should be able to just do this in my package file's target to use it
.target(
name: "MyTarget",
dependencies: [.product(name: "Siesta", package: "Siesta")]),
But when I try to build my package, I get an error:
/Users/blahblah/Desktop/MyPackage/Package.swift: unknown package 'Siesta' in dependencies of target 'MyTarget'
And not only that, all the targets for my single run scheme on my package go missing and I can't build again without discarding all my local version control changes. What's going on here?
With Swift tools version 5.2 you have to provide a name argument when declaring your package dependency.
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
A working example of a Package.swift file:
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyLibrary",
targets: ["MyTarget"]),
],
dependencies: [
// make sure to provide a `name` argument here
.package(name: "Siesta", url: "https://github.com/bustoutsolutions/siesta", from: "1.5.1")
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "Siesta", package: "Siesta")
]),
]
)
Source: https://forums.swift.org/t/package-names-in-swift-5-2/34886/6

No such module 'PackageDescription' in Xcode 11

I have an existing projects of a framework and library and would like them to use Swift Package Manager.
I have a Xcode 11 and have already already read Creating a Swift Package with Xcode.
After adding Package.swift to the project and an application target
import PackageDescription
let package = Package(
name: "MyLibrary",
platforms: [
.macOS(.v10_13),
],
products: [
.library(name: "MyLibrary", targets: ["MyLibrary"]),
],
dependencies: [
.package(url: "https://url/of/another/package/named/Utility", from: "1.0.0"),
],
targets: [
.target(name: "MyLibrary", dependencies: ["Utility"]),
.testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
]
)
I get an error
No such module 'PackageDescription'
How can I import PackageDescription module?
I just added line below at the very first line of the Package.swift file and it worked for me.
// swift-tools-version:4.0
Note: Please do not write any thing even comment before this line
After that do not forget to run swift build command

Resources