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.
This commit is contained in:
Antonin Décimo
2015-06-14 18:17:43 +02:00
parent 06d5369877
commit 72ada933f1
7 changed files with 60 additions and 23 deletions

View File

@@ -11,7 +11,7 @@ SED_INSTALL_DIR=$(shell echo "$(INSTALL_DIR)" | sed -e 's/[\/&]/\\&/g')
love-release: clean love-release: clean
mkdir -p $(BUILD_DIR) 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' cp love-release.1 '$(BUILD_DIR)/love-release.1'
gzip '$(BUILD_DIR)/love-release.1' gzip '$(BUILD_DIR)/love-release.1'
@@ -26,9 +26,9 @@ install:
embedded: clean embedded: clean
mkdir -p '$(BUILD_DIR)' mkdir -p '$(BUILD_DIR)'
sed 's/EMBEDDED=false/EMBEDDED=true/' love-release.sh > '$(BUILD_DIR)/love-release.sh' 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")"; \ 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"; \ echo "$$content" >> "$(BUILD_DIR)/tmp"; \
done done
sed -i.bak -e '/include_scripts_here$$/r $(BUILD_DIR)/tmp' '$(BUILD_DIR)/love-release.sh'; sed -i.bak -e '/include_scripts_here$$/r $(BUILD_DIR)/tmp' '$(BUILD_DIR)/love-release.sh';

View File

@@ -35,6 +35,7 @@ check_deps ()
# Reset script variables # Reset script variables
reset_vars () { reset_vars () {
TITLE="$(basename $(pwd))" TITLE="$(basename $(pwd))"
MODULE=
RELEASE_DIR=releases RELEASE_DIR=releases
CACHE_DIR=~/.cache/love-release CACHE_DIR=~/.cache/love-release
} }
@@ -168,7 +169,7 @@ f()
love.conf(t) love.conf(t)
-- "love", "windows", "osx", "debian" or "android" -- "love", "windows", "osx", "debian" or "android"
os = "$OS" os = "$1"
fields = { fields = {
"identity", "version", "game_version", "icon", "exclude", "identity", "version", "game_version", "icon", "exclude",
@@ -178,8 +179,8 @@ for _, f in ipairs(fields) do
t[f] = t[f] or "" t[f] = t[f] or ""
end end
for i in ipairs(t.os) do for _, v in ipairs(t.os) do
t.os[os] = {} t.os[v] = {}
end end
if not t.os or #t.os == 0 then t.os.love = {} end if not t.os or #t.os == 0 then t.os.love = {} end
@@ -203,7 +204,7 @@ else
print(os:upper()..'=false') print(os:upper()..'=false')
end 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.x86 = t.os.windows.x86 and true or false
t.os.windows.x64 = t.os.windows.x64 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 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_DEF_VERSION=$LOVE_DEF_VERSION"
echo "LOVE_WEB_VERSION=$LOVE_WEB_VERSION" echo "LOVE_WEB_VERSION=$LOVE_WEB_VERSION"
echo echo
echo "RELEASE_DIR=$RELEASE_DIR"
echo "CACHE_DIR=$CACHE_DIR"
echo
echo "IDENTITY=$IDENTITY" echo "IDENTITY=$IDENTITY"
echo "GAME_VERSION=$GAME_VERSION" echo "GAME_VERSION=$GAME_VERSION"
echo "ICON=$ICON" echo "ICON=$ICON"
@@ -244,30 +248,39 @@ dump_var () {
# Modules functions # Modules functions
# Test if module should be executed
## $1: Module name ## $1: Module name
## return: true if module should be executed ## return: true if module should be executed
execute_module () execute_module ()
{ {
reset_vars
local module="$1" local module="$1"
MODULE="$module"
read_config "$module" read_config "$module"
module=${module^^} module=${module^^}
if [[ ${!module} == true && -z $DEFAULT_MODULE ]]; then if [[ ${!module} == true ]]; then
if [[ ${module} == "LOVE" ]]; then if [[ -z $DEFAULT_MODULE ]]; then
DEFAULT_MODULE=true if [[ ${module} == "LOVE" ]]; then
else DEFAULT_MODULE=true
DEFAULT_MODULE=false else
DEFAULT_MODULE=false
fi
fi fi
else
reset_vars
fi fi
echo "${!module}" echo "${!module}"
} }
# Init module # Init module
## $1: Pretty module name
init_module () init_module ()
{ {
reset_vars MODULE="$1"
mkdir -p "$RELEASE_DIR" mkdir -p "$RELEASE_DIR"
mkdir -p "$CACHE_DIR"/"$LOVE_VERSION" mkdir -p "$CACHE_DIR"
echo "Generating $TITLE with Love $LOVE_VERSION for ${MODULE}..." echo "Generating $TITLE with LÖVE $LOVE_VERSION for ${MODULE}..."
} }
# Create the LÖVE file # Create the LÖVE file
@@ -283,14 +296,19 @@ create_love_file ()
# Exit module # Exit module
## $1: exit code. ## $1: exit code.
## 0 - success, other - failure ## 0 - success
## 1 - binary not found or downloaded
## other - failure
## $2: error message ## $2: error message
exit_module () exit_module ()
{ {
if [[ -z $1 || "$1" == "0" ]]; then if [[ -z $1 || "$1" == "0" ]]; then
echo "Done !" echo "Done !"
elif [[ "$1" == "1" ]]; then
>&2 echo -e "$2"
>&2 echo "LÖVE $LOVE_VERSION could not be found or downloaded."
else else
echo -e "$2" >&2 echo -e "$2"
exit $1 exit $1
fi fi
} }
@@ -321,17 +339,19 @@ if [[ $EMBEDDED == true ]]; then
elif [[ $INSTALLED == true ]]; then elif [[ $INSTALLED == true ]]; then
SCRIPTS_DIR="scripts" SCRIPTS_DIR="scripts"
for file in "$SCRIPTS_DIR"/*.sh; do for file in "$SCRIPTS_DIR"/*.sh; do
MODULE="$(basename -s '.sh' "$file")" (source "$file")
if [[ $(execute_module "$MODULE") == true ]]; then
source "$file"
fi
done done
fi fi
if [[ -z $DEFAULT_MODULE || $DEFAULT_MODULE == true ]]; then 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 create_love_file 9
exit_module exit_module
)
fi fi
exit 0

View File

@@ -1,4 +1,8 @@
# Android debug package # Android debug package
if [[ $(execute_module "android") == false ]]; then
return
fi
init_module "Android" init_module "Android"

View File

@@ -1,4 +1,8 @@
# Debian package # Debian package
if [[ $(execute_module "debian") == false ]]; then
return
fi
init_module "Debian" init_module "Debian"

View File

@@ -1,4 +1,5 @@
# Example # Example
return
# This example script should show you how to write a module for love-release. # This example script should show you how to write a module for love-release.

View File

@@ -1,4 +1,8 @@
# Mac OS X # Mac OS X
if [[ $(execute_module "osx") == false ]]; then
return
fi
init_module "Mac OS X" init_module "Mac OS X"

View File

@@ -1,4 +1,8 @@
# Windows # Windows
if [[ $(execute_module "windows") == false ]]; then
return
fi
init_module "Windows" init_module "Windows"