From 4386cf8c29ff1884e6ecde01bc94ac6dece9fed5 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Mon, 29 Jan 2024 15:16:15 -0800 Subject: [PATCH] refactor and fix determining environment file name added getting environment file, library path and build source from subcommands env_file,lib,build_src --- build | 45 +++++++++++++++++++++++++++++++++------------ lib/build.lib | 29 +++++++++++++++++------------ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/build b/build index bf989ff..8d99758 100755 --- a/build +++ b/build @@ -3,7 +3,7 @@ udbuild () { local targets=(dev arm64 amd64 publish multi default) -local log_dir; local no_prompt; local packages +local log_dir; local no_prompt; local packages; local befile declare -A dimage; declare -A dinstall; declare -A dupdate declare OPTION; declare OPTARG; declare OPTIND @@ -18,16 +18,32 @@ BUILD_EFILE="" case "$1" in try) shift 1; try_container "$@"; return $? ;; - load_env_file) - echo -e "@@@@@@ loading build environment file for external use @@@@@@" - BUILD_EFILE=$(echo -- "$@" | grep -oP -- '(?<=-e )[^ ]*') - if source_env_file "$BUILD_EFILE"; then - echo -e "@@@@@@@@@@@@@@@@@ returning to calling script @@@@@@@@@@@@@@@" - else - return 1 - fi + + env_file) + shift 1 + befile=$(echo -- "$@" | grep -oP -- '(?<=-e )[^ ]*') + env_file ${befile:-$BUILD_EFILE} ; + return $? + ;; + + lib) + shift 1 + echo $BDIR/lib/build.lib + return 0 + ;; + + build_src) + shift 1 + befile=$(echo -- "$@" | grep -oP -- '(?<=-e )[^ ]*') + befile=${befile:-$BUILD_EFILE} + if befile=$(env_file $befile); then + [[ $befile ]] && source_env_file $befile + get_build_src + return $? + else + return 3 + fi ;; - build_src) shift 1; get_build_src "$@"; return $? ;; help) ;& --help) @@ -142,9 +158,10 @@ done shift $((OPTIND - 1)) + [[ ! $BUILD_EFILE ]] && source_env_file -if ! get_build_src; then +if ! get_build_src > /dev/null ; then if [[ $no_prompt ]] ; then echo aborting the build... echo -e "\e[1;31mNOTE: use '_core_' to explicitly build with only the UCI core repo\e[1;37m" @@ -229,6 +246,8 @@ if [[ ! $BUILD_SRC = "_core_" ]]; then /bin/cp -a ${BUILD_SRC:-src}/. $BDIR/.src > /dev/null 2>&1 /bin/cp -a $BDIR/.src/rootfs/opt/env/. $BDIR/core/rootfs/opt/env > /dev/null 2>&1 fi + ls -la $BDIR/.src/rootfs + ls -la $BDIR/.src/rootfs/root fi echo run environment directory copied to core at $BDIR/core/$_env_dir @@ -263,6 +282,8 @@ fi EOF fi +[[ -f $BDIR/.src/custom-core.sh ]] && /bin/cp $BDIR/.src/custom-core.sh $BDIR/core/ + pushd "$BDIR" > /dev/null || return 3 export BUILDING=true @@ -276,7 +297,7 @@ popd > /dev/null || return 4 # cleanup echo cleaning up.. -rm -rf $BDIR/.src $BDIR/core/build.env $BDIR/core/*-permits > /dev/null 2<&1 +rm -rf $BDIR/.src $BDIR/core/build.env $BDIR/core/custom-core.sh $BDIR/core/*-permits > /dev/null 2<&1 echo done cleaning # try in container if [[ ($TRY_CMD || $TARGET == "dev") ]]; then diff --git a/lib/build.lib b/lib/build.lib index 4c5ae71..97b0ad1 100755 --- a/lib/build.lib +++ b/lib/build.lib @@ -53,10 +53,14 @@ clean_env_file () { } env_file () { - env=${1:-.env} - [[ -f "${env}" ]] && { echo $env; return 0; } # || echo not $env - [[ -f "${env}.env" ]] && { echo "${env}.env"; return 0; } # || echo not ${env}.env - [[ -f "${env}/.env" ]] && { echo "${env}/.env"; return 0; } # || echo not ${env}/.env + local efile + efile=$(echo -- "$@" | grep -oP -- '(?<=-e )[^ ]*') + if [[ ! $efile ]]; then + [[ ! "$1" == *-* ]] && efile=${1:-.env} + fi + [[ -f "$(realpath "${efile}.env" 2> /dev/null)" ]] && { echo "${efile}.env"; return 0; } # || echo not ${efile}.env + [[ -f "$(realpath "${efile}/.env" 2> /dev/null)" ]] && { echo "${efile}/.env"; return 0; } # || echo not ${efile}/.env + [[ "${efile##*.}" == "env" ]] && [[ -f "$(realpath "${efile}" 2> /dev/null)" ]] && { echo $efile; return 0; } # || echo not $env return 1 } @@ -90,7 +94,7 @@ read_env_file() { source_env_file () { local default;local efile - if efile=$(env_file ${1:-$BUILD_EFILE}); then + if efile=$(env_file "${@:-$BUILD_EFILE}"); then [[ $efile == ".env" ]] && default=true [[ ! $(isAbsPath $efile) ]] && efile=$(realpath $efile) quiet echo -e "\e[1;37m********************\e[0;37m" @@ -236,24 +240,25 @@ unset BUILD_SRC if check_dir $src; then BUILD_SRC=$(realpath $src) + echo $BUILD_SRC return 0 fi -echo build source path \'$src\' not initially found, echo looking -echo in \$PWD,\$PWD/src/,../\$PWD/src for valid build source -echo to avoid this search use BUILD_SRC= in an environment file +quiet echo build source path \'$src\' not initially found, echo looking +quiet echo in \$PWD,\$PWD/src/,../\$PWD/src for valid build source +quiet echo to avoid this search use BUILD_SRC= in an environment file spaths="$PWD $PWD/src $(dirname $PWD)/$src $(dirname $PWD)" for spath in $spaths; do - echo checking for source in: $spath + quiet echo checking for source in: $spath if check_dir $spath; then - echo found valid source directory! - echo using $spath + quiet echo found valid source directory! BUILD_SRC=$spath + echo $BUILD_SRC return 0 fi done - echo -e "\e[1;31mERROR: unable to find a build source directory as $src \e[1;37m" +quiet echo -e "\e[1;31mERROR: unable to find a build source directory as $src \e[1;37m" return 1 }