Search code examples
iosxcode11swift-package-managerswiftlint

How to integrate SwiftLint with an iOS app using Swift Package Manager?


I'm creating a new iOS app using Xcode 11 (beta 5), and I'd like to use Swift Package Manager instead of CocoaPods for managing dependencies.

A common pattern when using SwiftLint and CocoaPods is to add SwiftLint as a dependency and then add a build phase to execute ${PODS_ROOT}/SwiftLint/swiftlint; this way all developers end up using the same version of SwiftLint.

If I try to add SwiftLint as a SwiftPM dependency in Xcode, the executable target that I need is disabled:

Add Package Screenshot

I was able to fake it by creating a dummy Package.swift with no product or target, and running swift run swiftlint in my build phase, but it feels like a hack:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "dummy-package",
    products: [],
    dependencies: [
        .package(url: "https://github.com/realm/SwiftLint.git", from: "0.34.0")
    ],
    targets: []
)

Is there a way do this without creating a dummy package? Or is Swift Package Manager just not the right tool for this particular use case?


Solution

  • All methods of abusing iOS code dependency managers to run build tools are hacky and weird.

    The correct way to version SPM-compliant tool dependencies is to use Mint: A package manager that installs and runs Swift CLI packages. See also Better iOS projects: How to manage your tooling with mint.