#!/usr/bin/env bash # LÖVE version LOVE_DEF_VERSION=0.9.2 # Helper functions # Dependencies check check_deps () { command -v curl > /dev/null 2>&1 || { echo "curl is not installed. Aborting." local EXIT=true } command -v zip > /dev/null 2>&1 || { echo "zip is not installed. Aborting." local EXIT=true } command -v unzip > /dev/null 2>&1 || { echo "unzip is not installed. Aborting." local EXIT=true } command -v lua > /dev/null 2>&1 || { echo "lua is not installed. Install it to ease your releases." } && { LUA=true } if [[ $EXIT == true ]]; then exit 1 fi } # Reset script variables reset_vars () { TITLE="$(basename $(pwd))" RELEASE_DIR=releases CACHE_DIR=~/.cache/love-release } # Get user confirmation, simple Yes/No question ## $1: message, usually just a question ## $2: default choice, 0 - no; 1 - yes, default - yes ## return: true - yes get_user_confirmation () { if [[ $2 == "0" ]]; then read -n 1 -p "$1 [y/N]: " yn local default=false else read -n 1 -p "$1 [Y/n]: " yn local default=true fi case $yn in [Yy]* ) echo "true"; echo >> "$(tty)";; [Nn]* ) echo "false"; echo >> "$(tty)";; "" ) echo "$default";; * ) echo "$default"; echo >> "$(tty)";; esac } # Generate LÖVE version variables ## $1: LÖVE version string gen_version () { if [[ $1 =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then LOVE_VERSION=$1 LOVE_VERSION_MAJOR=${BASH_REMATCH[1]} LOVE_VERSION_MINOR=${BASH_REMATCH[2]} LOVE_VERSION_REVISION=${BASH_REMATCH[3]} fi } # Compare two LÖVE versions ## $1: First LÖVE version ## $2: comparison operator ## "ge", "le", "gt" "lt" ## ">=", "<=", ">", "<" ## $3: Second LÖVE version ## return: "true" or "false" compare_version () { if [[ $1 =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then local v1_maj=${BASH_REMATCH[1]} local v1_min=${BASH_REMATCH[2]} local v1_rev=${BASH_REMATCH[3]} else echo "false" return fi if [[ $2 =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then local v2_maj=${BASH_REMATCH[1]} local v2_min=${BASH_REMATCH[2]} local v2_rev=${BASH_REMATCH[3]} else echo "false" return fi case $2 in ge|\>= ) if (( $v1_maj >= $v2_maj && $v1_min >= $v2_min && $v1_rev >= $v2_rev )); then echo "true" else echo "false" fi ;; le|\<= ) if (( $v1_maj <= $v2_maj && $v1_min <= $v2_min && $v1_rev <= $v2_rev )); then echo "true" else echo "false" fi ;; gt|\> ) if (( $v1_maj > $v2_maj || ( $v1_max == $v2_max && $v1_min > $v2_min ) || ( $v1_max == $v2_max && $v1_min == $v2_min && $v1_rev > $v2_rev ) )); then echo "true" else echo "false" fi ;; lt|\< ) if (( $v1_maj > $v2_maj || ( $v1_max == $v2_max && $v1_min > $v2_min ) || ( $v1_max == $v2_max && $v1_min == $v2_min && $v1_rev > $v2_rev ) )); then echo "true" else echo "false" fi ;; esac } # Escape directory name for zip ## $1: directory path ## return: escaped directory path dir_escape () { local dir="$1" if [ -d "$dir" ]; then if [ "${dir::1}" != "/" ]; then dir="/$dir" fi if [ "${dir: -1}" != "*" ]; then if [ "${dir: -1}" != "/" ]; then dir="$dir/*" else dir="$dir*" fi fi fi echo "$dir" } # Read configuration ## $1: system name read_config () { if [[ $LUA == true ]] && [[ -f "conf.lua" ]]; then local var=$(lua - <" "$VERSION") == true ]]; then if [[ $(get_user_confirmation "LÖVE $LOVE_VERSION is out ! Your project uses LÖVE $VERSION. Continue ?") == false ]]; then exit fi gen_version $VERSION unset VERSION fi fi } dump_var () { echo "LOVE_VERSION=$LOVE_VERSION" echo "LOVE_DEF_VERSION=$LOVE_DEF_VERSION" echo "LOVE_WEB_VERSION=$LOVE_WEB_VERSION" echo echo "IDENTITY=$IDENTITY" echo "GAME_VERSION=$GAME_VERSION" echo "ICON=$ICON" echo echo "TITLE=$TITLE" echo "AUTHOR=$AUTHOR" echo "EMAIL=$EMAIL" echo "URL=$URL" echo "DESCRIPTION=$DESCRIPTION" } # Modules functions ## $1: Module name ## return: true if module should be executed execute_module () { local module="$1" read_config "$module" module=${module^^} echo "${!module}" } # Init module init_module () { reset_vars mkdir -p "$RELEASE_DIR" mkdir -p "$CACHE_DIR"/"$LOVE_VERSION" echo "Generating $TITLE with Love $LOVE_VERSION for ${MODULE}..." } # Create the LÖVE file ## $1: Compression level 0-9 create_love_file () { LOVE_FILE="$RELEASE_DIR"/"$TITLE".love zip -FS -$1 -r "$LOVE_FILE" \ -x "$0" "${RELEASE_DIR#$PWD/}/*" \ $(ls -Ap | grep "^\." | sed -e 's/^/\//g' -e 's/\/$/\/*/g') @ \ . } # Exit module ## $1: exit code. ## 0 - success, other - failure ## $2: error message exit_module () { if [[ -z $1 || "$1" == "0" ]]; then echo "Done !" else echo -e "$2" exit $1 fi } # Main check_deps # Get latest LÖVE version number gen_version $LOVE_DEF_VERSION LOVE_WEB_VERSION=$(curl -s https://love2d.org/releases.xml | grep -m 2 "" | tail -n 1 | grep -Eo "[0-9]+.[0-9]+.[0-9]+") gen_version $LOVE_WEB_VERSION INSTALLED=false EMBEDDED=false if [[ $INSTALLED == false && $EMBEDDED == false ]]; then >&2 echo "love-release has not been installed, and is not embedded into one script. Consider doing one of the two." INSTALLED=true fi if [[ $EMBEDDED == true ]]; then : # include_scripts_here elif [[ $INSTALLED == true ]]; then SCRIPTS_DIR="scripts" for file in "$SCRIPTS_DIR"/*.sh; do MODULE="$(basename -s '.sh' "$file")" if [[ $(execute_module "$1") == true ]]; then source "$file" fi done fi