Skip to content

Instantly share code, notes, and snippets.

@ppabcd
Last active November 8, 2023 08:29
Show Gist options
  • Save ppabcd/176d0276e047f89b16596f9a5fdb4aa2 to your computer and use it in GitHub Desktop.
Save ppabcd/176d0276e047f89b16596f9a5fdb4aa2 to your computer and use it in GitHub Desktop.
Laravel 10 pre-commit
#!/bin/sh
FILE="${PWD}/.php-cs-fixer.php"
COMPOSER_PATH="/usr/bin/composer"
COMMAND="lint"
CONFIG_URL="https://gist.githubusercontent.com/ppabcd/0dc95e7130ba46e9edc095ca57f23af6/raw/db5b07ee97ddbd2e2183d86ab996a0fefacc4dea/.php-cs-fixer.php"
# Version and Release Manager
VERSION_FILE="${PWD}/.version"
RELEASE="${PWD}/release"
VERSION="0.1.0"
if [ -f "$FILE" ]; then
echo "$FILE exists."
else
echo "$FILE does not exits.\n"
echo "Downloading config file."
wget -c $CONFIG_URL
fi
if [ -f "$COMPOSER_PATH" ]; then
echo "$COMPOSER_PATH exists."
else
echo "$COMPOSER_PATH does not exist."
wget https://getcomposer.org/download/latest-stable/composer.phar -O composer
if [ "$(id -u)" -eq 0 ]; then
chmod +x composer && mv composer /usr/bin/composer
else
sudo chmod +x composer && sudo mv composer /usr/bin/composer
fi
fi
if [ -f "$RELEASE" ]; then
if [ -f "$VERSION_FILE" ]; then
# Read file
VERSION=$(cat "$VERSION_FILE")
# delete v from text
VERSION=${VERSION#v}
# increment version
VERSION=$(echo "$VERSION" | awk -F. '{$NF+=1; print}')
# replace space to .
VERSION=$(echo "$VERSION" | sed 's/ /./g')
# write version to file
echo "${VERSION}" >"$VERSION_FILE"
echo "Version: v$VERSION"
else
echo "Version file does not exist. Generate v0.1.0"
echo "0.1.0" >"$VERSION_FILE"
fi
echo "Release file exists. Delete it."
git add "$VERSION_FILE"
git tag -a "${VERSION}" -m "Release v$VERSION"
git push origin "${VERSION}"
rm "$RELEASE"
fi
$COMPOSER_PATH $COMMAND
# Check for modified .php files
MODIFIED_FILES=$(git diff --name-only | grep -E '\.(php)$')
# If there are modified files, exit with non-zero to stop the commit
if [ ! -z "$MODIFIED_FILES" ]; then
echo "The following files were modified by composer lint:"
echo "$MODIFIED_FILES"
for file in $MODIFIED_FILES
do
git add "$file"
done
echo "Please add them to the staging area and retry the commit."
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment