bash-script-compiler/bash-script-compiler

264 lines
6.9 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# This script is generated. Please do checkout the source code for editing!
#
# This is a tool for creating big bash script projects in smaller files / chunks.
# Source Code: https://code.manhart.space/mmit/bash-script-compiler.git
# Maintainer: Manuel Manhart
# Company: Manuel Manhart IT e.U.
# License: MIT
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
# set default constants / variables
VERSION="0.2"
# 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"}
TARGET_DIR_NAME="dist"
SRC_DIR_NAME="src"
MAIN_FUNC_FILE_NAME="main.sh"
INTRO_FILE_NAME="intro.sh"
PARSE_ARGS_PREFIX="parseArgs_"
# TODO (minor) we do not want to print every command which is executed, only our specific "echo"s
# 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
configRead
"$action" "${@:2}"
}
compile_addFunctions() {
PRJ_FUNC=$(ls -d ./functions/*)
for filename in ${PRJ_FUNC[*]}; do
cat ${filename} >> ${TARGET_FILE}
echo "" >> ${TARGET_FILE}
done
}
compile_copyCompletionFile() {
cp *-completion ../${TARGET_DIR_NAME}
}
compile_defineMainFunction() {
touch ${TARGET_FILE}
if [ ! -f "${INTRO_FILE_NAME}" ]; then
echo "WARN: Could not find intro file '${INTRO_FILE_NAME}' in ${PRJ_DIR}"
else
cat "${INTRO_FILE_NAME}" > ${TARGET_FILE}
echo "" >> ${TARGET_FILE}
fi
if [ ! -f "${MAIN_FUNC_FILE_NAME}" ]; then
echo "ERROR: Could not find main function file '${MAIN_FUNC_FILE_NAME}' in ${PRJ_DIR}"
help
exit
else
echo -e "function main() {" >> ${TARGET_FILE}
cat "${MAIN_FUNC_FILE_NAME}" | sed -e 's/^/ /g' >> ${TARGET_FILE}
echo -e "\n}\n" >> ${TARGET_FILE}
fi
}
compile_invokeMainFunction() {
echo "main \"\$@\"" >> ${TARGET_FILE}
}
compile_setTargetFile() {
local bashScriptName=$(basename "$(pwd)")
if [ ! -d "${TARGET_DIR_NAME}" ]; then
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
}
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 >/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() {
local progName=$(echo "$0" | rev | cut -d'/' -f1 | rev)
cat <<-END
$progName is a tool for creating big bash script projects in smaller files / chunks.
Usage:
$progName [options]
The options are:
need a param (you can combine like ubd)
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: $VERSION
END
exit
}
new_InitProject() {
echo "Not yet implemented"
# TODO implement copying from ./init to project name
}
new_ReadProjectData() {
local year=$(date +'%Y')
local author=$USER
echo "Creating a new bash project"
echo "Project name:"
echo "Author name ($author):"
echo "Year ($year):"
echo "Is it MIT License (Y/n)?"
# echo "Create sample data" # not sure if we should really ask that
# TODO read the data into global variables
}
new() {
new_ReadProjectData
new_InitProject
replaceFileNames
replaceVariablesInFiles
echo "Project created successfully"
}
parseArgs_compile() {
if [ -z $1 ]; then
echo "Please give a directory to the bash script to compile"
help
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
help
else
PRJ_DIR=$1
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
fi
}
replaceFileNames() {
echo "Not yet implemented"
# TODO implement replacing the file names beginning with BASH_SCRIPT_COMPILER_SCRIPT_NAME
}
replaceVariablesInFiles() {
echo "Not yet implemented"
# TODO implement replacing the file content variables BASH_SCRIPT_COMPILER_*
}
main "$@"