2019-03-09 16:07:44 +00:00
#!/usr/bin/env bash
# Make sure the current directory is the location of this script to simplify matters
cd "$(dirname $(readlink -f $0))";
################
### Settings ###
################
# The name of this project
project_name="micro-lanterns";
# The path to the lantern build engine git submodule
lantern_path="./lantern-build-engine";
###
# Custom Settings
###
# Put any custom settings here.
build_output_folder="./dist";
2019-03-12 21:01:16 +00:00
deploy_ssh_user="ci";
deploy_ssh_host="starbeamrainbowlabs.com";
deploy_ssh_port="2403";
2019-03-09 16:07:44 +00:00
###############################################################################
# Check out the lantern git submodule if needed
if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi
source "${lantern_path}/lantern.sh";
if [[ "$#" -lt 1 ]]; then
echo -e "${FBLE}${project_name}${RS} build script";
echo -e " by Starbeamrainbowlabs";
echo -e "${LC}Powered by the lantern build engine, v${version}${RS}";
echo -e "";
echo -e "${CSECTION}Usage${RS}";
echo -e " ./build ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ${CTOKEN}{action}${RS} ...";
echo -e "";
echo -e "${CSECTION}Available actions${RS}";
echo -e " ${CACTION}setup${RS} - Perform initial setup";
echo -e " ${CACTION}main${RS} - Build the slide deck";
2019-03-09 16:46:03 +00:00
echo -e " ${CACTION}main-watch${RS} - Build the slide deck automatically when something changes";
2019-03-09 16:07:44 +00:00
echo -e " ${CACTION}slides${RS} - Build the slides HTML";
echo -e " ${CACTION}js-css${RS} - Build the javascript / css";
echo -e " ${CACTION}dev-server${RS} - Start a development server";
echo -e " ${CACTION}dev-server-stop${RS} - Stop the currently running development server";
echo -e "";
exit 1;
fi
###############################################################################
task_setup() {
2019-03-11 16:52:26 +00:00
stage_begin "Setting up";
2019-03-09 16:07:44 +00:00
check_command git true;
check_command node true;
check_command npm true;
2019-10-21 12:08:54 +00:00
check_command pandoc true;
check_command weasyprint true;
2019-03-09 16:07:44 +00:00
2019-03-11 16:52:26 +00:00
task_begin "Creating build output directory";
2019-03-09 16:07:44 +00:00
mkdir -p "${build_output_folder}";
2019-03-11 16:52:26 +00:00
subtask_end $? "Error: Failed to create build output directory at ${HC}${build_output_folder}${RS}";
2019-03-09 16:07:44 +00:00
2019-03-11 16:52:26 +00:00
task_begin "Initialising submodules";
2019-03-09 16:07:44 +00:00
git submodule update --init;
2019-03-11 16:52:26 +00:00
task_end $? "Error: Failed to initialise submodules";
2019-03-09 16:07:44 +00:00
2019-03-11 16:52:26 +00:00
task_begin "Installing dependencies";
npm install;
task_end $? "Error: Failed to install dependencies";
stage_end 0;
2019-03-09 16:07:44 +00:00
}
# ██████ ███████ ██ ██ ███████ ███████ ██████ ██ ██ ███████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██ ██ █████ ██ ██ █████ ███████ █████ ██████ ██ ██ █████ ██████
# ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ████ ███████ ███████ ██ ██ ████ ███████ ██ ██
task_dev-server() {
task_begin "Starting development server";
2019-03-09 16:46:03 +00:00
php -S [::1]:50678 -t "${build_output_folder}" &
2019-03-09 16:07:44 +00:00
exit_code=$?;
echo $! >/tmp/micro-lantern-dev-server.pid;
task_end $?; # Should be 0 unless php died for some reason
sleep 1;
}
task_dev-server-stop() {
task_begin "Stopping development server";
kill "$(cat /tmp/micro-lantern-dev-server.pid)";
rm /tmp/micro-lantern-dev-server.pid;
task_end $?;
}
2019-03-11 16:50:10 +00:00
task_main-watch() {
2019-03-09 16:46:03 +00:00
set_title "Build Watcher";
echo -e "Watching for changes.";
while :; do # : = infinite loop
# Wait for an update
# inotifywait's non-0 exit code forces an exit for some reason :-/
inotifywait -qr --event modify --format '%:e %f' index.html images css js;
# Rebuild the client code - spawn a sub-process to avoid the hard exit
# This still doesn't work though, which is *really* annoying
stage_begin "Rebuilding";
./build main;
stage_end 0;
done
}
2019-03-09 16:07:44 +00:00
task_main() {
2019-03-09 16:46:03 +00:00
tasks_run js-css;
2019-03-09 16:07:44 +00:00
2019-10-21 12:08:54 +00:00
if [ ! -d "${build_output_folder}/201" ]; then
task_begin "Creating 201 output subdirectory";
mkdir "${build_output_folder}/201";
task_end "$?";
fi
2019-03-09 16:07:44 +00:00
task_begin "Copying html";
cp index.html "${build_output_folder}";
2019-10-21 12:08:54 +00:00
cp 201/index.html "${build_output_folder}/201";
task_end "$?";
2019-03-09 16:07:44 +00:00
2019-03-09 16:46:03 +00:00
task_begin "Copying images";
cp -r images/ "${build_output_folder}";
2019-10-21 12:08:54 +00:00
task_end "$?";
task_begin "Rendering lab sheets: markdown -> html [phase 1/3]";
pandoc 201/Lab-Sheet.md --standalone --output 201/Lab-Sheet.html;
task_end "$?";
task_begin "Rendering lab sheets: html -> pdf [phase 2/3]";
weasyprint 201/Lab-Sheet.html 201/Lab-Sheet.pdf --stylesheet css/lab-sheet.css --format pdf --verbose;
exit_code="$?";
rm 201/Lab-Sheet.html;
task_end "${exit_code}";
task_begin "Rendering lab sheets: archiving [phase 3/3]";
mv 201/Lab-Sheet.pdf "${build_output_folder}/201";
task_end "$?";
2019-03-09 16:07:44 +00:00
}
task_js-css() {
task_begin "Packaging Javascript & CSS";
node_modules/rollup/bin/rollup --sourcemap --config rollup.config.js;
task_end $? "Error: rollup packing failed!";
}
2019-03-28 18:38:22 +00:00
# ██████ ██
# ██ ██
# ██ ██
# ██ ██
# ██████ ██
2019-03-11 16:50:10 +00:00
task_ci() {
task_begin "Environment Information";
execute git --version;
execute node --version;
execute npm --version;
task_end $?;
2019-03-12 21:08:08 +00:00
# Run setup in 'development' mode to get the right dependencies installed
2019-03-11 16:57:06 +00:00
tasks_run setup;
export NODE_ENV=production;
2019-03-12 21:08:08 +00:00
# Run the rest in 'production' mode to create a production build
2019-03-12 21:01:16 +00:00
tasks_run main archive deploy;
2019-03-11 16:50:10 +00:00
}
task_archive() {
task_begin "Archiving";
2019-03-12 21:01:16 +00:00
mv "${build_output_folder}" "Linux101/";
2019-03-11 16:50:10 +00:00
tar cafv "${ARCHIVE}/slides.tar.bz2" "Linux101/";
2019-03-12 21:01:16 +00:00
exit_code=$?;
mv "Linux101/" "${build_output_folder}";
task_end "${exit_code}";
}
task_deploy() {
2019-03-28 18:39:44 +00:00
task_begin "Deploying to ${deploy_ssh_host}....";
2019-03-12 21:01:16 +00:00
if [ "${SSH_KEY_PATH}" == "" ]; then
task_end 1 "Error: Can't find the SSH key as the environment variable SSH_KEY_PATH isn't set.";
fi
2019-03-12 21:24:55 +00:00
# TODO: If we experience issues, we need to somehow figure out how to recursively delete the contents of the directory before uploading. We may have to install lftp and use that instead
2019-03-12 21:01:16 +00:00
sftp -i "${SSH_KEY_PATH}" -P "${deploy_ssh_port}" -o PasswordAuthentication=no "${deploy_ssh_user}@${deploy_ssh_host}" << SFTPCOMMANDS
2019-03-12 21:28:59 +00:00
put -r ${build_output_folder}/* Linux101
2019-03-12 21:24:55 +00:00
bye
2019-03-12 21:01:16 +00:00
SFTPCOMMANDS
2019-03-11 16:50:10 +00:00
task_end $?;
}
2019-03-09 16:07:44 +00:00
###############################################################################
tasks_run $@;