From dadc3632aed3b2f33956d82e2ce04cbe9b8cd85f Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Sat, 14 Feb 2015 13:32:06 -0500 Subject: [PATCH] Better sourcing of lib files --- lib/utils.sh | 69 ++++++++++++++++++------------------- macControl/moomAtDesk.sh | 6 ++-- scriptTemplate.sh | 6 ++-- setupScripts/casks.sh | 6 ++-- setupScripts/dropbox.sh | 10 +++--- setupScripts/fileTest.sh | 6 ++-- setupScripts/homebrew.sh | 6 ++-- setupScripts/mackup.sh | 6 ++-- setupScripts/newMacSetup.sh | 10 +++--- setupScripts/osx.sh | 6 ++-- setupScripts/ruby.sh | 6 ++-- setupScripts/ssh.sh | 6 ++-- syncScripts/SyncTemplate.sh | 6 ++-- 13 files changed, 86 insertions(+), 63 deletions(-) diff --git a/lib/utils.sh b/lib/utils.sh index 3572cf0..ec4da97 100755 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -15,41 +15,6 @@ # # ################################################## - -# Source additional files -# ------------------------------------------------------ -# The list of additional utility files to be sourced -# ------------------------------------------------------ - -# First we locate this script and populate the $SCRIPTPATH variable -# Doing so allows us to source additional files from this utils file. -SOURCE="${BASH_SOURCE[0]}" -while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink - DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" - SOURCE="$(readlink "${SOURCE}")" - [[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located -done -SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" - -# Write the list of utility files to be sourced -FILES=" - sharedVariables.sh - sharedFunctions.sh - setupScriptFunctions.sh -" - -# Source the Utility Files -for file in $FILES -do - if [ -f "${SOURCEPATH}/${file}" ]; then - source "${SOURCEPATH}/${file}" - else - error "${file} does not exist. Exiting" - Exit 1 - fi -done - - # Logging and Colors # ------------------------------------------------------ # Here we set the colors for our script feedback. @@ -121,6 +86,40 @@ verbose() { fi } + +# Source additional files +# ------------------------------------------------------ +# The list of additional utility files to be sourced +# ------------------------------------------------------ + +# First we locate this script and populate the $SCRIPTPATH variable +# Doing so allows us to source additional files from this utils file. +SOURCE="${BASH_SOURCE[0]}" +while [ -h "${SOURCE}" ]; do # resolve ${SOURCE} until the file is no longer a symlink + DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" + SOURCE="$(readlink "${SOURCE}")" + [[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +SOURCEPATH="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" + +# Write the list of utility files to be sourced +FILES=" + sharedVariables.sh + sharedFunctions.sh + setupScriptFunctions.sh +" + +# Source the Utility Files +for file in $FILES +do + if [ -f "${SOURCEPATH}/${file}" ]; then + source "${SOURCEPATH}/${file}" + else + die "${file} does not exist." + fi +done + + # Notes to self # #################### # This is how you create a variable with multiple lines diff --git a/macControl/moomAtDesk.sh b/macControl/moomAtDesk.sh index 9c96c3b..57d3bb2 100755 --- a/macControl/moomAtDesk.sh +++ b/macControl/moomAtDesk.sh @@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/scriptTemplate.sh b/scriptTemplate.sh index 9bb5769..0b7a9ce 100755 --- a/scriptTemplate.sh +++ b/scriptTemplate.sh @@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "lib/utils.sh" ]; then - source "lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/lib/utils.sh" ]; then + source "${SCRIPTDIR}/lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/casks.sh b/setupScripts/casks.sh index 9faca45..11f9357 100755 --- a/setupScripts/casks.sh +++ b/setupScripts/casks.sh @@ -34,12 +34,14 @@ scriptTemplateVersion="1.0.1" # Version of scriptTemplate.sh # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/dropbox.sh b/setupScripts/dropbox.sh index b830021..4eab428 100755 --- a/setupScripts/dropbox.sh +++ b/setupScripts/dropbox.sh @@ -1,12 +1,14 @@ #!/usr/bin/env bash -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else - echo "You must have utils.sh to run. Exiting." - exit 0 + echo "Please find the file util.sh and add a reference to it in this script. Exiting." + exit 1 fi + # Confirm we have Dropbox hasDropbox diff --git a/setupScripts/fileTest.sh b/setupScripts/fileTest.sh index 6df543e..943a507 100755 --- a/setupScripts/fileTest.sh +++ b/setupScripts/fileTest.sh @@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/homebrew.sh b/setupScripts/homebrew.sh index 7a45117..459738a 100755 --- a/setupScripts/homebrew.sh +++ b/setupScripts/homebrew.sh @@ -32,12 +32,14 @@ scriptTemplateVersion="1.0.1" # Version of scriptTemplate.sh # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/mackup.sh b/setupScripts/mackup.sh index 7a72d35..78755f6 100755 --- a/setupScripts/mackup.sh +++ b/setupScripts/mackup.sh @@ -33,12 +33,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/newMacSetup.sh b/setupScripts/newMacSetup.sh index 47d8e20..78ab80b 100755 --- a/setupScripts/newMacSetup.sh +++ b/setupScripts/newMacSetup.sh @@ -11,13 +11,15 @@ # ################################################## # Source global utilities -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else - echo "You must have utils.sh to run. Exiting." - exit + echo "Please find the file util.sh and add a reference to it in this script. Exiting." + exit 1 fi + seek_confirmation "Do you want to run the Dropbox script to install it first?" if is_confirmed; then if is_file "./dropbox.sh"; then diff --git a/setupScripts/osx.sh b/setupScripts/osx.sh index 11ba021..b089c8d 100755 --- a/setupScripts/osx.sh +++ b/setupScripts/osx.sh @@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/ruby.sh b/setupScripts/ruby.sh index 6874a8c..52b05b6 100755 --- a/setupScripts/ruby.sh +++ b/setupScripts/ruby.sh @@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/setupScripts/ssh.sh b/setupScripts/ssh.sh index a0d8171..54839de 100755 --- a/setupScripts/ssh.sh +++ b/setupScripts/ssh.sh @@ -32,12 +32,14 @@ scriptTemplateVersion="1.1.0" # Version of scriptTemplate.sh that this script is # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely diff --git a/syncScripts/SyncTemplate.sh b/syncScripts/SyncTemplate.sh index 817de12..9545fb0 100755 --- a/syncScripts/SyncTemplate.sh +++ b/syncScripts/SyncTemplate.sh @@ -43,13 +43,15 @@ scriptTemplateVersion="1.0.0" # Version of Script Template # ----------------------------------- # If these can't be found, update the path to the file # ----------------------------------- -if [ -f "../lib/utils.sh" ]; then - source "../lib/utils.sh" +SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +if [ -f "${SCRIPTDIR}/../lib/utils.sh" ]; then + source "${SCRIPTDIR}/../lib/utils.sh" else echo "Please find the file util.sh and add a reference to it in this script. Exiting." exit 1 fi + # trapCleanup Function # ----------------------------------- # Any actions that should be taken if the script is prematurely