Skip to content

Commit

Permalink
added swiftlint to BuildAndTest workflow
Browse files Browse the repository at this point in the history
also added swiftlint precommit hook script with an installation script.
  • Loading branch information
bryce-b committed Jan 10, 2025
1 parent bf9cd75 commit 8b39a9b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/BuildAndTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ name: Build and Test
on: [push, pull_request, workflow_dispatch]

jobs:
SwiftLint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: GitHub Action for SwiftLint (Only files changed in the PR)
uses: norio-nomura/[email protected]
env:
args: --strict
DIFF_BASE: ${{ github.base_ref }}
macOS:
runs-on: macos-15
steps:
Expand Down
18 changes: 18 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
included:
- "Sources"
excluded:
- "Sources/Exporters/OpenTelemetryProtocolCommon/proto"
- ".build"
disabled_rules:
- nesting
- line_length
- type_body_length
- identifier_name
- file_length
- force_cast
- type_name
- large_tuple
- function_body_length
- function_parameter_count
- cyclomatic_complexity
- force_try
9 changes: 9 additions & 0 deletions Scripts/hooks/install-precommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

HOOK_DIR=${SCRIPT_DIR}/../../.git/hooks

cp "${SCRIPT_DIR}/precommit-script.sh" "${HOOK_DIR}/precommit"

chmod 0755 "${HOOK_DIR}/precommit"
32 changes: 32 additions & 0 deletions Scripts/hooks/precommit-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/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$"))
IFS="$OIFS"

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

0 comments on commit 8b39a9b

Please sign in to comment.