#!/usr/bin/env bash INSTALLED_CBUILD_VERSION=2.1.3 # 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/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] [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 "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 if ask_yn "create default cbuild project config?"; then project_config_path="$new_project_dir/project.config" cp "$CBUILD_INSTALL_DIR/default.config" "$project_config_path" myprint "Enter project name: " read -r project_name sed -i "s,\%PROJECT_NAME\%,$project_name,g" "$project_config_path" myprint "${GREEN}created config at '$project_config_path'" fi if ask_yn "Copy default .gitignore?"; then cp "$CBUILD_INSTALL_DIR/.gitignore" "$new_project_dir/" 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}$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 "$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