Search code examples
iosswift-package-managerswiftlint

What should be the path of SwiftLint when installed with Swift Package Dependency


I have installed SwiftLint with Swift Package Dependancy. I have added the following Run script in Build Phases as described in the manual:

if which swiftlint >/dev/null; then
  swiftlint
else
  echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi

When running my project, I receive the warning message:

warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint

So I guess thy system can't find the swiftlint executable. Where is it located if installed with Swift Package Dependency?


Solution

  • You can't and you can at the same time.

    The Swift Package Manager (SPM) "integrated" in Xcode can be used for "embedding code" into your app. Not to embed an executable (like Swiftlint).

    So, the solution is do create a "parallel" project with SPM alongside your app.

    It's described in the following two links:

    It's time to use Swift Package Manager
    Using SPM for Xcode build phase tools

    So the idea, is to have a Package.swift with Swiftlint, build/run it and it should launch then Swiftlint.
    The first time, it will fetch the code, build and then run. So it might take some times.

    It's also the solution given by SwiftFormat on their ReadMe.

    Applied here, here the file hierarchy. I added a folder & a file.

    MyProject:
    .
    ├── BuildTools
    │   └── Package.swift
    ├── MyProject
    │   ├── .swiftlint.yml
    │   ├── AppDelegate.swift
    │   ├── Assets.xcassets
    │   │   ├── AppIcon.appiconset
    │   │   │   └── Contents.json
    │   │   └── Contents.json
    │   ├── Base.lproj
    │   │   ├── LaunchScreen.storyboard
    │   │   └── Main.storyboard
    │   ├── Info.plist
    │   ├── SceneDelegate.swift
    │   └── ViewController.swift
    └── MyProject.xcodeproj
    
    

    In Build Phases script:

    cd BuildTools
    SDKROOT=(xcrun --sdk macosx --show-sdk-path)
    #swift package update #Uncomment this line temporarily to update the version used to the latest matching your BuildTools/Package.swift file
    swift run -c release swiftlint "$SRCROOT" --config ../MyProject/.swiftlint.yml --path ../MyProject
    

    And Package.swift being:

    // swift-tools-version:5.5
    import PackageDescription
    
    let package = Package(
        name: "BuildTools",
        platforms: [.macOS(.v10_11)],
        dependencies: [
             .package(url: "https://github.com/Realm/SwiftLint", from: "0.44.0")
        ],
        targets: [.target(name: "BuildTools", path: "")]
    )
    

    Since we are creating another project and not taking advantage of the integrated Xcode integrated solution, solution like using Mint, HomeBrew, etc. might be better. Mint is, for instance, a "full SPM tool". If you have more tools, might be indeed a good solution to concentrate them into one.