Compare commits

..

2 Commits

Author SHA1 Message Date
4fc62f65b4 v2.1.0 2024-07-19 01:56:33 +03:00
f4c12e9b0e optimized line printing 2024-07-19 01:43:57 +03:00
7 changed files with 181 additions and 53 deletions

View File

@ -1,7 +1,8 @@
# NEXT: v2.1.0 # v2.1.0
+ **config**: no more `current.config` and `default.config`, just `project.config` + **config**: no more `current.config` and `default.config`, just `project.config`
+ improved version checking + improved version checking
+ `setup.sh` now installs each minor version in separate dicectory + `setup.sh` now installs each minor version in separate dicectory
+ added `bootstrap.sh` which automaticly selects cbuild version specified in project config
# v2.0.2 # v2.0.2
+ new dependency resolution system (see **config** and `example_dependency_configs`) + new dependency resolution system (see **config** and `example_dependency_configs`)

85
bootstrap.sh Normal file
View File

@ -0,0 +1,85 @@
#!/usr/bin/env bash
CBUILD_BOOTSTRAP_VERSION=1.0.0
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"
}
# parse command line arguments
project_config_path="./project.config"
args=($@)
args_count=${#args[@]}
i=0
function get_next_arg {
i=$((i+1))
safeprint "${args[i]}"
}
while [ $i -lt $args_count ]
do
case "${args[i]}" in
'-v' | '--version')
echo "cbuild-bootstrap v$CBUILD_BOOTSTRAP_VERSION"
;;
'-c' | '--config')
project_config_path="$(get_next_arg)"
;;
'-n' | '--new-project')
echo "enter project cbuild version (example: 2.1.0)"
read -r CBUILD_VERSION
;;
*)
;;
esac
i=$((i+1))
done
# read version from project config if not specified
if [ -z "$CBUILD_VERSION" ]; then
exec_script_line "$project_config_path" 2
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' \
-i "$LOG_FILE"
fi

View File

@ -1,27 +1,29 @@
#!/usr/bin/env bash #!/usr/bin/env bash
INSTALLED_CBUILD_VERSION=2.1.0
# set \t size to 4 spaces
tabs 4 tabs 4
# exit on errors # exit on errors
set -eo pipefail set -eo pipefail
INSTALLED_CBUILD_VERSION=2.1.0
function version_parse { function version_parse {
local v="$1" local value="$1"
var_name="$2"
IFS_backup="$IFS" IFS_backup="$IFS"
IFS='.' IFS='.'
v_array=($v) local v_array=($value)
IFS="$IFS_backup" IFS="$IFS_backup"
version_major=${v_array[0]} eval ${var_name}_version_major=${v_array[0]}
version_minor=${v_array[1]} eval ${var_name}_version_minor=${v_array[1]}
version_patch=${v_array[2]} eval ${var_name}_version_patch=${v_array[2]}
} }
version_parse $INSTALLED_CBUILD_VERSION version_parse $INSTALLED_CBUILD_VERSION installed
if [ -z "$CBUILD_INSTALL_DIR" ]; then if [ -z "$CBUILD_INSTALL_DIR" ]; then
CBUILD_INSTALL_DIR="~/.local/share/cbuild/${version_major}.${version_minor}" CBUILD_INSTALL_DIR="$HOME/.local/share/cbuild/${installed_version_major}.${installed_version_minor}"
if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then
CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${version_major}.${version_minor}" CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${installed_version_major}.${installed_version_minor}"
if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then
echo "CBUILD_INSTALL_DIR '$CBUILD_INSTALL_DIR' doesn't exist" echo "CBUILD_INSTALL_DIR '$CBUILD_INSTALL_DIR' doesn't exist"
exit 1 exit 1
@ -52,6 +54,7 @@ function print_help {
myprint " -c, --config FILE Set project config file path (default=./project.config)" myprint " -c, --config FILE Set project config file path (default=./project.config)"
} }
# parse command line arguments
project_config_path="./project.config" project_config_path="./project.config"
args=($@) args=($@)
args_count=${#args[@]} args_count=${#args[@]}
@ -74,14 +77,11 @@ do
exit 0 exit 0
;; ;;
'-v' | '--version') '-v' | '--version')
myprint $INSTALLED_CBUILD_VERSION myprint "cbuild v$INSTALLED_CBUILD_VERSION"
exit 0 exit 0
;; ;;
'-c' | '--config') '-c' | '--config')
i=$((i+1)) project_config_path="$(get_next_arg)"
project_config_path="${args[i]}"
myprint "<$project_config_path>"
exit 0
;; ;;
'-n' | '--new-project') '-n' | '--new-project')
new_project_dir="$(get_next_arg)" new_project_dir="$(get_next_arg)"

View File

@ -4,14 +4,6 @@ include cbuild/myprint.sh
include cbuild/functions.sh include cbuild/functions.sh
include cbuild/detect_os.sh include cbuild/detect_os.sh
function myprint_quiet {
local quiet=$1
local text="$2"
if [ "$quiet" != true ]; then
myprint "$text"
fi
}
function load_config { function load_config {
local project_config_path="$1" local project_config_path="$1"
TASK="$2" TASK="$2"
@ -44,14 +36,8 @@ function load_config {
myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION"
# checking versions # checking versions
version_parse $INSTALLED_CBUILD_VERSION version_parse $INSTALLED_CBUILD_VERSION installed
installed_version_major=$version_major version_parse $CBUILD_VERSION config
installed_version_minor=$version_minor
installed_version_patch=$version_patch
version_parse $CBUILD_VERSION
config_version_major=$version_major
config_version_minor=$version_minor
config_version_patch=$version_patch
if [ "$installed_version_major.$installed_version_minor" != "$config_version_major.$config_version_minor" ]; then if [ "$installed_version_major.$installed_version_minor" != "$config_version_major.$config_version_minor" ]; then
error "config was created for cbuild$config_version_major.$config_version_minor, but loaded whith cbuild$installed_version_major.$installed_version_minor which is incompatible" error "config was created for cbuild$config_version_major.$config_version_minor, but loaded whith cbuild$installed_version_major.$installed_version_minor which is incompatible"
fi fi

View File

@ -8,6 +8,9 @@ function exec_script_line {
local quiet=$3 local quiet=$3
myprint_quiet $quiet "${BLUE}reading line $line_num from $script" myprint_quiet $quiet "${BLUE}reading line $line_num from $script"
local line_str="$(sed $line_num'!d' $script)" local line_str="$(sed $line_num'!d' $script)"
if [ -z "$line_str" ]; then
error "script line is empty"
fi
myprint_quiet $quiet "$line_str" myprint_quiet $quiet "$line_str"
eval "$line_str" eval "$line_str"
} }

View File

@ -21,6 +21,14 @@ function myprint {
printf "${GRAY}$@${GRAY}\n" printf "${GRAY}$@${GRAY}\n"
} }
function myprint_quiet {
local quiet=$1
local text="$2"
if [ "$quiet" != true ]; then
myprint "$text"
fi
}
# print message and exit # print message and exit
function error { function error {
myprint "${RED}$@" myprint "${RED}$@"
@ -36,6 +44,16 @@ function ask_yn {
return $([[ "$answ" = [Yy] ]]); return $([[ "$answ" = [Yy] ]]);
} }
function char_multiply {
local character="$1"
local length="$2"
i=0
while [ $i -lt $length ]; do
printf $character
i=$((i+1))
done
}
# prints horizontal line occupying whole terminal row # prints horizontal line occupying whole terminal row
# https://en.wikipedia.org/wiki/Box-drawing_characters # https://en.wikipedia.org/wiki/Box-drawing_characters
function print_hline { function print_hline {
@ -47,7 +65,10 @@ function print_hline {
if [ -z "$character" ]; then if [ -z "$character" ]; then
character="-"; character="-";
fi fi
printf "${color}%.s${character}" $(seq 2 $(tput cols)) local term_width=$(tput cols)
local line_length=$((term_width - 1))
printf "${color}"
char_multiply "$character" $line_length
printf "${GRAY}\n" printf "${GRAY}\n"
} }
@ -65,10 +86,11 @@ function print_header {
local term_width=$(tput cols) local term_width=$(tput cols)
local label_length=${#label} local label_length=${#label}
local line_characters_count=$((term_width - label_length - 2)) local line_characters_count=$((term_width - label_length - 2))
local letf_line_length=$(( line_characters_count / 2 )) local left_line_length=$(( line_characters_count / 2 ))
local right_line_length=$(( letf_line_length + line_characters_count % 2 )) local right_line_length=$(( left_line_length - 1 + line_characters_count % 2 ))
printf "${color}%.s${character}" $(seq 1 $letf_line_length) printf "${color}"
printf "[${label}]" char_multiply "$character" $left_line_length
printf "${color}%.s${character}" $(seq 2 $right_line_length) printf "[${label}]${color}"
char_multiply "$character" $right_line_length
printf "${GRAY}\n" printf "${GRAY}\n"
} }

View File

@ -1,33 +1,49 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# USAGE: ./setup.sh install cbuild to /usr/local # USAGE: ./setup.sh install cbuild to /usr/local
# ./setup.sh --local install cbuild to ~/.local # ./setup.sh --local install cbuild to $HOME/.local
# exit on errors # exit on errors
set -xeo pipefail set -xeo pipefail
CBUILD_VERSION=2.1.0 CBUILD_VERSION=2.1.0
function version_parse { function version_parse {
local v="$1" local value="$1"
var_name="$2"
IFS_backup="$IFS" IFS_backup="$IFS"
IFS='.' IFS='.'
v_array=($v) local v_array=($value)
IFS="$IFS_backup" IFS="$IFS_backup"
version_major=${v_array[0]} eval ${var_name}_version_major=${v_array[0]}
version_minor=${v_array[1]} eval ${var_name}_version_minor=${v_array[1]}
version_patch=${v_array[2]} eval ${var_name}_version_patch=${v_array[2]}
} }
version_parse $CBUILD_VERSION 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"
}
version_parse $CBUILD_VERSION local
if [ -z "$CBUILD_INSTALL_DIR" ]; then if [ -z "$CBUILD_INSTALL_DIR" ]; then
if [ "$1" = "--local" ]; then if [ "$1" = "--local" ]; then
CBUILD_INSTALL_DIR="~/.local/share/cbuild/${version_major}.${version_minor}" if [ ! -d "$HOME" ]; then
CBUILD_BIN_DIR="~/.local/bin" echo "ERROR: home directory '$HOME' doesn't exist"
mkdir -p "~/.local" exit 1
mkdir -p "~/.local/share" fi
mkdir -p "~/.local/share/cbuild" CBUILD_INSTALL_DIR="$HOME/.local/share/cbuild/${local_version_major}.${local_version_minor}"
mkdir -p "~/.local/bin" CBUILD_BIN_DIR="$HOME/.local/bin"
mkdir -p "$HOME/.local"
mkdir -p "$HOME/.local/share"
mkdir -p "$HOME/.local/share/cbuild"
mkdir -p "$HOME/.local/bin"
else else
CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${version_major}.${version_minor}" CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${local_version_major}.${local_version_minor}"
CBUILD_BIN_DIR="/usr/local/bin" CBUILD_BIN_DIR="/usr/local/bin"
mkdir -p "/usr/local/share" mkdir -p "/usr/local/share"
mkdir -p "/usr/local/share/cbuild" mkdir -p "/usr/local/share/cbuild"
@ -38,6 +54,21 @@ fi
rm -rf "$CBUILD_INSTALL_DIR" rm -rf "$CBUILD_INSTALL_DIR"
cp -r ./ "$CBUILD_INSTALL_DIR" cp -r ./ "$CBUILD_INSTALL_DIR"
rm -rf "$CBUILD_INSTALL_DIR/.git" rm -rf "$CBUILD_INSTALL_DIR/.git"
ln -sf "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "$CBUILD_BIN_DIR/cbuild${version_major}.${version_minor}" ln -sf "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "$CBUILD_BIN_DIR/cbuild${local_version_major}.${local_version_minor}"
ln -sf "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "$CBUILD_BIN_DIR/cbuild" bootstrap_install_path="$CBUILD_INSTALL_DIR/../bootstrap.sh"
if [ -f "$bootstrap_install_path" ]; then
exec_script_line "$bootstrap_install_path" 2
installed_bootstrap_version=$CBUILD_BOOTSTRAP_VERSION
installed_bootstrap_version_int=$(echo $installed_bootstrap_version | sed 's/\.//g')
exec_script_line "./bootstrap.sh" 2
local_bootstrap_version=$CBUILD_BOOTSTRAP_VERSION
local_bootstrap_version_int=$(echo $local_bootstrap_version | sed 's/\.//g')
if [[ $local_bootstrap_version_int > $installed_bootstrap_version_int ]]; then
ln -sf "$(realpath $CBUILD_INSTALL_DIR/bootstrap.sh)" -T "$bootstrap_install_path"
fi
else
ln -sf "$(realpath $CBUILD_INSTALL_DIR/bootstrap.sh)" -T "$bootstrap_install_path"
fi
ln -sf "$(realpath $bootstrap_install_path)" -T "$CBUILD_BIN_DIR/cbuild"