thus ends the scourge of the script_adp function RIP
authorThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Wed, 30 Oct 2024 01:37:50 +0000 (01:37 +0000)
committerThomas Walker Lynch <xtujpz@reasoningtechnology.com>
Wed, 30 Oct 2024 01:37:50 +0000 (01:37 +0000)
developer/tool/env
env_administrator
env_developer
env_tester
tester/tool/env
tool_shared/bespoke/env
tool_shared/bespoke/env3 [deleted file]
tool_shared/bespoke/env4 [new file with mode: 0644]
tool_shared/bespoke/env_orig [deleted file]
tool_shared/bespoke/test_env

index 711f81e..26e049e 100644 (file)
@@ -1,4 +1,5 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
 # input guards
 
index 4b691ce..25591f4 100644 (file)
@@ -1,9 +1,6 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
-script_path="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
-export REPO_HOME="${script_path%}/"
-echo REPO_HOME "$REPO_HOME"
+source tool_shared/bespoke/env
 
-cd "$REPO_HOME"
-source tool/env
 
index 8659f86..5593044 100644 (file)
@@ -1,10 +1,6 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
-script_path="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
-export REPO_HOME="${script_path%}/"
-echo REPO_HOME "$REPO_HOME"
-
-cd "$REPO_HOME"
 source tool_shared/bespoke/env
 source developer/tool/env
 
index d1bd2d5..b7d4d78 100644 (file)
@@ -1,9 +1,5 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
-script_path="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
-export REPO_HOME="${script_path%}/"
-echo REPO_HOME "$REPO_HOME"
-
-cd "$REPO_HOME"
 source tool_shared/bespoke/env
 source tester/tool/env
index a31f599..01ffc74 100644 (file)
@@ -1,4 +1,5 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
 # input guards
 
index 80c44fb..849287f 100644 (file)
@@ -1,4 +1,5 @@
 #!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
 
 # This is the base environment shared by all roles in the project.
 
@@ -8,34 +9,23 @@
 
 # Absolute path to script file. The use of eval makes it suitable for exporting
 # and use with other scripts
-
-       read -r -d '' script_afp_string <<-"EOF"
-         # get the top of the BASH_SOURCE stack
-         local top_index=$(( ${#BASH_SOURCE[@]} - 1 ))
-         local bash_source="${BASH_SOURCE[$top_index]}"
-       
-         # echo
-         # for (( i=0; i<=top_index; i++ )); do
-         #   echo "$i: ${BASH_SOURCE[$i]}"
-         # done
-         # echo "bash_source (top of stack): $bash_source"
-       
-         # Resolve the absolute file path
-         local afp
-         afp=$(realpath "$bash_source" 2>/dev/null)
-         if [[ $? -ne 0 ]]; then
-           echo 'Error: Failed to resolve path in script_afp.' >&2
-           return 1
-         fi
-         echo "$afp"
-       EOF
-       
-  script_afp(){
-    eval "$script_afp_string"
-  }
+#
+# IMHO it is not possible write a function in bash that reliably returns the
+# script's path in all execution scenarios of direct execute, sourcing, and
+# usage in other functions.  So instead, define a variable script_afp at the top
+# of each script:
+#
+#   script_afp=realpath "${BASH_SOURCE[0]}"
+#
+# read -r -d '' script_afp_string <<'EOF'
+# realpath "${BASH_SOURCE[0]}" 2>/dev/null
+# EOF
+#   script_afp(){
+#     eval "$script_afp_string"
+#   }
 
   script_adp(){
-    dirname "$(script_afp)"
+    dirname "$script_afp"
   }
 
 
 
   ## script's filename
   script_fn(){
-    basename "$(script_afp)"
+    basename "$script_afp"
   }
 
   ## script's dirpath relative to $REPO_HOME
   script_fp(){
-    realpath --relative-to="${REPO_HOME}" "$(script_afp)"
+    realpath --relative-to="${REPO_HOME}" "$script_afp"
   }
 
   ## script's dirpath relative to $REPO_HOME
@@ -72,8 +62,9 @@
 # Bash has no 'closure' hence when exporting a function, one must also export all the pieces.
 
   export REPO_HOME PROJECT PROMPT_DECOR
-  export script_afp_string
-  export -f script_afp script_adp script_fn script_dp script_fp
+#  export script_afp_string
+#  export -f script_afp script_adp script_fn script_dp script_fp
+  export -f script_adp script_fn script_dp script_fp
 
   export ENV=$(script_fp)
   echo ENV "$ENV"
diff --git a/tool_shared/bespoke/env3 b/tool_shared/bespoke/env3
deleted file mode 100644 (file)
index 9fb26ef..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-#!/usr/bin/bash
-
-# This is the base environment shared by all roles in the project.
-
-# The project administrator sets up the following tools for all roles to use:
-#
-export JAVA_HOME="$REPO_HOME/tool_shared/third_party/jdk-11"
-
-# Absolute path to script directory, for local use in other functions, but the use
-# of eval makes it suitable for exporting and use with other scripts if that
-# is found to be useful. However note the functions that return project relative
-# paths further below.
-
-read -r -d '' script_adp_string <<-"EOF"
-  local bash_source
-  if [[ -n "${BASH_SOURCE[1]}" ]]; then
-    bash_source="${BASH_SOURCE[1]}"
-  elif [[ -n "${BASH_SOURCE[0]}" ]]; then
-    bash_source="${BASH_SOURCE[0]}"
-  else
-    echo 'Error: Unable to determine script path in script_adp.' >&2
-    return 1
-  fi
-  #echo
-  #echo bash_source: $bash_source
-  local absolute_script_dp
-  absolute_script_dp=$(dirname "$(realpath "$bash_source" 2>/dev/null)")
-  if [[ $? -ne 0 ]]; then
-    echo 'Error: Failed to resolve path in script_adp.' >&2
-    return 1
-  fi
-  echo "$absolute_script_dp"
-EOF
-script_adp(){
-  eval "$script_adp_string"
-}
-
-
-
-# Exports, and give the exported environment a name
-#
-
-  export script_adp_string 
-  export -f script_adp
-#  export -f script_adp script_fn script_dp script_fp workspace
-#  export ENV=$(script_fp)
-#  echo ENV "$ENV"
-  
diff --git a/tool_shared/bespoke/env4 b/tool_shared/bespoke/env4
new file mode 100644 (file)
index 0000000..39abce6
--- /dev/null
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
+
+# This is the base environment shared by all roles in the project.
+
+# The project administrator sets up the following tools for all roles to use:
+#
+  export JAVA_HOME="$REPO_HOME/tool_shared/third_party/jdk-11"
+
+# Absolute path to script file. The use of eval makes it suitable for exporting
+# and use with other scripts
+#
+# IMHO it is not possible write a function in bash that reliably returns the
+# script's path in all execution scenarios of direct execute, sourcing, and
+# usage in other functions.  So instead, define a variable script_afp at the top
+# of each script:
+#
+#   script_afp=realpath "${BASH_SOURCE[0]}"
+#
+# read -r -d '' script_afp_string <<'EOF'
+# realpath "${BASH_SOURCE[0]}" 2>/dev/null
+# EOF
+#   script_afp(){
+#     eval "$script_afp_string"
+#   }
+
+  script_adp(){
+    dirname "$script_afp"
+  }
+
+
+# This script assumes it is located at $REPO_HOME/tools_shared/bespoke and works
+# backwards to recover $REPO_HOME, etc.
+
+  REPO_HOME=$(dirname "$(dirname "$(script_adp)")")
+  echo REPO_HOME "$REPO_HOME"
+
+  PROJECT=$(basename "$REPO_HOME")
+  echo PROJECT "$PROJECT"
+  PROMPT_DECOR=$PROJECT
+
+
+# These functions are offered as a convenience to be run inside other scripts.
+# These produce $REPO_HOME relative results, and thus preferred over script_adp.
+
+  ## script's filename
+  script_fn(){
+    basename "$script_afp"
+  }
+
+  ## script's dirpath relative to $REPO_HOME
+  script_fp(){
+    realpath --relative-to="${REPO_HOME}" "$script_afp"
+  }
+
+  ## script's dirpath relative to $REPO_HOME
+  script_dp(){
+    dirname "$(script_fp)"
+  }
+
+# Exports, and give the exported environment a name
+# Bash has no 'closure' hence when exporting a function, one must also export all the pieces.
+
+  export REPO_HOME PROJECT PROMPT_DECOR
+#  export script_afp_string
+#  export -f script_afp script_adp script_fn script_dp script_fp
+  export -f script_adp script_fn script_dp script_fp
+
+  export ENV=$(script_fp)
+  echo ENV "$ENV"
+
+echo
+echo "--------------------------------------------------------------------------------"
+echo "from within, at the end, of test_shared/bespoke/env the script functions return the following."
+echo
+echo "REPO_HOME:" "$REPO_HOME"
+echo "PROJECT:" "$PROJECT"
+echo "script_afp:" "$script_afp"
+echo "script_adp:" "$(script_adp)"
+echo "script_fn:" "$(script_fn)"
+echo "script_fp:" "$(script_fp)"
+echo "script_dp:" "$(script_dp)"
+echo "ENV:" "$ENV"
+echo "---------"
+echo "the stack"
+         top_index=$(( ${#BASH_SOURCE[@]} - 1 ))
+         for (( i=0; i<=top_index; i++ )); do
+           echo "$i: ${BASH_SOURCE[$i]}"
+         done
+
diff --git a/tool_shared/bespoke/env_orig b/tool_shared/bespoke/env_orig
deleted file mode 100644 (file)
index 04daca8..0000000
+++ /dev/null
@@ -1,118 +0,0 @@
-#!/usr/bin/bash
-
-# This is the base environment shared by all roles in the project.
-
-# The project administrator sets up the following tools for all roles to use:
-#
-export JAVA_HOME="$REPO_HOME/tool_shared/third_party/jdk-11"
-
-# Absolute path to script directory, the use of eval makes it suitable for
-# exporting and use with other scripts
-
-read -r -d '' script_adp_string <<-"EOF"
-  local bash_source
-  if [[ -n "${BASH_SOURCE[1]}" ]]; then
-    bash_source="${BASH_SOURCE[1]}"
-  elif [[ -n "${BASH_SOURCE[0]}" ]]; then
-    bash_source="${BASH_SOURCE[0]}"
-  else
-    echo 'Error: Unable to determine script path in script_adp.' >&2
-    return 1
-  fi
-  echo
-  echo 0: ${BASH_SOURCE[0]}
-  echo 1: ${BASH_SOURCE[1]}
-  echo bash_source: $bash_source
-  local adp
-  adp=$(dirname "$(realpath "$bash_source" 2>/dev/null)")
-  if [[ $? -ne 0 ]]; then
-    echo 'Error: Failed to resolve path in script_adp.' >&2
-    return 1
-  fi
-  echo "$adp"
-EOF
-  script_adp(){
-    eval "$script_adp_string"
-  }
-
-
-# This script assumes it is located at $REPO_HOME/tools_shared/bespoke and works
-# backwards to recover $REPO_HOME, etc.
-
-  REPO_HOME=$(dirname "$(dirname "$(script_adp)")")
-  echo REPO_HOME "$REPO_HOME"
-
-  PROJECT=$(basename "$REPO_HOME")
-  echo PROJECT "$PROJECT"
-  PROMPT_DECOR=$PROJECT
-
-
-# These functions are offered as a convenience to be run inside other scripts.
-# These produce $REPO_HOME relative results, and thus preferred over script_adp.
-
-  ## script's filename
-read -r -d '' script_fn_string <<-"EOF" 
-  local source_file="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}"
-  if [[ -z "$source_file" ]]; then
-    echo 'Error: Unable to determine script filename in script_fn.' >&2
-    return 1
-  fi
-  echo $(basename "$source_file")
-EOF
-  script_fn(){
-    eval "$script_fn_string"
-  }
-
-
-  ## path to script directory relative to $REPO_HOME
-read -r -d '' script_dp_string <<-"EOF" 
-  local rh="${REPO_HOME}"
-  echo
-  echo "REPO_HOME: ${rh}"
-
-  local adp
-  adp=$(script_adp)
-  echo "adp: ${adp}"
-
-  if [[ -z "${adp}" ]]; then
-    echo 'Error: script_adp returned an empty path in script_dp.' >&2
-    return 1
-  fi
-
-  realpath --relative-to="${rh}" "${adp}" || echo 'Error resolving path in script_dp' >&2
-EOF
-  script_dp(){
-    eval "$script_dp_string"
-    echo dp
-  }
-
-   ## script's filepath relative to $REPO_HOME
-read -r -d '' script_fp_string <<-"EOF" 
-  local dp fn
-  dp=$(script_dp)
-  fn=$(script_fn)
-  if [[ -z "$dp" || -z "$fn" ]]; then
-    echo 'Error: script_fp encountered an empty path component.' >&2
-    return 1
-  fi
-  echo "$dp/$fn"
-EOF
-  script_fp(){
-    eval "$script_fp_string"
-  }
-
-  ## this script assumes it is located in workspace/tool/env, so
-  workspace(){
-    eval "echo \$(dirname \$(script_dp))"
-  }
-
-# Exports, and give the exported environment a name
-# Bash has no 'closure' hence when exporting a function, one must also export all the pieces.
-
-  export REPO_HOME PROJECT PROMPT_DECOR PPS1 PPS2 
-  export script_adp_string script_fn_string script_dp_string script_fp_string
-  export -f script_adp script_fn script_dp script_fp workspace
-
-  export ENV=$(script_fp)
-  echo ENV "$ENV"
-
index 59546a1..18d75f9 100755 (executable)
@@ -1,12 +1,24 @@
 #!/bin/env bash
+script_afp=$(realpath "${BASH_SOURCE[0]}")
+
 # try both running and sourcing this test
 
+echo
+echo "--------------------------------------------------------------------------------"
+echo "from within test_shared/bespoke/test_env:"
+echo
 echo "REPO_HOME:" "$REPO_HOME"
 echo "PROJECT:" "$PROJECT"
-
-echo "script_afp:" "$(script_afp)"
+echo "script_afp:" "$script_afp"
 echo "script_adp:" "$(script_adp)"
 echo "script_fn:" "$(script_fn)"
 echo "script_fp:" "$(script_fp)"
 echo "script_dp:" "$(script_dp)"
 echo "ENV:" "$ENV"
+echo "-----------------------"
+echo "the BASH_SOURCE stack:"
+
+         top_index=$(( ${#BASH_SOURCE[@]} - 1 ))
+         for (( i=0; i<=top_index; i++ )); do
+           echo "$i: ${BASH_SOURCE[$i]}"
+         done