v0.3 changes

This commit is contained in:
manuel.manhart committed 2024-03-29 13:01:40 +01:00
1 parent 5f802c9273
commit 51314163ea
34 files changed
+246 -244

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
# sets the version of the script
BASH_SCRIPT_COMPILER_VERSION=0.3
# optionally set the name of the compiled script (otherwise the directory name will be taken)
#BASH_SCRIPT_COMPILER_SCRIPT_NAME=bash-script-compiler
@@ -1,4 +1,5 @@
_bash-script-compiler-completion()
# version _BASH_SCRIPT_COMPILER_VERSION_
__BASH_SCRIPT_COMPILER_SCRIPT_NAME_-completion()
{
local cur prev opts tag help
COMPREPLY=()
@@ -9,7 +10,7 @@ _bash-script-compiler-completion()
config="print set init"
# TODO check if this is still the most efficient way to list help options
HELP_PATH=${BASH_SOURCE%/*}/bash-script-compiler-help
HELP_PATH=${BASH_SOURCE%/*}/_BASH_SCRIPT_COMPILER_SCRIPT_NAME_-help
help=${opts}
#help=`ls $HELP_PATH`
@@ -46,4 +47,4 @@ createFromDirectory() {
popd >/dev/null
}
complete -F _bash-script-compiler-completion bash-script-compiler
complete -F __BASH_SCRIPT_COMPILER_SCRIPT_NAME_-completion _BASH_SCRIPT_COMPILER_SCRIPT_NAME_
+4 -2
View File
@@ -1,11 +1,13 @@
compile() {
pushd $PRJ_DIR >/dev/null 2>&1
compile_setTargetFile
cd $SRC_DIR_NAME
compile_readVariables
compile_setBuildTarget
compile_defineMainFunction
compile_addFunctions
compile_invokeMainFunction
compile_copyCompletionFile
compile_otherFileProcessing
compile_replaceVariables
echo "Compiled successfully"
popd >/dev/null 2>&1
}
@@ -1,3 +0,0 @@
compile_copyCompletionFile() {
cp *-completion ../${TARGET_DIR_NAME}
}
+5 -1
View File
@@ -13,6 +13,10 @@ compile_defineMainFunction() {
else
echo -e "function main() {" >> ${TARGET_FILE}
cat "${MAIN_FUNC_FILE_NAME}" | sed -e 's/^/ /g' >> ${TARGET_FILE}
echo -e "\n}\n" >> ${TARGET_FILE}
cat >>${TARGET_FILE} <<-END
}
END
fi
}
@@ -0,0 +1,4 @@
function compile_otherFileProcessing() {
cp *-completion ${TARGET_PATH}
chmod +x ${TARGET_FILE}
}
+15
View File
@@ -0,0 +1,15 @@
function compile_readVariables() {
local directory="$TARGET_PATH"
# read VARIABLES
if [ -f ./VARIABLES ]; then
source VARIABLES
#echo "DEBUG: Found variables file, setting: `cat VARIABLES`"
fi
if [ -n $BASH_SCRIPT_COMPILER_SCRIPT_NAME ]; then
pushd .. >/dev/null 2>&1
BASH_SCRIPT_COMPILER_SCRIPT_NAME=$(basename "$(pwd)")
popd >/dev/null 2>&1
echo "BASH_SCRIPT_COMPILER_SCRIPT_NAME was not set, setting to: $BASH_SCRIPT_COMPILER_SCRIPT_NAME"
fi
}
+49
View File
@@ -0,0 +1,49 @@
function compile_replaceVariables() {
local directory="$TARGET_PATH"
local bashScriptName=$(basename "$(pwd)")
# read VARIABLES
if [ -f ./VARIABLES ]; then
source VARIABLES
cat VARIABLES
fi
# Loop through each file in the directory
for file in "$directory"/*; do
if [ -f "$file" ]; then
#echo "DEBUG: replacing variables for $file"
# Replace pattern in file content
local content=$(cat $file)
local replacement=$(compile_replaceVariablesIntern "$content")
# Replace the file content
#echo "DEBUG: file content: $replacement"
if [ "$content" != "$replacement" ]; then
echo -e "$replacement" >$file
fi
# Extract the filename without the directory path
local filename=$(basename "$file")
local replacement=$(compile_replaceVariablesIntern "$filename")
# Rename the file if the new filename is different
if [ "$filename" != "$replacement" ]; then
mv "$file" "$directory/$replacement"
fi
fi
done
}
# does the real replacement of content (first argument) and echoes it
# call it like this: $replacement=$(compile_replaceVariablesIntern $CONTENT_WHERE_YOU_WANT_TO_REPLACE_VARIABLES)
# Be aware that everything that is echoed in here will be in the replacement as well
function compile_replaceVariablesIntern() {
local content=$1
while [[ "$content" =~ _BASH_SCRIPT_COMPILER_[A-Z_]+_ ]]; do
#echo "DEBUG: found following variable to replace in content: ${BASH_REMATCH[0]}"
local matchedPattern="${BASH_REMATCH[0]}"
local varName=$(echo "$matchedPattern" | sed 's/^_//; s/_$//')
local replacementValue="${!varName}"
#echo "DEBUG: replacing $matchedPattern with $varName = $replacementValue in content"
content=$(echo "$content" | sed "s/${matchedPattern}/${replacementValue}/")
done
echo -e "$content"
}
+9
View File
@@ -0,0 +1,9 @@
compile_setBuildTarget() {
TARGET_PATH=../${TARGET_DIR_NAME}
if [ ! -d "${TARGET_PATH}" ]; then
echo "Creating output dir '${TARGET_PATH}'"
mkdir -p ${TARGET_PATH}
fi
# we need to add ../ since we will be cd-ing into the src dir inside the PRJ_DIR
TARGET_FILE=${TARGET_PATH}/${BASH_SCRIPT_COMPILER_SCRIPT_NAME}
}
-9
View File
@@ -1,9 +0,0 @@
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
}
+5 -7
View File
@@ -8,17 +8,15 @@
# Maintainer: Manuel Manhart
# Company: Manuel Manhart IT e.U.
# License: MIT
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
# Created by bash-script-compiler version _BASH_SCRIPT_COMPILER_VERSION_
# set default constants / variables
VERSION="0.2"
VERSION="_BASH_SCRIPT_COMPILER_VERSION_"
# 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"}
CONFIG_FILE_NAME="_BASH_SCRIPT_COMPILER_SCRIPT_NAME_.config"
CONFIG_PATH="${HOME}/.config/mmit"
CONFIG_FILE="${CONFIG_PATH}/${CONFIG_FILE_NAME}"
TARGET_DIR_NAME="dist"
SRC_DIR_NAME="src"