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

added swiftformat to precommit script and docs #694

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ To test from the command line you need `swift` version `5.0+`.
```sh
swift test
```

### SwiftLint
the SwiftLint Xcode plugin can be optionally enabled during development by using an environmental variable when opening the project from the commandline.
### Linting
#### SwiftLint
The SwiftLint Xcode plugin can be optionally enabled during development by using an environmental variable when opening the project from the commandline.
```
OTEL_ENABLE_SWIFTLINT=1 open Package.swift
```
Note: Xcode must be completely closed before running the above command, close Xcode using `⌘Q` or running `killall xcode` in the commandline.

#### SwiftFormat
SwiftFormat is also used to enforce formatting rules where Swiftlint isn't able.
It will also run in the optionally enabled pre-commit hook if installed via `brew install swiftformat`.

### Make your modifications

Always work in a branch from your fork:
Expand Down
47 changes: 26 additions & 21 deletions Scripts/hooks/precommit-script.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#!/bin/bash

if ! command -v swiftlint 2>&1 >/dev/null
then

echo "install swiftlint \(v0.0.57\) for pre-commit linting."
exit 0
fi

if [ "$(swiftlint --version)" != "0.57.1" ]
then
echo "swiftlint installed with incorrect version (`swiftlint --version`). Please use version 0.57.1 for pre-commit linting."
exit 0
fi
OIFS="$IFS"
IFS=$'\n'
FILE_LIST=($(git diff --cached --name-only | grep "\.swift$"))
Expand All @@ -21,12 +9,29 @@ if [ -z "${FILE_LIST}" ]; then
exit 0
fi

swiftlint lint --config .swiftlint.yml --strict "${FILE_LIST[@]}"
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo -e "Swiftlint failed!\n Fix issues above or run:\n\tswiftlint lint --fix $(printf "\"%s\" " "${FILE_LIST[@]}")"
echo "Skip linting with '--no-verify'."
exit 1
fi
echo "Swiftlint passed!"
exit 1
if [ -e $(command -v swiftlint 2>&1 >/dev/null) ] && [ "$(swiftlint --version)" == "0.57.1" ]
then
swiftlint lint --config .swiftlint.yml --strict "${FILE_LIST[@]}"
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo -e "Swiftlint failed!\n Fix issues above or run:\n\tswiftlint lint --fix $(printf "\"%s\" " "${FILE_LIST[@]}")"
echo "Skip linting with '--no-verify'."
exit 1
fi
echo "Swiftlint passed!"
else
echo "Install SwiftLint at version 0.57.1 to enable pre-commit linting."
fi

if [ -e $(command -v swiftformat 2>&1 > /dev/null) ]; then
swiftformat --lint "${FILE_LIST[@]}"
RESULT=$?
if [ $RESULT -ne 0 ]; then
echo -e "Swiftformat linting failed.\n Fix the issues above or run:\n\tswiftformat $(printf "\"%s\" " "${FILE_LIST[@]}")"
echo "Skip linting with '--no-verify'."
exit 1
fi
echo "SwiftFormat passed!"
else
echo "Install swiftformat to enable pre-commit format linting."
fi
Loading