Debian Codegraph @

bin/rbenv-install

raw
1 #!/usr/bin/env bash
2 #
3 # Summary: Install a Ruby version using ruby-build
4 #
5 # Usage: rbenv install [-f|-s] [-kpv] <version> [-- <configure-args...>]
6 # rbenv install [-f|-s] [-kpv] <definition-file>
7 # rbenv install --list
8 # rbenv install --version
9 #
10 # -l, --list List latest stable versions for each Ruby
11 # -L, --list-all List all local versions, including outdated ones
12 # -f, --force Allow overwriting an existing installed version
13 # -s, --skip-existing Avoid overwriting an existing installed version
14 #
15 # ruby-build options:
16 #
17 # -v, --verbose Verbose mode: forward all build output to stdout/stderr
18 # -p, --patch Apply a patch from stdin before building
19 # -k, --keep Keep source tree in RBENV_BUILD_ROOT after installation
20 # (defaults to "RBENV_ROOT/sources")
21 # --version Show version of ruby-build
22 #
23 # For detailed information on installing Ruby versions with ruby-build,
24 # including a list of environment variables for adjusting compilation,
25 # see: https://github.com/rbenv/ruby-build#usage
26 #
27 set -e
28 [ -n "$RBENV_DEBUG" ] && set -x
29
30 # Add `share/ruby-build/` directory from each rbenv plugin to the list of
31 # paths where build definitions are looked up.
32 shopt -s nullglob
33 for plugin_path in "$RBENV_ROOT"/plugins/*/share/ruby-build; do
34 RUBY_BUILD_DEFINITIONS="${RUBY_BUILD_DEFINITIONS}:${plugin_path}"
35 done
36 export RUBY_BUILD_DEFINITIONS
37 shopt -u nullglob
38
39 # Provide rbenv completions
40 if [ "$1" = "--complete" ]; then
41 echo --list
42 echo --list-all
43 echo --force
44 echo --skip-existing
45 echo --keep
46 echo --patch
47 echo --verbose
48 echo --version
49 exec ruby-build --definitions
50 fi
51
52 # Load shared library functions
53 eval "$(ruby-build --lib)"
54
55 usage() {
56 rbenv-help install 2>/dev/null
57 [ -z "$1" ] || exit "$1"
58 }
59
60 definitions() {
61 local query="$1"
62 ruby-build --definitions | $(type -p ggrep grep | head -1) -F "$query" || true
63 }
64
65 suggest_selecting_global() {
66 # shellcheck disable=SC2155
67 local version_file="$(rbenv-version-file)"
68 [[ "$version_file" != "$RBENV_ROOT"/version || -e "$version_file" ]] && return 0
69 echo
70 colorize 1 "NOTE:"
71 echo -n " to activate this Ruby version as the new default, run: "
72 colorize 33 "rbenv global $VERSION_NAME"
73 echo
74 }
75
76 colorize() {
77 if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
78 else printf "%s" "$2"
79 fi
80 }
81
82 indent() {
83 sed 's/^/ /'
84 }
85
86 unset FORCE
87 unset SKIP_EXISTING
88 unset KEEP
89 unset VERBOSE
90 unset HAS_PATCH
91
92 parse_options "$@"
93 for option in "${OPTIONS[@]}"; do
94 case "$option" in
95 "h" | "help" )
96 usage 0
97 ;;
98 "l" | "list" )
99 ruby-build --list
100 [ ! -t 1 ] || {
101 echo
102 echo "Only latest stable releases for each Ruby implementation are shown."
103 echo "Use \`rbenv install --list-all' to show all local versions."
104 } 1>&2
105 exit
106 ;;
107 "L" | "list-all" )
108 ruby-build --definitions
109 exit
110 ;;
111 "f" | "force" )
112 FORCE=true
113 ;;
114 "s" | "skip-existing" )
115 SKIP_EXISTING=true
116 ;;
117 "k" | "keep" )
118 [ -n "${RBENV_BUILD_ROOT}" ] || RBENV_BUILD_ROOT="${RBENV_ROOT}/sources"
119 ;;
120 "v" | "verbose" )
121 VERBOSE="-v"
122 ;;
123 "p" | "patch" )
124 HAS_PATCH="-p"
125 ;;
126 "version" )
127 exec ruby-build --version
128 ;;
129 * )
130 usage 1 >&2
131 ;;
132 esac
133 done
134
135 [ "${#ARGUMENTS[@]}" -le 1 ] || usage 1 >&2
136
137 # The first argument contains the definition to install. If the
138 # argument is missing, try to install whatever local app-specific
139 # version is specified by rbenv. Show usage instructions if a local
140 # version is not specified.
141 DEFINITION="${ARGUMENTS[0]}"
142 [ -n "$DEFINITION" ] || DEFINITION="$(rbenv-local 2>/dev/null || true)"
143 [ -n "$DEFINITION" ] || usage 1 >&2
144
145 if VERSION_NAME="$(ruby-build --resolve "$DEFINITION")"; then
146 DEFINITION="$VERSION_NAME"
147 else
148 unset VERSION_NAME
149 fi
150
151 # Define `before_install` and `after_install` functions that allow
152 # plugin hooks to register a string of code for execution before or
153 # after the installation process.
154 declare -a before_hooks after_hooks
155
156 # shellcheck disable=SC2317
157 before_install() {
158 local hook="$1"
159 before_hooks["${#before_hooks[@]}"]="$hook"
160 }
161
162 # shellcheck disable=SC2317
163 after_install() {
164 local hook="$1"
165 after_hooks["${#after_hooks[@]}"]="$hook"
166 }
167
168 IFS=$'\n' read -d '' -r -a scripts <<<"$(rbenv-hooks install)" || true
169 # shellcheck disable=SC1090
170 for script in "${scripts[@]}"; do source "$script"; done
171
172
173 # Set VERSION_NAME from $DEFINITION, if it is not already set. Then
174 # compute the installation prefix.
175 [ -n "$VERSION_NAME" ] || VERSION_NAME="${DEFINITION##*/}"
176 PREFIX="${RBENV_ROOT}/versions/${VERSION_NAME}"
177
178 [ -d "${PREFIX}" ] && PREFIX_EXISTS=1
179
180 # If the installation prefix exists, prompt for confirmation unless
181 # the --force option was specified.
182 if [ -d "${PREFIX}/bin" ]; then
183 if [ -z "$FORCE" ] && [ -z "$SKIP_EXISTING" ]; then
184 echo "rbenv: $PREFIX already exists" >&2
185 if [ ! -t 0 ]; then
186 echo "rbenv: must use \`--force' or \`--skip-existing' in non-interactive mode" >&2
187 exit 1
188 fi
189 read -rp "continue with installation? (y/N) "
190
191 case "$REPLY" in
192 y* | Y* ) ;;
193 * ) exit 1 ;;
194 esac
195 elif [ -n "$SKIP_EXISTING" ]; then
196 # Since we know the ruby version is already installed, and are opting to
197 # not force installation of existing versions, we just `exit 0` here to
198 # leave things happy
199 exit 0
200 fi
201 fi
202
203 # If RBENV_BUILD_ROOT is set, always pass keep options to ruby-build.
204 if [ -n "${RBENV_BUILD_ROOT}" ]; then
205 export RUBY_BUILD_BUILD_PATH="${RBENV_BUILD_ROOT}/${VERSION_NAME}"
206 KEEP="-k"
207 fi
208
209 # Set RUBY_BUILD_CACHE_PATH to $RBENV_ROOT/cache, if the directory
210 # exists and the variable is not already set.
211 if [ -z "${RUBY_BUILD_CACHE_PATH}" ] && [ -d "${RBENV_ROOT}/cache" ]; then
212 export RUBY_BUILD_CACHE_PATH="${RBENV_ROOT}/cache"
213 fi
214
215 # Default RBENV_VERSION to the globally-specified Ruby version. (The
216 # REE installer requires an existing Ruby installation to run. An
217 # unsatisfied local .ruby-version file can cause the installer to
218 # fail.)
219 # shellcheck disable=SC2155
220 export RBENV_VERSION="$(rbenv-global 2>/dev/null || true)"
221
222
223 # Execute `before_install` hooks.
224 for hook in "${before_hooks[@]}"; do eval "$hook"; done
225
226 # Plan cleanup on unsuccessful installation.
227 cleanup() {
228 [ -z "${PREFIX_EXISTS}" ] && rm -rf "$PREFIX"
229 }
230
231 trap cleanup SIGINT
232
233 build_args=(${KEEP:+--keep} ${VERBOSE:+--verbose} ${HAS_PATCH:+--patch} "$DEFINITION" "$PREFIX")
234 [ ${#EXTRA_ARGUMENTS[@]} -eq 0 ] || build_args+=(-- "${EXTRA_ARGUMENTS[@]}")
235
236 # Invoke `ruby-build` and record the exit status in $STATUS.
237 STATUS=0
238 ruby-build "${build_args[@]}" || STATUS="$?"
239
240 # Display a more helpful message if the definition wasn't found.
241 if [ "$STATUS" == "2" ]; then
242 { candidates="$(definitions "$DEFINITION")"
243 here="$(dirname "${0%/*}")"
244 if [ -n "$candidates" ]; then
245 echo
246 echo "The following versions contain \`$DEFINITION' in the name:"
247 echo "$candidates" | indent
248 fi
249 echo
250 echo "See all available versions with \`rbenv install --list-all'."
251 echo
252 echo -n "If the version you need is missing, try upgrading ruby-build"
253 if [ "$here" != "${here#"$(brew --prefix 2>/dev/null)"}" ]; then
254 printf ":\n\n"
255 echo " brew upgrade ruby-build"
256 elif [ -d "${here}/.git" ]; then
257 display_here="$here"
258 [[ -z "${HOME%/}" || $here != "${HOME}/"* ]] || display_here="~${here#"$HOME"}"
259 printf ":\n\n"
260 echo " git -C $display_here pull"
261 else
262 printf ".\n"
263 fi
264 } >&2
265 fi
266
267 # Execute `after_install` hooks.
268 for hook in "${after_hooks[@]}"; do eval "$hook"; done
269
270 # Run `rbenv-rehash` after a successful installation.
271 if [ "$STATUS" == "0" ]; then
272 rbenv-rehash
273 suggest_selecting_global
274 else
275 cleanup
276 fi
277
278 exit "$STATUS"