Skip to content

Commit

Permalink
More reliable way to find relative paths to .dotfiles folder in diffe…
Browse files Browse the repository at this point in the history
…rent shells
  • Loading branch information
duzun committed Sep 15, 2024
1 parent 7abdde9 commit 37573a9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
9 changes: 8 additions & 1 deletion .source
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

# source only once
if ! command -v init_dotfiles >/dev/null 2>&1; then
_profile=${BASH_SOURCE:-$0}
if [ -n "$ZSH_NAME" ] || [ -n "$ZSH_VERSION" ]; then
_profile="${(%):-%N}"
elif [ -n "$BASH" ]; then
_profile=${BASH_SOURCE:-$0}
else
_profile=$(lsof -p $$ 2> /dev/null | grep "\b$$\b" | tail -1 | tr ' \t' '\n' | tail -1)
fi

# ------------------------------------------------------------------------------
if ! command -v realpath >/dev/null; then
_realpath() {
Expand Down
20 changes: 16 additions & 4 deletions ~/.aliasrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
# Functions & Aliases #
#######################

# ------------------------------------------------------------------------------
_alias=$(realpath "${BASH_SOURCE:-$0}")
_dotfiles=$(dirname "$_alias")

# ------------------------------------------------------------------------------
# Detect OS type
ostype() {
Expand Down Expand Up @@ -1069,3 +1065,19 @@ composerbin() {
}

# ------------------------------------------------------------------------------
case $(shelltype) in
bash) _alias="${BASH_SOURCE}" ;; # if empty, not sourced -> don't care about $_alias
zsh) _alias="${(%):-%N}" ;;
# the best we have in the general case
busybox|*)
# BusyBox doesn't respect -p, thus use grep
_alias="$(lsof -p $$ 2> /dev/null | grep "\b$$\b" | tail -1 | tr ' \t' '\n' | tail -1)"
;;
esac

if [ -n "$_alias" ]; then
_alias=$(realpath "$_alias")
_dotfiles=$(dirname "$_alias")
fi

# ------------------------------------------------------------------------------
8 changes: 7 additions & 1 deletion ~/.profile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# This file name is deprecated in favour of dotfiles/~/source (which is symlinked to ~/.source)

_profile=${BASH_SOURCE:-$0}
if [ -n "$ZSH_NAME" ] || [ -n "$ZSH_VERSION" ]; then
_profile="${(%):-%N}"
elif [ -n "$BASH" ]; then
_profile=${BASH_SOURCE:-$0}
else
_profile=$(lsof -p $$ 2> /dev/null | grep "\b$$\b" | tail -1 | tr ' \t' '\n' | tail -1)
fi
# ------------------------------------------------------------------------------
if ! command -v realpath > /dev/null; then
_realpath() {
Expand Down
32 changes: 20 additions & 12 deletions ~/source
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#!/usr/bin/env bash
###################################
# DUzun's dotfiles main source #
# @version 2.3.3 #
# @version 2.4.0 #
# @author Dumitru Uzun (DUzun.Me) #
###################################

# ------------------------------------------------------------------------------
# echo .: ${BASH_SOURCE[@]}
# if [ -z "$BASH_SOURCE" ]; then local BASH_SOURCE=$_; fi
if [ -n "$ZSH_NAME" ] || [ -n "$ZSH_VERSION" ]; then
_profile="${(%):-%N}"
elif [ -n "$BASH" ]; then
_profile=${BASH_SOURCE:-$0}
else
_profile=$(lsof -p $$ 2> /dev/null | grep "\b$$\b" | tail -1 | tr ' \t' '\n' | tail -1)
fi

_profile=${BASH_SOURCE:-$0}
# ------------------------------------------------------------------------------
if ! command -v realpath >/dev/null; then
_realpath() {
Expand All @@ -36,24 +40,28 @@ fi
_profile=$(realpath "$_profile")
_dotfiles=$(dirname "$_profile")

# ------------------------------------------------------------------------------
. "$_dotfiles/.aliasrc"
# ------------------------------------------------------------------------------

# non-POSIX section bellow!

declare -l _os
declare -l _shell

_os=$(ostype)
_shell=$(shelltype)

_git_aliases_complete=()
_aliases_complete=(
dfh du1 du0
dcs dcsb dcsu dcsd
ccur
)

declare -l _os
declare -l _shell

# ------------------------------------------------------------------------------
. "$_dotfiles/.aliasrc"
. "$_dotfiles/.aliasrc.dotfiles"

# ------------------------------------------------------------------------------
_os=$(ostype)
_shell=$(shelltype)

# ------------------------------------------------------------------------------
_init_commands() {
[ -d "$1" ] || return 1
Expand Down

0 comments on commit 37573a9

Please sign in to comment.