Skip to content

Commit

Permalink
Webapp rebuild script with cli arguments + post-merge hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ChisSoc authored and ChisSoc committed Oct 12, 2021
1 parent fa75c9f commit 731f3e9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
84 changes: 84 additions & 0 deletions .githooks/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash

# Script to be run by git hook: post merge, i.e. git pull
#
# TO ACTIVATE: cp .githooks/post-merge .git/hooks/.
#
# Checks:
# - Changes to web app
# - Changes to web dependency
# - Changes to python requirements
#
# Actions are not triggered automatically as they can take some
# time. The user is warned if an action is required.
#
# Inspired by
# https://davidwalsh.name/git-hook-npm-install-package-json-modified

warn_npm_dependency() {
echo -e "\n"
echo "************************************************************"
echo "ATTENTION: npm dependencies have changed since last pull!"
echo ""
echo "To update dependencies and rebuilt WebApp run:"
echo "$ cd src/webapp && ./run_rebuild.sh -u"
echo "************************************************************"
echo -e "\n"
}

warn_webapp() {
echo -e "\n"
echo "************************************************************"
echo "ATTENTION: Web App sources have changed since last pull!"
echo ""
echo "To rebuilt the WebApp run:"
echo "$ cd src/webapp && ./run_rebuild.sh"
echo "************************************************************"
echo -e "\n"
}

warn_python_requirements() {
echo -e "\n"
echo "************************************************************"
echo "ATTENTION: Python requirements have changed since last pull!"
echo ""
echo "To update python requirements on the RPi run"
echo "$ sudo python3 -m pip install --upgrade -r requirements.txt"
echo "************************************************************"
echo -e "\n"
}

warn_githooks() {
echo -e "\n"
echo "************************************************************"
echo "ATTENTION: Recommended git hooks changed!"
echo ""
echo "To update, REVIEW and copy .githooks/* to .git/hooks"
echo "$ cp .githooks/* .git/hooks/."
echo "************************************************************"
echo -e "\n"

}

# files_changed="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
webapp_changed="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD src/webapp)"
webapp_dep_changed="$(git diff --name-only --no-commit-id ORIG_HEAD HEAD src/webapp/package.json)"
python_req_changed="$(git diff --name-only --no-commit-id ORIG_HEAD HEAD requirements.txt)"
githooks_changed="$(git diff --name-only --no-commit-id ORIG_HEAD HEAD .githooks)"

if [[ -n $python_req_changed ]]; then
warn_python_requirements
fi

if [[ -n $webapp_dep_changed ]]; then
warn_npm_dependency
elif [[ -n $webapp_changed ]]; then
warn_webapp
fi

if [[ -n $githooks_changed ]]; then
warn_githooks
fi

echo "To see a summary of what happened since your last pull, do:"
echo -e "git show --oneline -s ORIG_HEAD HEAD\n"
31 changes: 28 additions & 3 deletions src/webapp/run_rebuild.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
#!/usr/bin/env bash

usage() {
echo -e "\nRebuild the Web App\n"
echo "${BASH_SOURCE[0]} [-u]"
echo -e " -u: Update NPM dependencies before rebuild (only necessary if package.json changed)\n\n"
}

UPDATE_DEPENDENCIES=false

while getopts ":uh" opt;
do
case ${opt} in
u ) UPDATE_DEPENDENCIES=true
;;
h ) usage
;;
\? ) usage
;;
esac
done


# Change working directory to location of script
SOURCE=${BASH_SOURCE[0]}
SCRIPT_DIR="$(dirname "$SOURCE")"
cd "$SCRIPT_DIR" || exit 1

# PIs with little memory need this to finish building the Webapp
export NODE_OPTIONS=--max-old-space-size=512

# In rare cases you will need to update the npm dependencies
# This is the case when the package.json changed
UPDATE_PACK=$(git diff HEAD~1 package.json | wc -l)
if [[ $UPDATE_PACK -gt 0 ]]
# This is the case when the file package.json changed
if [[ $UPDATE_DEPENDENCIES == true ]]
then
npm install
fi
Expand Down

0 comments on commit 731f3e9

Please sign in to comment.