Swift
Vapor
ServerSideSwift

VaporでMySQL Providerを追加する方法(Swift4)

公式ドキュメント(https://docs.vapor.codes/2.0/mysql/package/)
の手順だけだと追加できない。
import MySQLProviderとすると怒られたりビルド時に以下のようなエラーが出たりする。

error: manifest parse error(s):
/Users/username/hoge/Package.swift:17:10: error: ambiguous reference to member 'package'
.package(url: "https://github.com/vapor/mysql.git", majorVersion: 2)
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/username/hoge/Package.swift:17:10: note: overloads for 'package' exist with these partially matching parameter lists: (url: String, from: Version), (url: String, Package.Dependency.Requirement), (url: String, Range), (url: String, ClosedRange), (url: String, version: Version), (url: String, branch: String), (url: String, revision: String), (url: String, range: Range)
.package(url: "https://github.com/vapor/mysql.git", majorVersion: 2)

ちょっと手順を加えるだけで大丈夫です

環境
MacOS HighSierra
Xcode 9
Swift 4
Vapor Toolbox: 3.1.2
Vapor Framework: 2.3.0

Package.swiftを編集する

Projectのすぐ下にあるPackage.swiftを以下のように編集する

Package.swift
import PackageDescription

let package = Package(
    name: "hoge",
    products: [
        .library(name: "App", targets: ["App"]),
        .executable(name: "Run", targets: ["Run"])
    ],
    dependencies: [
        .package(url: "https://github.com/vapor/vapor.git", .upToNextMajor(from: "2.2.0")),
        .package(url: "https://github.com/vapor/leaf-provider.git", .upToNextMajor(from: "1.1.0")),
        .package(url: "https://github.com/vapor/mysql-provider.git", .upToNextMajor(from: "2.0.0")),
        .package(url: "https://github.com/vapor/fluent.git", .upToNextMajor(from:"2.0.0")),
        .package(url: "https://github.com/vapor/mysql-driver.git", .upToNextMajor(from:"2.0.0"))
    ],
    targets: [
        .target(name: "App", dependencies: ["Vapor", "LeafProvider","MySQLProvider"],
               exclude: [
                   "Config",
                   "Database",
                   "Public",
                   "Resources"
               ]),
        .target(name: "Run", dependencies: ["App"]),
        .testTarget(name: "AppTests", dependencies: ["App", "Testing"])
    ]
)

Swidt4ではpackageの書き方が違うのが引っかかりポイント。あと、Targetのところに"MySQL Provider"を追加しないとビルドが通りません。

swift package updateする

$ swift package update

これだけでOK!