canvas-lms/script/canvas_update

159 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# TODO: improve usage / help
function usage {
echo "usage: canvas_update [[[-q]] | [-h]]"
}
function echo_console_and_log {
echo "$1"
echo "$1" >>"$LOG"
}
function is_git_dir {
git rev-parse --is-inside-git-dir >/dev/null 2>&1 && [ -d .git ]
return $?
}
function is_canvas_root {
CANVAS_IN_README=$(head -1 README.md 2>/dev/null | grep 'Canvas LMS')
[[ "$CANVAS_IN_README" != "" ]] && is_git_dir
return $?
}
function ensure_in_canvas_root_directory {
if ! is_canvas_root; then
echo "Please run from a Canvas root directory"
exit 0
fi
}
function intro_message {
echo "Bringing Canvas up to date ..."
echo " Log file is $LOG"
echo >>"$LOG"
echo "-----------------------------" >>"$LOG"
echo "Canvas Update ($(date)):" >>"$LOG"
echo "-----------------------------" >>"$LOG"
}
function update_plugin {
(
cd "$1"
if is_git_dir; then
echo_console_and_log " Updating plugin $1 ..."
( git checkout master >>"$LOG" 2>&1 && \
git pull --rebase >>"$LOG" 2>&1 ) || \
( echo " failed to pull plugin (see $LOG)"; kill -INT $$ )
fi
)
}
function update_plugins {
# Loop through each plugin dir, and if it's a git repo, update it
# This needs to be done first so that db:migrate can pull in any plugin-
# precipitated changes to the database.
for dir in {gems,vendor}; do
if [ -d "$dir/plugins" ]; then
for plugin in $dir/plugins/*; do update_plugin "$plugin"; done
fi
done
}
function rebase_canvas {
echo_console_and_log " Pulling Canvas code ..."
( git checkout master >>"$LOG" 2>&1 && git pull --rebase >>"$LOG" 2>&1 ) || \
( echo " failed to pull Canvas (see $LOG)"; kill -INT $$ )
}
function bundle_install_with_check {
echo_console_and_log " Checking your gems (bundle check) ..."
if bundle check >>"$LOG" 2>&1 ; then
echo_console_and_log " Gems are up to date, no need to bundle install ..."
else
bundle_install
fi
}
function bundle_install {
echo_console_and_log " Installing gems (bundle install) ..."
rm Gemfile.lock* >/dev/null 2>&1
bundle install >>"$LOG" 2>&1 || \
( echo " failed to bundle install (see $LOG)"; kill -INT $$ )
}
function rake_db_migrate_dev_and_test {
echo_console_and_log " Migrating DB ..."
RAILS_ENV=development bundle exec rake db:migrate >>"$LOG" 2>&1 || \
( echo " failed to migrate db (development) (see $LOG)"; kill -INT $$ )
RAILS_ENV=test bundle exec rake db:migrate >>"$LOG" 2>&1 || \
( echo " failed to migrate db (test) (see $LOG)"; kill -INT $$ )
}
function yarn_install {
echo_console_and_log " Installing node packages ..."
yarn install >>"$LOG" 2>&1 || \
( echo " failed to install node packages (see $LOG)"; kill -INT $$ )
}
function compile_assets {
echo_console_and_log " compiling assets ..."
bundle exec rake 'canvas:compile_assets_dev' >>"$LOG" 2>&1 || \
( echo " failed to generate JS (see $LOG)"; kill -INT $$ )
}
# TODO: moar tips plz
function tips {
echo "Tips:"
echo " - 'bundle exec guard': auto-compiles JS files while developing"
echo " - 'script/delayed_job run': run delayed jobs in the foreground"
}
function update_canvas {
ensure_in_canvas_root_directory
intro_message
update_plugins
rebase_canvas
bundle_install_with_check
rake_db_migrate_dev_and_test
yarn_install
# skip if QUICK_MODE
if ! $1; then
compile_assets
fi
tips
}
LOG="$(pwd)/log/canvas_update.log"
# default options
PERFORM_UPDATE=true
QUICK_MODE=false
# parse options
# http://www.tldp.org/LDP/abs/html/internal.html#EX33
while getopts ":qh" Option
do
case $Option in
q )
echo "Quick mode enabled (assumes you have guard running and don't want to generate docs)"
QUICK_MODE=true;;
h )
PERFORM_UPDATE=false
usage;;
* )
PERFORM_UPDATE=false
echo "Sorry, that's not a valid option!"
usage;;
esac
done
if $PERFORM_UPDATE; then
update_canvas $QUICK_MODE
fi