Skip to content

Commit

Permalink
Add SwiftLint command
Browse files Browse the repository at this point in the history
  • Loading branch information
tinder-cfuller committed Feb 1, 2024
1 parent b053325 commit 8674113
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
17 changes: 0 additions & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
# Contributing to Layout

- [Dependencies](#dependencies)
- [Open Package in Xcode](#open-package-in-xcode)
- [Static Analysis](#static-analysis)
- [Testing](#testing)

## Dependencies

Follow the [Swift Package Resources installation instructions](https://github.com/TinderApp/Swift-Package-Resources) to install tooling dependencies.

## Open Package in Xcode

> The file header comment template will also be installed.
Expand All @@ -21,12 +16,6 @@ make open

> SwiftLint violations are visible in Xcode as well.
Package dependencies must be resolved to download the SwiftLint binary.

```
swift package resolve
```

To run SwiftLint from the command line:

```
Expand All @@ -39,12 +28,6 @@ To run analysis rules:
make analyze
```

To enable new rules whenever SwiftLint is upgraded to a new version:

```
make rules
```

## Testing

To re-record all existing snapshot references, delete all using the following command and then run the tests.
Expand Down
16 changes: 4 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#
# The SwiftLint recipes require the Swift Package Resources scripts to be installed.
#
# https://github.com/TinderApp/Swift-Package-Resources#installation
#

.PHONY: open
open: fix
open:
Expand All @@ -24,7 +18,8 @@ fix:
.PHONY: lint
lint: format ?= emoji
lint:
@swiftlint lint --strict --progress --reporter "$(format)"
@swift package plugin \
swiftlint lint --strict --progress --reporter "$(format)"

.PHONY: analyze
analyze: target ?= Layout
Expand All @@ -40,11 +35,8 @@ analyze:
-configuration "Debug" \
CODE_SIGNING_ALLOWED="NO" \
> "$$XCODEBUILD_LOG"; \
swiftlint analyze --strict --progress --reporter "$(format)" --compiler-log-path "$$XCODEBUILD_LOG"

.PHONY: rules
rules:
@swiftlint rules | lint-rules
swift package plugin \
swiftlint analyze --strict --progress --reporter "$(format)" --compiler-log-path "$$XCODEBUILD_LOG"

.PHONY: delete-snapshots
delete-snapshots:
Expand Down
6 changes: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ let package = Package(
plugins: [
.plugin(name: SwiftLint.plugin),
]),
.plugin(
name: "SwiftLintCommand",
capability: .command(intent: .custom(verb: "swiftlint", description: "SwiftLint Command Plugin")),
dependencies: [
.target(name: SwiftLint.binary)
]),
.plugin(
name: SwiftLint.plugin,
capability: .buildTool(),
Expand Down
35 changes: 35 additions & 0 deletions Plugins/SwiftLintCommand/SwiftLintCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//

Check failure on line 1 in Plugins/SwiftLintCommand/SwiftLintCommand.swift

View workflow job for this annotation

GitHub Actions / Swift

Header comments should be consistent with project patterns (file_header)
// Copyright © 2024 Tinder (Match Group, LLC)
//

import Foundation
import PackagePlugin

@main
internal struct SwiftLintCommand: CommandPlugin {

private let toolName: String = "swiftlint"

internal func performCommand(
context: PluginContext,
arguments: [String]
) throws {
let tool: PluginContext.Tool = try context.tool(named: toolName)
guard !arguments.contains("--cache-path")
else { return Diagnostics.error("Setting Cache Path Not Allowed") }
let process: Process = .init()
process.currentDirectoryURL = URL(fileURLWithPath: context.package.directory.string)
process.executableURL = URL(fileURLWithPath: tool.path.string)
process.arguments = arguments + ["--cache-path", "\(context.pluginWorkDirectory.string)"]
try process.run()
process.waitUntilExit()
switch process.terminationReason {
case .exit:
break
case .uncaughtSignal:
return Diagnostics.error("Uncaught Signal")
}
guard process.terminationStatus == EXIT_SUCCESS
else { return Diagnostics.error("Command Failed") }
}
}

0 comments on commit 8674113

Please sign in to comment.