Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update library #1

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a3cc53c
fix deprecated options
lukepistrol Oct 31, 2022
2c46eef
add command plugin for fixing
lukepistrol Oct 31, 2022
db8dc51
Merge pull request #5 from lukepistrol/feat/command-plugin
lukepistrol Oct 31, 2022
6ca6c1c
Update README.md
lukepistrol Oct 31, 2022
47f04c1
Update README.md
lukepistrol Nov 8, 2022
ebd70f6
update swiftlint to 0.50.0
lukepistrol Nov 20, 2022
dfdf5d2
update swiftlint to 0.50.1
lukepistrol Nov 27, 2022
74f9c24
update SwiftLint to 0.50.3
lukepistrol Dec 10, 2022
f69b412
Update README.md
lukepistrol Dec 10, 2022
afc4065
Update README.md
lukepistrol Dec 13, 2022
e4aaf65
Update README.md
lukepistrol Dec 13, 2022
f3586ed
Update SwiftLint to 0.51.0
lukepistrol Mar 27, 2023
3592b3b
Bump version to 0.52.0
kenji21 May 11, 2023
37f526f
Merge pull request #9 from openium/main
lukepistrol May 11, 2023
1a16266
update swiftlint to 0.52.1
lukepistrol May 11, 2023
0503293
Merge pull request #10 from lukepistrol/update-swiftlint-binary
lukepistrol May 11, 2023
bfaf669
update swiftlint to 0.52.2
lukepistrol May 12, 2023
d3ec7fb
Merge pull request #11 from lukepistrol/update-swiftlint-binary
lukepistrol May 12, 2023
2843e8f
Create stale.yml
lukepistrol May 15, 2023
c8f1c47
update swiftlint to 0.52.3
lukepistrol Jun 22, 2023
d20d553
update swiftlint to 0.52.4
lukepistrol Jul 7, 2023
b1090ec
Merge branch 'update-swiftlint-binary'
lukepistrol Jul 7, 2023
d1f3b26
Update stale.yml
lukepistrol Aug 20, 2023
c20840c
Disable swiftlint using DISABLE_SWIFTLINT env var
kenji21 Aug 22, 2023
964840e
Merge pull request #13 from openium/disable-linting-using-an-env-var
lukepistrol Aug 22, 2023
a376472
Update README.md
lukepistrol Aug 22, 2023
e5d5096
Update README.md
lukepistrol Aug 22, 2023
7c10744
Update FUNDING.yml
lukepistrol Sep 16, 2023
ea918d6
update to swiftlint 0.53.0
lukepistrol Sep 26, 2023
8e6e84a
Update README.md
lukepistrol Sep 26, 2023
a0f2eda
quickfix for ProcessInfo
lukepistrol Oct 11, 2023
ea6d3ca
update SwiftLint to 0.54.0
lukepistrol Nov 10, 2023
e09a1a5
restructure build command to be reusable, disable caching on Xcode Cl…
lukepistrol Feb 27, 2024
e5dcdd1
Update README.md
lukepistrol Feb 27, 2024
9ecf2bd
update to swiftlint 0.55.0
lukepistrol May 14, 2024
a0f7b12
update SwiftLint to 0.55.1
lukepistrol May 17, 2024
7ad00e3
add workflow (#22)
lukepistrol May 17, 2024
960dafd
only update on changes
lukepistrol May 17, 2024
49a4104
Update SwiftLint to 0.56.1
github-actions[bot] Aug 7, 2024
5a65f40
Update SwiftLint to 0.56.2
github-actions[bot] Aug 23, 2024
bea71d2
Update SwiftLint to 0.57.0
github-actions[bot] Sep 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# These are supported funding model platforms

github: # Replace with Github sponsors username
github: lukepistrol # Replace with Github sponsors username
patreon: # Replace with a single Patreon username
open_collective: # Replace with open collective username
ko_fi: # Replace with a single Ko-fi username
Expand Down
17 changes: 17 additions & 0 deletions .github/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 20
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 7
# Issues with these labels will never be considered stale
exemptLabels:
- pinned
- security
# Label to use when marking an issue as stale
staleLabel: wontfix
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
61 changes: 61 additions & 0 deletions .github/workflows/update-swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Update SwiftLint

on:
schedule:
- cron: '0 0 * * 1' # Runs every Monday at midnight
workflow_dispatch:

jobs:
update-swiftlint:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Get latest SwiftLint release
id: swiftlint_release
run: |
latest_release=$(curl --silent "https://api.github.com/repos/realm/SwiftLint/releases/latest" | jq -r '.tag_name')
echo "Latest release: $latest_release"
echo "::set-output name=latest_release::$latest_release"

- name: Fetch SwiftLint binary URL and checksum
id: fetch_details
run: |
latest_release=${{ steps.swiftlint_release.outputs.latest_release }}
binary_url="https://github.com/realm/SwiftLint/releases/download/${latest_release}/SwiftLintBinary-macos.artifactbundle.zip"
checksum=$(curl -L $binary_url | shasum -a 256 | awk '{ print $1 }')
echo "Binary URL: $binary_url"
echo "Checksum: $checksum"
echo "::set-output name=binary_url::$binary_url"
echo "::set-output name=checksum::$checksum"

- name: Update Swift Package
run: |
binary_url=${{ steps.fetch_details.outputs.binary_url }}
checksum=${{ steps.fetch_details.outputs.checksum }}
sed -i 's|url: "https://github.com/realm/SwiftLint/releases/download/.*"|url: "'"${binary_url}"'"|' Package.swift
sed -i 's|checksum: ".*"|checksum: "'"${checksum}"'"|' Package.swift

- name: Check for changes
id: check_changes
run: |
if git diff --quiet; then
echo "No changes detected"
echo "::set-output name=changes_detected::false"
else
echo "Changes detected"
echo "::set-output name=changes_detected::true"
fi

- name: Commit and push changes
if: steps.check_changes.outputs.changes_detected == 'true'
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
git add Package.swift
git commit -m "Update SwiftLint to ${{ steps.swiftlint_release.outputs.latest_release }}"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 21 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.6
// swift-tools-version: 5.7

import PackageDescription

Expand All @@ -13,19 +13,33 @@ let package = Package(
products: [
.plugin(
name: "SwiftLint",
targets: ["SwiftLint"]),
],
dependencies: [
targets: ["SwiftLint"]
),
.plugin(
name: "SwiftLintFix",
targets: ["SwiftLintFix"]
),
],
targets: [
.binaryTarget(
name: "SwiftLintBinary",
url: "https://github.com/realm/SwiftLint/releases/download/0.47.1/SwiftLintBinary-macos.artifactbundle.zip",
checksum: "82ef90b7d76b02e41edd73423687d9cedf0bb9849dcbedad8df3a461e5a7b555"
url: "https://github.com/realm/SwiftLint/releases/download/0.57.0/SwiftLintBinary-macos.artifactbundle.zip",
checksum: "a1bbafe57538077f3abe4cfb004b0464dcd87e8c23611a2153c675574b858b3a"
),

.plugin(
name: "SwiftLint",
capability: .buildTool(),
dependencies: ["SwiftLintBinary"]),
dependencies: ["SwiftLintBinary"]
),

.plugin(
name: "SwiftLintFix",
capability: .command(
intent: .sourceCodeFormatting(),
permissions: [.writeToPackageDirectory(reason: "Fixes fixable lint issues")]
),
dependencies: ["SwiftLintBinary"]
),
]
)
78 changes: 47 additions & 31 deletions Plugins/SwiftLint/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,53 @@
// https://github.com/realm/SwiftLint/issues/3840#issuecomment-1085699163
//

import Foundation
import PackagePlugin

@main
struct SwiftLintPlugin: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
return buildCommands(
workingDirectory: context.pluginWorkDirectory,
packageDirectory: context.package.directory,
targetDirectory: target.directory,
tool: try context.tool(named: "swiftlint")
)
}

private func buildCommands(
workingDirectory: Path,
packageDirectory: Path,
targetDirectory: Path,
tool: PluginContext.Tool
) -> [Command] {
// If the DISABLE_SWIFTLINT environment variable is set, don't run SwiftLint.
if ProcessInfo.processInfo.environment["DISABLE_SWIFTLINT"] != nil {
return []
}

var arguments = [
"lint",
"--config",
"\(packageDirectory.string)/.swiftlint.yml",
]

// If running in Xcode Cloud, disable caching.
if ProcessInfo.processInfo.environment["CI_XCODE_CLOUD"] == "TRUE" {
arguments.append("--no-cache")
} else {
arguments.append("--cache-path")
arguments.append("\(workingDirectory)")
}

arguments.append(targetDirectory.string)

return [
.buildCommand(
displayName: "Running SwiftLint for \(target.name)",
executable: try context.tool(named: "swiftlint").path,
arguments: [
"lint",
"--in-process-sourcekit",
"--path",
target.directory.string,
"--config",
"\(context.package.directory.string)/.swiftlint.yml",
"--cache-path",
"\(context.pluginWorkDirectory.string)/cache"
],
environment: [:]
.prebuildCommand(
displayName: "Running SwiftLint for \(targetDirectory.lastComponent)",
executable: tool.path,
arguments: arguments,
outputFilesDirectory: workingDirectory.appending("Output")
)
]
}
Expand All @@ -38,23 +65,12 @@ import XcodeProjectPlugin

extension SwiftLintPlugin: XcodeBuildToolPlugin {
func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
return [
.buildCommand(
displayName: "Running SwiftLint for \(target.displayName)",
executable: try context.tool(named: "swiftlint").path,
arguments: [
"lint",
"--in-process-sourcekit",
"--path",
context.xcodeProject.directory.string,
"--config",
"\(context.xcodeProject.directory.string)/.swiftlint.yml",
"--cache-path",
"\(context.pluginWorkDirectory.string)/cache"
],
environment: [:]
)
]
return buildCommands(
workingDirectory: context.pluginWorkDirectory,
packageDirectory: context.xcodeProject.directory,
targetDirectory: context.xcodeProject.directory,
tool: try context.tool(named: "swiftlint")
)
}
}
#endif
43 changes: 43 additions & 0 deletions Plugins/SwiftLintFix/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// File.swift
//
//
// Created by Lukas Pistrol on 31.10.22.
//

import PackagePlugin
import Foundation

@main
struct MyCommandPlugin: CommandPlugin {

func performCommand(context: PluginContext, arguments: [String]) throws {
let tool = try context.tool(named: "swiftlint")
let toolUrl = URL(fileURLWithPath: tool.path.string)

for target in context.package.targets {
guard let target = target as? SourceModuleTarget else { continue }

let process = Process()
process.executableURL = toolUrl
process.arguments = [
"--fix",
"--config",
"\(context.package.directory.string)/.swiftlint.yml",
"\(target.directory)"
]

print(toolUrl.path, process.arguments!.joined(separator: " "))

try process.run()
process.waitUntilExit()

if process.terminationReason == .exit && process.terminationStatus == 0 {
print("Formatted the source code in \(target.directory).")
} else {
let problem = "\(process.terminationReason):\(process.terminationStatus)"
Diagnostics.error("swift-format invocation failed: \(problem)")
}
}
}
}
55 changes: 39 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<p>
<img src="https://img.shields.io/badge/Swift-5.6-f05318.svg" />
<img src="https://img.shields.io/badge/iOS->= 13.0-blue.svg" />
<img src="https://img.shields.io/badge/macOS->= 10.15-blue.svg" />
<img src="https://img.shields.io/badge/watchOS->= 6.0-blue.svg" />
<img src="https://img.shields.io/badge/tvOS->= 13.0-blue.svg" />
<img alt="GitHub" src="https://img.shields.io/github/license/lukepistrol/SwiftLintPlugin">
<a href="https://twitter.com/lukeeep_">
<img src="https://img.shields.io/badge/Twitter-@lukeeep_-1e9bf0.svg?style=flat" alt="Twitter: @lukeeep_" />
</a>
</p>

# SwiftLintPlugin

A Swift Package Plugin for [SwiftLint](https://github.com/realm/SwiftLint/) that will run SwiftLint on build time and show errors & warnings in Xcode.
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flukepistrol%2FSwiftLintPlugin%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/lukepistrol/SwiftLintPlugin)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Flukepistrol%2FSwiftLintPlugin%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/lukepistrol/SwiftLintPlugin)
[![](https://img.shields.io/github/license/lukepistrol/SwiftLintPlugin)](https://github.com/lukepistrol/SwiftLintPlugin/blob/main/LICENSE)
[![Twitter: @lukeeep_](https://img.shields.io/badge/Twitter-@lukeeep_-1e9bf0.svg?style=flat)](https://twitter.com/lukeeep_)

Once SwiftLint offers their own implementation, this will be obsolete.
A Swift Package Plugin for [SwiftLint](https://github.com/realm/SwiftLint/) that will run SwiftLint on build time and show errors & warnings in Xcode.

> Implementation proposed [here](https://github.com/realm/SwiftLint/issues/3840#issuecomment-1085699163) by [@marcoboerner](https://github.com/marcoboerner).
> **Note**
> There now is an official version in the [SwiftLint repo](https://github.com/realm/SwiftLint#plug-in-support)!
> Though this package will still be maintained and updated since it brings the benefit of being a smaller repository and
> therefore faster to download as a dependency

## Add to Package

Expand All @@ -25,7 +19,7 @@ First add a dependency from this package:
```swift
dependencies: [
// ...
.package(url: "https://github.com/lukepistrol/SwiftLintPlugin", from: "0.0.4"),
.package(url: "https://github.com/lukepistrol/SwiftLintPlugin", from: "0.2.2"),
]
```

Expand All @@ -51,6 +45,35 @@ Starting with Xcode 14, plugins can also work on Xcode Project's targets. To do

> You may need to enable & trust the plugin before you can actually run it during builds.

## Fix Warnings

As of version `0.1.0` this package also includes a command plugin which can be called on any target.

1. Select a project or package in the project navigator.
2. Richt-click and select `SwiftLintFix`.
- alternatively you can select `File > Packages > SwiftLintFix`.
3. Choose the target(s) to run the `swiftlint --fix` command on.

<img width="224" alt="Screenshot 2022-10-31 at 12 59 53" src="https://user-images.githubusercontent.com/9460130/199005629-b214758f-e184-4b3b-8031-e6364c6549c7.png">

## Run on CI

Important to notice is that when building a package/project on any CI provider (e.g. GitHub Actions) it is mandatory to pass the `-skipPackagePluginValidation` flag to the `xcodebuild` command. This will skip the validation prompt which in Xcode looks like this:

<img width="263" alt="Screenshot 2022-12-13 at 17 48 44" src="https://user-images.githubusercontent.com/9460130/207394170-9490e687-e066-4bfa-862c-a4f816b6b43b.png">

### Example

```bash
xcodebuild \
-scheme "$SCHEME" \
-destination "$PLATFORM" \
-skipPackagePluginValidation \ # this is mandatory
clean build
```

If you need to disable linting (for release/app store builds), you can set`DISABLE_SWIFTLINT` environment variable

-----

<a href="https://www.buymeacoffee.com/lukeeep" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>