Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
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
30 changes: 23 additions & 7 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/bin/bash

FIX=false

if [[ "$1" == "--fix" ]]; then
FIX=true
fi

if [[ -n "$CI" ]]; then
echo "Skipping SwiftLint run in CI"
exit 0
fi

# Add brew into PATH
if [[ -f /opt/homebrew/bin/brew ]]; then
eval $(/opt/homebrew/bin/brew shellenv)
eval $(/opt/homebrew/bin/brew shellenv)
fi

run_swiftlint_for_modified_files () {
Expand All @@ -19,7 +25,7 @@ run_swiftlint_for_modified_files () {
do
# collect .swift files separately for Unit Tests and Code Files
if [[ "${FILE_NAME##*.}" == "swift" ]]; then
# check if file exists (replacing back #001 with space)
# check if file exists (replacing back #001 with space)
if [ -f "`echo "${FILE_NAME}" | tr '\001' ' '`" ]; then
if [[ "$FILE_NAME" == *"Tests/"* ]]; then
TEST_FILES+=" \"${FILE_NAME}\""
Expand All @@ -30,18 +36,28 @@ run_swiftlint_for_modified_files () {
fi
done

SWIFTLINT_COMMAND="swiftlint lint"
if $FIX; then
SWIFTLINT_COMMAND="swiftlint lint --fix"
fi

if [ -n "${CODE_FILES}" ]; then
# replace back #001 with space and feed to swiftlint
echo "${CODE_FILES}" | tr '\001' ' ' | xargs swiftlint lint --force-exclude
# replace back #001 with space and feed to swiftlint
echo "${CODE_FILES}" | tr '\001' ' ' | xargs $SWIFTLINT_COMMAND
fi
if [ -n "${TEST_FILES}" ]; then
echo "${TEST_FILES}" | tr '\001' ' ' | xargs swiftlint lint --force-exclude --config .swiftlint.tests.yml
echo "${TEST_FILES}" | tr '\001' ' ' | xargs $SWIFTLINT_COMMAND --config .swiftlint.tests.yml
fi
}

if which swiftlint >/dev/null; then
if [ "$CONFIGURATION" = "Release" ]; then
swiftlint lint --strict
if $FIX; then
swiftlint lint --fix --strict
else
swiftlint lint --strict
fi

if [ $? -ne 0 ]; then
echo "error: SwiftLint validation failed."
exit 1
Expand All @@ -52,4 +68,4 @@ if which swiftlint >/dev/null; then
else
echo "error: SwiftLint not installed. Install using \`brew install swiftlint\`"
exit 1
fi
fi
4 changes: 4 additions & 0 deletions scripts/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

SCRIPT_URL="https://raw.githubusercontent.com/duckduckgo/BrowserServicesKit/main/scripts/pre-commit.sh"
curl -s "${SCRIPT_URL}" | bash -s -- "$@"