cbuild/cbuild.sh

177 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
INSTALLED_CBUILD_VERSION=2.3.0
# set \t size to 4 spaces
tabs 4
# exit on errors
set -eo pipefail
function version_parse {
local value="$1"
var_name="$2"
IFS_backup="$IFS"
IFS='.'
local v_array=($value)
IFS="$IFS_backup"
eval ${var_name}_version_major=${v_array[0]}
eval ${var_name}_version_minor=${v_array[1]}
eval ${var_name}_version_patch=${v_array[2]}
}
version_parse $INSTALLED_CBUILD_VERSION installed
if [ -z "$CBUILD_INSTALL_DIR" ]; then
CBUILD_INSTALL_DIR="$HOME/.local/share/cbuild/${installed_version_major}.${installed_version_minor}"
if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then
CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${installed_version_major}.${installed_version_minor}"
if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then
echo "CBUILD_INSTALL_DIR '$CBUILD_INSTALL_DIR' doesn't exist"
exit 1
fi
fi
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/include/myprint.sh"
include "@cbuild/include/functions.sh"
include "@cbuild/include/config.sh"
function print_help {
myprint "${GRAY}cbuild v$INSTALLED_CBUILD_VERSION"
myprint "C/C++ project build system written in bash."
myprint "Usage: cbuild [OPTIONS] [TASKS]"
myprint "Options:"
myprint " -h, --help Show this message"
myprint " -v, --version Shows version"
myprint " -c, --config FILE Set project config file path (default=./project.config)"
myprint " -n, --new-project [DIR] Initialize new cbuild project directory (default=./)"
}
# parse command line arguments
project_config_path="./project.config"
args=($@)
args_count=${#args[@]}
selected_tasks_array=()
i=0
if [ $args_count -eq 0 ]; then
print_help
exit 1
fi
while [ $i -lt $args_count ]
do
case "${args[i]}" in
'-h' | '--help')
print_help
exit 0
;;
'-v' | '--version')
myprint "${GRAY}cbuild v$INSTALLED_CBUILD_VERSION"
exit 0
;;
'-c' | '--config')
i=$((i+1))
project_config_path="${args[i]}"
;;
'-n' | '--new-project')
i=$((i+1))
new_project_dir="${args[i]}"
if [ -z "$new_project_dir" ]; then
new_project_dir="."
else
mkdir -p "$new_project_dir"
fi
# create project config
project_config_path="$new_project_dir/project.config"
cp "$CBUILD_INSTALL_DIR/project.config.default" "$project_config_path.temp"
myprint "${WHITE}Enter project name: "
read -r project_name
sed "s,\%PROJECT_NAME\%,$project_name,g" \
"$project_config_path.temp" > "$project_config_path"
rm "$project_config_path.temp"
myprint "${GREEN}Created '$project_config_path'"
# create project user default config
project_user_config_path="$new_project_dir/project.config.user.default"
cp "$CBUILD_INSTALL_DIR/project.config.user.default" "$project_user_config_path"
myprint "${GREEN}Created '$project_user_config_path'"
if ask_yn "${WHITE}Copy default .gitignore?"; then
cp -v "$CBUILD_INSTALL_DIR/.gitignore" "$new_project_dir/"
fi
if ask_yn "${WHITE}Copy default .vscode launch tasks?"; then
new_project_vscode_dir="$new_project_dir/.vscode"
mkdir -pv "$new_project_vscode_dir"
for vscode_dir_f in $(find "$CBUILD_INSTALL_DIR/default_vscode/" -type f); do
cp -vr "$vscode_dir_f" "$new_project_vscode_dir/"
done
sed "s,\%PROJECT_NAME\%,$project_name,g" \
"$new_project_vscode_dir/launch.json" > "$new_project_vscode_dir/launch.json.temp"
mv "$new_project_vscode_dir/launch.json.temp" "$new_project_vscode_dir/launch.json"
fi
exit 0
;;
*)
selected_tasks_array+=(${args[i]})
;;
esac
i=$((i+1))
done
function call_task {
local project_config_path="$1"
local task="$2"
TASK_ARGS="$3"
print_header "${CYAN}" "─" "$PROJECT/$task"
load_config "$project_config_path" "$task" true
if [ ! -z "$PRE_TASK_SCRIPT" ]; then
myprint "${BLUE}executing ${WHITE}'$PRE_TASK_SCRIPT'"
include "$PRE_TASK_SCRIPT"
fi
if [ ! -z "$TASK_SCRIPT" ]; then
myprint "${BLUE}executing ${WHITE}'$TASK_SCRIPT'"
include "$TASK_SCRIPT"
fi
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 "$project_config_path" "" false
print_header "${WHITE}" "═" "$PROJECT"
project_dir="$(pwd)"
for task_str in $tasks ; do
task=$(safeprint "$task_str" | sed 's/=.*//g')
local args_str=$(safeprint "$task_str" | grep -oP '(?<=\=).*' || echo "")
IFS_backup="$IFS"
IFS=',;'
args_array=($args_str)
IFS="$IFS_backup"
call_task "$project_config_path" "$task" "${args_array[@]}"
cd "$project_dir"
done
}
print_hline "${WHITE}" "═"
selected_tasks_count=${#selected_tasks_array[@]}
if [ $selected_tasks_count -gt 0 ]; then
call_tasks "${selected_tasks_array[@]}"
fi