Skip to content

Instantly share code, notes, and snippets.

@mcany
Created February 5, 2019 10:53
Show Gist options
  • Save mcany/eb392a05d00cbefad1f7d9a17d40c3b9 to your computer and use it in GitHub Desktop.
Save mcany/eb392a05d00cbefad1f7d9a17d40c3b9 to your computer and use it in GitHub Desktop.
#bin/bash
set -e
set -o pipefail
# Run SwiftLint
START_DATE=$(date +"%s")
SWIFT_LINT=/usr/local/bin/swiftlint
SWIFT_LINT_YAML=.swiftlint.yml
EXCLUDED=($(sed -nE '/excluded:/,/(^$|:$)/p' ${SWIFT_LINT_YAML} | grep -v ':' | grep -v -e '^[[:space:]]*$' | sed -e 's/ - //g'))
contains () {
local seeking=$1
local in=1
for element in "${EXCLUDED[@]}"; do
if [ "$seeking" != "${seeking%$element*}" ]; then
in=0
break
fi
done
return $in
}
# Run SwiftLint for given filename
run_swiftlint() {
local filename="${1}"
if [[ "${filename##*.}" == "swift" ]] && [[ $(contains $filename) == 1 ]]; then
${SWIFT_LINT} autocorrect --path "${filename}" --quiet
${SWIFT_LINT} lint --strict --path "${filename}" --quiet | sed 's/warning:/error:/g'
fi
}
if [[ -e "${SWIFT_LINT}" ]]; then
echo "SwiftLint version: $(${SWIFT_LINT} version)"
# Run for both staged and unstaged files
git diff --diff-filter=d --name-only | while read filename; do run_swiftlint "${filename}"; done
git diff --diff-filter=d --cached --name-only | while read filename; do run_swiftlint "${filename}"; done
else
echo "${SWIFT_LINT} is not installed."
exit 0
fi
END_DATE=$(date +"%s")
DIFF=$(($END_DATE - $START_DATE))
echo "SwiftLint took $(($DIFF / 60)) minutes and $(($DIFF % 60)) seconds to complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment