130 lines
3.6 KiB
Bash
130 lines
3.6 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
tabs 4
|
|
|
|
# exit on errors
|
|
set -eo pipefail
|
|
|
|
INSTALLED_CBUILD_VERSION=2.0.1
|
|
|
|
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/$(echo $script_path | sed 's,^cbuild/,,')"
|
|
fi
|
|
# echp "including script $script_path"
|
|
. "$script_path"
|
|
}
|
|
|
|
include "cbuild/myprint.sh"
|
|
include "cbuild/functions.sh"
|
|
include "cbuild/config.sh"
|
|
|
|
function print_help {
|
|
myprint "cbuild v$INSTALLED_CBUILD_VERSION"
|
|
myprint "C/C++ project build system written in bash."
|
|
myprint "Usage: cbuild [OPTIONS] [TASK]"
|
|
myprint " -h, --help Show this message"
|
|
myprint " -v, --version Shows version"
|
|
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)"
|
|
}
|
|
|
|
current_config_path="./current.config"
|
|
default_config_path="./default.config"
|
|
args=($@)
|
|
args_count=${#args[@]}
|
|
selected_tasks_array=()
|
|
i=0
|
|
if [ $args_count -eq 0 ]; then
|
|
print_help
|
|
fi
|
|
|
|
function get_next_arg {
|
|
i=$((i+1))
|
|
safeprint "${args[i]}"
|
|
}
|
|
|
|
while [ $i -lt $args_count ]
|
|
do
|
|
case "${args[i]}" in
|
|
'-h' | '--help')
|
|
print_help
|
|
;;
|
|
'-v' | '--version')
|
|
myprint $INSTALLED_CBUILD_VERSION
|
|
;;
|
|
'-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"
|
|
myprint "Enter project name: "
|
|
read -r project_name
|
|
sed -i "s,\%PROJECT_NAME\%,$project_name,g" "default.config"
|
|
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}" "─" "$PROJECT/$task"
|
|
load_config "$current_config_path" "$default_config_path" "$task" true
|
|
|
|
if [ ! -z "$PRE_TASK_SCRIPT" ]; then
|
|
myprint "${BLUE}executing ${WHITE}$TASK_SCRIPT"
|
|
include "$PRE_TASK_SCRIPT"
|
|
fi
|
|
|
|
myprint "${BLUE}executing ${WHITE}$TASK_SCRIPT"
|
|
include "$TASK_SCRIPT"
|
|
|
|
if [ ! -z "$POST_TASK_SCRIPT" ]; then
|
|
myprint "${BLUE}executing ${WHITE}$POST_TASK_SCRIPT"
|
|
include "$POST_TASK_SCRIPT"
|
|
fi
|
|
}
|
|
|
|
function call_tasks {
|
|
local tasks="$@"
|
|
load_config "$current_config_path" "$default_config_path" "" false
|
|
print_header "${WHITE}" "═" "$PROJECT"
|
|
for task in $tasks ; do
|
|
call_task "$current_config_path" "$default_config_path" "$task"
|
|
done
|
|
}
|
|
|
|
print_hline "${WHITE}" "═"
|
|
selected_tasks_count=${#selected_tasks_array[@]}
|
|
if [ $selected_tasks_count -gt 0 ]; then
|
|
call_tasks "${selected_tasks_array[@]}"
|
|
fi
|