cbuild/cbuild.sh
2024-07-12 01:55:58 +03:00

135 lines
3.5 KiB
Bash

#!/usr/bin/env bash
tabs 4
# exit on errors
set -eo pipefail
INSTALLED_CBUILD_VERSION=2.0.0
if [ -z "$CBUILD_INSTALL_DIR" ]; then
CBUILD_INSTALL_DIR="/usr/local/share/cbuild"
fi
function include {
local script_path="$1"
if [[ "$script_path" == cbuild/* ]]; then
script_path="$CBUILD_INSTALL_DIR/$(safeprint $script_path | sed 's,^cbuild/,,')"
fi
# echp "including script $script_path"
. "$script_path"
}
function include_if_not_null {
if [ ! -z "$1" ]; then
include "$1"
fi
}
include "cbuild/myprint.sh"
include "cbuild/functions.sh"
include "cbuild/config.sh"
function print_help_and_exit {
myprint "Usage: cbuild [OPTIONS] [TASK]"
myprint " -h, --help Show this message"
myprint " -n, --new-project [PROJ_DIR] Initialize new cbuild project directory (default=./)"
myprint " -c, --current-config FILE Set current config file path (default=./current.config)"
myprint " -d, --default-config FILE Set default config file path (default=./current.config)"
exit 0
}
current_config_path="./current.config"
default_config_path="./default.config"
args=($@)
args_len=${#args[@]}
selected_tasks_array=()
i=0
if [ $args_len -eq 0 ]; then
print_help_and_exit
fi
function get_next_arg {
i=$((i+1))
safeprint "${args[i]}"
}
while [ $i -lt $args_len ]
do
case "${args[i]}" in
'-h' | '--help')
print_help_and_exit
;;
'-c' | '--current-config')
i=$((i+1))
current_config_path="${args[i]}"
myprint "<$current_config_path>"
exit
;;
'-d' | '--default-config')
default_config_path="$(get_next_arg)"
;;
'-n' | '--new-project')
new_project_dir="$(get_next_arg)"
if [ -z "$new_project_dir" ]; then
new_project_dir="./"
fi
if ask_yn "create default.config?"; then
cp "$CBUILD_INSTALL_DIR/default.config" "$new_project_dir"
project_name="$(ask 'Enter project name')"
sed -i "s,\%PROJECT_NAME\%,$project_name,g"
fi
if ask_yn "Copy default .gitignore?"; then
cp "$CBUILD_INSTALL_DIR/.gitignore" "$new_project_dir"
fi
;;
*)
selected_tasks_array+=(${args[i]})
;;
esac
i=$((i+1))
done
function call_task {
local current_config_path="$1"
local default_config_path="$2"
local task="$3"
print_header "${CYAN}" "─" "$task"
load_config "$task" "$current_config_path" "$default_config_path"
resolve_dependencies "$DEPS_BASEDIR" "$DEPS"
myprint "${BLUE}executing $PRE_TASK_SCRIPT"
# include_if_not_null "$PRE_TASK_SCRIPT"
myprint "${BLUE}executing $TASK_SCRIPT"
# include "$TASK_SCRIPT"
myprint "${BLUE}executing $POST_TASK_SCRIPT"
# include_if_not_null "$POST_TASK_SCRIPT"
myprint "error"
}
function call_tasks {
local tasks="$@"
myprint "$tasks"
print_header "${WHITE}" "═" "$PROJECT"
for task in $tasks ; do
call_task "$task"
done
print_hline "${WHITE}" "═"
}
time call_tasks "${selected_tasks_array[@]}" #2>&1 | tee cbuild.log
# remove terminal escape codes
sed -e 's/[^[:blank:][:print:]]//g' \
-e 's/\[0;[0-9][0-9]m//g' \
-e 's/\[0;[0-9]m//g' \
-e 's/\[[0-9][0-9]m//g' \
-e 's/\[[0-9]m//g' \
-e 's/ H //g' \
-e 's/\[3gH //g' \
-i cbuild.log