MohammadReza Ansari
MohammadReza Ansari

Reputation: 222

Is there any way to apply SwiftLint to SwiftPackages

I'm struggling to find a way to apply SwiftLint to my swift packages.
But after searching a lot, I couldn't find any way.

I use micro application architecture, which leads me to many micro packages. I am looking for a way to use SwiftLint for my swift packages.

Upvotes: 5

Views: 2116

Answers (5)

musakokcen
musakokcen

Reputation: 394

If anybody has this issue nowadays, I think that the solution is a sample project which locally imports the packages. Let's say the following is your project structure;

Main Folder
 - sample (folder)
 - microPackage1 (folder)
 - microPackage2 (folder)
 - swiftlint.yml

You need to specify the directories of packages in the swiftlint.yml such as;

included:
  - microPackage1
  - microPackage2 excluded:
  - sample // (eg: if you have tests in sample and you do not want to swiftlint them)

since sample.xcodeproj is under sample folder and the yml file will be in the parent folder, you should specify the path in the run script phase to that;

swiftlint --config ${SRCROOT}/../.swiftlint.yml

Upvotes: 0

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119148

You can use it as a SPM plugin in your package.swift file:

let package = Package(
    name: "MyPackage",
    products: [
        .library(name: "MyPackage", targets: ["MyPackage"])
    ],
    dependencies: [
        .package(url: "https://github.com/realm/SwiftLint", from: "0.52.4") // 👈 add here
    ],
    targets: [
        .target(
            name: "MyPackage",
            dependencies: [
                .product(name: "MyPackage", package: "MyPackage")
            ],
            plugins: [
                .plugin(name: "SwiftLintPlugin", package: "SwiftLint")  // 👈 use here
            ]
        )
    ]
)

Upvotes: 0

Hattori Hanzō
Hattori Hanzō

Reputation: 2483

Ok, SwiftLint just updated main repository, commit 3fd1573 added support through SwiftPM & Run Build Tool Plug-ins. Take in account that you need Xcode 14 or later to use this approach.

Upvotes: 0

Andrew Bogaevskyi
Andrew Bogaevskyi

Reputation: 2615

I have the same problem. The current version of SPM does not support Run Scripts so you can't run SwiftLint in a way like on the main project. But future versions of SPM will support it.

Currently, I use the command line to find all the issues. This is not very convenient but something.

In the package folder:

swiftlint --config [path to swiftlint.yml] > [path to file to save a report]

e.g.

swiftlint --config ~/Desktop/swiftlint.yml > ~/Desktop/swiftlintreport.txt

Upvotes: 4

EmilioPelaez
EmilioPelaez

Reputation: 19884

When I'm working on a SPM package I usually create an Xcode project that locally imports the package and use that project to work on the project.

This allows me to run stuff like SwiftLint. It's also handy to remember to use the correct access control modifiers.

You can see an example in this project of mine.

Upvotes: -1

Related Questions