ezbuild/libraries/common

197 lines
6.3 KiB
Plaintext
Raw Normal View History

2011-12-27 16:34:26 +01:00
#!/bin/bash
2011-12-29 14:29:24 +01:00
# Copyright 2010-2011, Asio Software Technologies
# Distributed under the terms of the GNU General Public License v3
2011-12-27 16:34:26 +01:00
#-------------------------------------------------------------------------------
# Checks whether a supplied variable is defined or not
# Parameters: %variable%
#-------------------------------------------------------------------------------
function isDefined() {
[[ ${!1-X} == ${!1-Y} ]]
}
#-------------------------------------------------------------------------------
# Checks whether a supplied variable is enabled or not
# Parameters: %variable%
#-------------------------------------------------------------------------------
function isEnabled() {
case "${1}" in
[Ee][Nn][Aa][Bb][Ll][Ee][Dd])
return 0
;;
[Tt][Rr][Uu][Ee])
return 0
;;
[Yy][Ee][Ss])
return 0
;;
1)
return 0
;;
esac
return 1
}
#-------------------------------------------------------------------------------
# Checks whether a variable is defined and if value's length > 0
# Parameters: %variable%
#-------------------------------------------------------------------------------
function isSet() {
if isDefined ${1}; then
if [[ -n ${!1} ]]; then
return 0
fi
fi
return 1
}
#-------------------------------------------------------------------------------
# Loads EzBuild configuration
#-------------------------------------------------------------------------------
function loadConfiguration() {
2011-12-28 14:57:22 +01:00
source ${EZROOT}/config/${EZNAME}.conf &> /dev/null || panic
source ${EZROOT}/config/layout.conf &> /dev/null || panic
source /etc/ezbuild/${EZNAME}.conf &> /dev/null || panic
source /etc/ezbuild/layout.conf &> /dev/null || panic
}
#-------------------------------------------------------------------------------
# Loads all EzBuild Libraries
#-------------------------------------------------------------------------------
function loadLibraries() {
2011-12-28 14:57:22 +01:00
source ${EZROOT}/libraries/colors &> /dev/null || panic
2011-12-28 14:35:43 +01:00
}
2011-12-29 23:58:25 +01:00
#-------------------------------------------------------------------------------
# Saves specified message into a log file
# Parameters: %message% %level%
#-------------------------------------------------------------------------------
function logMessage() {
if isEnabled ${LOGGING}; then
2011-12-30 00:46:06 +01:00
local MESSAGE="${1}"
local TYPE="${2}"
local DATE=$(date +"${LOGDATEFORMAT}")
2011-12-29 23:58:25 +01:00
if [ -z ${TYPE} ]; then
2011-12-30 00:46:06 +01:00
local TYPE=" - "
2011-12-29 23:58:25 +01:00
fi
echo -e "[${DATE}][${TYPE}] ${MESSAGE}" >> ${LOGFILE}
fi
}
#-------------------------------------------------------------------------------
# Saves executed command multiline output into a log file
# Parameters: %command_output%
#-------------------------------------------------------------------------------
function logOutput() {
if isEnabled ${LOGGING}; then
echo -e "${@}" | awk '{ print " )> ", $0; }' >> ${LOGFILE}
fi
}
2011-12-28 14:35:43 +01:00
#-------------------------------------------------------------------------------
# Outputs error message and aborts program execution
#-------------------------------------------------------------------------------
function panic() {
2011-12-28 14:57:22 +01:00
echo -e "FATAL ERROR: Unable to load necessary files!"
echo -e "Your EzBuild installation seems to be broken..."
2011-12-28 14:35:43 +01:00
exit 1
}
#-------------------------------------------------------------------------------
2011-12-29 23:58:25 +01:00
# Outputs formatted error message to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printError() {
logMessage "${1}" "ERROR"
echo -e " ${MESSAGE_ERROR} ${@}"
}
#-------------------------------------------------------------------------------
# Outputs formatted information to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printInfo() {
logMessage "${1}" "INFO"
echo -e " ${MESSAGE_INFO} ${@}"
}
#-------------------------------------------------------------------------------
# Outputs formatted warning to both display and log file
# Parameters: %message%
#-------------------------------------------------------------------------------
function printWarn() {
logMessage "${1}" "WARN"
echo -e " ${MESSAGE_WARN} ${@}"
}
#-------------------------------------------------------------------------------
# Restores original Internal Field Separator (IFS)
#-------------------------------------------------------------------------------
function restoreIFS() {
if [ "${ORGIFS:-unset}" != "unset" ]; then
IFS="${ORGIFS}"
unset ORGIFS
else
unset IFS
fi
}
2011-12-29 23:58:25 +01:00
#-------------------------------------------------------------------------------
# Silently executes given command and saves its output to log file if enabled
# Parameters: %command%
#-------------------------------------------------------------------------------
function run() {
2011-12-30 00:46:06 +01:00
local COMMAND="${1}"
2011-12-29 23:58:25 +01:00
logMessage "Executing: \"${COMMAND}\"" "DEBUG"
2011-12-30 00:46:06 +01:00
local OUTPUT=$(${COMMAND} 2>&1)
local RESULT=${?}
2011-12-29 23:58:25 +01:00
logOutput "${OUTPUT}"
return ${RESULT}
}
#-------------------------------------------------------------------------------
# Saves original Internal Field Separator (IFS) and optionally sets new value
# Parameters: %new_ifs%
#-------------------------------------------------------------------------------
function saveIFS() {
if [ "${IFS:-unset}" != "unset" ]; then
ORGIFS="${IFS}"
fi
local NEWIFS="${1}"
if isSet NEWIFS; then
IFS="${NEWIFS}"
fi
}
#-------------------------------------------------------------------------------
2011-12-27 16:34:26 +01:00
# Returns a UNIX timestamp
#-------------------------------------------------------------------------------
function timestamp() {
echo $(date +%s)
}
#-------------------------------------------------------------------------------
# Returns lowercase string
# Parameters: %string%
#-------------------------------------------------------------------------------
function toLower() {
echo "${@}" | tr '[:upper:]' '[:lower:]'
}
#-------------------------------------------------------------------------------
# Returns uppercase string
# Parameters: %string%
#-------------------------------------------------------------------------------
function toUpper() {
echo "${@}" | tr '[:lower:]' '[:upper:]'
}
#-------------------------------------------------------------------------------
# Returns only the first part of a string, delimited by tabs or spaces
# Parameters: %string%
#-------------------------------------------------------------------------------
function trim() {
echo ${1}
}