v0.2 added config file and deploy function

This commit is contained in:
Manuel Manhart 2024-03-28 15:17:16 +01:00
parent df1999fd4f
commit 5f802c9273
16 changed files with 182 additions and 65 deletions

View File

@ -46,6 +46,11 @@ script, so all of the functions defined in modules under the `lib` directory wil
## Changelog ## Changelog
__0.2__
* Added support for deploying (copying) to a (configurable) path
* Added configuration
__0.1__ __0.1__
* Created the initial script * Created the initial script

View File

@ -11,9 +11,11 @@
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
# set default constants / variables # set default constants / variables
SCRIPT_VERSION="0.1" VERSION="0.2"
CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config # TODO implement variable replacement during compilation and use this here instead of the hardcoded value:
#CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config
CONFIG_FILE_NAME=bash-script-compiler.config
CONFIG_PATH=${HOME}/.config/mmit CONFIG_PATH=${HOME}/.config/mmit
CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME} CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME}
ACTION=${1:-"help"} ACTION=${1:-"help"}
@ -34,6 +36,7 @@ function main() {
if [ "function" == "$isParseArgsDefined" ]; then if [ "function" == "$isParseArgsDefined" ]; then
"$parseArgsFunc" "${@:2}" "$parseArgsFunc" "${@:2}"
fi fi
configRead
"$action" "${@:2}" "$action" "${@:2}"
} }
@ -78,19 +81,90 @@ compile_setTargetFile() {
echo "Creating output dir '${TARGET_DIR_NAME}'" echo "Creating output dir '${TARGET_DIR_NAME}'"
mkdir -p ${TARGET_DIR_NAME} mkdir -p ${TARGET_DIR_NAME}
fi fi
# we need to add ../ since we will be cd-ing into the src dir inside the PRJ_DIR
TARGET_FILE=../${TARGET_DIR_NAME}/$bashScriptName TARGET_FILE=../${TARGET_DIR_NAME}/$bashScriptName
cd $SRC_DIR_NAME
} }
compile() { compile() {
pushd $PRJ_DIR >/dev/null 2>&1 pushd $PRJ_DIR >/dev/null 2>&1
compile_setTargetFile compile_setTargetFile
cd $SRC_DIR_NAME
compile_defineMainFunction compile_defineMainFunction
compile_addFunctions compile_addFunctions
compile_invokeMainFunction compile_invokeMainFunction
compile_copyCompletionFile compile_copyCompletionFile
echo "Compiled successfully" echo "Compiled successfully"
popd popd >/dev/null 2>&1
}
configInit() {
if [ ! -f "${CONFIG_FILE}" ]; then
echo Could not find any config file, creating a file for you to fill in the values in ${CONFIG_FILE}
if [ ! -d "${CONFIG_PATH}" ]; then
echo "Config directory '${CONFIG_PATH}' does not exist yet, creating..."
mkdir -p ${CONFIG_PATH}
fi
cat <<END >${CONFIG_FILE}
# this file was created by bash-script-compiler script by Manuel Manhart
# it is now in your hands and will not be changed by scripts anymore
# the path where a compiled bash script should be copied to (usually a folder in your environment PATH variable)
DEPLOY_PATH=~/bin
# possible values all, bin
COPY=all
END
fi
}
# reads the configuration file where the docker container names are saved
configPrint() {
echo ""
echo "Configuration file:"
cat ${CONFIG_FILE} | while read line
do
echo "$line"
done
echo ""
echo "Version: $VERSION"
}
# reads the configuration file where the docker container names are saved
configRead() {
if [ -f "${CONFIG_FILE}" ]; then
echo found config file in ${CONFIG_FILE}
source ${CONFIG_FILE}
fi
# as fallback search in script home directory
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
if [ -f "${SCRIPTPATH}/${CONFIG_FILE_NAME}" ]; then
echo "found config file in ${SCRIPTPATH}/${CONFIG_FILE_NAME}"
declare $(env -i `cat ${SCRIPTPATH}/${CONFIG_FILE_NAME}` >/dev/null 2>&1)
fi
}
deploy() {
pushd $PRJ_DIR >/dev/null 2>&1
if [ ! -d "${DEPLOY_PATH}" ]; then
echo "Deploy path is set to: '${DEPLOY_PATH}', please call the script with configInit first"
help configInit
fi
if [ ! -d "./${TARGET_DIR_NAME}" ]; then
echo "Could not find any deployable script, have you compiled successfully?"
help deploy
fi
local bashScriptName=$(basename "$(pwd)")
local copySource="${TARGET_DIR_NAME}/*"
if [ "$COPY" == "bin" ]; then
copySource="${TARGET_DIR_NAME}/${bashScriptName}"
fi
if [ "$DEBUG" == "true" ]; then
echo "DEBUG - pwd: `pwd`"
echo "DEBUG - ls: `ls`"
echo "DEBUG - buildTargetDir: ${TARGET_DIR_NAME}"
echo "DEBUG - deployPath: ${DEPLOY_PATH}"
echo "DEBUG: cp -r \"${copySource}\" \"${DEPLOY_PATH}\""
fi
cp -r $copySource "${DEPLOY_PATH}"
popd >/dev/null 2>&1
} }
help() { help() {
@ -104,15 +178,16 @@ Usage:
The options are: The options are:
need a param (you can combine like ubd) need a param (you can combine like ubd)
config init ... for creating a config file (if none exists yet - automatically checked) configInit ... for creating a config file (if none exists yet - automatically checked)
config print ... for printing the config file content configPrint ... for printing the config file content
compile PRJ_PATH ... compiles the bash project in the given path (./ compiles itself) compile PRJ_PATH ... compiles the bash project in the given path (./ compiles itself)
deploy PRJ_PATH ... deploys a compiled bash project in the configured path
new ... initializes a new bash project new ... initializes a new bash project
help ... show this page help ... show this page
Version: $SCRIPT_VERSION Version: $VERSION
END END
exit exit
@ -156,6 +231,19 @@ parseArgs_compile() {
fi fi
} }
parseArgs_deploy() {
if [ -z $1 ]; then
echo "Please give a directory to the bash script to deploy"
help
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
help
else
PRJ_DIR=$1
fi
}
parseArgs() { parseArgs() {
if [ -z $1 ]; then if [ -z $1 ]; then
help help

View File

@ -7,5 +7,5 @@ configPrint() {
echo "$line" echo "$line"
done done
echo "" echo ""
echo "Version: $SCRIPT_VERSION" echo "Version: $VERSION"
} }

View File

@ -20,7 +20,7 @@ The options are:
help ... show this page help ... show this page
Version: $SCRIPT_VERSION Version: $VERSION
END END
exit exit

View File

@ -11,7 +11,7 @@
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
# set default constants / variables # set default constants / variables
SCRIPT_VERSION="0.1" VERSION="0.1"
# define your global variables here # define your global variables here
CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config

View File

@ -5,11 +5,11 @@ _bash-script-compiler-completion()
cur="${COMP_WORDS[COMP_CWORD]}" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
prevPrev="${COMP_WORDS[COMP_CWORD-2]}" prevPrev="${COMP_WORDS[COMP_CWORD-2]}"
opts="compile new help config" opts="compile deploy new help config"
config="print set init" config="print set init"
# TODO check if this is still the most efficient way to list help options # TODO check if this is still the most efficient way to list help options
HELP_PATH=${BASH_SOURCE%/*}/gt-help HELP_PATH=${BASH_SOURCE%/*}/bash-script-compiler-help
help=${opts} help=${opts}
#help=`ls $HELP_PATH` #help=`ls $HELP_PATH`
@ -24,6 +24,8 @@ _bash-script-compiler-completion()
return 0 return 0
elif [ "$prev" == "compile" ] || [ "$prev" == "-c" ]; then elif [ "$prev" == "compile" ] || [ "$prev" == "-c" ]; then
createFromDirectory "." createFromDirectory "."
elif [ "$prev" == "deploy" ] || [ "$prev" == "-d" ]; then
createFromDirectory "."
else else
createFromString "${opts}" createFromString "${opts}"
fi fi

View File

@ -1,10 +1,11 @@
compile() { compile() {
pushd $PRJ_DIR >/dev/null 2>&1 pushd $PRJ_DIR >/dev/null 2>&1
compile_setTargetFile compile_setTargetFile
cd $SRC_DIR_NAME
compile_defineMainFunction compile_defineMainFunction
compile_addFunctions compile_addFunctions
compile_invokeMainFunction compile_invokeMainFunction
compile_copyCompletionFile compile_copyCompletionFile
echo "Compiled successfully" echo "Compiled successfully"
popd popd >/dev/null 2>&1
} }

View File

@ -4,6 +4,6 @@ compile_setTargetFile() {
echo "Creating output dir '${TARGET_DIR_NAME}'" echo "Creating output dir '${TARGET_DIR_NAME}'"
mkdir -p ${TARGET_DIR_NAME} mkdir -p ${TARGET_DIR_NAME}
fi fi
# we need to add ../ since we will be cd-ing into the src dir inside the PRJ_DIR
TARGET_FILE=../${TARGET_DIR_NAME}/$bashScriptName TARGET_FILE=../${TARGET_DIR_NAME}/$bashScriptName
cd $SRC_DIR_NAME
} }

View File

@ -1,28 +1,18 @@
configInit() { configInit() {
if [ ! -f "${DEV_CONFIG_FILE}" ]; then if [ ! -f "${CONFIG_FILE}" ]; then
echo Could not find any config file, creating a file for you to fill in the values in ${DEV_CONFIG_FILE} echo Could not find any config file, creating a file for you to fill in the values in ${CONFIG_FILE}
if [ ! -d "${DEV_CONFIG_FILE_PATH}" ]; then if [ ! -d "${CONFIG_PATH}" ]; then
echo "Config directory '${DEV_CONFIG_FILE_PATH}' does not exist yet, creating..." echo "Config directory '${CONFIG_PATH}' does not exist yet, creating..."
mkdir -p ${DEV_CONFIG_FILE_PATH} mkdir -p ${CONFIG_PATH}
fi fi
if [ -f "/IMAGE_NAME" ]; then cat <<END >${CONFIG_FILE}
# || [ `whoami` = 'dev' ]; then # this file was created by bash-script-compiler script by Manuel Manhart
MODE=devcontainer
GRADLE_DEFAULT=wrapper
else
MODE=host
GRADLE_DEFAULT=normal
fi
cat <<END >${DEV_CONFIG_FILE}
# this file was created by dev script by Manuel Manhart
# it is now in your hands and will not be changed by scripts anymore # it is now in your hands and will not be changed by scripts anymore
MODE=$MODE # the path where a compiled bash script should be copied to (usually a folder in your environment PATH variable)
LIFERAY_DOCKER=( DEPLOY_PATH=~/bin
"TODO" # change this to the real path # possible values all, bin
) COPY=all
END END
fi fi
} }

View File

@ -2,10 +2,10 @@
configPrint() { configPrint() {
echo "" echo ""
echo "Configuration file:" echo "Configuration file:"
cat ${DEV_CONFIG_FILE} | while read line cat ${CONFIG_FILE} | while read line
do do
echo "$line" echo "$line"
done done
echo "" echo ""
echo "Version: $SCRIPT_VERSION" echo "Version: $VERSION"
} }

View File

@ -1,23 +1,13 @@
# reads the configuration file where the docker container names are saved # reads the configuration file where the docker container names are saved
configRead() { configRead() {
if [ -f "${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME}" ]; then if [ -f "${CONFIG_FILE}" ]; then
echo found config file in ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME} echo found config file in ${CONFIG_FILE}
source ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME} source ${CONFIG_FILE}
# Read common vars from the config file
# the incantation here ensures (by env) that only key=value pairs are present
# then declare-ing the result puts those vars in our environment
# declare $(env -i `cat ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME}` >/dev/null 2>&1) >/dev/null 2>&1
# IFS=$'\n' read -d '' -r -a LIFERAY_DOCKER < ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME}
fi fi
# as fallback search in script home directory # as fallback search in script home directory
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
if [ -f "${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME}" ]; then if [ -f "${SCRIPTPATH}/${CONFIG_FILE_NAME}" ]; then
echo found config file in ${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME} echo "found config file in ${SCRIPTPATH}/${CONFIG_FILE_NAME}"
declare $(env -i `cat ${SCRIPTPATH}/${CONFIG_FILE_NAME}` >/dev/null 2>&1)
declare $(env -i `cat ${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME}` >/dev/null 2>&1)
# source ${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME}
# IFS=$'\n' read -d '' -r -a LIFERAY_DOCKER < ${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME}
fi fi
} }

25
src/functions/deploy.sh Normal file
View File

@ -0,0 +1,25 @@
deploy() {
pushd $PRJ_DIR >/dev/null 2>&1
if [ ! -d "${DEPLOY_PATH}" ]; then
echo "Deploy path is set to: '${DEPLOY_PATH}', please call the script with configInit first"
help configInit
fi
if [ ! -d "./${TARGET_DIR_NAME}" ]; then
echo "Could not find any deployable script, have you compiled successfully?"
help deploy
fi
local bashScriptName=$(basename "$(pwd)")
local copySource="${TARGET_DIR_NAME}/*"
if [ "$COPY" == "bin" ]; then
copySource="${TARGET_DIR_NAME}/${bashScriptName}"
fi
if [ "$DEBUG" == "true" ]; then
echo "DEBUG - pwd: `pwd`"
echo "DEBUG - ls: `ls`"
echo "DEBUG - buildTargetDir: ${TARGET_DIR_NAME}"
echo "DEBUG - deployPath: ${DEPLOY_PATH}"
echo "DEBUG: cp -r \"${copySource}\" \"${DEPLOY_PATH}\""
fi
cp -r $copySource "${DEPLOY_PATH}"
popd >/dev/null 2>&1
}

View File

@ -9,15 +9,16 @@ Usage:
The options are: The options are:
need a param (you can combine like ubd) need a param (you can combine like ubd)
config init ... for creating a config file (if none exists yet - automatically checked) configInit ... for creating a config file (if none exists yet - automatically checked)
config print ... for printing the config file content configPrint ... for printing the config file content
compile PRJ_PATH ... compiles the bash project in the given path (./ compiles itself) compile PRJ_PATH ... compiles the bash project in the given path (./ compiles itself)
deploy PRJ_PATH ... deploys a compiled bash project in the configured path
new ... initializes a new bash project new ... initializes a new bash project
help ... show this page help ... show this page
Version: $SCRIPT_VERSION Version: $VERSION
END END
exit exit

View File

@ -0,0 +1,12 @@
parseArgs_deploy() {
if [ -z $1 ]; then
echo "Please give a directory to the bash script to deploy"
help
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
help
else
PRJ_DIR=$1
fi
}

View File

@ -11,9 +11,11 @@
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
# set default constants / variables # set default constants / variables
SCRIPT_VERSION="0.1" VERSION="0.2"
CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config # TODO implement variable replacement during compilation and use this here instead of the hardcoded value:
#CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config
CONFIG_FILE_NAME=bash-script-compiler.config
CONFIG_PATH=${HOME}/.config/mmit CONFIG_PATH=${HOME}/.config/mmit
CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME} CONFIG_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME}
ACTION=${1:-"help"} ACTION=${1:-"help"}

View File

@ -4,4 +4,5 @@ local isParseArgsDefined=$(type -t $parseArgsFunc)
if [ "function" == "$isParseArgsDefined" ]; then if [ "function" == "$isParseArgsDefined" ]; then
"$parseArgsFunc" "${@:2}" "$parseArgsFunc" "${@:2}"
fi fi
configRead
"$action" "${@:2}" "$action" "${@:2}"