Skip to content

Commit b5de82e

Browse files
committed
Refactor the Ruby feature to use functions
This will make easier to add other version managers in the future.
1 parent ac09857 commit b5de82e

File tree

3 files changed

+74
-25
lines changed

3 files changed

+74
-25
lines changed

features/src/ruby/devcontainer-feature.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
]
1111
}
1212
},
13-
"containerEnv": {
14-
"PATH": "/usr/local/share/rbenv/bin:${PATH}"
15-
},
1613
"installsAfter": [
1714
"ghcr.io/devcontainers/features/common-utils"
1815
],

features/src/ruby/install.sh

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,86 @@ set -e
33

44
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
55

6-
apt-get update -y
7-
apt-get -y install --no-install-recommends git curl ca-certificates libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential \
8-
libyaml-dev libncurses5-dev libffi-dev libgdbm-dev libxml2-dev rustc
6+
# Function to install dependencies needed for building Ruby
7+
install_dependencies() {
8+
apt-get update -y
9+
DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \
10+
git \
11+
curl \
12+
ca-certificates \
13+
libssl-dev \
14+
libreadline-dev \
15+
zlib1g-dev \
16+
autoconf \
17+
bison \
18+
build-essential \
19+
libyaml-dev \
20+
libncurses5-dev \
21+
libffi-dev \
22+
libgdbm-dev \
23+
libxml2-dev \
24+
rustc
25+
}
926

10-
git clone https://github.com/rbenv/rbenv.git /usr/local/share/rbenv
11-
git clone https://github.com/rbenv/ruby-build.git /usr/local/share/ruby-build
27+
# Function to add lines to shell initialization files
28+
add_to_shell_init() {
29+
_user="$1"
30+
_bash_line="$2"
31+
_zsh_line="${3:-$_bash_line}" # Use bash_line as default if zsh_line not provided
1232

13-
mkdir -p /root/.rbenv/plugins
14-
ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build
33+
if [ "$_user" = "root" ]; then
34+
_home_dir="/root"
35+
else
36+
_home_dir="/home/$_user"
37+
fi
1538

16-
if [ "${USERNAME}" != "root" ]; then
17-
user_home="/home/${USERNAME}"
18-
mkdir -p "${user_home}/.rbenv/plugins"
19-
ln -s /usr/local/share/ruby-build "${user_home}/.rbenv/plugins/ruby-build"
39+
echo "$_bash_line" >> "$_home_dir/.bashrc"
2040

21-
chown -R "${USERNAME}" "${user_home}/.rbenv/"
22-
chmod -R g+r+w "${user_home}/.rbenv"
41+
if [ -f "$_home_dir/.zshrc" ]; then
42+
echo "$_zsh_line" >> "$_home_dir/.zshrc"
43+
fi
44+
}
2345

24-
# shellcheck disable=SC2016
25-
echo 'eval "$(rbenv init -)"' >> "${user_home}/.bashrc"
46+
# Function to setup rbenv
47+
setup_rbenv() {
48+
_user="$1"
49+
_user_home="/home/$_user"
2650

27-
if [ -f "${user_home}/.zshrc" ]; then
28-
# shellcheck disable=SC2016
29-
echo 'eval "$(rbenv init -)"' >> "${user_home}/.zshrc"
51+
# Clone rbenv and ruby-build
52+
git clone https://github.com/rbenv/rbenv.git /usr/local/share/rbenv
53+
git clone https://github.com/rbenv/ruby-build.git /usr/local/share/ruby-build
54+
55+
# Setup plugins for root
56+
mkdir -p /root/.rbenv/plugins
57+
ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build
58+
59+
# Setup for non-root user if needed
60+
if [ "$_user" != "root" ]; then
61+
mkdir -p "$_user_home/.rbenv/plugins"
62+
ln -s /usr/local/share/ruby-build "$_user_home/.rbenv/plugins/ruby-build"
63+
chown -R "$_user" "$_user_home/.rbenv/"
64+
chmod -R g+r+w "$_user_home/.rbenv"
3065
fi
31-
fi
3266

33-
su "${USERNAME}" -c "/usr/local/share/rbenv/bin/rbenv install $VERSION"
34-
su "${USERNAME}" -c "/usr/local/share/rbenv/bin/rbenv global $VERSION"
67+
# shellcheck disable=SC2016
68+
add_to_shell_init "$_user" 'export PATH="/usr/local/share/rbenv/bin:$PATH"'
69+
# shellcheck disable=SC2016
70+
add_to_shell_init "$_user" 'eval "$(rbenv init -)"'
71+
}
72+
73+
# Function to install Ruby with rbenv
74+
install_ruby_rbenv() {
75+
_user="$1"
76+
_version="$2"
77+
78+
su "$_user" -c "/usr/local/share/rbenv/bin/rbenv install $_version"
79+
su "$_user" -c "/usr/local/share/rbenv/bin/rbenv global $_version"
80+
}
81+
82+
install_dependencies
83+
84+
setup_rbenv "${USERNAME}"
85+
86+
install_ruby_rbenv "$USERNAME" "$VERSION"
3587

3688
rm -rf /var/lib/apt/lists/*

images/ruby/.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"ghcr.io/rails/devcontainer/features/ruby": {
1919
"version": "${localEnv:RUBY_VERSION}"
20-
},
20+
}
2121
},
2222
// Set `remoteUser` to `root` to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
2323
"remoteUser": "vscode"

0 commit comments

Comments
 (0)