cbuild/bootstrap.sh

126 lines
3.4 KiB
Bash

#!/usr/bin/env bash
CBUILD_BOOTSTRAP_VERSION=1.0.3
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]}
}
function safeprint {
printf "%s" "$@"
}
function exec_script_line {
local script="$1"
local line_num="$2"
local line_str="$(sed $line_num'!d' $script)"
if [ -z "$line_str" ]; then
echo "script line is empty"
exit 1
fi
eval "$line_str"
}
function print_version_list {
dir_local="$HOME/.local/share/cbuild"
dir_global="/usr/local/share/cbuild"
files=""
if [ -d "$dir_local" ]; then
files+=$(find "$dir_local" -maxdepth 2 -name 'CBUILD_VERSION')
fi
if [ -d "$dir_global" ]; then
files+=$(find "$dir_global" -maxdepth 2 -name 'CBUILD_VERSION')
fi
for f in $files; do
cat $f
echo " at $(dirname $f)"
done | sort -V
}
function print_help {
echo "cbuild-bootstrap v$CBUILD_BOOTSTRAP_VERSION"
echo "Script that launches a specific cbuild version selected by user or defined in a project config."
echo "Usage: cbuild [OPTIONS]"
echo "Options:"
echo " -h, --help Show this message"
echo " -v, --version Shows version"
echo " -c, --config FILE Set project config file path (default=./project.config)"
echo " --list-versions Shows list of installed cbuild versions"
}
# parse command line arguments
project_config_path="./project.config"
args=($@)
args_count=${#args[@]}
i=0
if [ $args_count -eq 0 ]; then
print_help
fi
while [ $i -lt $args_count ]
do
case "${args[i]}" in
'-v' | '--version')
echo "cbuild-bootstrap v$CBUILD_BOOTSTRAP_VERSION"
echo "list of installed cbuild versions:"
print_version_list
exit 0
;;
'-h' | '--help')
print_help
;;
'-c' | '--config')
i=$((i+1))
project_config_path="${args[i]}"
;;
*)
;;
esac
i=$((i+1))
done
if [ -z "$CBUILD_VERSION" ]; then
if [ -f "$project_config_path" ]; then
# read version from project config
exec_script_line "$project_config_path" 2
else
echo "project config not found"
echo "list of installed cbuild versions:"
print_version_list
# cut installation directories from list
versions=$(print_version_list | cut -d ' ' -f 1)
latest_version=$(echo "$versions" | tail -n 1)
echo "select version (default=$latest_version):"
read -r CBUILD_VERSION
[ -z "$CBUILD_VERSION" ] && CBUILD_VERSION=$latest_version
fi
fi
version_parse "$CBUILD_VERSION"
cbuild_ommand="cbuild${_version_major}.${_version_minor} $@"
LOG_FILE="$(realpath cbuild.log)"
set +eo pipefail
# enable logging stdout and stderr to file
$cbuild_ommand 2>&1 | tee "$LOG_FILE"
# log file can be deleted by clean task
if [ -f "$LOG_FILE" ]; then
# 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' \
"$LOG_FILE" > "$LOG_FILE.clean"
mv "$LOG_FILE.clean" "$LOG_FILE"
fi