#!/bin/bash
set -e
SCRIPT_PATH=$(readlink -f "${BASH_SOURCE:-$0}")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
SERVER_MODS=${STK_SERVER_MODULES:=""}
exec_spacetime() {
# args: target, environment, build root, modules
echo "[*] Running configure command: 'python3 $SCRIPT_DIR/spacetime_py/spacetime.py $1 $2 $SCRIPT_DIR $SPACETIME_VERBOSE'"
python3 "$SCRIPT_DIR/spacetime_py/spacetime.py" "$1" "$2" "$SCRIPT_DIR" "$SPACETIME_VERBOSE" "$4"
}
exec_ninja() {
# args: target
echo "[*] Running build command: 'ninja -C $SCRIPT_DIR $1'"
ninja -C "$SCRIPT_DIR" "$1"
}
sub_help() {
echo "Spacetime - StarKingdoms build utility"
echo "Spacetime is a small utility program to generate Ninja build manifests for compiling StarKingdoms."
echo "Available targets:"
echo " help - Show this help screen" # done
echo " run_http - Compile the client and run a development http server for testing it" # done
echo " run_server (default) - Compile and run the game server" # done
echo " build_server - Compile the game server" # done
echo " run_server_prod - Compile and run the game server with optimizations enabled" # done
echo " build_server_prod - Compile the game server with optimizations enabled" # done
echo " run_api - Compile and run the API server" # done
echo " build_api - Compile the API server" # done
echo " run_api_prod - Compile and run the API server with optimizations enabled" # done
echo " build_api_prod - Compile the API server with optimizations enabled" # done
echo " install_tooling - Install the compilation utilities required for compiling StarKingdoms" # done
echo " build_assets - Compile spritesheets in all three texture sizes for textures-fast" # done
echo " build_assets_full - Compile spritesheets in full size for textures-fast" # done
echo " build_assets_375 - Commpile 37.5% spritesheets for textures-fast" # done
echo " build_assets_125 - Compile 12.5% spritesheets for textures-fast" # done
echo " clean - Remove all generated files" # done
echo " build_docker_api - Build the API dockerfile" # done
echo " build_docker_server - Build the server dockerfile" # done
echo " build_docker_web - Build the web dockerfile" # done
echo " build_docker - Build the API, web and server containers" # done
echo " build_docker_api_stable - Build the API container and push it as api-stable" # done
echo " build_docker_server_stable - Build the server container and push it as server-stable" # done
echo " build_docker_web_stable - Build the web dockerfile and push it as web-stable" # done
echo " build_docker_stable - Build the stable api, web and server containers" # done
echo " infra [action] - Run an infrastructure command. Requires an infrastructure key" # done
}
check_install_cargo() {
echo "[*] Checking for $1"
if ! command -v "$1" &> /dev/null
then
echo "[+] $1 was not found, installing via Cargo..."
cargo install "$2" $3
fi
}
check() {
echo "[*] Checking for $1"
if ! command -v "$1" &> /dev/null
then
echo "[x] $1 was not found but is required for the build process to continue. Install it with your system package manager, or, if supported, 'st install_tooling'"
exit 1
fi
}
check_all() {
check inkscape
check protoc
check atlasify
}
sub_clean() {
rm -rf web/dist
rm -rf assets/dist
rm -rf assets/final
rm -rf target
rm -rf client/pkg
}
sub_install_tooling() {
check inkscape
check protoc
check atlasify
echo "[*] All required tools are installed"
}
sub_run_http() {
check_all
exec_spacetime client prod "$SCRIPT_DIR" "$CLIENT_MODS"
exec_ninja asset
cd client && yarn && yarn run dev
}
sub_build_server() {
check_all
exec_spacetime server dev "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja server
}
sub_run_server() {
check_all
exec_spacetime server dev "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja server
exec "$SCRIPT_DIR/target/debug/starkingdoms-server"
}
sub_build_server_prod() {
check_all
exec_spacetime server prod "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja server
}
sub_run_server_prod() {
check_all
exec_spacetime server prod "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja server
exec "$SCRIPT_DIR/target/release/starkingdoms-server"
}
sub_build_api() {
check_all
exec_spacetime api dev "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja api
}
sub_run_api() {
check_all
exec_spacetime api dev "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja api
cd api && exec "$SCRIPT_DIR/target/debug/starkingdoms-api"
}
sub_build_api_prod() {
check_all
exec_spacetime api prod "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja api
}
sub_run_api_prod() {
check_all
exec_spacetime api prod "$SCRIPT_DIR" "$SERVER_MODS"
exec_ninja api
cd api && exec "$SCRIPT_DIR/target/release/starkingdoms-api"
}
sub_build_assets() {
check_all
exec_spacetime asset dev "$SCRIPT_DIR"
exec_ninja asset
}
sub_build_assets_full() {
check_all
exec_spacetime asset dev "$SCRIPT_DIR"
exec_ninja asset-full
}
sub_build_assets_375() {
check_all
exec_spacetime asset dev "$SCRIPT_DIR"
exec_ninja asset-375
}
sub_build_assets_125() {
check_all
exec_spacetime asset dev "$SCRIPT_DIR"
exec_ninja asset-125
}
build_docker() {
docker buildx build -f "$SCRIPT_DIR/$1".Dockerfile -t registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:"$1"-$(git rev-parse --short HEAD) "$SCRIPT_DIR"
docker buildx build -f "$SCRIPT_DIR/$1".Dockerfile -t registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:"$1"-"$2" "$SCRIPT_DIR"
docker push registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:"$1"-$(git rev-parse --short HEAD)
docker push registry.gitlab.com/starkingdoms.tk/starkingdoms.tk:"$1"-"$2"
}
swap_out_server_for() {
echo "[*] Swapping out API server"
sed -i'orig' "s/let api_server = \"http:\\/\\/localhost:8080\";/let api_server = \"https:\\/\\/api.${1}.${2}\";/" "$SCRIPT_DIR/client/index.html"
echo "[*] Swapping out game server"
sed -i "s/let servers = \[\"localhost:3000\"\];/let servers = [\"${1}.${2}\"];/" "$SCRIPT_DIR/client/index.html"
}
sub_swap_server() {
swap_out_server_for "$1" "$2"
}
sub_reset_server() {
mv client/index.htmlorig client/index.html
}
sub_build_docker_api() {
sub_build_api_prod
build_docker "api" "bleeding"
}
sub_build_docker_server() {
sub_build_server_prod
build_docker "server" "bleeding"
}
sub_build_docker_web() {
swap_out_server_for "bleeding" "starkingdoms.io"
build_docker "web" "bleeding"
mv "$SCRIPT_DIR/client/index.htmlorig" "$SCRIPT_DIR/client/index.html"
}
sub_build_docker_web_stable() {
swap_out_server_for "starkingdoms" "io"
build_docker "web" "stable"
mv "$SCRIPT_DIR/client/index.htmlorig" "$SCRIPT_DIR/client/index.html"
}
sub_build_docker_api_stable() {
sub_build_api_prod
build_docker "api" "stable"
}
sub_build_docker_server_stable() {
sub_build_server_prod
build_docker "server" "stable"
}
sub_build_docker_api_beta() {
sub_build_api_prod
build_docker "api" "beta"
}
sub_build_docker_server_beta() {
sub_build_server_prod
build_docker "server" "beta"
}
sub_build_docker_web_beta() {
swap_out_server_for "beta" "starkingdoms.io"
build_docker "web" "beta"
mv "$SCRIPT_DIR/client/index.htmlorig" "$SCRIPT_DIR/client/index.html"
}
sub_build_docker() {
sub_build_docker_api
sub_build_docker_server
sub_build_docker_web
}
sub_build_docker_beta() {
sub_build_docker_api_beta
sub_build_docker_server_beta
sub_build_docker_web_beta
}
sub_build_docker_stable() {
sub_build_docker_api_stable
sub_build_docker_server_stable
sub_build_docker_web_stable
}
sub_infra() {
echo "[*] Connecting to infrastructure manager server. If you are prompted for a password, enter your infrastructure key. You may be prompted several times."
ssh team@10.16.1.3 /home/team/run_ansible.sh "$1"
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help" | "help")
sub_help
;;
*)
echo "[*] Running build command $subcommand"
shift
sub_${subcommand} $@
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run 'st --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac