-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathswitch.sh
More file actions
executable file
·114 lines (91 loc) · 2.48 KB
/
switch.sh
File metadata and controls
executable file
·114 lines (91 loc) · 2.48 KB
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
#!/usr/bin/env bash
set -euo pipefail
is_sourced=0
if [[ "${BASH_SOURCE[0]}" != "$0" ]]; then
is_sourced=1
fi
die() {
echo "switch.sh: $*" >&2
if ((is_sourced)); then
return 1
fi
exit 1
}
usage() {
cat <<'EOF'
Usage: source ./switch.sh <ruby-version>
./switch.sh <ruby-version>
Switch to the requested Ruby version, clean vendor/bundle, and reinstall gems.
EOF
if ((is_sourced)); then
return 0
fi
exit 0
}
if [[ $# -lt 1 || "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
fi
ruby_version="$1"
run_with_nounset_disabled() {
local had_nounset=0
case $- in
*u*) had_nounset=1 ;;
esac
set +u
"$@"
local status=$?
if ((had_nounset)); then
set -u
fi
return "$status"
}
if ! command -v rvm >/dev/null 2>&1; then
if [[ -s "$HOME/.rvm/scripts/rvm" ]]; then
# shellcheck source=/dev/null
run_with_nounset_disabled source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/etc/profile.d/rvm.sh" ]]; then
# shellcheck source=/dev/null
run_with_nounset_disabled source "/etc/profile.d/rvm.sh"
else
die "rvm not found; run inside an RVM shell or install RVM."
fi
fi
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$root_dir"
if [[ ! -f "Gemfile" ]]; then
die "Gemfile not found in $root_dir."
fi
run_with_nounset_disabled rvm use "$ruby_version" --install
bundler_version=""
if [[ -f "Gemfile.lock" ]]; then
bundler_version="$(awk 'found && NF {print $1; exit} /BUNDLED WITH/{found=1}' Gemfile.lock)"
fi
cpu_count() {
if command -v nproc >/dev/null 2>&1; then
nproc
elif command -v sysctl >/dev/null 2>&1; then
sysctl -n hw.ncpu 2>/dev/null || echo 1
else
echo 1
fi
}
jobs="$(cpu_count)"
bundle_cmd=(bundle)
if [[ -n "$bundler_version" ]]; then
if ! gem list -i bundler -v "$bundler_version" >/dev/null 2>&1; then
gem install bundler -v "$bundler_version"
fi
bundle_cmd=(bundle "_${bundler_version}_")
fi
rm -rf vendor/bundle
"${bundle_cmd[@]}" config set --local path vendor/bundle
if [[ -n "$jobs" && "$jobs" -gt 1 ]]; then
"${bundle_cmd[@]}" config set --local jobs "$jobs"
fi
MAKEFLAGS="-j${jobs}" BUNDLE_FORCE_RUBY_PLATFORM=true "${bundle_cmd[@]}" install
MAKEFLAGS="-j${jobs}" "${bundle_cmd[@]}" pristine json
echo "Ruby: $(ruby -v)"
"${bundle_cmd[@]}" exec ruby -e 'require "json"; puts "JSON: #{JSON::VERSION}"; p JSON::Ext::ParserConfig'
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
echo "Note: run 'source ./switch.sh $ruby_version' to keep the Ruby selection in your current shell."
fi