chore(build): update utils.inc.sh

This commit is contained in:
Daniel
2019-01-23 08:02:24 +01:00
parent 999853eb32
commit c1d7ce13dd
+241 -244
View File
@@ -1,244 +1,241 @@
# bash utils from angularjs # bash utils from angularjs
# This file provides:
# This file provides: # - a default control flow
# - a default control flow # * initializes the environment
# * initializes the environment # * call a function in your script based on the arguments
# * call a function in your script based on the arguments # - named argument parsing and automatic generation of the "usage" for your script
# - named argument parsing and automatic generation of the "usage" for your script # - utility functions
# - utility functions #
# # Usage:
# Usage: # - define the variable ARGS_DEF (see below) with the arguments for your script
# - define the variable ARGS_DEF (see below) with the arguments for your script # - include this file using `source utils.inc` at the end of your script.
# - include this file using `source utils.inc` at the end of your script. #
# # Default control flow:
# Default control flow: # 0. Set the current directory to the directory of the script. By this
# 0. Set the current directory to the directory of the script. By this # the script can be called from anywhere.
# the script can be called from anywhere. # 1. Parse the named arguments
# 1. Parse the named arguments # 2. [Redacted]
# 2. [Redacted] # 3. If the parameter "verbose" is set, the `-x` flag will be set in bash.
# 3. If the parameter "verbose" is set, the `-x` flag will be set in bash. # 4. The function "init" will be called if it exists
# 4. The function "init" will be called if it exists # 5. If the parameter "action" is set, it will call the function with the name of that parameter.
# 5. If the parameter "action" is set, it will call the function with the name of that parameter. # Otherwise the function "run" will be called.
# Otherwise the function "run" will be called. #
# # Named Argument Parsing:
# Named Argument Parsing: # - The variable ARGS_DEF defines the valid command arguments
# - The variable ARGS_DEF defines the valid command arguments # * Required args syntax: --paramName=paramRegex
# * Required args syntax: --paramName=paramRegex # * Optional args syntax: [--paramName=paramRegex]
# * Optional args syntax: [--paramName=paramRegex] # * e.g. ARG_DEFS=("--required_param=(.+)" "[--optional_param=(.+)]")
# * e.g. ARG_DEFS=("--required_param=(.+)" "[--optional_param=(.+)]") # - Checks that:
# - Checks that: # * all arguments match to an entry in ARGS_DEF
# * all arguments match to an entry in ARGS_DEF # * all required arguments are present
# * all required arguments are present # * all arguments match their regex
# * all arguments match their regex # - Afterwards, every paramter value will be stored in a variable
# - Afterwards, every paramter value will be stored in a variable # with the name of the parameter in upper case (with dash converted to underscore).
# with the name of the parameter in upper case (with dash converted to underscore). #
# # Special arguments that are always available:
# Special arguments that are always available: # - "--action=.*": This parameter will be used to dispatch to a function with that name when the
# - "--action=.*": This parameter will be used to dispatch to a function with that name when the # script is started
# script is started # - "--verbose=true": This will set the `-x` flag in bash so that all calls will be logged
#
# - "--verbose=true": This will set the `-x` flag in bash so that all calls will be logged # Utility functions:
# # - readJsonProp
# Utility functions: # - replaceJsonProp
# - readJsonProp # - resolveDir
# - replaceJsonProp # - getVar
# - resolveDir # - serVar
# - getVar # - isFunction
# - serVar # always stop on errors
# - isFunction set -e
# always stop on errors function usage {
set -e echo "Usage: ${0} ${ARG_DEFS[@]}"
exit 1
function usage { }
echo "Usage: ${0} ${ARG_DEFS[@]}"
exit 1
} function parseArgs {
local REQUIRED_ARG_NAMES=()
function parseArgs { # -- helper functions
local REQUIRED_ARG_NAMES=() function varName {
# everything to upper case and dash to underscore
# -- helper functions echo ${1//-/_} | tr '[:lower:]' '[:upper:]'
function varName { }
# everything to upper case and dash to underscore
echo ${1//-/_} | tr '[:lower:]' '[:upper:]' function readArgDefs {
} local ARG_DEF
local AD_OPTIONAL
function readArgDefs { local AD_NAME
local ARG_DEF local AD_RE
local AD_OPTIONAL
local AD_NAME # -- helper functions
local AD_RE function parseArgDef {
local ARG_DEF_REGEX="(\[?)--([^=]+)=(.*)"
# -- helper functions if [[ ! $1 =~ $ARG_DEF_REGEX ]]; then
function parseArgDef { echo "Internal error: arg def has wrong format: $ARG_DEF"
local ARG_DEF_REGEX="(\[?)--([^=]+)=(.*)" exit 1
if [[ ! $1 =~ $ARG_DEF_REGEX ]]; then fi
echo "Internal error: arg def has wrong format: $ARG_DEF" AD_OPTIONAL="${BASH_REMATCH[1]}"
exit 1 AD_NAME="${BASH_REMATCH[2]}"
fi AD_RE="${BASH_REMATCH[3]}"
AD_OPTIONAL="${BASH_REMATCH[1]}" if [[ $AD_OPTIONAL ]]; then
AD_NAME="${BASH_REMATCH[2]}" # Remove last bracket for optional args.
AD_RE="${BASH_REMATCH[3]}" # Can't put this into the ARG_DEF_REGEX somehow...
if [[ $AD_OPTIONAL ]]; then AD_RE=${AD_RE%?}
# Remove last bracket for optional args. fi
# Can't put this into the ARG_DEF_REGEX somehow... }
AD_RE=${AD_RE%?}
fi # -- run
} for ARG_DEF in "${ARG_DEFS[@]}"
do
# -- run parseArgDef $ARG_DEF
for ARG_DEF in "${ARG_DEFS[@]}"
do local AD_NAME_UPPER=$(varName $AD_NAME)
parseArgDef $ARG_DEF setVar "${AD_NAME_UPPER}_OPTIONAL" "$AD_OPTIONAL"
setVar "${AD_NAME_UPPER}_RE" "$AD_RE"
local AD_NAME_UPPER=$(varName $AD_NAME) if [[ ! $AD_OPTIONAL ]]; then
setVar "${AD_NAME_UPPER}_OPTIONAL" "$AD_OPTIONAL" REQUIRED_ARG_NAMES+=($AD_NAME)
setVar "${AD_NAME_UPPER}_RE" "$AD_RE" fi
if [[ ! $AD_OPTIONAL ]]; then done
REQUIRED_ARG_NAMES+=($AD_NAME) }
fi
done function readAndValidateArgs {
} local ARG_NAME
local ARG_VALUE
function readAndValidateArgs { local ARG_NAME_UPPER
local ARG_NAME
local ARG_VALUE # -- helper functions
local ARG_NAME_UPPER function parseArg {
local ARG_REGEX="--([^=]+)=?(.*)"
# -- helper functions
function parseArg { if [[ ! $1 =~ $ARG_REGEX ]]; then
local ARG_REGEX="--([^=]+)=?(.*)" echo "Can't parse argument $i"
usage
if [[ ! $1 =~ $ARG_REGEX ]]; then fi
echo "Can't parse argument $i"
usage ARG_NAME="${BASH_REMATCH[1]}"
fi ARG_VALUE="${BASH_REMATCH[2]}"
ARG_NAME_UPPER=$(varName $ARG_NAME)
ARG_NAME="${BASH_REMATCH[1]}" }
ARG_VALUE="${BASH_REMATCH[2]}"
ARG_NAME_UPPER=$(varName $ARG_NAME) function validateArg {
} local AD_RE=$(getVar ${ARG_NAME_UPPER}_RE)
function validateArg { if [[ ! $AD_RE ]]; then
local AD_RE=$(getVar ${ARG_NAME_UPPER}_RE) echo "Unknown option: $ARG_NAME"
usage
if [[ ! $AD_RE ]]; then fi
echo "Unknown option: $ARG_NAME"
usage if [[ ! $ARG_VALUE =~ ^${AD_RE}$ ]]; then
fi echo "Wrong format: $ARG_NAME"
usage;
if [[ ! $ARG_VALUE =~ ^${AD_RE}$ ]]; then fi
echo "Wrong format: $ARG_NAME"
usage; # validate that the "action" option points to a valid function
fi if [[ $ARG_NAME == "action" ]] && ! isFunction $ARG_VALUE; then
echo "No action $ARG_VALUE defined in this script"
# validate that the "action" option points to a valid function usage;
if [[ $ARG_NAME == "action" ]] && ! isFunction $ARG_VALUE; then fi
echo "No action $ARG_VALUE defined in this script" }
usage;
fi # -- run
} for i in "$@"
do
# -- run parseArg $i
for i in "$@" validateArg
do setVar "${ARG_NAME_UPPER}" "$ARG_VALUE"
parseArg $i done
validateArg }
setVar "${ARG_NAME_UPPER}" "$ARG_VALUE"
done function checkMissingArgs {
} local ARG_NAME
for ARG_NAME in "${REQUIRED_ARG_NAMES[@]}"
function checkMissingArgs { do
local ARG_NAME ARG_VALUE=$(getVar $(varName $ARG_NAME))
for ARG_NAME in "${REQUIRED_ARG_NAMES[@]}"
do if [[ ! $ARG_VALUE ]]; then
ARG_VALUE=$(getVar $(varName $ARG_NAME)) echo "Missing: $ARG_NAME"
usage;
if [[ ! $ARG_VALUE ]]; then fi
echo "Missing: $ARG_NAME" done
usage; }
fi
done # -- run
} readArgDefs
readAndValidateArgs "$@"
# -- run checkMissingArgs
readArgDefs
readAndValidateArgs "$@" }
checkMissingArgs
# getVar(varName)
} function getVar {
echo ${!1}
# getVar(varName) }
function getVar {
echo ${!1} # setVar(varName, varValue)
} function setVar {
eval "$1=\"$2\""
# setVar(varName, varValue) }
function setVar {
eval "$1=\"$2\"" # isFunction(name)
} # - to be used in an if, so return 0 if successful and 1 if not!
function isFunction {
# isFunction(name) if [[ $(type -t $1) == "function" ]]; then
# - to be used in an if, so return 0 if successful and 1 if not! return 0
function isFunction { else
if [[ $(type -t $1) == "function" ]]; then return 1
return 0 fi
else }
return 1
fi # readJsonProp(jsonFile, property)
} # - restriction: property needs to be on an own line!
function readJsonProp {
# readJsonProp(jsonFile, property) echo $(sed -En 's/.*"'$2'"[ ]*:[ ]*"(.*)".*/\1/p' $1)
# - restriction: property needs to be on an own line! }
function readJsonProp {
echo $(sed -En 's/.*"'$2'"[ ]*:[ ]*"(.*)".*/\1/p' $1) # replaceJsonProp(jsonFile, property, newValue)
} # - note: propertyRegex will be automatically placed into a
# capturing group! -> all other groups start at index 2!
# replaceJsonProp(jsonFile, property, newValue) function replaceJsonProp {
# - note: propertyRegex will be automatically placed into a replaceInFile $1 "\"$2\": \".*?\"" "\"$2\": \"$3\""
# capturing group! -> all other groups start at index 2! }
function replaceJsonProp {
replaceInFile $1 "\"$2\": \".*?\"" "\"$2\": \"$3\"" # replaceInFile(file, findPattern, replacePattern)
} function replaceInFile {
perl -pi -e "s/$2/$3/g;" $1
# replaceInFile(file, findPattern, replacePattern) }
function replaceInFile {
perl -pi -e "s/$2/$3/g;" $1 # resolveDir(relativeDir)
} # - resolves a directory relative to the current script
function resolveDir {
# resolveDir(relativeDir) echo $(cd $SCRIPT_DIR; cd $1; pwd)
# - resolves a directory relative to the current script }
function resolveDir {
echo $(cd $SCRIPT_DIR; cd $1; pwd) function main {
} # normalize the working dir to the directory of the script
cd $(dirname $0);SCRIPT_DIR=$(pwd)
function main {
# normalize the working dir to the directory of the script ARG_DEFS+=("[--verbose=(true|false)]")
cd $(dirname $0);SCRIPT_DIR=$(pwd) parseArgs "$@"
ARG_DEFS+=("[--verbose=(true|false)]")
parseArgs "$@" # --verbose argument
if [[ $VERBOSE == "true" ]]; then
set -x
# --verbose argument fi
if [[ $VERBOSE == "true" ]]; then
set -x if isFunction init; then
fi init "$@"
fi
if isFunction init; then
init "$@" # jump to the function denoted by the --action argument,
fi # otherwise call the "run" function
if [[ $ACTION ]]; then
# jump to the function denoted by the --action argument, $ACTION "$@"
# otherwise call the "run" function else
if [[ $ACTION ]]; then run "$@"
$ACTION "$@" fi
else }
run "$@"
fi
} main "$@"
main "$@"