blob: ad313103dad2fcc36f5492d75b637f54ef916a71 (
plain) (
blame)
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
|
#!/usr/bin/env bash
#
# Copyright 2014 David Vazgenovich Shakaryan
# Distributed under the terms of the MIT License.
#
# rpath
# https://github.com/omp/rpath
# Manage multiple Ruby installations with no black magic.
#
# In order to prevent environment pollution, the output of this script
# is a list of commands which must be sourced. This can be achieved
# through a simple shell function:
#
# rpath() { source <(/path/to/rpath.sh "${@}"); }
#
# Two methods of version matching are supported. If the specified
# argument consists of only numbers, it is matched against the list
# number shown in `rpath ls`. Otherwise, a substring match is performed
# against the directory name.
: ${RUBIES_PATH:="${HOME}/.rubies"}
_echo() {
echo "echo '${@//\'/\'\\\'\'}'"
}
_export() {
echo "export PATH='${PATH//\'/\'\\\'\'}'"
echo "hash -r"
}
_warn() {
echo "echo 'rpath: ${@//\'/\'\\\'\'}' 1>&2"
}
_die() {
[[ -n "${@}" ]] && _warn "${@}"
echo "false"
exit 1
}
_populate_dirs() {
[[ -d "${RUBIES_PATH}" ]] \
|| _die "directory ${RUBIES_PATH} does not exist."
# Cannot handle paths containing a newline. Only an idiot would
# encounter this in practice.
readarray -t dirs < <(shopt -s nullglob; \
printf '%s\0' "${RUBIES_PATH}"/* | sort -zV | xargs -0n1)
[[ -n "${dirs}" ]] || _die "directory ${RUBIES_PATH} is empty."
}
_populate_selected() {
local dir dirs
unset selected
IFS=':' read -a dirs <<< "${PATH}"
for dir in "${dirs[@]}"; do
if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then
dir="${dir%/bin}"
selected+=("${dir##*/}")
fi
done
[[ -n "${selected}" ]] || return 1
[[ "${#selected[@]}" -eq 1 ]] \
|| _warn 'warning: multiple rubies found in PATH.'
}
_clear() {
local dir dirs cdirs
IFS=':' read -a dirs <<< "${PATH}"
for dir in "${dirs[@]}"; do
if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then
_echo "Removing ${dir} from PATH."
else
cdirs+=("${dir}")
fi
done
[[ "${#cdirs[@]}" -ne "${#dirs[@]}" ]] || return 1
PATH="$(IFS=':'; echo "${cdirs[*]}")"
}
_add() {
_echo "Adding ${1}/bin to PATH."
PATH="${1}/bin:${PATH}"
}
_match() {
if [[ "${1}" =~ ^[0-9]+$ ]]; then
[[ "${2}" == "${1}" ]] && return
else
[[ "${3}" == *"${1}"* ]] && return
fi
return 1
}
rpath_ls() {
_populate_dirs
_populate_selected
counter=0
for dir in "${dirs[@]}"; do
str=" [$((++counter))] ${dir##*/}"
[[ "${dir##*/}" == "${selected}" ]] && str="${str/ /*}"
_echo "${str}"
done
}
rpath_get() {
_populate_selected || _die 'no rubies found in PATH.'
for dir in "${selected[@]}"; do
_echo "${dir}"
done
}
rpath_set() {
[[ -n "${1}" ]] || _die 'set command requires an argument.'
_populate_dirs
counter=0
for dir in "${dirs[@]}"; do
if _match "${1}" "$((++counter))" "${dir##*/}"; then
_clear
_add "${dir}"
_export
return
fi
done
_die 'no matching ruby found.'
}
rpath_clear() {
_clear || _die 'no rubies found in PATH.'
_export
}
rpath_help() {
_echo 'Usage: rpath <command> [args]'
_echo
_echo 'Commands:'
_echo ' ls List all available rubies.'
_echo ' get Display currently selected ruby.'
_echo ' set Select specified ruby.'
_echo ' clear Clear path of any rubies.'
_echo ' help Display this help information.'
}
case "${1}" in
'ls')
rpath_ls ;;
'get')
rpath_get ;;
'set')
rpath_set "${2}" ;;
'clear')
rpath_clear ;;
*)
rpath_help ;;
esac
|