-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactivate.sh
90 lines (72 loc) · 2.39 KB
/
activate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
#
# activate.sh
#
# - Sets up the shell environment.
#
# - Dependency functions are defined here for simplicity, rather than in
# separate scripts or ".dot.sh" files.
function ,realpath() {
# Get absolute path without depending on `realpath` or `readlink`, which
# aren't installed on OS X by default (at least on some older versions).
echo "$(cd "$(dirname "$1")" && pwd -P)/$(basename "$1")"
}
function ,path() {
# usage: ,path [--prepend] [<directories>]
#
# Adds current directory or given directories to path. If --prepend not
# given, directories are appended.
#
# Also updates $_OLD_VIRTUAL_PATH set by Python's virtualenv, so that path
# changes persist through a venv deactivate command.
if [[ -z "$1" ]]; then
echo 'PATH: '
IFS=: eval printf "%s\\\n" \$${1:-PATH}
return 0
fi
if [[ $1 = "--prepend" ]]; then
prepend=true
paths=(${@:2})
else
prepend=false
paths=($@)
fi
if [ -z "$paths" ]; then
paths[0]=$PWD
fi
for path in "${paths[@]}"; do
# Ignore paths that don't exist
if [ ! -f "$path" ] && [ ! -d "$path" ]; then
continue
fi
fullpath="$(cd "$path"; echo $PWD)"
if [[ ":$PATH:" != *":$fullpath:"* ]]; then
if [[ "$prepend" = true ]]; then
export PATH="$fullpath:$PATH"
export _OLD_VIRTUAL_PATH="$fullpath:$PATH"
else
export PATH="$PATH:$fullpath"
export _OLD_VIRTUAL_PATH="$PATH:$fullpath"
fi
fi
done
}
function ,dotfiles-ls {
# In numerical order, list paths ending in .dot.xx.sh, then list paths
# ending in .dot.sh.
if [[ $(uname -a) == *Darwin* ]]; then
for f in $(find -E $DOTFILES -regex '.+\.dot\.[0-9][0-9]\.sh' | sort -t "." -k 2); do echo $f; done
else
for f in $(find $DOTFILES -regextype posix-extended -regex '.+\.dot\.[0-9][0-9]\.sh'| sort -t "." -k 2); do echo $f; done
fi
for f in $(find "$DOTFILES" -name "*.dot.sh"); do echo "$f"; done
}
# $DOTFILES represents this directory
THIS_FILE=${BASH_SOURCE[0]}
export DOTFILES=$(dirname "$(,realpath "$THIS_FILE")")
# Append all "bin" directories in $DOTFILES to $PATH
,path $(find $DOTFILES -type d -name bin)
# Source all ".dot.sh" files in $DOTFILES
for f in $(,dotfiles-ls); do
source "$f"
done