From 6bfc0f5e9a8b58bcf20a5f745e50b40ca3c6c201 Mon Sep 17 00:00:00 2001 From: Timerix Date: Sun, 9 Nov 2025 23:09:34 +0500 Subject: [PATCH] added file_copy_default_if_not_present() and replace_var_value_in_script() --- CHANGELOG.md | 3 +++ include/config.sh | 6 ++---- include/functions.sh | 23 +++++++++++++++++++++++ include/myprint.sh | 1 + 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 245daa3..704e9ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ + **CONFIG:** Changed `cbuild/default_tasks` to `@cbuild/default_tasks` + Moved most scripts to `include/` + Renamed default config to `./project.config.default` ++ Added functions: + + `file_copy_default_if_not_present()` + + `replace_var_value_in_script()` ## 2.2.4 + *default config*: C standard changed to C99 diff --git a/include/config.sh b/include/config.sh index 47a90df..a82aba4 100644 --- a/include/config.sh +++ b/include/config.sh @@ -7,6 +7,7 @@ include "@cbuild/include/detect_os.sh" function load_config { local project_config_path="$1" TASK="$2" + #bool local quiet=$3 myprint "${BLUE}loading ${WHITE}'$project_config_path'" @@ -53,10 +54,7 @@ function load_config { # load project user config local project_user_config_path="$project_config_path.user" - if [ ! -f "$project_user_config_path" ]; then - myprint "${YELLOW}creating default project user config ${WHITE}$project_user_config_path" - cp "$project_user_config_path.default" "$project_user_config_path" - fi + file_copy_default_if_not_present "$project_user_config_path" "$project_user_config_path.default" myprint "${BLUE}loading ${WHITE}'$project_user_config_path'" include "$project_user_config_path" diff --git a/include/functions.sh b/include/functions.sh index 5fd5ae6..bbdae48 100644 --- a/include/functions.sh +++ b/include/functions.sh @@ -5,7 +5,9 @@ include "@cbuild/include/myprint.sh" function exec_script_line { local script="$1" local line_num="$2" + #bool 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 @@ -15,6 +17,18 @@ function exec_script_line { eval "$line_str" } +function replace_var_value_in_script(){ + local script="$1" + local var_name="$2" + local new_value="$3" + + myprint "${BLUE}setting $var_name to ${CYAN}'$new_value' in '$script'" + cp "$script" "$script.tmp" + sed "s,$var_name=\".*\",$var_name=\"$new_value\",g" \ + "$script.tmp" > "$script" + rm "$script.tmp" +} + function clean_dir { local dir="$1" myprint "${WHITE}cleaning $dir" @@ -36,6 +50,15 @@ function try_delete_dir_or_file { fi } +file_copy_default_if_not_present(){ + local file="$1" + local file_default="$2" + if [ ! -f "$file" ]; then + myprint "${YELLOW}creating default ${WHITE}$file" + cp -r "$file_default" "$file" + fi +} + function exec_command { local command="$@" if [ ! -z "$command" ]; then diff --git a/include/myprint.sh b/include/myprint.sh index 2c63670..6bf071e 100644 --- a/include/myprint.sh +++ b/include/myprint.sh @@ -22,6 +22,7 @@ function myprint { } function myprint_quiet { + #bool local quiet=$1 local text="$2" if [ "$quiet" != true ]; then