diff --git a/README.md b/README.md index 0d21702..5d03bac 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ script, so all of the functions defined in modules under the `lib` directory wil ## Changelog +__0.2__ + +* Added support for deploying (copying) to a (configurable) path +* Added configuration + __0.1__ * Created the initial script diff --git a/bash-script-compiler b/bash-script-compiler index dfd8bf3..259cacc 100755 --- a/bash-script-compiler +++ b/bash-script-compiler @@ -11,9 +11,11 @@ # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # 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_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME} ACTION=${1:-"help"} @@ -28,13 +30,14 @@ PARSE_ARGS_PREFIX="parseArgs_" # TODO (medium) implement support for PRJ_HELP (for loop can only be in one line it seems) function main() { - local action=${1:-"help"} - local parseArgsFunc="${PARSE_ARGS_PREFIX}${action}" - local isParseArgsDefined=$(type -t $parseArgsFunc) - if [ "function" == "$isParseArgsDefined" ]; then - "$parseArgsFunc" "${@:2}" - fi - "$action" "${@:2}" + local action=${1:-"help"} + local parseArgsFunc="${PARSE_ARGS_PREFIX}${action}" + local isParseArgsDefined=$(type -t $parseArgsFunc) + if [ "function" == "$isParseArgsDefined" ]; then + "$parseArgsFunc" "${@:2}" + fi + configRead + "$action" "${@:2}" } compile_addFunctions() { @@ -78,19 +81,90 @@ compile_setTargetFile() { echo "Creating output dir '${TARGET_DIR_NAME}'" mkdir -p ${TARGET_DIR_NAME} 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 - cd $SRC_DIR_NAME } compile() { pushd $PRJ_DIR >/dev/null 2>&1 compile_setTargetFile + cd $SRC_DIR_NAME compile_defineMainFunction compile_addFunctions compile_invokeMainFunction compile_copyCompletionFile 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 <${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() { @@ -104,15 +178,16 @@ Usage: The options are: need a param (you can combine like ubd) - config init ... for creating a config file (if none exists yet - automatically checked) - config print ... for printing the config file content + configInit ... for creating a config file (if none exists yet - automatically checked) + configPrint ... for printing the config file content 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 help ... show this page -Version: $SCRIPT_VERSION +Version: $VERSION END exit @@ -156,6 +231,19 @@ parseArgs_compile() { 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() { if [ -z $1 ]; then help diff --git a/init/src/functions/configPrint.sh b/init/src/functions/configPrint.sh index c4ba1ef..d15ba87 100644 --- a/init/src/functions/configPrint.sh +++ b/init/src/functions/configPrint.sh @@ -7,5 +7,5 @@ configPrint() { echo "$line" done echo "" - echo "Version: $SCRIPT_VERSION" + echo "Version: $VERSION" } diff --git a/init/src/functions/help.sh b/init/src/functions/help.sh index e93c457..f9ab9ba 100644 --- a/init/src/functions/help.sh +++ b/init/src/functions/help.sh @@ -20,7 +20,7 @@ The options are: help ... show this page -Version: $SCRIPT_VERSION +Version: $VERSION END exit diff --git a/init/src/intro.sh b/init/src/intro.sh index ac9cb92..dba98a1 100644 --- a/init/src/intro.sh +++ b/init/src/intro.sh @@ -11,7 +11,7 @@ # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # set default constants / variables -SCRIPT_VERSION="0.1" +VERSION="0.1" # define your global variables here CONFIG_FILE_NAME=BASH_SCRIPT_COMPILER_SCRIPT_NAME.config diff --git a/src/bash-script-compiler-completion b/src/bash-script-compiler-completion index 1da22db..cf4ff93 100644 --- a/src/bash-script-compiler-completion +++ b/src/bash-script-compiler-completion @@ -5,11 +5,11 @@ _bash-script-compiler-completion() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" prevPrev="${COMP_WORDS[COMP_CWORD-2]}" - opts="compile new help config" + opts="compile deploy new help config" config="print set init" # 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=`ls $HELP_PATH` @@ -24,6 +24,8 @@ _bash-script-compiler-completion() return 0 elif [ "$prev" == "compile" ] || [ "$prev" == "-c" ]; then createFromDirectory "." + elif [ "$prev" == "deploy" ] || [ "$prev" == "-d" ]; then + createFromDirectory "." else createFromString "${opts}" fi diff --git a/src/functions/compile.sh b/src/functions/compile.sh index 136ada1..9550a94 100644 --- a/src/functions/compile.sh +++ b/src/functions/compile.sh @@ -1,10 +1,11 @@ compile() { pushd $PRJ_DIR >/dev/null 2>&1 compile_setTargetFile + cd $SRC_DIR_NAME compile_defineMainFunction compile_addFunctions compile_invokeMainFunction compile_copyCompletionFile echo "Compiled successfully" - popd + popd >/dev/null 2>&1 } \ No newline at end of file diff --git a/src/functions/compile_setTargetFile.sh b/src/functions/compile_setTargetFile.sh index 820d2c6..7ccda7f 100644 --- a/src/functions/compile_setTargetFile.sh +++ b/src/functions/compile_setTargetFile.sh @@ -4,6 +4,6 @@ compile_setTargetFile() { echo "Creating output dir '${TARGET_DIR_NAME}'" mkdir -p ${TARGET_DIR_NAME} 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 - cd $SRC_DIR_NAME } diff --git a/src/functions/configInit.sh b/src/functions/configInit.sh index c784541..c7c5cc1 100644 --- a/src/functions/configInit.sh +++ b/src/functions/configInit.sh @@ -1,28 +1,18 @@ configInit() { - if [ ! -f "${DEV_CONFIG_FILE}" ]; then - echo Could not find any config file, creating a file for you to fill in the values in ${DEV_CONFIG_FILE} - if [ ! -d "${DEV_CONFIG_FILE_PATH}" ]; then - echo "Config directory '${DEV_CONFIG_FILE_PATH}' does not exist yet, creating..." - mkdir -p ${DEV_CONFIG_FILE_PATH} + 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 - if [ -f "/IMAGE_NAME" ]; then - # || [ `whoami` = 'dev' ]; then - MODE=devcontainer - GRADLE_DEFAULT=wrapper - else - MODE=host - GRADLE_DEFAULT=normal - fi - - cat <${DEV_CONFIG_FILE} -# this file was created by dev script by Manuel Manhart + cat <${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 -MODE=$MODE -LIFERAY_DOCKER=( - "TODO" # change this to the real path -) +# 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 } diff --git a/src/functions/configPrint.sh b/src/functions/configPrint.sh index c4ba1ef..88c76b0 100644 --- a/src/functions/configPrint.sh +++ b/src/functions/configPrint.sh @@ -2,10 +2,10 @@ configPrint() { echo "" echo "Configuration file:" - cat ${DEV_CONFIG_FILE} | while read line + cat ${CONFIG_FILE} | while read line do echo "$line" done echo "" - echo "Version: $SCRIPT_VERSION" + echo "Version: $VERSION" } diff --git a/src/functions/configRead.sh b/src/functions/configRead.sh index d9a57b1..8670c6a 100644 --- a/src/functions/configRead.sh +++ b/src/functions/configRead.sh @@ -1,23 +1,13 @@ # reads the configuration file where the docker container names are saved configRead() { - if [ -f "${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME}" ]; then - echo found config file in ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME} - source ${DEV_CONFIG_FILE_PATH}/${DEV_CONFIG_FILE_NAME} - - # 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} + 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}/${DEV_CONFIG_FILE_NAME}" ]; then - echo found config file in ${SCRIPTPATH}/${DEV_CONFIG_FILE_NAME} - - 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} + 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 } diff --git a/src/functions/deploy.sh b/src/functions/deploy.sh new file mode 100644 index 0000000..1788c6a --- /dev/null +++ b/src/functions/deploy.sh @@ -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 +} diff --git a/src/functions/help.sh b/src/functions/help.sh index 83ed7c0..0193725 100644 --- a/src/functions/help.sh +++ b/src/functions/help.sh @@ -9,15 +9,16 @@ Usage: The options are: need a param (you can combine like ubd) - config init ... for creating a config file (if none exists yet - automatically checked) - config print ... for printing the config file content + configInit ... for creating a config file (if none exists yet - automatically checked) + configPrint ... for printing the config file content 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 help ... show this page -Version: $SCRIPT_VERSION +Version: $VERSION END exit diff --git a/src/functions/parseArgs_deploy.sh b/src/functions/parseArgs_deploy.sh new file mode 100644 index 0000000..4b80b91 --- /dev/null +++ b/src/functions/parseArgs_deploy.sh @@ -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 +} diff --git a/src/intro.sh b/src/intro.sh index dcff5a7..debf036 100644 --- a/src/intro.sh +++ b/src/intro.sh @@ -11,9 +11,11 @@ # Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION # 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_FILE=${CONFIG_PATH}/${CONFIG_FILE_NAME} ACTION=${1:-"help"} diff --git a/src/main.sh b/src/main.sh index de9c5ab..be6134e 100644 --- a/src/main.sh +++ b/src/main.sh @@ -4,4 +4,5 @@ local isParseArgsDefined=$(type -t $parseArgsFunc) if [ "function" == "$isParseArgsDefined" ]; then "$parseArgsFunc" "${@:2}" fi +configRead "$action" "${@:2}" \ No newline at end of file