Air-Quality-Web/build

256 lines
8.2 KiB
Plaintext
Raw Normal View History

2019-01-12 23:23:55 +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))";
2019-03-10 00:30:34 +00:00
if [ ! -d "${PWD}/.git" ]; then
echo -e "\033[1m\033[31mError: The .git folder does not appear to exist. Please ensure you clone this repository with git, like this:\n\n\tgit clone https://github.com/ConnectedHumber/Air-Quality-Web.git\n\033[0m";
exit 1;
fi
2019-01-12 23:23:55 +00:00
################
### Settings ###
################
# The name of this project
project_name="Air Quality Mapper";
# The path to the lantern build engine git submodule
lantern_path="./lantern-build-engine";
###
# Custom Settings
###
# Put any custom settings here.
# Client-side build output
cache_dir="./.cache";
2019-01-13 13:22:13 +00:00
build_output_folder="./app";
2019-01-12 23:23:55 +00:00
# Database settings for ssh port forwarding task
2019-01-14 20:50:12 +00:00
database_host="db.connectedhumber.org";
database_name="aq_db";
# Minimum major version of npm
min_npm_version_major="6";
2019-01-12 23:23:55 +00:00
###############################################################################
# Check out the lantern git submodule if needed
2019-01-13 12:50:47 +00:00
if [ ! -f "${lantern_path}/lantern.sh" ]; then git submodule update --init "${lantern_path}"; fi
2019-01-12 23:23:55 +00:00
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";
2019-01-12 23:38:05 +00:00
echo -e " ${CACTION}setup-dev${RS} - Perform additional setup for development environments. Run after ${CACTION}setup${RS}.";
2019-01-12 23:23:55 +00:00
echo -e " ${CACTION}database${RS} - Connect to the database via SSH & open MariaDB CLI connection, prompting for a password";
echo -e " ${CACTION}dev-server${RS} - Start a development server";
echo -e " ${CACTION}dev-server-stop${RS} - Stop the currently running development server";
2019-01-13 13:15:11 +00:00
echo -e " ${CACTION}client${RS} - Build the client web app";
2019-03-17 13:29:20 +00:00
echo -e " ${CACTION}client-watch${RS} - Watch for changes to the client code & rebuild automatically";
echo -e " ${CACTION}docs${RS} - Render the documentation";
echo -e " ${CACTION}ci${RS} - Perform CI tasks";
2019-01-12 23:23:55 +00:00
echo -e "";
exit 1;
fi
###############################################################################
2019-03-17 13:29:20 +00:00
# ███████ ███████ ████████ ██ ██ ██████
# ██ ██ ██ ██ ██ ██ ██
# ███████ █████ ██ ██ ██ ██████
# ██ ██ ██ ██ ██ ██
# ███████ ███████ ██ ██████ ██
task_download-composer() {
if [ -f "${cache_dir}/composer" ]; then
return 0;
fi
2019-03-30 12:03:25 +00:00
[ ! -d "${cache_dir}" ] && mkdir "${cache_dir}";
task_begin "Downloading composer";
curl "https://getcomposer.org/composer.phar" -o "${cache_dir}/composer"; exit_code=$?;
chmod +x "${cache_dir}/composer";
task_end ${exit_code};
}
2019-03-17 13:29:20 +00:00
task_setup() {
2019-01-12 23:23:55 +00:00
stage_begin "Setting up";
task_begin "Checking environment";
check_command git true;
check_command php true;
check_command node true;
check_command npm true;
2019-03-10 00:30:34 +00:00
if [ ! -w "${PWD}" ]; then
task_end 1 "${HC}${FRED}Error: Can't write to the repository directory! This usually you have a permission error of some kind. Try using sudo to run this build command as the user that owns this cloned repository.";
fi
npm_version_major="$(npm --version | head -c 1)";
if [[ "${npm_version_major}" -lt "${min_npm_version_major}" ]]; then
echo "${FRED}${HC}Error: Your version of npm is too far out of date. You're running version $(npm --version), but version 6+ is required.";
fi
2019-01-12 23:23:55 +00:00
task_end $?;
task_begin "Initialising submodules";
git submodule update --init;
task_end $?;
task_begin "Installing client dependencies";
2019-01-20 23:15:40 +00:00
echo -e "${HC}Not including developement dependencies. To complete setup for development, execute the ${CACTION}setup-dev${RS} ${HC}build task.${RS}";
2019-01-12 23:23:55 +00:00
npm install --production;
task_end $?;
tasks_run download-composer;
2019-01-12 23:23:55 +00:00
task_begin "Installing server dependencies";
"${cache_dir}/composer" install --no-dev;
2019-01-12 23:23:55 +00:00
task_end $?;
if [ ! -d "./data" ]; then
task_begin "Setting up initial data folder";
mkdir "./data"; chmod 0700 "./data";
echo -e "# -------[ Custom Settings File - Last updated $(date) ]-------" >"./data/settings.toml";
echo -e '[database]\nusername = "INSERT_DATABASE_USERNAME_HERE"\npassword = "INSERT_DATABASE_PASSWORD_HERE";' >>"./data/settings.toml";
chmod 0600 "./data/settings.toml";
echo -e "${HC}Don't forget to edit './data/settings.toml' to specify the database username and password${RS}";
2019-03-07 18:40:30 +00:00
echo -e "";
task_end $?;
fi
2019-01-12 23:23:55 +00:00
stage_end $?;
}
2019-03-17 13:29:20 +00:00
task_setup-dev() {
2019-01-12 23:23:55 +00:00
task_begin "Checking environment";
check_command mysql true optional;
task_end $?;
task_begin "Installing client development dependencies";
npm install;
task_end $?;
task_begin "Installing server development dependencies";
"${cache_dir}/composer" install;
2019-01-13 12:50:47 +00:00
task_end $?;
}
2019-03-17 13:29:20 +00:00
# ██████ ███████ ██ ██
# ██ ██ ██ ██ ██
# ██ ██ █████ ██ ██
# ██ ██ ██ ██ ██
# ██████ ███████ ████
task_database() {
2019-01-13 12:50:47 +00:00
task_begin "Connecting to the database";
set-title "Database";
2019-01-14 20:50:12 +00:00
ssh -TN "${database_host}" -L 3306:localhost:3306 &
2019-01-13 12:50:47 +00:00
ssh_pid=$!;
2019-01-14 20:50:12 +00:00
sleep 1;
mysql --host 127.0.0.1 --port 3306 --database "${database_name}" --user "${USER}" --password;
2019-01-13 12:50:47 +00:00
kill "${ssh_pid}"; wait; sleep 0.5;
2019-01-12 23:23:55 +00:00
task_end $?;
}
2019-03-17 13:29:20 +00:00
task_dev-server() {
2019-01-12 23:23:55 +00:00
task_begin "Starting development server";
php -S [::1]:40482 &
2019-01-12 23:23:55 +00:00
exit_code=$?;
echo $! >/tmp/micro-lantern-dev-server.pid;
task_end $?; # Should be 0 unless php died for some reason
sleep 1;
}
2019-03-17 13:29:20 +00:00
task_dev-server-stop() {
2019-01-12 23:23:55 +00:00
task_begin "Stopping development server";
kill "$(cat /tmp/micro-lantern-dev-server.pid)";
rm /tmp/micro-lantern-dev-server.pid;
task_end $?;
}
2019-01-13 13:15:11 +00:00
2019-03-17 13:29:20 +00:00
# ██████ ██ ██ ███████ ███ ██ ████████
# ██ ██ ██ ██ ████ ██ ██
# ██ ██ ██ █████ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ██ ██ ██
# ██████ ███████ ██ ███████ ██ ████ ██
task_client() {
2019-01-13 13:15:11 +00:00
task_begin "Packaging Javascript";
node_modules/rollup/bin/rollup --sourcemap --config rollup.config.js;
task_end $? "Error: rollup packing failed!";
2019-01-12 23:23:55 +00:00
task_begin "Copying html";
2019-01-13 13:22:13 +00:00
cp client_src/index.html "${build_output_folder}";
2019-01-12 23:23:55 +00:00
task_end $?;
2019-01-17 12:48:09 +00:00
# task_begin "Copying css";
# # FUTURE: Package this up too?
# cp -r client_src/css/ "${build_output_folder}";
# task_end $?;
2019-01-12 23:23:55 +00:00
}
2019-03-17 13:29:20 +00:00
task_client-watch() {
set-title "Client Watcher";
2019-01-13 13:15:11 +00:00
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' client_src;
# Rebuild the client code - spawn a sub-process to avoid the hard exit
# This still doesn't work though, which is *really* annoying
2019-01-13 13:15:11 +00:00
stage_begin "Rebuilding client code";
./build client;
2019-01-13 13:15:11 +00:00
stage_end $?;
done
}
2019-03-17 13:29:20 +00:00
# ██████ ██████ ██████ ███████
# ██ ██ ██ ██ ██ ██
# ██ ██ ██ ██ ██ ███████
# ██ ██ ██ ██ ██ ██
# ██████ ██████ ██████ ███████
task_docs() {
task_begin "Rendering docs";
node_modules/.bin/nightdocs --config nightdocs.toml;
task_end $?;
}
# ██████ ██
# ██ ██
# ██ ██
# ██ ██
# ██████ ██
task_ci() {
tasks_run setup setup-dev client docs;
}
2019-01-12 23:23:55 +00:00
#########################################################################
tasks_run $@;