3

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?

Luda
  • 7,282
  • 12
  • 79
  • 139
  • https://artsy.github.io/blog/2019/01/05/its-time-to-use-spm/ or https://blog.apptekstudios.com/2019/12/spm-xcode-build-tools/ ? – Larme Oct 07 '21 at 12:27
  • @Larme, I didn't find an answer in these blogs unfortunately. Maybe you would consider creating an empty project and try to install SwiftLint with SPM. Maybe it will work for you and the rest of the world could benefit. :) – Luda Oct 10 '21 at 09:23
  • I didn't test, but the links sample seems pretty forward. Especially https://blog.apptekstudios.com/2019/12/spm-xcode-build-tools/ In otherwords: Don't rely on XCode SPM integration, but instead do it the old way with a `Package.swift` In this one add the dependency you need. Then in Build Phase, you can "retrieve that "Package.swift", and "run it" with `swift run etc.` (this will download the linked packages, build, and run). This will allow you access then Swiftlint, SwiftFormat etc, and use it in the build phase. It's like a "side project" for tooling. – Larme Oct 11 '21 at 12:52
  • @Larme SwiftFormat works with SPM, SwiftLint no unfortunately. – Luda Oct 12 '21 at 05:50
  • And yet, the solution is given there. It's also the solution given by `SwiftFormat` on their ReadMe: https://github.com/nicklockwood/SwiftFormat#xcode-build-phase – Larme Oct 12 '21 at 13:59

1 Answers1

2

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.

Vadim Belyaev
  • 2,456
  • 2
  • 18
  • 23
Larme
  • 24,190
  • 6
  • 51
  • 81
  • I gave you the points, couse you were so nice and took the time to write a detailed answer, but unfortunately it didn't work for me. I have created a new project, added a SwiftLint package, added the BuildTools folder and Package.swift with content described above and added Build Phases script. When running, I get the following error: no executable product named 'swiftlint'. Did you have the same issue or did it go smoothly for you? – Luda Oct 14 '21 at 08:10
  • I had to clean from times to times derived data too. I got that issue at first when trying to understand the tutorial I linked. Since I have Swiftlint with Homebrew (I think), I don't this it's running it through it tough, but I'm not 100% sure (and I don't want to remove it, to test and reinstall it), but I think my solution worked. You don't add the Swiftlint Package in the new project. – Larme Oct 14 '21 at 08:25