Last active
September 3, 2023 10:29
-
-
Save noah0316/e9875fe05dbfebded076e3496bbc5cab to your computer and use it in GitHub Desktop.
SwiftLint를 Git Hook과 함께 사용하는 방법을 소개합니다 :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# written by Noah | |
# 자세한 사용 방법은 https://noah-ios.dev/swiftlint-githooks/ 제 블로그를 참조해주세요! | |
LINT=$(which swiftlint) | |
if [[ -e "${LINT}" ]]; then | |
echo "🚀 SwiftLint 시작..." | |
echo "🔍 lint 적용 경로: $(pwd)" | |
count=0 | |
for file_path in $(git ls-files -m --exclude-from=.gitignore | grep ".swift$"); do | |
export SCRIPT_INPUT_FILE_$count=$file_path | |
count=$((count + 1)) | |
done | |
else | |
echo "SwiftLint가 존재하지 않습니다, 공식문서를 확인해주세요. 🌐 https://github.com/realm/SwiftLint" | |
exit 1 | |
fi | |
# Unstaged/Staged area에서 수정된 swift 파일 확인 | |
for file_path in $(git diff --name-only --cached | grep ".swift$"); do | |
export SCRIPT_INPUT_FILE_$count=$file_path | |
count=$((count + 1)) | |
done | |
export SCRIPT_INPUT_FILE_COUNT=$count | |
# lint rule 정의 파일 | |
RESULT=$($LINT lint --quiet --use-script-input-files --config .precommit.yml) | |
if [ "$RESULT" == '' ]; then | |
printf "✨ SwiftLint 적용을 완료했습니다!! 고생하셨습니다👏 👏 👏\n" | |
else | |
echo "" | |
printf "✔ SwiftLint Failed 아래 내용을 확인해주세요:\n" | |
while read -r line; do | |
FILEPATH=$(echo $line | cut -d : -f 1) | |
L=$(echo $line | cut -d : -f 2) | |
C=$(echo $line | cut -d : -f 3) | |
TYPE=$(echo $line | cut -d : -f 4 | cut -c 2-) | |
MESSAGE=$(echo $line | cut -d : -f 5 | cut -c 2-) | |
DESCRIPTION=$(echo $line | cut -d : -f 6 | cut -c 2-) | |
if [ $TYPE == 'warning' ]; then | |
printf "\n 🚧 $TYPE\n" | |
elif [ $TYPE == 'error' ]; then | |
printf "\n 🚨 $TYPE\n" | |
fi | |
printf " ✔ $FILEPATH:$L:$C\n" | |
printf " 📌 $MESSAGE: - $DESCRIPTION\n" | |
done <<< "$RESULT" | |
printf "\n 🚑 커밋실패!! Swiftlint rule에 맞게 코드를 변경해주세요:)\n" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment