Initial commit of hooks + manage_hook script

This commit is contained in:
Spiker985 2022-12-16 16:57:17 -05:00
parent fd476842cc
commit 3aed0bcea3
6 changed files with 219 additions and 0 deletions

22
hooks/HOW_TO_USE.md Normal file
View File

@ -0,0 +1,22 @@
### Contents
| Filename | Description |
| - | - |
| post-checkout | This hook executes after a branch checkout, or branch switch has occurred. |
| post-merge | This hook executes after a branch merge has occurred |
| update_editor.sh | The actual brains of the hooks. Performs a yarn install, yarn build, yarn build:apm, and syncs all submodules. |
### Usage
There are several ways to apply these hooks:
- You can manually copy the files over to the `<pulsar-repo-root>/.git/hooks` folder and validate that they are executable - the effect should be immediate. This is the preferred option for Windows.
- You can use manage_hooks.sh to copy/symlink the hooks you choose. This is the preferred option for Linux/MacOS.
- Your mileage may vary on MacOS as it has not been tested outright, but should work in theory.
### Instructions
- Open your favorite terminal
- Navigate to `<pulsar-repo-root>/hooks`.
- IMPORTANT: The bash completions will only work within this directory, and are activated when using exactly `./manage-hooks.sh`.
- If you have bash-completions, source the `manage_hooks-completion.bash` file to allow for auto-complete ie `source manage_hooks-completion.bash`.
- Allow the auto-complete responses to guide you.
- Standard commands are `list`, `install` and `remove`
- The `install` and `remove` commands require the hook you wish to install, followed by an optional parameter for `hardlink` vs `symlink` with symlink being the default.
- A symbolically linked hook allows you to receive updates in the future. If you plan on adjusting your hook(s), you probably want to `hardlink` ie copy the files to the `<pulsar-repo-root>/.git/hooks` directory

View File

@ -0,0 +1,33 @@
#!/bin/bash
function __completion() {
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="list install remove"
case ${COMP_CWORD} in
1)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
;;
2)
case ${prev} in
install)
COMPREPLY=( $(compgen -W "all $(ls --ignore='*.md' --ignore='*.sh' --ignore='*.bash')" -- ${cur}) )
;;
remove)
COMPREPLY=( $(compgen -W "all $(ls ../.git/hooks --ignore='*.sample')" -- ${cur}) )
;;
esac
;;
3)
case "${COMP_WORDS[COMP_CWORD-2]}" in
install)
COMPREPLY=( $(compgen -W "hardlink symbolic" -- ${cur}) )
;;
esac
;;
esac
}
complete -F __completion ./manage_hooks.sh

69
hooks/manage_hooks.sh Executable file
View File

@ -0,0 +1,69 @@
#!/bin/bash
cd "$(dirname "$(readlink -f "$0")")"
USAGEMSG="Usage: manage_hooks <action> <hook> <option>"
if [[ $# -lt 1 ]] || [[ $# -gt 3 ]]; then
echo "$USAGEMSG"
exit 1
fi
action="$1"
case $1 in
list)
ls --color=if-tty -l ../.git/hooks --ignore='*.sample'
;;
install)
if [[ "$2" == "all" ]]; then
hooks=( $(readlink -f $(ls --ignore='*.md' --ignore='*.sh' --ignore='*.bash')) )
else
hooks=( $(readlink -f "$2") )
fi
if [[ -z $3 ]]; then
option="symbolic"
else
option="$3"
fi
case $option in
hardlink)
hooks+=( "$(readlink -f "update_editor.sh")" "${hooks[@]}" )
for hook in "${hooks[@]}"; do
if [[ ! -f "../.git/hooks/$(basename "$hook")" ]]; then
echo "$hook"
cp "$hook" "../.git/hooks"
fi
done
;;
symbolic)
cd "../.git/hooks"
for hook in "${hooks[@]}"; do
if [[ ! -f "$(basename $hook)" ]]; then
echo "$(readlink -f .)/$(basename $hook)"
ln --symbolic --relative "../../hooks/$(basename "$hook")"
fi
done
;;
esac
;;
remove)
cd "../.git/hooks"
if [[ "$2" == "all" ]]; then
hooks=( $(realpath --no-symlinks $(ls --ignore='*.sample' --ignore='*.sh')) )
else
hooks=( $(realpath --no-symlinks "$2") )
fi
for hook in "${hooks[@]}"; do
echo "$hook"
rm "$hook"
done
;;
*)
echo "$USAGEMSG"
exit 1
;;
esac

24
hooks/post-checkout Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
#This hook launches after a branch change, or branch checkout. It cannot affect
#the outcome of a git switch or git checkout with the exception that the hook
#exit status becomes the exit status of the antecedent command
#See https://git-scm.com/docs/githooks#_post_checkout
if [[ -L "$0" ]]; then
#If our launched script is a link, grab the root
hookRoot="$(dirname $(readlink --canonicalize "$0"))"
else
#Otherwise use the launched script directory
hookRoot="$(dirname "$0")"
fi
if [[ "$1" == "$2" ]]; then
#Previous HEAD and new HEAD are the same, ignore
exit 0
fi
if [[ "$3" == '1' ]]; then
#A branch checkout will always be flagged as 1, and a file checkout as 0
"${hookRoot}/update_editor.sh" $0
fi

15
hooks/post-merge Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
#This hook launches after a git pull. It cannot affect a git merge, and will not
#be executed if a merge fails
#See https://git-scm.com/docs/githooks#_post_merge
if [[ -L "$0" ]]; then
#If our launched script is a link, grab the root
hookRoot="$(dirname $(readlink --canonicalize $0))"
else
#Otherwise use the launched script directory
hookRoot="$(dirname "$0")"
fi
"${hookRoot}/update_editor.sh" $0

56
hooks/update_editor.sh Executable file
View File

@ -0,0 +1,56 @@
#!/bin/bash
case $1 in
*post-checkout)
ACTION='Branch change'
;;
*post-merge)
ACTION='Remote pull'
;;
*)
ACTION="Unknown event ($1)"
;;
esac
echo "${ACTION} occurred, rebuilding editor"
export ATOM_ELECTRON_VERSION=$(cat package.json | rg --trim --replace "" '"electronVersion": "' | rg --replace "" '",')
replacement="1.100.$(date +'%Y%m%d%H%k%M')"
filter='("version": ")[0-9\.]+(",)'
regex="s/$filter/\1$replacement\2/"
sed --regexp-extended --in-place "$regex" package.json
echo ' Installing editor packages'
yarn install &> /dev/null
if [[ $? == 0 ]]; then
echo ' Install completed successfully'
else
echo ' Install failed'
exit 1
fi
echo ' Rebuilding modules'
yarn build &> /dev/null
if [[ $? == 0 ]]; then
echo ' Module build completed successfully'
else
echo ' Module build failed'
exit 1
fi
echo ' Rebuilding PPM'
if [[ -d "ppm" ]]; then
yarn build:apm &> /dev/null
if [[ $? == 0 ]]; then
echo ' PPM build completed successfully'
else
echo ' PPM build failed'
exit 1
fi
else
echo ' PPM folder not found'
fi
git submodule sync && git submodule update