Skip to content

Commit 2ab53bd

Browse files
committed
fix: refactor root path resolution and update default flags in scripts
- Extracted root path resolution logic to a dedicated `dnp::find_dnp_root_path` function for improved reusability. - Updated `.env.dockerized-norlab-project` defaults to disable `DNP_DEBUG` and enable `DNP_CLEAR_CONSOLE_ACTIVATED`. - Corrected comments and enhanced debug messaging for better traceability during script execution. Issue NMO-663
1 parent 3de4882 commit 2ab53bd

File tree

3 files changed

+67
-42
lines changed

3 files changed

+67
-42
lines changed

.env.dockerized-norlab-project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ DNP_MOCK_SUPER_PROJECT_ROOT="${DNP_ROOT}/utilities/tmp/dockerized-norlab-project
5050
# (CRITICAL) ToDo: on task end >> NMO-663 feat: DNP_PATH path resolution
5151
DNP_PATH="${DNP_ROOT}/src/bin/dnp"
5252

53-
DNP_DEBUG=true
54-
DNP_CLEAR_CONSOLE_ACTIVATED=false
53+
DNP_DEBUG=false
54+
DNP_CLEAR_CONSOLE_ACTIVATED=true

src/lib/core/utils/import_dnp_lib.bash

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
# Usage:
66
# $ source import_dnp_lib.bash
77
#
8+
# Global
9+
# read/write DNP_ROOT
10+
#
811
# =================================================================================================
912
MSG_ERROR_FORMAT="\033[1;31m"
1013
MSG_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

117142
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then

src/lib/core/utils/load_super_project_config.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ declare -x SUPER_PROJECT_REPO_NAME
2828
# Outputs:
2929
# An error message to to stderr in case of failure
3030
# Globals:
31-
# write SUPER_PROJECT_ROOT
31+
# read/write SUPER_PROJECT_ROOT
3232
# write SUPER_PROJECT_REPO_NAME
3333
# Returns:
3434
# 1 on faillure, 0 otherwise
@@ -103,7 +103,7 @@ function dnp::load_super_project_configurations() {
103103

104104
# =================================================================================================
105105
# Function to find the DNP user side project path. It seek for the .dockerized_norlab_project
106-
# directory which sould be at the project root by moving up the directory tree from cwd.
106+
# directory which should be at the project root by moving up the directory tree from cwd.
107107
#
108108
# Usage:
109109
# $ dnp::find_dnp_super_project_dir

0 commit comments

Comments
 (0)