From 4fc62f65b46071fa90f7930026a7d848bb15e865 Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 19 Jul 2024 01:56:33 +0300 Subject: [PATCH] v2.1.0 --- CHANGELOG.md | 3 +- bootstrap.sh | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ cbuild.sh | 28 ++++++++--------- config.sh | 18 ++--------- functions.sh | 3 ++ setup.sh | 63 ++++++++++++++++++++++++++++---------- 6 files changed, 153 insertions(+), 47 deletions(-) create mode 100644 bootstrap.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index a5cb163..d2efa5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -# NEXT: v2.1.0 +# v2.1.0 + **config**: no more `current.config` and `default.config`, just `project.config` + improved version checking + `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 + new dependency resolution system (see **config** and `example_dependency_configs`) diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..e0a2b8e --- /dev/null +++ b/bootstrap.sh @@ -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 diff --git a/cbuild.sh b/cbuild.sh index 7024591..c5ab7dc 100644 --- a/cbuild.sh +++ b/cbuild.sh @@ -1,27 +1,29 @@ #!/usr/bin/env bash +INSTALLED_CBUILD_VERSION=2.1.0 +# set \t size to 4 spaces tabs 4 # exit on errors set -eo pipefail -INSTALLED_CBUILD_VERSION=2.1.0 function version_parse { - local v="$1" + local value="$1" + var_name="$2" IFS_backup="$IFS" IFS='.' - v_array=($v) + local v_array=($value) IFS="$IFS_backup" - version_major=${v_array[0]} - version_minor=${v_array[1]} - version_patch=${v_array[2]} + 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 +version_parse $INSTALLED_CBUILD_VERSION installed 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 - 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 echo "CBUILD_INSTALL_DIR '$CBUILD_INSTALL_DIR' doesn't exist" exit 1 @@ -52,6 +54,7 @@ function print_help { myprint " -c, --config FILE Set project config file path (default=./project.config)" } +# parse command line arguments project_config_path="./project.config" args=($@) args_count=${#args[@]} @@ -74,14 +77,11 @@ do exit 0 ;; '-v' | '--version') - myprint $INSTALLED_CBUILD_VERSION + myprint "cbuild v$INSTALLED_CBUILD_VERSION" exit 0 ;; '-c' | '--config') - i=$((i+1)) - project_config_path="${args[i]}" - myprint "<$project_config_path>" - exit 0 + project_config_path="$(get_next_arg)" ;; '-n' | '--new-project') new_project_dir="$(get_next_arg)" diff --git a/config.sh b/config.sh index 21ebf1d..4096569 100644 --- a/config.sh +++ b/config.sh @@ -4,14 +4,6 @@ include cbuild/myprint.sh include cbuild/functions.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 { local project_config_path="$1" TASK="$2" @@ -44,14 +36,8 @@ function load_config { myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" # checking versions - version_parse $INSTALLED_CBUILD_VERSION - installed_version_major=$version_major - 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 + version_parse $INSTALLED_CBUILD_VERSION installed + version_parse $CBUILD_VERSION config 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" fi diff --git a/functions.sh b/functions.sh index 7bba584..daba609 100755 --- a/functions.sh +++ b/functions.sh @@ -8,6 +8,9 @@ function exec_script_line { local quiet=$3 myprint_quiet $quiet "${BLUE}reading line $line_num from $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" eval "$line_str" } diff --git a/setup.sh b/setup.sh index 98708b2..b2a0d64 100644 --- a/setup.sh +++ b/setup.sh @@ -1,33 +1,49 @@ #!/usr/bin/env bash # 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 set -xeo pipefail CBUILD_VERSION=2.1.0 function version_parse { - local v="$1" + local value="$1" + var_name="$2" IFS_backup="$IFS" IFS='.' - v_array=($v) + local v_array=($value) IFS="$IFS_backup" - version_major=${v_array[0]} - version_minor=${v_array[1]} - version_patch=${v_array[2]} + 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 $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 [ "$1" = "--local" ]; then - CBUILD_INSTALL_DIR="~/.local/share/cbuild/${version_major}.${version_minor}" - CBUILD_BIN_DIR="~/.local/bin" - mkdir -p "~/.local" - mkdir -p "~/.local/share" - mkdir -p "~/.local/share/cbuild" - mkdir -p "~/.local/bin" + if [ ! -d "$HOME" ]; then + echo "ERROR: home directory '$HOME' doesn't exist" + exit 1 + fi + CBUILD_INSTALL_DIR="$HOME/.local/share/cbuild/${local_version_major}.${local_version_minor}" + 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 - 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" mkdir -p "/usr/local/share" mkdir -p "/usr/local/share/cbuild" @@ -38,6 +54,21 @@ fi rm -rf "$CBUILD_INSTALL_DIR" cp -r ./ "$CBUILD_INSTALL_DIR" 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"