-
Notifications
You must be signed in to change notification settings - Fork 1
/
basepath.sh
214 lines (180 loc) · 7.25 KB
/
basepath.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# The below functions were taken from a Stack Exchange answer, and with
# inspiration from some other sources, adapted to work on arbitrary variables.
#
# @see https://unix.stackexchange.com/a/480523
# @see https://linuxfromscratch.ru/blfs/view/svn/postlfs/profile.html
# @see https://misterpinchy.wordpress.com/2012/10/16/indirect-variable-references-in-bash-and-zsh/
# string +variable.value(str variable)
#
# Print the value of the variable whose name is passed to this function.
# Basically a more portable way of resolving indirect references.
#
# Works in Bash and Zsh.
+variable.value() {
# Extract the variable from the function argument.
local variable="${1}"
# If the variable doesn't exist, just return immediately.
if [[ ! -v ${variable} ]]; then
return 0;
fi
# Expand the variable based on whether the current shell is Bash or Zsh.
if [[ "$BASH_VERSION" ]]; then
echo "${!variable}"
elif [[ "$ZSH_VERSION" ]]; then
echo "${(P)pathvariable}"
fi
}
# void +path.append(str pathvariable, str dirname, ...)
#
# Append each passed existing directory to the current user's $$pathvariable in
# a safe manner silently ignoring:
#
# * Relative directories (*NOT* prefixed by the directory separator).
# * Duplicate directories (already listed in the current ${!pathvariable}).
# * Non-existent directories.
+path.append() {
# Extract the path variable into which we're injecting the other paths.
local pathvariable="${1}"
# If the variable exists in the environment, start building the new value
# with its current value, otherwise start with an empty string.
if [[ -v ${pathvariable} ]]; then
local currentpath="$(+variable.value ${pathvariable})"
else
local currentpath=""
fi;
# Shift the path variable from the argument list, leaving only the paths
# which need to be appended.
shift
# For each passed dirname:
local dirname
for dirname in "${@}"; do
# Strip the trailing directory separator if any from this dirname,
# reducing this dirname to the canonical form expected by the
# test for uniqueness performed below.
dirname="${dirname%/}"
# If this dirname is either relative, duplicate, or non-existent, then
# silently ignore this dirname and continue to the next. Note that the
# existency test is the least performant test and hence deferred.
[[ "${dirname:0:1}" == '/' &&
":${currentpath}:" != *":${dirname}:"* &&
-d "${dirname}" ]] || continue
# Else, this is an existing absolute unique dirname. In this case,
# append this dirname to the current ${PATH}.
currentpath="${currentpath}:${dirname}"
done
# Strip an erroneously leading delimiter from the final value, if any, a
# common edge case when the initial value is the empty string, and export
# the new variable to subprocesses.
export $pathvariable="${currentpath#:}"
}
# void +path.prepend(str pathvariable, str dirname, ...)
#
# Prepend each passed existing directory to the current user's ${!pathvariable}
# in a safe manner silently ignoring:
#
# * Relative directories (*NOT* prefixed by the directory separator).
# * Duplicate directories (already listed in the current ${!pathvariable}).
# * Non-existent directories.
+path.prepend() {
# Extract the path variable into which we're injecting the other paths.
local pathvariable="${1}"
# If the variable exists in the environment, start building the new value
# with its current value, otherwise start with an empty string.
if [[ -v ${pathvariable} ]]; then
local currentpath="$(+variable.value ${pathvariable})"
else
local currentpath=""
fi;
# Shift the path variable from the argument list, leaving only the paths
# which need to be appended.
shift
# For each passed dirname:
local dirname
for dirname in "${@}"; do
# Strip the trailing directory separator if any from this dirname,
# reducing this dirname to the canonical form expected by the
# test for uniqueness performed below.
dirname="${dirname%/}"
# If this dirname is either relative, duplicate, or non-existent, then
# silently ignore this dirname and continue to the next. Note that the
# existency test is the least performant test and hence deferred.
[[ "${dirname:0:1}" == '/' &&
":${currentpath}:" != *":${dirname}:"* &&
-d "${dirname}" ]] || continue
# Else, this is an existing absolute unique dirname. In this case,
# append this dirname to the current ${PATH}.
currentpath="${dirname}:${currentpath}"
done
# Strip an erroneously trailing delimiter from the final value, if any, a
# common edge case when the initial value is the empty string, and export
# the new variable to subprocesses.
export $pathvariable="${currentpath%:}"
}
# This is a default path configuration which should be loaded before all the
# other scripts, to make sure they have access to everything they might need.
PATH=""
GOPATH=""
MANPATH=""
INFOPATH=""
# Default path configuration - system programs should always be first for
# security purposes. This could all be a one-liner, in fact the whole file
# could, but it's more readable and maintainable this way.
+path.append "PATH" "/usr/local/sbin"
+path.append "PATH" "/usr/local/bin"
+path.append "PATH" "/usr/sbin"
+path.append "PATH" "/usr/bin"
+path.append "PATH" "/sbin"
+path.append "PATH" "/bin"
+path.append "PATH" "/usr/local/games"
+path.append "PATH" "/usr/games"
# Dotfiles-bundled programs.
+path.append "PATH" "${DOTFILES}/bin"
# User-installed programs.
+path.append "PATH" "${HOME}/bin"
+path.append "PATH" "${HOME}/.local/bin"
# Composer-installed programs.
+path.append "PATH" "${HOME}/.composer/vendor/bin"
# Yarn-installed programs.
+path.append "PATH" "${HOME}/.yarn/bin"
# npm-installed programs.
+path.append "PATH" "${HOME}/.npm/bin"
# Python 2.7-installed programs.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Do nothing on Linux.
elif [[ "$OSTYPE" == "darwin"* ]]; then
+path.append "PATH" "${HOME}/Library/Python/2.7/bin"
fi
# Heroku Toolbelt programs.
+path.append "PATH" "/usr/local/heroku/bin"
# Cabal-installed programs.
+path.append "PATH" "${HOME}/.cabal/bin"
# Go path configuration and programs.
+path.append "GOPATH" "${HOME}/.go"
+path.append "PATH" "${GOPATH}/bin"
# Homebrew.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
+path.append "PATH" "/home/linuxbrew/.linuxbrew/bin"
+path.append "PATH" "/home/linuxbrew/.linuxbrew/sbin"
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Do nothing on macOS.
fi
# Default MANPATH configuration.
+path.append "MANPATH" "/usr/share/man"
# Add Homebrew to MANPATH.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
+path.append "MANPATH" "/home/linuxbrew/.linuxbrew/share/man"
elif [[ "$OSTYPE" == "darwin"* ]]; then
+path.append "MANPATH" "/usr/local/share/man"
fi
# Default INFOPATH configuration.
+path.append "INFOPATH" "/usr/share/info"
# Add Homebrew to INFOPATH
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
+path.append "INFOPATH" "/home/linuxbrew/.linuxbrew/share/info"
elif [[ "$OSTYPE" == "darwin"* ]]; then
+path.append "INFOPATH" "/usr/local/share/info"
fi
export PATH
export GOPATH
export MANPATH
export INFOPATH