Using Alamofire in a command line OS X app (Xcode 7) - code encapsulation? - swift

I'm trying to figure out how to use Alamofire in a command line app.
I can't use frameworks, so I've added the Alamofire source code into the app (so no import statement) and I'm able to directly reference the request() and other methods.
Is there a cleaner way to encapsulate Alamofire or is this a limitation in Swift 2.X at the moment?

For those who gonna need it here is the solution.
If you don't have the file xcodeproject please generate
$ swift package generate-xcodeproj
You may need to fix the Deployment Target
Now time to add the Alamofire
import PackageDescription
let package = Package(
name: "SuiteTest",
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0")
],
targets: [
.target(
name: "SuiteTest",
dependencies: ["Files", "Alamofire"]),
.testTarget(
name: "ASuiteTestTests",
dependencies: ["Assure-SuiteTest"]),
]
)
I hope I have helped anyone who has the same problem :)

Related

Sharing code across test targets when using the Swift Package Manager

I have some code that I need to share across test targets, while I'm using the Swift Package Manager. To do this, I have a .testTarget that I also name as a dependency in another .testTarget.
Here is a simple example:
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "ExampleLib",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "ExampleLib",
targets: ["ExampleLib"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.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: "ExampleLib",
dependencies: []),
.testTarget(
name: "Common",
dependencies: ["ExampleLib"]),
.testTarget(
name: "ExampleLibTests",
dependencies: ["Common"]),
]
)
If I try to build this package in Xcode, I get the following error:
Unable to resolve build file: XCBCore.BuildFile (The workspace has a reference to a missing target with GUID 'PACKAGE-TARGET:Common')
However, if I build from the command line (swift build) or test from the command line (swift test), I get success.
I'm using Xcode 12 beta 6, but have also tried Xcode 11.5 (with a change to the Package.swift header) and get the same results.
Here is the complete Swift package example:
https://www.dropbox.com/s/h6ypvbfonnb2zyk/ExampleLib.zip?dl=0
I really would like to use this in Xcode to build for iOS. Thoughts?
I faced the same issue and solved it by defining a Target (not Test Target) instead for Common.
My Package.swift file:
// ...
.target(name: "Common", dependencies: ["App"], path: "Tests/Common"),
.testTarget(name: "AppTests", dependencies: ["App", "Common"]),
.testTarget(name: "IntegrationTests", dependencies: ["App", "Common"])
// ...

swift CryptoKit on Ubuntu

I am trying to compile a small swift program, "main.swift" to an executable on Ubuntu 18.08.
I use the Swift Package Manager to manage my dependencies. In this very simple case I only have one dependency, namely this open-source CryptoKit.
I have one swift file which just tries to import CryptoKit.
import Foundation
import CryptoKit
print("phew")
My Package.swift file looks like this:
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "decryp",
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
],
targets: [
.target(
name: "decryp",
dependencies: ["swift-crypto"]
),
.testTarget(
name: "decrypTests",
dependencies: ["decryp"]),
]
)
When I try to build the executable with swift build it fetches the repository, but then gives an error with a product not found.
stdout from swift build:
Fetching https://github.com/apple/swift-crypto.git
Cloning https://github.com/apple/swift-crypto.git
Resolving https://github.com/apple/swift-crypto.git at 1.0.2
'decryp' /home/kah/decryp: error: product 'swift-crypto' not found. It is required by target 'decryp'.
warning: dependency 'swift-crypto' is not used by any target
Maybe I am missing something obvious? I am still a beginner in the swift world.
Looking at swift-crypto Package.swift gave a clue. The swift-crypto library name is "Crypto", so I tried with that instead which gave an error:
error: dependency 'Crypto' in target 'decryp' requires explicit declaration;
reference the package in the target dependency with '.product(name: "Crypto",
package: "swift-crypto")'
But that says what to do, so when I changed my Package.swift to the following and imported "Crypto" instead of "CryptoKit" everything works!
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "decryp",
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", .upToNextMajor(from: "1.0.1"))
],
targets: [
.target(
name: "decryp",
dependencies: [
.product(name: "Crypto", package: "swift-crypto")
]
),
.testTarget(
name: "decrypTests",
dependencies: ["decryp"]),
]
)
Now I can send an encrypted message from my phone to a server and then decrypt it locally on my ubuntu laptop, thanks to the swift-crypto team!

How to add a .framework as dependency to a swift package in Xcode?

I want to know if there is anyway to link a swift package against a framework like SQLite.framework in Xcode? I'm trying to make a swift package for a sqlite library wrapper.
Here is my current swift package manifest:
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "SQLiteDB",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "SQLiteDB",
targets: ["SQLiteDB"]),
],
dependencies: [
// .package(url: /* package url */, from: "1.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 which this package depends on.
.target(
name: "SQLiteDB",
dependencies: []),
.testTarget(
name: "SQLiteDBTests",
dependencies: ["SQLiteDB"]),
]
)
I ended up creating my own Sqlite package by embedding the amalgamation sources of sqlite. This gives you the ability to have any arbitrary version of Sqlite in your apps.
With the Xcode 12 beta, you can do this if you update your swift-tools-version to 5.3, and then add a binaryTarget to your package's targets:
.binaryTarget(
name: "Stripe",
url: "https://github.com/stripe/stripe-ios/releases/download/v19.3.0/Stripe.xcframework.zip",
checksum: "fe459dd443beee5140018388fd6933e09b8787d5b473ec9c2234d75ff0d968bd"
)

Cannot add SwiftCalendar as a dependency in vapor3 project via package manager

I am currently trying to add SwiftDate to a Vapor3 project via the swift package manager. Here is my package file:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "timeshare",
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
// Custom dependencies
.package(url: "https://github.com/malcommac/SwiftDate.git", from: "5.0.0"),
],
targets: [
.target(name: "App", dependencies: ["Vapor", "SwiftDate"]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"]),
]
)
However, when I try to build my project I get 82 issues in Xcode (all from the SwiftCalendar module). For example this:
I have no idea how I can possible have caused errors in a third party library. Any input is highly appreciated.
Thanks
More errors can be seen here:
PS: Sorry for including screenshots, I know they might be hard to read, but I could not find a way to copy the error list as text.
The project isn’t in the correct format for SPM. All the files need to be declared in the correct target directory, but DateRepresentable is outside of that https://github.com/malcommac/SwiftDate/tree/master/Sources
It’s also worth noting that this doesn’t seem to have any tests on Linux and involves DateManipulation. I’d be very weary about using it since Dates on Linux are notoriously crashy

Add Alamofire as dependency through Swift 3 package manager

Swift 3 introduced package manager, with which I can add dependencies to my project.
For example, in my Package.swift , I can declare my dependency by:
import PackageDescription
let package = Package(
name: "my-project",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 0, minor: 16),
]
)
Now, I need to add Alamofire to my project, how can I get it through Swift 3 package manager?
You can't use Alamofire yet because they haven't released Swift 3 support.
Once they've released this, I'm sure they will make it obvious on their GitHub readme how to import it.
Luckily Vapor comes with a great HTTP and even WebSockets client. Read more about the HTTP client in the documentation: https://vapor.github.io/documentation/http/client.html
Well, today you just have to add the following line in your Package.swift file:
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
Really easy :)