├── .gitignore ├── Images └── Screenshot.png ├── Package.swift ├── Plugins └── SwiftLintXcode │ └── plugin.swift ├── README.md └── Sources └── MyApp └── main.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/config/registries.json 8 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 9 | .netrc 10 | -------------------------------------------------------------------------------- /Images/Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/juozasvalancius/ExampleSPMProjectWithSwiftLint/03c1bc0c16b06485c39a221d19487900b62abe18/Images/Screenshot.png -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.6 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "MyApp", 7 | products: [ 8 | .executable(name: "MyApp", targets: ["MyApp"]), 9 | ], 10 | targets: [ 11 | // 1. Specify where to download the compiled swiftlint tool from. 12 | .binaryTarget( 13 | name: "SwiftLintBinary", 14 | url: "https://github.com/juozasvalancius/SwiftLint/releases/download/spm-accommodation/SwiftLintBinary-macos.artifactbundle.zip", 15 | checksum: "cdc36c26225fba80efc3ac2e67c2e3c3f54937145869ea5dbcaa234e57fc3724" 16 | ), 17 | // 2. Define the SPM plugin. 18 | .plugin( 19 | name: "SwiftLintXcode", 20 | capability: .buildTool(), 21 | dependencies: ["SwiftLintBinary"] 22 | ), 23 | // 3. Use the plugin in your project. 24 | .executableTarget( 25 | name: "MyApp", 26 | plugins: ["SwiftLintXcode"] 27 | ) 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /Plugins/SwiftLintXcode/plugin.swift: -------------------------------------------------------------------------------- 1 | import PackagePlugin 2 | 3 | @main 4 | struct SwiftLintPlugins: BuildToolPlugin { 5 | func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] { 6 | return [ 7 | .buildCommand( 8 | displayName: "Linting \(target.name)", 9 | executable: try context.tool(named: "swiftlint").path, 10 | arguments: [ 11 | "lint", 12 | "--in-process-sourcekit", // alternative to the environment variable 13 | "--path", 14 | target.directory.string // only lint the files in the target directory 15 | ], 16 | // environment: ["IN_PROCESS_SOURCEKIT": "YES"] // doesn't work in Xcode 13.3 17 | environment: [:] 18 | ) 19 | ] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example SPM Project With SwiftLint 2 | 3 | ![](Images/Screenshot.png) 4 | 5 | ## How to implement it in your project 6 | 7 | 1. Check the `Package.swift` file to see how to add SwiftLint to your Swift 8 | package. 9 | 10 | 2. Copy the `Plugins/SwiftLintXcode/plugin.swift` file to your project. 11 | 12 | 3. Done! Build your package with Xcode and SwiftLint will display warnings and 13 | errors in Xcode. 14 | 15 | This is the only way I was able to get it working in Xcode 13.3. Ideally, the 16 | plugin itself could be declared in some shared repository that everyone could 17 | use, so they wouldn't need to manually add swiftlint binary checksums or 18 | implement the plugin. However, I wasn't able to get that working. 19 | -------------------------------------------------------------------------------- /Sources/MyApp/main.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | let someForceCast = NSObject() as! Int 4 | let colonOnWrongSide :Int = 0 5 | --------------------------------------------------------------------------------