From 72ada933f179bc6abd947e8f51cff95ad7efcfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Sun, 14 Jun 2015 18:17:43 +0200 Subject: [PATCH] Improves modules sourcing A module should always begin with a test to `execute_module` to see if it should be executed. As modules are always sourced (even when embedded), a simple `return` will stop the subscript. Modules are executed in a subscript, so the global space will not be polluted, and will also be reset after the execution of the module. This commit also solves some bugs in Makefile and lua parser. --- Makefile | 6 ++--- love-release.sh | 60 ++++++++++++++++++++++++++++++---------------- scripts/android.sh | 4 ++++ scripts/debian.sh | 4 ++++ scripts/example.sh | 1 + scripts/macosx.sh | 4 ++++ scripts/windows.sh | 4 ++++ 7 files changed, 60 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index c9ca29c..3a9e22d 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ SED_INSTALL_DIR=$(shell echo "$(INSTALL_DIR)" | sed -e 's/[\/&]/\\&/g') love-release: clean mkdir -p $(BUILD_DIR) - sed -e 's/INSTALL=false/INSTALL=true/' -e 's/SCRIPTS_DIR="scripts"/SCRIPTS_DIR="$(SED_INSTALL_DIR)\/scripts"/' love-release.sh > '$(BUILD_DIR)/love-release' + sed -e 's/INSTALLED=false/INSTALLED=true/' -e 's/SCRIPTS_DIR="scripts"/SCRIPTS_DIR="$(SED_INSTALL_DIR)\/scripts"/' love-release.sh > '$(BUILD_DIR)/love-release' cp love-release.1 '$(BUILD_DIR)/love-release.1' gzip '$(BUILD_DIR)/love-release.1' @@ -26,9 +26,9 @@ install: embedded: clean mkdir -p '$(BUILD_DIR)' sed 's/EMBEDDED=false/EMBEDDED=true/' love-release.sh > '$(BUILD_DIR)/love-release.sh' - for file in scripts/*; do \ + for file in scripts/*.sh; do \ module="$$(basename -s '.sh' "$$file")"; \ - content='if [[ $$(execute_module "'"$$module"'") == true ]]; then'$$'\n'"$$(cat $$file)"$$'\n''fi'$$'\n\n'; \ + content='(source <(cat <<\EndOfModule'$$'\n'"$$(cat $$file)"$$'\n''EndOfModule'$$'\n''))'$$'\n\n'; \ echo "$$content" >> "$(BUILD_DIR)/tmp"; \ done sed -i.bak -e '/include_scripts_here$$/r $(BUILD_DIR)/tmp' '$(BUILD_DIR)/love-release.sh'; diff --git a/love-release.sh b/love-release.sh index 5ee3a79..83452d5 100755 --- a/love-release.sh +++ b/love-release.sh @@ -35,6 +35,7 @@ check_deps () # Reset script variables reset_vars () { TITLE="$(basename $(pwd))" + MODULE= RELEASE_DIR=releases CACHE_DIR=~/.cache/love-release } @@ -168,7 +169,7 @@ f() love.conf(t) -- "love", "windows", "osx", "debian" or "android" -os = "$OS" +os = "$1" fields = { "identity", "version", "game_version", "icon", "exclude", @@ -178,8 +179,8 @@ for _, f in ipairs(fields) do t[f] = t[f] or "" end -for i in ipairs(t.os) do - t.os[os] = {} +for _, v in ipairs(t.os) do + t.os[v] = {} end if not t.os or #t.os == 0 then t.os.love = {} end @@ -203,7 +204,7 @@ else print(os:upper()..'=false') end -if os == "windows" then +if t.os.windows and os == "windows" then t.os.windows.x86 = t.os.windows.x86 and true or false t.os.windows.x64 = t.os.windows.x64 and true or false t.os.windows.installer = t.os.windows.installer and true or false @@ -231,6 +232,9 @@ dump_var () { 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" @@ -244,30 +248,39 @@ dump_var () { # Modules functions + +# Test if module should be executed ## $1: Module name ## return: true if module should be executed execute_module () { + reset_vars local module="$1" + MODULE="$module" read_config "$module" module=${module^^} - if [[ ${!module} == true && -z $DEFAULT_MODULE ]]; then - if [[ ${module} == "LOVE" ]]; then - DEFAULT_MODULE=true - else - DEFAULT_MODULE=false + if [[ ${!module} == true ]]; then + if [[ -z $DEFAULT_MODULE ]]; then + if [[ ${module} == "LOVE" ]]; then + DEFAULT_MODULE=true + else + DEFAULT_MODULE=false + fi fi + else + reset_vars fi echo "${!module}" } # Init module +## $1: Pretty module name init_module () { - reset_vars + MODULE="$1" mkdir -p "$RELEASE_DIR" - mkdir -p "$CACHE_DIR"/"$LOVE_VERSION" - echo "Generating $TITLE with Love $LOVE_VERSION for ${MODULE}..." + mkdir -p "$CACHE_DIR" + echo "Generating $TITLE with LÖVE $LOVE_VERSION for ${MODULE}..." } # Create the LÖVE file @@ -283,14 +296,19 @@ create_love_file () # Exit module ## $1: exit code. -## 0 - success, other - failure +## 0 - success +## 1 - binary not found or downloaded +## other - failure ## $2: error message exit_module () { if [[ -z $1 || "$1" == "0" ]]; then echo "Done !" + elif [[ "$1" == "1" ]]; then + >&2 echo -e "$2" + >&2 echo "LÖVE $LOVE_VERSION could not be found or downloaded." else - echo -e "$2" + >&2 echo -e "$2" exit $1 fi } @@ -321,17 +339,19 @@ if [[ $EMBEDDED == true ]]; then elif [[ $INSTALLED == true ]]; then SCRIPTS_DIR="scripts" for file in "$SCRIPTS_DIR"/*.sh; do - MODULE="$(basename -s '.sh' "$file")" - if [[ $(execute_module "$MODULE") == true ]]; then - source "$file" - fi + (source "$file") done fi if [[ -z $DEFAULT_MODULE || $DEFAULT_MODULE == true ]]; then - MODULE="love" - init_module + ( + reset_vars + read_config "love" + init_module "LÖVE" create_love_file 9 exit_module + ) fi +exit 0 + diff --git a/scripts/android.sh b/scripts/android.sh index 70bbf31..91bc547 100644 --- a/scripts/android.sh +++ b/scripts/android.sh @@ -1,4 +1,8 @@ # Android debug package +if [[ $(execute_module "android") == false ]]; then + return +fi + init_module "Android" diff --git a/scripts/debian.sh b/scripts/debian.sh index 46975c8..7633ce8 100644 --- a/scripts/debian.sh +++ b/scripts/debian.sh @@ -1,4 +1,8 @@ # Debian package +if [[ $(execute_module "debian") == false ]]; then + return +fi + init_module "Debian" diff --git a/scripts/example.sh b/scripts/example.sh index 6fad307..6eb3679 100644 --- a/scripts/example.sh +++ b/scripts/example.sh @@ -1,4 +1,5 @@ # Example +return # This example script should show you how to write a module for love-release. diff --git a/scripts/macosx.sh b/scripts/macosx.sh index cb1e5f3..bb2a6fd 100644 --- a/scripts/macosx.sh +++ b/scripts/macosx.sh @@ -1,4 +1,8 @@ # Mac OS X +if [[ $(execute_module "osx") == false ]]; then + return +fi + init_module "Mac OS X" diff --git a/scripts/windows.sh b/scripts/windows.sh index c33dd3c..59a8243 100644 --- a/scripts/windows.sh +++ b/scripts/windows.sh @@ -1,4 +1,8 @@ # Windows +if [[ $(execute_module "windows") == false ]]; then + return +fi + init_module "Windows"