55# Usage:
66# $ source import_dnp_lib.bash
77#
8+ # Global
9+ # read/write DNP_ROOT
10+ #
811# =================================================================================================
912MSG_ERROR_FORMAT=" \033[1;31m"
1013MSG_END_FORMAT=" \033[0m"
14+ MSG_DONE_FORMAT=" \033[1;32m"
1115
1216# ....Variable set for export......................................................................
1317
@@ -19,6 +23,8 @@ MSG_END_FORMAT="\033[0m"
1923#
2024# Arguments:
2125# none
26+ # Global:
27+ # read DNP_ROOT
2228# Outputs:
2329# An error message to to stderr in case of failure
2430# Returns:
@@ -29,13 +35,13 @@ function dnp::import_lib_and_dependencies() {
2935 # ....Setup......................................................................................
3036 local TMP_CWD
3137 TMP_CWD=$( pwd)
32- local DEBUG
38+ local _debug
3339
3440 # ....cli..........................................................................................
3541 while [ $# -gt 0 ]; do
3642 case $1 in
3743 --debug)
38- DEBUG =" true"
44+ _debug =" true"
3945 shift # Remove argument (--debug)
4046 ;;
4147 * ) # Default case
@@ -45,51 +51,92 @@ function dnp::import_lib_and_dependencies() {
4551 done
4652
4753 # ....Find path to script........................................................................
48- # Note: can handle both sourcing cases
49- # i.e. from within a script or from an interactive terminal session
50- local SCRIPT_PATH
51- local TARGET_ROOT
52- SCRIPT_PATH=" $( realpath " ${BASH_SOURCE[0]:- ' .' } " ) "
53- TARGET_ROOT=" $( dirname " ${SCRIPT_PATH} " ) "
54-
55- # ....Find path to target parent directory.......................................................
56- while [[ $( basename " ${TARGET_ROOT} " ) != " dockerized-norlab-project" ]]; do
57- TARGET_ROOT=" $( dirname " $TARGET_ROOT " ) "
58- done
54+ dnp::find_dnp_root_path || return 1
5955
6056 # ....Pre-condition..............................................................................
6157 # Test extracted path
62- if [[ ! -d " ${TARGET_ROOT :? err} " ]]; then
63- echo -e " \n${MSG_ERROR_FORMAT} [DNP error]${MSG_END_FORMAT} dockerized-norlab-project is unreachable at '${TARGET_ROOT } '!" 1>&2
58+ if [[ ! -d " ${DNP_ROOT :? err} " ]]; then
59+ echo -e " \n${MSG_ERROR_FORMAT} [DNP error]${MSG_END_FORMAT} dockerized-norlab-project is unreachable at '${DNP_ROOT } '!" 1>&2
6460 return 1
6561 fi
6662
6763 # ....Load DNP .env file for N2ST................................................................
68- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
64+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
6965
7066 # ....Load NBS...................................................................................
7167 cd " ${NBS_PATH:? ' Variable not set' } " || return 1
7268 source " import_norlab_build_system_lib.bash" || return 1
7369
7470 # ....(Quickhack) Reload project .env file for N2ST..............................................
75- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
71+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
7672
7773 # ....Load N2ST..................................................................................
7874 cd " ${N2ST_PATH:? ' Variable not set' } " || return 1
7975 source " import_norlab_shell_script_tools_lib.bash" || return 1
8076
8177 # ....(Quickhack) Reload project .env file for N2ST..............................................
82- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
78+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
8379
8480 # ....Teardown...................................................................................
85- if [[ " ${DNP_DEBUG} " == " true" ]] || [[ " ${DEBUG } " == " true" ]]; then
81+ if [[ " ${DNP_DEBUG} " == " true" ]] || [[ " ${_debug } " == " true" ]]; then
8682 export DNP_DEBUG=true
8783 echo -e " ${MSG_DONE_FORMAT} [DNP]${MSG_END_FORMAT} librairies loaded"
8884 fi
8985 cd " ${TMP_CWD} " || { echo " Return to original dir error" 1>&2 && return 1; }
9086 return 0
9187}
9288
89+ # =================================================================================================
90+ # Function to find the DNP root path. It seek for the .env.dockerized-norlab-project
91+ # file which should be at the project root by moving up the directory tree from cwd.
92+ #
93+ # Usage:
94+ # $ dnp::find_dnp_root_path
95+ #
96+ # Arguments:
97+ # none
98+ # Outputs:
99+ # An error message to to stderr in case of failure
100+ # Globals:
101+ # write DNP_ROOT
102+ # Returns:
103+ # 1 on faillure, 0 otherwise
104+ # =================================================================================================
105+ dnp::find_dnp_root_path () {
106+
107+ # ....Find path to script........................................................................
108+ # Note: can handle both sourcing cases
109+ # i.e. from within a script or from an interactive terminal session
110+ local SCRIPT_PATH
111+ SCRIPT_PATH=" $( realpath " ${BASH_SOURCE[0]:- ' .' } " ) "
112+ DNP_ROOT=" $( dirname " ${SCRIPT_PATH} " ) "
113+
114+ local max_iterations=10 # Safety limit to prevent infinite loops
115+ local iterations_count=0
116+
117+ while [[ " $DNP_ROOT " != " /" && $iterations_count -lt $max_iterations ]]; do
118+
119+ # Move up to parent directory path
120+ DNP_ROOT=" $( dirname " $DNP_ROOT " ) "
121+ (( iterations_count++ ))
122+
123+ # Note: the .env.dockerized-norlab-project check instead of dir dockerized-norlab-project
124+ # check is for case where the repo root was clone with a different name, e.g., in teamcity
125+ if [[ -f " ${DNP_ROOT} /.env.dockerized-norlab-project" ]]; then
126+ echo -e " ${MSG_DONE_FORMAT} [DNP]${MSG_END_FORMAT} Found .env.dockerized-norlab-project in: $DNP_ROOT "
127+ export DNP_ROOT
128+ return 0
129+ elif [[ " ${DNP_DEBUG} " == " true" ]]; then
130+ echo " Level ${iterations_count} › DNP_ROOT=$DNP_ROOT "
131+ fi
132+
133+ done
134+
135+ # If we get here, the directory was not found
136+ echo -e " ${MSG_ERROR_FORMAT} [DNP error]${MSG_END_FORMAT} dockerized-norlab-project root directory not found in any parent directory" >&2
137+ return 1
138+ }
139+
93140# ::::Main:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
94141
95142if [[ " ${BASH_SOURCE[0]} " = " $0 " ]]; then
0 commit comments