|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Usage |
| 4 | +# To install the typechecking linters, simply run the script in your terminal: |
| 5 | + |
| 6 | +# sh |
| 7 | +# This will install the linters and set up the pre-commit hook to run them on each changed Python file. |
| 8 | +# -o, --post-commit, -p, --pre-commit, default -o post commit install |
| 9 | + |
| 10 | +# Requirements |
| 11 | +# This script requires the following tools to be installed: |
| 12 | + |
| 13 | +# Git |
| 14 | +# Black |
| 15 | +# Ruff |
| 16 | +# Mypy |
| 17 | +# Make sure these tools are installed and available in your system's PATH before running the script. |
| 18 | + |
| 19 | +# Define the hook script |
| 20 | +HOOK_SCRIPT="#!/bin/bash |
| 21 | +# Get the list of changed Python files in the darwin/future folder |
| 22 | +FILES=\$(git diff --diff-filter=MA --name-only master | grep 'darwin/future/.*\.py$') |
| 23 | +if [ -z \"\$FILES\" ]; then |
| 24 | + exit 0 |
| 25 | +fi |
| 26 | +RED='\033[0;31m' |
| 27 | +GREEN='\033[0;32m' |
| 28 | +echo Typechecking Hook |
| 29 | +echo ---------------------------------------- |
| 30 | +echo checking \$FILES |
| 31 | +# Run the linters on each changed file |
| 32 | +echo -e '\nRunning Black' |
| 33 | +echo ---------------------------------------- |
| 34 | +BLACK_FAILED=0 |
| 35 | +black --check \$FILES || BLACK_FAILED=1 |
| 36 | +
|
| 37 | +echo -e '\nRunning Ruff' |
| 38 | +echo ---------------------------------------- |
| 39 | +RUFF_FAILED=0 |
| 40 | +ruff check \$FILES || RUFF_FAILED=1 |
| 41 | +
|
| 42 | +echo -e '\nRunning Mypy' |
| 43 | +echo ---------------------------------------- |
| 44 | +MYPY_FAILED=0 |
| 45 | +mypy \$FILES || MYPY_FAILED=1 |
| 46 | +
|
| 47 | +# Check if any linter failed |
| 48 | +echo Summary |
| 49 | +echo ---------------------------------------- |
| 50 | +if [ \$BLACK_FAILED -eq 1 ]; then |
| 51 | + echo -e \"\${RED}Black failed.\" |
| 52 | +fi |
| 53 | +if [ \$RUFF_FAILED -eq 1 ]; then |
| 54 | + echo -e \"\${RED}Ruff failed.\" |
| 55 | +fi |
| 56 | +if [ \$MYPY_FAILED -eq 1 ]; then |
| 57 | + echo -e \"\${RED}Mypy failed.\" |
| 58 | +fi |
| 59 | +if [ \$BLACK_FAILED -eq 0 ] && [ \$RUFF_FAILED -eq 0 ] && [ \$MYPY_FAILED -eq 0 ]; then |
| 60 | + echo -e \"\${GREEN}All checks passed.\" |
| 61 | +fi |
| 62 | +" |
| 63 | + |
| 64 | +# Define the hook name |
| 65 | +HOOK_NAME="linters" |
| 66 | + |
| 67 | +# Define the hook directory |
| 68 | +HOOK_DIR="$(git rev-parse --show-toplevel)/.git/hooks" |
| 69 | + |
| 70 | +# Define the hook file names |
| 71 | +PRE_COMMIT_FILE="$HOOK_DIR/pre-commit" |
| 72 | +POST_COMMIT_FILE="$HOOK_DIR/post-commit" |
| 73 | + |
| 74 | +# Define the hook file names with the hook name |
| 75 | +PRE_COMMIT_HOOK_FILE="$HOOK_DIR/pre-commit" |
| 76 | +POST_COMMIT_HOOK_FILE="$HOOK_DIR/post-commit" |
| 77 | + |
| 78 | +# Define the default hook file name |
| 79 | +DEFAULT_HOOK_FILE="$POST_COMMIT_FILE" |
| 80 | + |
| 81 | +# Define the default hook type |
| 82 | +DEFAULT_HOOK_TYPE="post-commit" |
| 83 | + |
| 84 | +# Parse the command line arguments |
| 85 | +while [[ $# -gt 0 ]] |
| 86 | +do |
| 87 | + key="$1" |
| 88 | + |
| 89 | + case $key in |
| 90 | + -p|--pre-commit) |
| 91 | + DEFAULT_HOOK_FILE="$PRE_COMMIT_FILE" |
| 92 | + DEFAULT_HOOK_TYPE="pre-commit" |
| 93 | + shift |
| 94 | + ;; |
| 95 | + -o|--post-commit) |
| 96 | + DEFAULT_HOOK_FILE="$POST_COMMIT_FILE" |
| 97 | + DEFAULT_HOOK_TYPE="post-commit" |
| 98 | + shift |
| 99 | + ;; |
| 100 | + *) |
| 101 | + echo "Unknown option: $key" |
| 102 | + exit 1 |
| 103 | + ;; |
| 104 | + esac |
| 105 | +done |
| 106 | + |
| 107 | +# Create the hook file |
| 108 | +echo "$HOOK_SCRIPT" > "$DEFAULT_HOOK_FILE" |
| 109 | + |
| 110 | +# Make the hook file executable |
| 111 | +chmod +x "$DEFAULT_HOOK_FILE" |
0 commit comments