avoid double call when .profile source .bashrc and vice-versa
This commit is contained in:
38
profile.sh
38
profile.sh
@@ -73,6 +73,29 @@ _profile_install_in_file()
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Detect whether $rc_file sources the file whose basename is $target_name.
|
||||||
|
# Returns 0 (true) if a sourcing statement referencing $target_name is found,
|
||||||
|
# ignoring commented-out lines.
|
||||||
|
_profile_file_sources()
|
||||||
|
{
|
||||||
|
local rc_file="$1"
|
||||||
|
local target_name="$2"
|
||||||
|
local line
|
||||||
|
|
||||||
|
[[ -f "$rc_file" ]] || return 1
|
||||||
|
|
||||||
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||||
|
# Strip comments so commented-out source lines are not matched.
|
||||||
|
line="${line%%#*}"
|
||||||
|
# Keep only lines whose first word is a source command (`source` or `.`).
|
||||||
|
[[ "$line" =~ ^[[:space:]]*(source|\.)[[:space:]] ]] || continue
|
||||||
|
# Match if the sourcing statement references the target file.
|
||||||
|
[[ "$line" == *"$target_name"* ]] && return 0
|
||||||
|
done < "$rc_file"
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
_profile_install()
|
_profile_install()
|
||||||
{
|
{
|
||||||
local install_bashrc=0
|
local install_bashrc=0
|
||||||
@@ -108,6 +131,19 @@ _profile_install()
|
|||||||
install_profile=1
|
install_profile=1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Avoid double loading when both targets are selected and one already
|
||||||
|
# sources the other: only configure the called file so profile.sh is
|
||||||
|
# loaded exactly once.
|
||||||
|
if (( install_bashrc == 1 && install_profile == 1 )); then
|
||||||
|
if _profile_file_sources "$HOME/.bashrc" ".profile"; then
|
||||||
|
printf "[ Info ] ~/.bashrc sources ~/.profile; configuring only ~/.profile to avoid double loading.\n"
|
||||||
|
install_bashrc=0
|
||||||
|
elif _profile_file_sources "$HOME/.profile" ".bashrc"; then
|
||||||
|
printf "[ Info ] ~/.profile sources ~/.bashrc; configuring only ~/.bashrc to avoid double loading.\n"
|
||||||
|
install_profile=0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
script_dir=$(dirname "$(realpath -s "${BASH_SOURCE[0]}")")
|
script_dir=$(dirname "$(realpath -s "${BASH_SOURCE[0]}")")
|
||||||
source_line="source \"$script_dir/profile.sh\""
|
source_line="source \"$script_dir/profile.sh\""
|
||||||
|
|
||||||
@@ -409,7 +445,7 @@ if [[ $INTERACTIVE ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
unset -f _profile_is_sourced _profile_finish _profile_install_in_file _profile_install
|
unset -f _profile_is_sourced _profile_finish _profile_install_in_file _profile_file_sources _profile_install
|
||||||
unset -f parse_conf load_alias load_conf
|
unset -f parse_conf load_alias load_conf
|
||||||
unset -f pathremove pathprepend pathappend
|
unset -f pathremove pathprepend pathappend
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user