mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-17 21:54:24 +00:00
refactor(path): consolidate x-path-{append,prepend,remove} as thin wrappers
Add source guard to x-path so its functions can be loaded without executing the main logic. Rewrite standalone path scripts to source x-path and call the appropriate function directly, eliminating code duplication while preserving source-ability for shell integration.
This commit is contained in:
@@ -227,6 +227,9 @@ do_check()
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# If sourced, provide functions without executing main logic
|
||||||
|
(return 0 2> /dev/null) && return
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
# Main routine: Parse subcommand and arguments, normalize PATH,
|
# Main routine: Parse subcommand and arguments, normalize PATH,
|
||||||
# and dispatch to the appropriate functionality.
|
# and dispatch to the appropriate functionality.
|
||||||
|
|||||||
@@ -1,44 +1,17 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#
|
#
|
||||||
# Optimized script to append directories to PATH.
|
# Thin wrapper — delegates to x-path append.
|
||||||
# For each given directory, it removes all duplicate occurrences from PATH
|
# Can be sourced (PATH changes propagate) or executed.
|
||||||
# and then appends it if the directory exists.
|
|
||||||
#
|
#
|
||||||
# Usage: x-path-append <directory1> [<directory2> ...]
|
# Usage: x-path-append <directory1> [<directory2> ...]
|
||||||
#
|
#
|
||||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
|
||||||
#
|
|
||||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||||
# License: MIT
|
# License: MIT
|
||||||
|
|
||||||
VERBOSE="${VERBOSE:-0}"
|
VERBOSE="${VERBOSE:-0}"
|
||||||
|
|
||||||
# Ensure that at least one directory is provided.
|
# shellcheck source=./x-path
|
||||||
[ "$#" -lt 1 ] && {
|
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||||
echo "Usage: $0 <directory> [<directory> ...]"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for dir in "$@"; do
|
normalize_path_var
|
||||||
# Check if the specified directory exists.
|
do_append "$@"
|
||||||
if [ ! -d "$dir" ]; then
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "(?) Directory '$dir' does not exist. Skipping."
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Remove all duplicate occurrences of the directory from PATH.
|
|
||||||
case ":$PATH:" in
|
|
||||||
*":$dir:"*)
|
|
||||||
PATH=":${PATH}:"
|
|
||||||
PATH="${PATH//:$dir:/:}"
|
|
||||||
PATH="${PATH#:}"
|
|
||||||
PATH="${PATH%:}"
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "Removed previous occurrences of '$dir' from PATH."
|
|
||||||
;;
|
|
||||||
*) ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Append the directory to PATH.
|
|
||||||
export PATH="${PATH:+$PATH:}$dir"
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "Appended '$dir' to PATH."
|
|
||||||
done
|
|
||||||
|
|||||||
@@ -1,50 +1,17 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#
|
#
|
||||||
# Optimized script to batch prepend directories to PATH.
|
# Thin wrapper — delegates to x-path prepend.
|
||||||
# For each given directory, it removes all duplicate occurrences from PATH
|
# Can be sourced (PATH changes propagate) or executed.
|
||||||
# and then prepends it. Directories that do not exist are skipped.
|
|
||||||
#
|
#
|
||||||
# Usage: x-path-prepend <directory1> [<directory2> ...]
|
# Usage: x-path-prepend <directory1> [<directory2> ...]
|
||||||
#
|
#
|
||||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
|
||||||
#
|
|
||||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||||
# License: MIT
|
# License: MIT
|
||||||
|
|
||||||
VERBOSE="${VERBOSE:-0}"
|
VERBOSE="${VERBOSE:-0}"
|
||||||
|
|
||||||
# Ensure that at least one argument is provided.
|
# shellcheck source=./x-path
|
||||||
[ "$#" -lt 1 ] && {
|
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||||
echo "Usage: $0 <directory> [<directory> ...]"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Save the arguments in an array.
|
normalize_path_var
|
||||||
dirs=("$@")
|
do_prepend "$@"
|
||||||
|
|
||||||
# Process the directories in reverse order so that the first argument ends up leftmost in PATH.
|
|
||||||
for ((idx = ${#dirs[@]} - 1; idx >= 0; idx--)); do
|
|
||||||
dir="${dirs[idx]}"
|
|
||||||
|
|
||||||
# Check if the specified directory exists.
|
|
||||||
if [ ! -d "$dir" ]; then
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "(?) Directory '$dir' does not exist. Skipping."
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Remove all duplicate occurrences of the directory from PATH using built-in string operations.
|
|
||||||
case ":$PATH:" in
|
|
||||||
*":$dir:"*)
|
|
||||||
PATH=":${PATH}:"
|
|
||||||
PATH="${PATH//:$dir:/:}"
|
|
||||||
PATH="${PATH#:}"
|
|
||||||
PATH="${PATH%:}"
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "Removed duplicate occurrences of '$dir' from PATH."
|
|
||||||
;;
|
|
||||||
*) ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Prepend the directory to PATH.
|
|
||||||
export PATH="$dir${PATH:+":$PATH"}"
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "Prepended '$dir' to PATH."
|
|
||||||
done
|
|
||||||
|
|||||||
@@ -1,41 +1,17 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
#
|
#
|
||||||
# Optimized script to remove directories from PATH.
|
# Thin wrapper — delegates to x-path remove.
|
||||||
# For each specified directory, all occurrences are removed from PATH.
|
# Can be sourced (PATH changes propagate) or executed.
|
||||||
#
|
#
|
||||||
# Usage: x-path-remove <directory1> [<directory2> ...]
|
# Usage: x-path-remove <directory1> [<directory2> ...]
|
||||||
#
|
#
|
||||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
|
||||||
#
|
|
||||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||||
# License: MIT
|
# License: MIT
|
||||||
|
|
||||||
VERBOSE="${VERBOSE:-0}"
|
VERBOSE="${VERBOSE:-0}"
|
||||||
|
|
||||||
# Ensure that at least one directory is provided.
|
# shellcheck source=./x-path
|
||||||
[ "$#" -lt 1 ] && {
|
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||||
echo "Usage: $0 <directory> [<directory> ...]"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
for dir in "$@"; do
|
normalize_path_var
|
||||||
# Remove trailing slash if present, unless the directory is "/"
|
do_remove "$@"
|
||||||
[ "$dir" != "/" ] && dir="${dir%/}"
|
|
||||||
|
|
||||||
# Check if the directory is present in PATH.
|
|
||||||
case ":$PATH:" in
|
|
||||||
*":$dir:"*)
|
|
||||||
# Remove all occurrences of the directory from PATH using parameter expansion.
|
|
||||||
PATH=":${PATH}:"
|
|
||||||
PATH="${PATH//:$dir:/:}"
|
|
||||||
PATH="${PATH#:}"
|
|
||||||
PATH="${PATH%:}"
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "Removed '$dir' from PATH."
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
[ "$VERBOSE" -eq 1 ] && echo "(?) '$dir' is not in PATH."
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
export PATH
|
|
||||||
|
|||||||
Reference in New Issue
Block a user