how to add swiftlint to third party packages

Asked on 07/31/2024

1 search

To add SwiftLint to third-party packages, you can follow these general steps:

  1. Add SwiftLint as a Dependency: You need to add SwiftLint as a dependency in your Package.swift file. This can be done by including it in the dependencies array.

    // swift-tools-version:5.3
    import PackageDescription
    
    let package = Package(
        name: "YourPackageName",
        dependencies: [
            .package(url: "https://github.com/realm/SwiftLint.git", from: "0.43.0")
        ],
        targets: [
            .target(
                name: "YourTargetName",
                dependencies: ["SwiftLint"]
            )
        ]
    )
    
  2. Configure SwiftLint: Create a .swiftlint.yml file in the root of your package to configure the rules and settings for SwiftLint.

    disabled_rules: # rule identifiers to exclude from running
      - line_length
      - type_name
    
    opt_in_rules: # some rules are only opt-in
      - empty_count
      - missing_docs
    
    included: # paths to include during linting. `--path` is ignored if present.
      - Source
    
    excluded: # paths to ignore during linting. Takes precedence over `included`.
      - Carthage
      - Pods
    
  3. Run SwiftLint: You can run SwiftLint manually from the command line or integrate it into your build process. To run it manually, navigate to your package directory and execute:

    swift run swiftlint
    
  4. Integrate with Xcode: If you are using Xcode, you can add a build phase to run SwiftLint automatically. Go to your Xcode project settings, select your target, and add a new "Run Script Phase" with the following script:

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

For more detailed information on working with Swift packages, you can refer to the session A Swift Tour: Explore Swift’s features and design.

how to add swiftlint to third party packages | Ask WWDC