Skip to content

Commit

Permalink
added swiftformat to precommit script and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-b committed Mar 4, 2025
1 parent f3f8e5c commit 09e2fb0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
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

0 comments on commit 09e2fb0

Please sign in to comment.