Skip to content

Instantly share code, notes, and snippets.

@aiKrice
Last active June 15, 2024 21:26
Show Gist options
  • Select an option

  • Save aiKrice/9c9a7f4dbb07a93ea4f53567510cc2fb to your computer and use it in GitHub Desktop.

Select an option

Save aiKrice/9c9a7f4dbb07a93ea4f53567510cc2fb to your computer and use it in GitHub Desktop.
Medium pre-commit
#! /bin/zsh
source "githooks/android/toolbox.sh"
hook="Pre-commit check"
progress "$hook"
if [ ! -z "$stagedKotlinFiles" ]; then
#run autoformatter
progress "Autoreformatting the code..."
ktlint -F $stagedKotlinFiles
if [ $? -eq 0 ]; then
printSuccess "Autoreformatting done"
# we added changes if success (you can also add them in the autoformatting step)
git add .
else
showFailureNotification "Autoreformatter" "failed: $?"
exitWithMessage "Ktlint failed"
fi
#Run linter
progress "Detekt lint in pogress..."
detekt -b config/linters/detekt/baseline.xml -c config/linters/detekt/detekt.yml -i $stagedKotlinFiles
if [ $? -eq 0 ]; then
printSuccess "Linting done"
else
showFailureNotification "Detekt failed" "failed: $?"
exitWithMessage "Detekt failed"
fi
#You can also git add . if you linter autocorrect
#Run Androidlinter
progress "AndroidLint in progress..."
./gradlew lint #implies that you define lint {} in your build{.gradle|.kts}
if [ $? -eq 0 ]; then
printSuccess "AndroidLint done"
else
showFailureNotification "AndroidLint failed" "failed: $?"
exitWithMessage "AndroidLint failed"
fi
success "Code sanitized with success"
else
printWarning "No kotlin files staged, checks skipped"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment