v0.3 changes
This commit is contained in:
parent
5f802c9273
commit
51314163ea
12
README.md
12
README.md
@ -46,6 +46,14 @@ script, so all of the functions defined in modules under the `lib` directory wil
|
|||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
|
__0.3__
|
||||||
|
|
||||||
|
* Added support for variable replacement
|
||||||
|
* Small code refactorings
|
||||||
|
* Added a VARIABLES file that is read for the variables
|
||||||
|
* Added run flag to the compiled script
|
||||||
|
* Updated the init files
|
||||||
|
|
||||||
__0.2__
|
__0.2__
|
||||||
|
|
||||||
* Added support for deploying (copying) to a (configurable) path
|
* Added support for deploying (copying) to a (configurable) path
|
||||||
@ -56,6 +64,10 @@ __0.1__
|
|||||||
* Created the initial script
|
* Created the initial script
|
||||||
* Split it up so it will compile itself (this therefor serves as example project)
|
* Split it up so it will compile itself (this therefor serves as example project)
|
||||||
|
|
||||||
|
## Open issues
|
||||||
|
|
||||||
|
* Implement creating a new bash script project
|
||||||
|
|
||||||
## Sources
|
## Sources
|
||||||
|
|
||||||
* [Initial project we forked](https://github.com/zinovyev/bash-project)
|
* [Initial project we forked](https://github.com/zinovyev/bash-project)
|
||||||
|
|||||||
@ -8,17 +8,15 @@
|
|||||||
# Maintainer: Manuel Manhart
|
# Maintainer: Manuel Manhart
|
||||||
# Company: Manuel Manhart IT e.U.
|
# Company: Manuel Manhart IT e.U.
|
||||||
# License: MIT
|
# License: MIT
|
||||||
# Created by bash-script-compiler version BASH_SCRIPT_COMPILER_VERSION
|
# Created by bash-script-compiler version 0.3
|
||||||
|
|
||||||
# set default constants / variables
|
# set default constants / variables
|
||||||
VERSION="0.2"
|
VERSION="0.3"
|
||||||
|
|
||||||
# TODO implement variable replacement during compilation and use this here instead of the hardcoded value:
|
# 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_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"}
|
|
||||||
|
|
||||||
TARGET_DIR_NAME="dist"
|
TARGET_DIR_NAME="dist"
|
||||||
SRC_DIR_NAME="src"
|
SRC_DIR_NAME="src"
|
||||||
@ -48,10 +46,6 @@ compile_addFunctions() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
compile_copyCompletionFile() {
|
|
||||||
cp *-completion ../${TARGET_DIR_NAME}
|
|
||||||
}
|
|
||||||
|
|
||||||
compile_defineMainFunction() {
|
compile_defineMainFunction() {
|
||||||
touch ${TARGET_FILE}
|
touch ${TARGET_FILE}
|
||||||
if [ ! -f "${INTRO_FILE_NAME}" ]; then
|
if [ ! -f "${INTRO_FILE_NAME}" ]; then
|
||||||
@ -67,7 +61,11 @@ compile_defineMainFunction() {
|
|||||||
else
|
else
|
||||||
echo -e "function main() {" >> ${TARGET_FILE}
|
echo -e "function main() {" >> ${TARGET_FILE}
|
||||||
cat "${MAIN_FUNC_FILE_NAME}" | sed -e 's/^/ /g' >> ${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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,24 +73,97 @@ compile_invokeMainFunction() {
|
|||||||
echo "main \"\$@\"" >> ${TARGET_FILE}
|
echo "main \"\$@\"" >> ${TARGET_FILE}
|
||||||
}
|
}
|
||||||
|
|
||||||
compile_setTargetFile() {
|
function compile_otherFileProcessing() {
|
||||||
|
cp *-completion ${TARGET_PATH}
|
||||||
|
chmod +x ${TARGET_FILE}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
function compile_replaceVariables() {
|
||||||
|
local directory="$TARGET_PATH"
|
||||||
local bashScriptName=$(basename "$(pwd)")
|
local bashScriptName=$(basename "$(pwd)")
|
||||||
if [ ! -d "${TARGET_DIR_NAME}" ]; then
|
|
||||||
echo "Creating output dir '${TARGET_DIR_NAME}'"
|
# read VARIABLES
|
||||||
mkdir -p ${TARGET_DIR_NAME}
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
compile_setBuildTarget() {
|
||||||
|
TARGET_PATH=../${TARGET_DIR_NAME}
|
||||||
|
if [ ! -d "${TARGET_PATH}" ]; then
|
||||||
|
echo "Creating output dir '${TARGET_PATH}'"
|
||||||
|
mkdir -p ${TARGET_PATH}
|
||||||
fi
|
fi
|
||||||
# we need to add ../ since we will be cd-ing into the src dir inside the PRJ_DIR
|
# 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_PATH}/${BASH_SCRIPT_COMPILER_SCRIPT_NAME}
|
||||||
}
|
}
|
||||||
|
|
||||||
compile() {
|
compile() {
|
||||||
pushd $PRJ_DIR >/dev/null 2>&1
|
pushd $PRJ_DIR >/dev/null 2>&1
|
||||||
compile_setTargetFile
|
|
||||||
cd $SRC_DIR_NAME
|
cd $SRC_DIR_NAME
|
||||||
|
compile_readVariables
|
||||||
|
compile_setBuildTarget
|
||||||
compile_defineMainFunction
|
compile_defineMainFunction
|
||||||
compile_addFunctions
|
compile_addFunctions
|
||||||
compile_invokeMainFunction
|
compile_invokeMainFunction
|
||||||
compile_copyCompletionFile
|
compile_otherFileProcessing
|
||||||
|
compile_replaceVariables
|
||||||
echo "Compiled successfully"
|
echo "Compiled successfully"
|
||||||
popd >/dev/null 2>&1
|
popd >/dev/null 2>&1
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,12 @@
|
|||||||
# gt bash script 0.1
|
# _BASH_SCRIPT_SCRIPT_NAME_ 0.1
|
||||||
|
|
||||||
A helper for git functions I really miss, like merging the lastest develop / main branch into my current branch, showing what I (or someone else) has checked in over the time, etc.
|
TODO: Your description here
|
||||||
|
|
||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
__0.1__
|
__0.1__
|
||||||
|
|
||||||
* Created the initial bash script
|
* Created the initial bash script
|
||||||
* Added config functions (for future use)
|
|
||||||
* Added commands pull, tag, mine, pullAndMerge
|
|
||||||
|
|
||||||
## Sources
|
## Sources
|
||||||
|
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
0.1
|
|
||||||
4
init/src/VARIABLES
Normal file
4
init/src/VARIABLES
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# sets the version of the script
|
||||||
|
BASH_SCRIPT_COMPILER_VERSION=0.1
|
||||||
|
# optionally set the name of the compiled script (otherwise the directory name will be taken)
|
||||||
|
#BASH_SCRIPT_COMPILER_SCRIPT_NAME=bash-script-compiler
|
||||||
@ -1,28 +1,15 @@
|
|||||||
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 _BASH_SCRIPT_COMPILER_VERSION_ 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
|
# TODO: Add your config values here like CONFIG_VAR_KEY="config var value"
|
||||||
LIFERAY_DOCKER=(
|
|
||||||
"TODO" # change this to the real path
|
|
||||||
)
|
|
||||||
END
|
END
|
||||||
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
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
|
||||||
|
|||||||
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
3
init/src/functions/demo.sh
Normal file
3
init/src/functions/demo.sh
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
demo() {
|
||||||
|
echo "$DEMO_GREETING"
|
||||||
|
}
|
||||||
@ -1,7 +0,0 @@
|
|||||||
gitPull() {
|
|
||||||
if [ -f ".git" ]; then
|
|
||||||
git pull
|
|
||||||
else
|
|
||||||
echo "Seems not to be a git repo"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
@ -1,17 +0,0 @@
|
|||||||
gitTag() {
|
|
||||||
if [ -n "$2" ]; then
|
|
||||||
git pull >/dev/null
|
|
||||||
local message=""
|
|
||||||
if [ -n "$3" ]; then
|
|
||||||
message="-m $3"
|
|
||||||
fi
|
|
||||||
git tag -a $2 $message
|
|
||||||
git push --tags
|
|
||||||
else
|
|
||||||
git fetch origin >/dev/null
|
|
||||||
echo "Existing tags"
|
|
||||||
git tag
|
|
||||||
echo ""
|
|
||||||
echo "If you want to create a new tag, just provide a tag version"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
@ -12,11 +12,7 @@ The options are:
|
|||||||
config init ... for creating a config file (if none exists yet - automatically checked)
|
config init ... for creating a config file (if none exists yet - automatically checked)
|
||||||
config print ... for printing the config file content
|
config print ... for printing the config file content
|
||||||
|
|
||||||
mine [USER] ... fetches the log of either the given user or the one defined in git config
|
demo (STRING) ... echoes a greeting or the given STRING
|
||||||
pullAndMerge BRANCH ... pulls the given branch and merges it into the current one (updating to latest develop / main)
|
|
||||||
pull ... for pulling from git (not working?)
|
|
||||||
tag NAME [MESSAGE] ... creates and pushes a new tag
|
|
||||||
branch NAME ... creates a new branch (TODO)
|
|
||||||
|
|
||||||
help ... show this page
|
help ... show this page
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
mine() {
|
|
||||||
# fetch user from args
|
|
||||||
local gitUser=$GIT_USER
|
|
||||||
# Get the user defined in the local config
|
|
||||||
if [ -z "$gitUser" ]; then
|
|
||||||
gitUser=$(git config --get user.name)
|
|
||||||
echo "Fetching user from git config: $gitUser"
|
|
||||||
fi
|
|
||||||
echo "Git user: $gitUser"
|
|
||||||
|
|
||||||
# get my commits
|
|
||||||
git log --author="$gitUser"
|
|
||||||
}
|
|
||||||
6
init/src/functions/parseArgs_demo.sh
Normal file
6
init/src/functions/parseArgs_demo.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
parseArgs_demo() {
|
||||||
|
DEMO_GREETING="Welcome to our demo, $USER"
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
DEMO_GREETING="$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
@ -1,6 +0,0 @@
|
|||||||
parseArgs_mine() {
|
|
||||||
echo "Fetching user: $1"
|
|
||||||
if [ -n "$1" ]; then
|
|
||||||
GIT_USER=$1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
parseArgs_pullAndMerge() {
|
|
||||||
echo "Pulling and merging branch $1 into current one"
|
|
||||||
if [ -z "$1" ]; then
|
|
||||||
echo "ERROR: Argument BRANCH is missing"
|
|
||||||
help pullAndMerge
|
|
||||||
else
|
|
||||||
GIT_BRANCH=$1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
@ -1,19 +0,0 @@
|
|||||||
preset() {
|
|
||||||
# not yet working
|
|
||||||
# FIXME properties file not correctly read
|
|
||||||
# FIXME how do we set multiple values in a simple properties file
|
|
||||||
# TODO think about a better mechanism
|
|
||||||
utilIsReadPropertiesFile ./.dev-presets presetOptions presetValues
|
|
||||||
printf "presetOptions %s\n" "${presetOptions[@]}"
|
|
||||||
printf "presetValues %s\n" "${presetValues[@]}"
|
|
||||||
echo 'Please enter your choice: '
|
|
||||||
local index=0
|
|
||||||
for item in "${presetOptions[@]}"
|
|
||||||
do
|
|
||||||
echo ${index}. ${item}
|
|
||||||
index="$((index + 1))"
|
|
||||||
done
|
|
||||||
read n
|
|
||||||
echo "opt: $n"
|
|
||||||
echo "presetValue: ${presetValues[$n]}"
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
pullAndMerge() {
|
|
||||||
# fetch user from args
|
|
||||||
local gitBranch=$GIT_BRANCH
|
|
||||||
local currentBranch=$(git rev-parse --abbrev-ref HEAD)
|
|
||||||
echo "current branch: $currentBranch"
|
|
||||||
|
|
||||||
# Update the current branch to the latest of the given branch
|
|
||||||
git checkout $gitBranch
|
|
||||||
git pull
|
|
||||||
git checkout $currentBranch
|
|
||||||
echo DEBUG: git merge $gitBranch
|
|
||||||
}
|
|
||||||
@ -1,34 +0,0 @@
|
|||||||
runScript() {
|
|
||||||
# go to source path (if neccessary)
|
|
||||||
pushd $SOURCE_PATH >/dev/null 2>&1
|
|
||||||
local runFunctions=${@// /|}
|
|
||||||
#echo "DEBUG - runFunctions: $runFunctions"
|
|
||||||
|
|
||||||
# in the execution there are in- and dependent tasks, this file ensures that everything
|
|
||||||
# will be executed in a helpful way
|
|
||||||
|
|
||||||
if [[ "$runFunctions" = *"help"* ]]; then
|
|
||||||
help
|
|
||||||
fi
|
|
||||||
# run config functions
|
|
||||||
if [[ "$runFunctions" = *"config"* ]]; then
|
|
||||||
if [[ "$runFunctions" = *"config init"* ]]; then
|
|
||||||
configInit
|
|
||||||
elif [[ "$runFunctions" = *"config print"* ]]; then
|
|
||||||
configPrint
|
|
||||||
exit
|
|
||||||
elif [[ "$runFunctions" = *"config set"* ]]; then
|
|
||||||
echo "Setting configurations is not yet supported"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
local parseArgsFunc="parseArgs_$ACTION"
|
|
||||||
local isParseArgsDefined=$(type -t $parseArgsFunc)
|
|
||||||
if [ "function" == "$isParseArgsDefined" ]; then
|
|
||||||
"$parseArgsFunc" "${@:2}"
|
|
||||||
fi
|
|
||||||
"$ACTION" "${@:2}"
|
|
||||||
|
|
||||||
# return to initial path (if neccessary)
|
|
||||||
popd >/dev/null 2>&1
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
utilIsExistingFile() {
|
|
||||||
if ls $1 1> /dev/null 2>&1; then
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
@ -1,21 +0,0 @@
|
|||||||
utilIsReadPropertiesFile() {
|
|
||||||
local fileName=$1
|
|
||||||
local keysName=$2
|
|
||||||
local valuesName=$3
|
|
||||||
local keys=()
|
|
||||||
local values=()
|
|
||||||
echo "reading properties file $1 into $2 and $3"
|
|
||||||
if [ -n "$fileName" ]; then
|
|
||||||
while IFS='=' read -r key value
|
|
||||||
do
|
|
||||||
key=$(echo $key | tr '.' '_')
|
|
||||||
keys+=("$key")
|
|
||||||
values+=("$value")
|
|
||||||
# eval ${key}=\${value}
|
|
||||||
done < "$fileName"
|
|
||||||
fi
|
|
||||||
echo "keys: $keys"
|
|
||||||
echo "values: $values"
|
|
||||||
eval $keysName="'$keys'"
|
|
||||||
eval $valuesName="'$values'"
|
|
||||||
}
|
|
||||||
@ -11,10 +11,9 @@
|
|||||||
# 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
|
||||||
VERSION="0.1"
|
VERSION="_BASH_SCRIPT_COMPILER_VERSION_"
|
||||||
|
|
||||||
# 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
|
||||||
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"}
|
|
||||||
|
|||||||
@ -1,12 +1,20 @@
|
|||||||
|
|
||||||
# main
|
# main
|
||||||
# import colors
|
# if there is a import colors script source it
|
||||||
if [ -f ${BASH_SOURCE%/*}/define-colors ]; then
|
if [ -f ${BASH_SOURCE%/*}/define-colors ]; then
|
||||||
. ${BASH_SOURCE%/*}/define-colors ""
|
. ${BASH_SOURCE%/*}/define-colors ""
|
||||||
fi
|
fi
|
||||||
# read configuration (if any)
|
# read configuration (if any)
|
||||||
configRead
|
configRead
|
||||||
|
# set the action to the first parameter (if your script runs without, just remove these lines)
|
||||||
|
local action=${1:-"help"}
|
||||||
|
local parseArgsFunc="${PARSE_ARGS_PREFIX}${action}"
|
||||||
|
# if there is a parseArgs_ARG1, call it
|
||||||
|
local isParseArgsDefined=$(type -t $parseArgsFunc)
|
||||||
|
if [ "function" == "$isParseArgsDefined" ]; then
|
||||||
|
"$parseArgsFunc" "${@:2}"
|
||||||
|
fi
|
||||||
# parse the arguments
|
# parse the arguments
|
||||||
parseArgs "$@"
|
parseArgs "$@"
|
||||||
# run the script
|
configRead
|
||||||
runScript "$@"
|
# run the script (by default calls the function given in the first arg)
|
||||||
|
"$action" "${@:2}"
|
||||||
4
src/VARIABLES
Normal file
4
src/VARIABLES
Normal 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
|
local cur prev opts tag help
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
@ -9,7 +10,7 @@ _bash-script-compiler-completion()
|
|||||||
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%/*}/bash-script-compiler-help
|
HELP_PATH=${BASH_SOURCE%/*}/_BASH_SCRIPT_COMPILER_SCRIPT_NAME_-help
|
||||||
help=${opts}
|
help=${opts}
|
||||||
#help=`ls $HELP_PATH`
|
#help=`ls $HELP_PATH`
|
||||||
|
|
||||||
@ -46,4 +47,4 @@ createFromDirectory() {
|
|||||||
popd >/dev/null
|
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_
|
||||||
@ -1,11 +1,13 @@
|
|||||||
compile() {
|
compile() {
|
||||||
pushd $PRJ_DIR >/dev/null 2>&1
|
pushd $PRJ_DIR >/dev/null 2>&1
|
||||||
compile_setTargetFile
|
|
||||||
cd $SRC_DIR_NAME
|
cd $SRC_DIR_NAME
|
||||||
|
compile_readVariables
|
||||||
|
compile_setBuildTarget
|
||||||
compile_defineMainFunction
|
compile_defineMainFunction
|
||||||
compile_addFunctions
|
compile_addFunctions
|
||||||
compile_invokeMainFunction
|
compile_invokeMainFunction
|
||||||
compile_copyCompletionFile
|
compile_otherFileProcessing
|
||||||
|
compile_replaceVariables
|
||||||
echo "Compiled successfully"
|
echo "Compiled successfully"
|
||||||
popd >/dev/null 2>&1
|
popd >/dev/null 2>&1
|
||||||
}
|
}
|
||||||
@ -1,3 +0,0 @@
|
|||||||
compile_copyCompletionFile() {
|
|
||||||
cp *-completion ../${TARGET_DIR_NAME}
|
|
||||||
}
|
|
||||||
@ -13,6 +13,10 @@ compile_defineMainFunction() {
|
|||||||
else
|
else
|
||||||
echo -e "function main() {" >> ${TARGET_FILE}
|
echo -e "function main() {" >> ${TARGET_FILE}
|
||||||
cat "${MAIN_FUNC_FILE_NAME}" | sed -e 's/^/ /g' >> ${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
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
4
src/functions/compile_otherFileProcessing.sh
Normal file
4
src/functions/compile_otherFileProcessing.sh
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
function compile_otherFileProcessing() {
|
||||||
|
cp *-completion ${TARGET_PATH}
|
||||||
|
chmod +x ${TARGET_FILE}
|
||||||
|
}
|
||||||
15
src/functions/compile_readVariables.sh
Normal file
15
src/functions/compile_readVariables.sh
Normal 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
src/functions/compile_replaceVariables.sh
Normal file
49
src/functions/compile_replaceVariables.sh
Normal 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
src/functions/compile_setBuildTarget.sh
Normal file
9
src/functions/compile_setBuildTarget.sh
Normal 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}
|
||||||
|
}
|
||||||
@ -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
|
|
||||||
}
|
|
||||||
12
src/intro.sh
12
src/intro.sh
@ -8,17 +8,15 @@
|
|||||||
# Maintainer: Manuel Manhart
|
# Maintainer: Manuel Manhart
|
||||||
# Company: Manuel Manhart IT e.U.
|
# Company: Manuel Manhart IT e.U.
|
||||||
# License: MIT
|
# 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
|
# 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:
|
# 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_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"}
|
|
||||||
|
|
||||||
TARGET_DIR_NAME="dist"
|
TARGET_DIR_NAME="dist"
|
||||||
SRC_DIR_NAME="src"
|
SRC_DIR_NAME="src"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user