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:
@@ -45,63 +51,31 @@ 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- local max_iterations=10 # Safety limit to prevent infinite loops
57- local iterations_count=0
58-
59- # while [[ ! -f "${TARGET_ROOT}/.env.dockerized-norlab-project" ]] || [[ $(basename "${TARGET_ROOT}") != "dockerized-norlab-project" ]]; do
60- while [[ " $current_dir " != " /" && $iterations_count -lt $max_iterations ]]; do
61- # Note: the .env.dockerized-norlab-project check is for case where the repo root was clone with a different name, e.g., in teamcity
62- TARGET_ROOT=" $( dirname " $TARGET_ROOT " ) "
63-
64- if [[ -f " ${TARGET_ROOT} /.env.dockerized-norlab-project" ]]; then
65- echo " Found .env.dockerized-norlab-project in: $TARGET_ROOT "
66- break
67- fi
68-
69- if [[ " ${DNP_DEBUG} " == " true" ]] || [[ " ${_debug} " == " true" ]]; then
70- echo " Level ${iterations_count} › TARGET_ROOT=$TARGET_ROOT "
71- echo
72- tree -L 1 -a " ${TARGET_ROOT} "
73- echo
74- fi
75- (( iterations_count++ ))
76- done
77- if [[ $iterations_count -ge $max_iterations ]]; then
78- echo -e " \n${MSG_ERROR_FORMAT} [DNP error]${MSG_END_FORMAT} dockerized-norlab-project is unreachable at '${TARGET_ROOT} '!" 1>&2
79- return 1
80- fi
54+ dnp::find_dnp_root_path || return 1
8155
8256 # ....Pre-condition..............................................................................
8357 # Test extracted path
84- if [[ ! -d " ${TARGET_ROOT :? err} " ]]; then
85- 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
8660 return 1
8761 fi
8862
8963 # ....Load DNP .env file for N2ST................................................................
90- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
64+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
9165
9266 # ....Load NBS...................................................................................
9367 cd " ${NBS_PATH:? ' Variable not set' } " || return 1
9468 source " import_norlab_build_system_lib.bash" || return 1
9569
9670 # ....(Quickhack) Reload project .env file for N2ST..............................................
97- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
71+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
9872
9973 # ....Load N2ST..................................................................................
10074 cd " ${N2ST_PATH:? ' Variable not set' } " || return 1
10175 source " import_norlab_shell_script_tools_lib.bash" || return 1
10276
10377 # ....(Quickhack) Reload project .env file for N2ST..............................................
104- source " ${TARGET_ROOT } /load_repo_dotenv.bash"
78+ source " ${DNP_ROOT } /load_repo_dotenv.bash"
10579
10680 # ....Teardown...................................................................................
10781 if [[ " ${DNP_DEBUG} " == " true" ]] || [[ " ${_debug} " == " true" ]]; then
@@ -112,6 +86,57 @@ function dnp::import_lib_and_dependencies() {
11286 return 0
11387}
11488
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+
115140# ::::Main:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
116141
117142if [[ " ${BASH_SOURCE[0]} " = " $0 " ]]; then
0 commit comments