From 4b4794e253dc73f28f284159d533903d68784603 Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 19 Jul 2024 00:13:12 +0300 Subject: [PATCH] parse_version --- .gitignore | 4 --- CHANGELOG.md | 5 ++++ cbuild.sh | 68 ++++++++++++++++++++++++++++----------------- config.sh | 75 +++++++++++++++++++------------------------------- default.config | 2 +- functions.sh | 10 +++++++ setup.sh | 44 +++++++++++++++++++++++------ 7 files changed, 123 insertions(+), 85 deletions(-) diff --git a/.gitignore b/.gitignore index 0a3318e..498924c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,10 +9,6 @@ obj/ *.user *.vcxproj.filters -# local cbuild config giles -current.config -*.current.config - # other files .old*/ old/ diff --git a/CHANGELOG.md b/CHANGELOG.md index df1d845..a5cb163 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# NEXT: 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 + # v2.0.2 + new dependency resolution system (see **config** and `example_dependency_configs`) + **config**: changed description of `OBJDIR` diff --git a/cbuild.sh b/cbuild.sh index f92cff7..7024591 100644 --- a/cbuild.sh +++ b/cbuild.sh @@ -4,11 +4,29 @@ tabs 4 # exit on errors set -eo pipefail +INSTALLED_CBUILD_VERSION=2.1.0 -INSTALLED_CBUILD_VERSION=2.0.2 +function version_parse { + local v="$1" + IFS_backup="$IFS" + IFS='.' + v_array=($v) + IFS="$IFS_backup" + version_major=${v_array[0]} + version_minor=${v_array[1]} + version_patch=${v_array[2]} +} +version_parse $INSTALLED_CBUILD_VERSION if [ -z "$CBUILD_INSTALL_DIR" ]; then - CBUILD_INSTALL_DIR="/usr/local/share/cbuild" + CBUILD_INSTALL_DIR="~/.local/share/cbuild/${version_major}.${version_minor}" + if [ ! -f "$CBUILD_INSTALL_DIR/cbuild.sh" ]; then + CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${version_major}.${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 { @@ -31,12 +49,10 @@ function print_help { myprint " -h, --help Show this message" myprint " -v, --version Shows version" myprint " -n, --new-project [PROJ_DIR] Initialize new cbuild project directory (default=./)" - myprint " -c, --current-config FILE Set current config file path (default=./current.config)" - myprint " -d, --default-config FILE Set default config file path (default=./current.config)" + myprint " -c, --config FILE Set project config file path (default=./project.config)" } -current_config_path="./current.config" -default_config_path="./default.config" +project_config_path="./project.config" args=($@) args_count=${#args[@]} selected_tasks_array=() @@ -55,34 +71,35 @@ do case "${args[i]}" in '-h' | '--help') print_help + exit 0 ;; '-v' | '--version') myprint $INSTALLED_CBUILD_VERSION + exit 0 ;; - '-c' | '--current-config') + '-c' | '--config') i=$((i+1)) - current_config_path="${args[i]}" - myprint "<$current_config_path>" - exit - ;; - '-d' | '--default-config') - default_config_path="$(get_next_arg)" + project_config_path="${args[i]}" + myprint "<$project_config_path>" + exit 0 ;; '-n' | '--new-project') new_project_dir="$(get_next_arg)" if [ -z "$new_project_dir" ]; then - new_project_dir="./" + new_project_dir="." fi - - if ask_yn "create default.config?"; then - cp "$CBUILD_INSTALL_DIR/default.config" "$new_project_dir" + 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" "default.config" + 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" + cp "$CBUILD_INSTALL_DIR/.gitignore" "$new_project_dir/" fi + exit 0 ;; *) selected_tasks_array+=(${args[i]}) @@ -92,13 +109,12 @@ do done function call_task { - local current_config_path="$1" - local default_config_path="$2" - local task="$3" - TASK_ARGS="$4" + local project_config_path="$1" + local task="$2" + TASK_ARGS="$3" print_header "${CYAN}" "─" "$PROJECT/$task" - load_config "$current_config_path" "$default_config_path" "$task" true + load_config "$project_config_path" "$task" true if [ ! -z "$PRE_TASK_SCRIPT" ]; then myprint "${BLUE}executing ${WHITE}$TASK_SCRIPT" @@ -116,7 +132,7 @@ function call_task { function call_tasks { local tasks="$@" - load_config "$current_config_path" "$default_config_path" "" false + load_config "$project_config_path" "" false print_header "${WHITE}" "═" "$PROJECT" project_dir="$(pwd)" for task_str in $tasks ; do @@ -126,7 +142,7 @@ function call_tasks { IFS=',;' args_array=($args_str) IFS="$IFS_backup" - call_task "$current_config_path" "$default_config_path" "$task" "${args_array[@]}" + call_task "$project_config_path" "$task" "${args_array[@]}" cd "$project_dir" done } diff --git a/config.sh b/config.sh index c2248e1..21ebf1d 100644 --- a/config.sh +++ b/config.sh @@ -12,29 +12,18 @@ function myprint_quiet { fi } -function exec_script_line { - local script="$1" - local line_num="$2" - local quiet=$3 - myprint_quiet $quiet "${BLUE}reading line $line_num from $script" - local line_str="$(sed $line_num'!d' $script)" - myprint_quiet $quiet "$line_str" - eval "$line_str" -} - function load_config { - local current_config_path="$1" - local default_config_path="$2" - TASK="$3" - local quiet=$4 + local project_config_path="$1" + TASK="$2" + local quiet=$3 - myprint "${BLUE}loading config current='$(realpath $current_config_path)' default='$(realpath $default_config_path)'" + myprint "${BLUE}loading config '$(realpath $project_config_path)'" - if [ -z "$current_config_path" ]; then - error "current_config_path is null" + if [ -z "$project_config_path" ]; then + error "config path is null" fi - if [ -z "$default_config_path" ]; then - error "default_config_path is null" + if [ ! -f "$project_config_path" ]; then + error "${YELLOW}$project_config_path doesn't exist" fi OS=$(detect_os) @@ -45,42 +34,36 @@ function load_config { error "couldnt get current cbuild installation version" fi - # getting version of default config - exec_script_line "$default_config_path" 3 $quiet - DEFAULT_CONFIG_VERSION="$CONFIG_VERSION" - unset CONFIG_VERSION - # undefined task [ -z "$TASK" ] && TASK="no_task" - # error on undefined - set -u + # getting cbuild version from config (CBUILD_VERSION declaration is at line 2) + exec_script_line "$project_config_path" 2 $quiet - # reading current config or creating default - if [ ! -f "$current_config_path" ]; then - myprint "${YELLOW}$current_config_path doesn't exist" - cp "$default_config_path" "$current_config_path" - myprint "${YELLOW}Created copy (${current_config_path}) of default config (${default_config_path})" - fi - - myprint_quiet $quiet "${BLUE}reading $current_config_path" - include "$current_config_path" - - myprint_quiet $quiet "${WHITE}project: ${CYAN}$PROJECT" - - myprint_quiet $quiet "${WHITE}${current_config_path} cbuild version: ${CYAN}$CBUILD_VERSION" + myprint_quiet $quiet "${WHITE}${project_config_path} cbuild version: ${CYAN}$CBUILD_VERSION" myprint_quiet $quiet "${WHITE}installed cbuild version: ${CYAN}$INSTALLED_CBUILD_VERSION" - myprint_quiet $quiet "${WHITE}${current_config_path} version: ${CYAN}$CONFIG_VERSION" - myprint_quiet $quiet "${WHITE}${default_config_path} version: ${CYAN}$DEFAULT_CONFIG_VERSION" # checking versions - if [ "$CBUILD_VERSION" != "$INSTALLED_CBUILD_VERSION" ]; then - error "config was created for outdated cbuild version" + 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 + 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 - if [ "$CONFIG_VERSION" != "$DEFAULT_CONFIG_VERSION" ]; then - error "current config version doesn't match default config version" + if [[ $installed_version_patch < $config_version_patch ]]; then + myprint "${YELLOW}New patch for cbuild$installed_version_major.$installed_version_minor is abaliable." + myprint "${YELLOW}Install it to get latest bugfixes." fi + # throw error on undefined variable usage + set -u + include "$project_config_path" + mkdir -p "$OUTDIR" mkdir -p "$OBJDIR/objects" mkdir -p "$OBJDIR/static_libs" @@ -91,4 +74,4 @@ function load_config { set +u myprint_quiet $quiet "${GREEN}loaded cbuild config" -} \ No newline at end of file +} diff --git a/default.config b/default.config index 957001a..d1f4c12 100644 --- a/default.config +++ b/default.config @@ -1,5 +1,5 @@ #!/usr/bin/env bash -CBUILD_VERSION=2.0.2 +CBUILD_VERSION=2.1.0 CONFIG_VERSION=1 PROJECT="%PROJECT_NAME%" diff --git a/functions.sh b/functions.sh index 3eddef6..7bba584 100755 --- a/functions.sh +++ b/functions.sh @@ -2,6 +2,16 @@ include "cbuild/myprint.sh" +function exec_script_line { + local script="$1" + local line_num="$2" + local quiet=$3 + myprint_quiet $quiet "${BLUE}reading line $line_num from $script" + local line_str="$(sed $line_num'!d' $script)" + myprint_quiet $quiet "$line_str" + eval "$line_str" +} + function clean_dir { local dir="$1" myprint "${WHITE}cleaning $dir" diff --git a/setup.sh b/setup.sh index bbaabc7..98708b2 100644 --- a/setup.sh +++ b/setup.sh @@ -1,15 +1,43 @@ #!/usr/bin/env bash +# USAGE: ./setup.sh install cbuild to /usr/local +# ./setup.sh --local install cbuild to ~/.local # exit on errors -set -eo pipefail -set -x +set -xeo pipefail +CBUILD_VERSION=2.1.0 +function version_parse { + local v="$1" + IFS_backup="$IFS" + IFS='.' + v_array=($v) + IFS="$IFS_backup" + version_major=${v_array[0]} + version_minor=${v_array[1]} + version_patch=${v_array[2]} +} + +version_parse $CBUILD_VERSION if [ -z "$CBUILD_INSTALL_DIR" ]; then - CBUILD_INSTALL_DIR="/usr/local/share/cbuild" + 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" + else + CBUILD_INSTALL_DIR="/usr/local/share/cbuild/${version_major}.${version_minor}" + CBUILD_BIN_DIR="/usr/local/bin" + mkdir -p "/usr/local/share" + mkdir -p "/usr/local/share/cbuild" + mkdir -p "/usr/local/bin" + fi fi -if [ "$CBUILD_INSTALL_DIR" != "." ]; then - cp -r ./ "$CBUILD_INSTALL_DIR" - rm -rf "$CBUILD_INSTALL_DIR/.git" -fi -ln -sf "$(realpath $CBUILD_INSTALL_DIR/cbuild.sh)" -T "/usr/local/bin/cbuild" +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"