diff --git a/lint.sh b/lint.sh index 1ec2dec115..bd86fdd2cb 100755 --- a/lint.sh +++ b/lint.sh @@ -1,5 +1,11 @@ #!/bin/bash +FIX=false + +if [[ "$1" == "--fix" ]]; then + FIX=true +fi + if [[ -n "$CI" ]]; then echo "Skipping SwiftLint run in CI" exit 0 @@ -7,7 +13,7 @@ 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 () { @@ -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}\"" @@ -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 @@ -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 \ No newline at end of file diff --git a/scripts/pre-commit.sh b/scripts/pre-commit.sh new file mode 100755 index 0000000000..cdbb1348ff --- /dev/null +++ b/scripts/pre-commit.sh @@ -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 -- "$@"