#!/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 || { >&2 echo "curl is not installed. Aborting." local EXIT=true } command -v zip > /dev/null 2>&1 || { >&2 echo "zip is not installed. Aborting." local EXIT=true } command -v unzip > /dev/null 2>&1 || { >&2 echo "unzip is not installed. Aborting." local EXIT=true } command -v getopt > /dev/null 2>&1 || { local opt=false } && { unset GETOPT_COMPATIBLE local out=$(getopt -T) if [[ $? -eq 4 && -z $out ]]; then local opt=false fi } if [[ $opt == false ]]; then >&2 echo "GNU getopt is not installed. Aborting." local EXIT=true fi 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_module "deps" fi } # Get user confirmation, simple Yes/No question ## $1: message, usually just a question ## $2: default choice, 0 - yes; 1 - no, default - yes ## return: 0 - yes, 1 - no get_user_confirmation () { if [[ -z $2 || $2 == "0" ]]; then read -n 1 -p "$1 [Y/n]: " yn local default=0 else read -n 1 -p "$1 [y/N]: " yn local default=1 fi case $yn in [Yy]* ) echo; return 0;; [Nn]* ) echo; return 1;; "" ) return $default;; * ) echo; return $default;; esac } # Generate LÖVE version variables ## $1: LÖVE version string ## return: 0 - string matched, 1 - else 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]} return 0 fi return 1 } # Compare two LÖVE versions ## $1: First LÖVE version ## $2: comparison operator ## "ge", "le", "gt" "lt" ## ">=", "<=", ">", "<" ## $3: Second LÖVE version ## return: 0 - true, 1 - 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 return 1 fi if [[ $3 =~ ^([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 return 1 fi case $2 in ge|\>= ) if (( $v1_maj >= $v2_maj && $v1_min >= $v2_min && $v1_rev >= $v2_rev )); then return 0 else return 1 fi ;; le|\<= ) if (( $v1_maj <= $v2_maj && $v1_min <= $v2_min && $v1_rev <= $v2_rev )); then return 0 else return 1 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 return 0 else return 1 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 return 0 else return 1 fi ;; esac } # Read configuration ## $1: system name read_config () { if [[ $LUA == true ]] && [[ -f "conf.lua" ]]; then local var=$(lua - < Set the project's title -r Set the release directory -v Set the LÖVE version Modules: -L LÖVE EndOfSHelp } dump_var () { echo "LOVE_VERSION=$LOVE_VERSION" echo "LOVE_DEF_VERSION=$LOVE_DEF_VERSION" echo "LOVE_WEB_VERSION=$LOVE_WEB_VERSION" echo echo "RELEASE_DIR=$RELEASE_DIR" echo "CACHE_DIR=$CACHE_DIR" 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" echo echo "${FILES[@]}" } # Modules functions # Init module ## $1: Pretty module name ## $2: Configuration module name ## $3: Module option ## return: 0 - if module should be executed, else exit 2 init_module () { ( opt="$3" if (( ${#opt} == 1 )); then opt="-$opt" elif (( ${#opt} >= 2 )); then opt="--$opt"; fi eval set -- "$ARGS" while true; do case "$1" in $opt ) exit 0 ;; -- ) exit 1 ;; * ) shift ;; esac done ) local opt=$? local module="$2" read_config "$module" module=${module^^} if (( $opt == 0 )); then if [[ ${!module} == false ]]; then read_config "default" fi else if [[ ${!module} == false ]]; then exit_module "execute" fi fi gen_version $VERSION unset VERSION MODULE="$1" CACHE_DIR="$CACHE_DIR/$2" read_options "$3" LOVE_FILE="${TITLE}.love" mkdir -p "$RELEASE_DIR" "$CACHE_DIR" echo "Generating $TITLE with LÖVE $LOVE_VERSION for ${MODULE}..." return 0 } # Create the LÖVE file ## $1: Compression level 0-9 create_love_file () { local dotfiles=() for file in .*; do if [[ $file == '.' || $file == '..' ]]; then continue; fi if [[ -d $file ]]; then file="$file/*"; fi dotfiles+=( "$file" ) done local release_dir="$(readlink -m "$RELEASE_DIR")" local wd="$(readlink -m "$PWD")" zip -FS -$1 -r "$RELEASE_DIR/$LOVE_FILE" \ -x "$0" "${release_dir//$wd\/}/*" "${dotfiles[@]}" "${EXCLUDE[@]}" @ \ "${FILES[@]}" } # Exit module ## $1: optional error identifier ## $2: optional error message exit_module () { if [[ -z $1 ]]; then echo "Done !" exit 0 fi if [[ -n $2 ]]; then >&2 echo -e "$2" fi case $1 in execute ) exit 2 ;; binary ) >&2 echo "LÖVE $LOVE_VERSION could not be found or downloaded." exit 3 ;; options ) exit 4 ;; version ) >&2 echo "LÖVE version string is invalid." exit 5 ;; deps ) exit 6 ;; undef|* ) exit 1 ;; esac } # 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 DEFAULT_MODULE=true TITLE="$(basename $(pwd))" PROJECT_DIR="$PWD" RELEASE_DIR=releases CACHE_DIR=~/.cache/love-release FILES=() EXCLUDE=() OPTIONS="La:d:e:hi:l:p:r:t:u:v:x:" LONG_OPTIONS="author:,clean,description:,email:,exclude:,help,icon:,love:,pkg:,release:,title:,url:,version:" ARGS=$(getopt -o "$OPTIONS" -l "$LONG_OPTIONS" -n 'love-release' -- "$@") if (( $? != 0 )); then short_help; exit_module "options"; fi eval set -- "$ARGS" read_options while [[ $1 != '--' ]]; do shift; done; shift for arg do FILES+=( "$arg" ) done if (( ${#FILES} == 0 )); then FILES+=( "." ); fi eval set -- "$ARGS" if [[ $INSTALLED == false && $EMBEDDED == false ]]; then exit_module "undef" "love-release has not been installed, and is not embedded into one script." fi if [[ ! -f "main.lua" ]]; then >&2 echo "No main.lua file was found." exit_module 1 fi if [[ $EMBEDDED == true ]]; then : # include_scripts_here elif [[ $INSTALLED == true ]]; then SCRIPTS_DIR="scripts" for file in "$SCRIPTS_DIR"/*.sh; do (source "$file") default_module done fi ( init_module "LÖVE" "love" "L" create_love_file 9 exit_module ) if [[ $? -ne 0 && $DEFAULT_MODULE == true ]]; then ( init_module "LÖVE" "default" create_love_file 9 exit_module ) fi exit 0