Skip to content

Commit

Permalink
[FEAT] Githook pre-commit SwiftLint 적용(#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
noah0316 committed Jan 11, 2022
1 parent d0297ce commit 74728aa
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh
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

##### Check for modified files in unstaged/Staged area #####
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

echo "${SCRIPT_INPUT_FILE_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

0 comments on commit 74728aa

Please sign in to comment.