|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +### Check we're running in the context of a plugin and get helpful dir variables ### |
| 4 | + |
| 5 | +REPO_DIR="$(git rev-parse --show-toplevel)" |
| 6 | +echo "Running pre-commit hook in repo: $REPO_DIR" |
| 7 | + |
| 8 | +if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then |
| 9 | + PLUGIN_PATH="plugins/${BASH_REMATCH[1]}" |
| 10 | +else |
| 11 | + echo "Not a plugin, not running any further checks" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | +MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||') |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +BASE_COMMAND="" |
| 19 | +# Use local PHP if setup |
| 20 | +if command -v php >/dev/null 2>&1; then |
| 21 | + BASE_COMMAND="${MATOMO_DIR}/vendor/bin/" |
| 22 | + PLUGIN_PATH=$REPO_DIR |
| 23 | +# Use ddev if setup (overridding local setup) |
| 24 | +elif command -v ddev >/dev/null 2>&1; then |
| 25 | + echo "PHP is not installed locally. Checking whether DDEV can be used..." |
| 26 | + if [ -d "$MATOMO_DIR/.ddev" ]; then |
| 27 | + cd "$MATOMO_DIR" || exit 1 |
| 28 | + if ddev status 2>&1 > /dev/null; then |
| 29 | + BASE_COMMAND="ddev exec " |
| 30 | + fi |
| 31 | + fi |
| 32 | +fi |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +# Ensure that PHPCBF command is available |
| 37 | +if [[ -z "${BASE_COMMAND}phpcbf" ]]; then |
| 38 | + echo "No way to run phpcbf found. It should be installed when running Matomo in dev mode." |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | +PHPCBF_COMMAND="${BASE_COMMAND}phpcbf ${PLUGIN_PATH} --standard=${PLUGIN_PATH}/phpcs.xml" |
| 42 | + |
| 43 | +echo "Running PHPCBF to fix any style issues which can be corrected automatically..." |
| 44 | +echo "Command being run: ${PHPCBF_COMMAND}" |
| 45 | +if ! $PHPCBF_COMMAND; then |
| 46 | + echo "Commit cancelled!" |
| 47 | + echo "PHPCBF fixed some style issues. Please review the changes, git add the altered files, and commit again." |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +echo "" |
| 54 | +# Ensure that PHPCS command is available |
| 55 | +if [[ -z "${BASE_COMMAND}phpcs" ]]; then |
| 56 | + echo "No way to run phpcs found. It should be installed when running Matomo in dev mode." |
| 57 | + exit 1 |
| 58 | +fi |
| 59 | +PHPCS_COMMAND="${BASE_COMMAND}phpcs --report-full --standard=${PLUGIN_PATH}/phpcs.xml ${PLUGIN_PATH}" |
| 60 | + |
| 61 | +echo "Running PHPCS check on repo: ${PHPCS_COMMAND}" |
| 62 | +if ! $PHPCS_COMMAND; then |
| 63 | + echo "Commit cancelled!" |
| 64 | + echo "There was an issue found during the PHPCS check. Please fix any issues from the report above and commit again." |
| 65 | + exit 1 |
| 66 | +fi |
| 67 | + |
| 68 | +echo "" |
| 69 | +echo "No issues found during style check. Continuing with commit..." |
| 70 | +echo "" |
| 71 | + |
| 72 | +exit 0 |
0 commit comments