Skip to content

Commit 0c8f764

Browse files
authored
Merge pull request #86 from rails/rm-mise
Change ruby feature to use mise by default
2 parents a121479 + e4e1a0d commit 0c8f764

File tree

10 files changed

+205
-50
lines changed

10 files changed

+205
-50
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Rails Dev Container Images & Features
22

3-
This repository contains dev container images and features that can be used to create a convenient and consistent
3+
This repository contains dev container images and features that can be used to create a convenient and consistent
44
development environment for working on Rails applications.
55

66
## What is a dev container?
77

8-
A **dev container** is a running Docker container that provides a fully-featured development environment which can be
9-
used to run an application, to separate tools, libraries, or runtimes needed for working with a codebase, and to aid in
8+
A **dev container** is a running Docker container that provides a fully-featured development environment which can be
9+
used to run an application, to separate tools, libraries, or runtimes needed for working with a codebase, and to aid in
1010
continuous integration and testing. **Dev container features** are self-contained units of installation and configuration
1111
that can be installed on top of a container image to provide additional functionality in a container. **Dev container
1212
images** are prebuilt docker images based on dev container features. For more information on the dev container
@@ -30,8 +30,8 @@ would look like this:
3030
}
3131
```
3232

33-
This dev container uses the Ruby image, which includes an installation of Ruby (in this case version 3.3.0) and the RBenv
34-
version manager, as well as other common utilities such as Git. It also uses the Active Storage feature, which installs
33+
This dev container uses the Ruby image, which includes an installation of Ruby (in this case version 3.3.0) and a Ruby
34+
version manager (mise by default, but configurable to use rbenv), as well as other common utilities such as Git. It also uses the Active Storage feature, which installs
3535
dependencies needed for Active Storage.
3636

3737
The dev container can be initialized [by VSCode](https://code.visualstudio.com/docs/devcontainers/containers) or by using
@@ -50,9 +50,9 @@ This repository is open for contributions. See [the contributions guide](CONTRIB
5050

5151
You can create your own features, and images based on them.
5252

53-
The best place to start is the [feature starter repository](https://github.com/devcontainers/feature-starter) which is
53+
The best place to start is the [feature starter repository](https://github.com/devcontainers/feature-starter) which is
5454
maintained by the devcontainers org.
5555

5656
## License
5757

58-
The repository is released under the [MIT License](https://opensource.org/licenses/MIT).
58+
The repository is released under the [MIT License](https://opensource.org/licenses/MIT).

features/src/ruby/NOTES.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
## OS Support
22

33
This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed.
4+
5+
## Changing the Ruby Version
6+
7+
```json
8+
"features": {
9+
"ghcr.io/rails/devcontainer/features/ruby:1": {
10+
"version": "3.3.0"
11+
}
12+
}
13+
```
14+
15+
## Changing the Version Manager
16+
17+
```json
18+
"features": {
19+
"ghcr.io/rails/devcontainer/features/ruby:1": {
20+
"versionManager": "rbenv"
21+
}
22+
}
23+
```

features/src/ruby/README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Ruby (via rbenv)
1+
# Ruby
22

3-
Installs Ruby, rbenv, and ruby-build as well as the dependencies needed to build Ruby.
3+
Installs Ruby and a version manager (mise or rbenv) along with the dependencies needed to build Ruby.
44

55
## Example Usage
66

7+
### Using mise (default)
8+
79
```json
810
"features": {
911
"ghcr.io/rails/devcontainer/features/ruby:1": {
@@ -12,11 +14,34 @@ Installs Ruby, rbenv, and ruby-build as well as the dependencies needed to build
1214
}
1315
```
1416

17+
### Using rbenv
18+
19+
```json
20+
"features": {
21+
"ghcr.io/rails/devcontainer/features/ruby:1": {
22+
"version": "3.3.0",
23+
"versionManager": "rbenv"
24+
}
25+
}
26+
```
27+
28+
### Using mise explicitly
29+
30+
```json
31+
"features": {
32+
"ghcr.io/rails/devcontainer/features/ruby:1": {
33+
"version": "3.3.0",
34+
"versionManager": "mise"
35+
}
36+
}
37+
```
38+
1539
## Options
1640

17-
| Options Id | Description | Type |
18-
|-----|-----|-----|
19-
| version | The version of ruby to be installed | string |
41+
| Options Id | Description | Type | Default Value |
42+
|-----|-----|-----|-----|
43+
| version | The version of ruby to be installed | string | 3.4.1 |
44+
| versionManager | The version manager to use for Ruby (mise or rbenv) | string | mise |
2045

2146
## Customizations
2247

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
{
22
"id": "ruby",
3-
"version": "1.1.3",
4-
"name": "Ruby (via rbenv)",
5-
"description": "Installs Ruby, rbenv, and ruby-build as well as the dependencies needed to build Ruby.",
3+
"version": "2.0.0",
4+
"name": "Ruby",
5+
"description": "Installs Ruby and a version manager (mise or rbenv) along with libraries needed to build Ruby.",
66
"customizations": {
77
"vscode": {
88
"extensions": [
99
"shopify.ruby-lsp"
1010
]
1111
}
1212
},
13-
"containerEnv": {
14-
"PATH": "/usr/local/share/rbenv/bin:${PATH}"
15-
},
1613
"installsAfter": [
1714
"ghcr.io/devcontainers/features/common-utils"
1815
],
@@ -21,6 +18,15 @@
2118
"type": "string",
2219
"default": "3.4.4",
2320
"description": "The ruby version to be installed"
21+
},
22+
"versionManager": {
23+
"type": "string",
24+
"proposals": [
25+
"mise",
26+
"rbenv"
27+
],
28+
"default": "mise",
29+
"description": "The version manager to use for Ruby (mise or rbenv)"
2430
}
2531
}
2632
}

features/src/ruby/install.sh

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,119 @@
22
set -e
33

44
USERNAME="${USERNAME:-"${_REMOTE_USER:-"automatic"}"}"
5+
VERSION_MANAGER="${VERSIONMANAGER:-"mise"}"
56

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

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
28+
# Function to add lines to shell initialization files
29+
add_to_shell_init() {
30+
_user="$1"
31+
_bash_line="$2"
32+
_zsh_line="${3:-$_bash_line}" # Use bash_line as default if zsh_line not provided
1233

13-
mkdir -p /root/.rbenv/plugins
14-
ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build
34+
if [ "$_user" = "root" ]; then
35+
_home_dir="/root"
36+
else
37+
_home_dir="/home/$_user"
38+
fi
39+
40+
echo "$_bash_line" >> "$_home_dir/.bashrc"
41+
42+
if [ -f "$_home_dir/.zshrc" ]; then
43+
echo "$_zsh_line" >> "$_home_dir/.zshrc"
44+
fi
45+
}
46+
47+
# Function to setup rbenv
48+
setup_rbenv() {
49+
_user="$1"
50+
_user_home="/home/$_user"
1551

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"
52+
# Clone rbenv and ruby-build
53+
git clone https://github.com/rbenv/rbenv.git /usr/local/share/rbenv
54+
git clone https://github.com/rbenv/ruby-build.git /usr/local/share/ruby-build
2055

21-
chown -R "${USERNAME}" "${user_home}/.rbenv/"
22-
chmod -R g+r+w "${user_home}/.rbenv"
56+
# Setup plugins for root
57+
mkdir -p /root/.rbenv/plugins
58+
ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build
2359

60+
# Setup for non-root user if needed
61+
if [ "$_user" != "root" ]; then
62+
mkdir -p "$_user_home/.rbenv/plugins"
63+
ln -s /usr/local/share/ruby-build "$_user_home/.rbenv/plugins/ruby-build"
64+
chown -R "$_user" "$_user_home/.rbenv/"
65+
chmod -R g+r+w "$_user_home/.rbenv"
66+
fi
67+
68+
# shellcheck disable=SC2016
69+
add_to_shell_init "$_user" 'export PATH="/usr/local/share/rbenv/bin:$PATH"'
2470
# shellcheck disable=SC2016
25-
echo 'eval "$(rbenv init -)"' >> "${user_home}/.bashrc"
71+
add_to_shell_init "$_user" 'eval "$(rbenv init -)"'
72+
}
73+
74+
# Function to install Ruby with rbenv
75+
install_ruby_rbenv() {
76+
_user="$1"
77+
_version="$2"
78+
79+
su "$_user" -c "/usr/local/share/rbenv/bin/rbenv install $_version"
80+
su "$_user" -c "/usr/local/share/rbenv/bin/rbenv global $_version"
81+
}
2682

27-
if [ -f "${user_home}/.zshrc" ]; then
28-
# shellcheck disable=SC2016
29-
echo 'eval "$(rbenv init -)"' >> "${user_home}/.zshrc"
83+
# Function to setup mise
84+
setup_mise() {
85+
_user="$1"
86+
87+
su "$_user" -c "curl https://mise.run | sh"
88+
89+
# shellcheck disable=SC2016
90+
add_to_shell_init "$_user" 'eval "$(~/.local/bin/mise activate bash)"' 'eval "$(~/.local/bin/mise activate zsh)"'
91+
}
92+
93+
# Function to install Ruby with mise
94+
install_ruby_mise() {
95+
_user="$1"
96+
_version="$2"
97+
98+
if [ "$_user" = "root" ]; then
99+
_home_dir="/root"
100+
else
101+
_home_dir="/home/$_user"
30102
fi
31-
fi
32103

33-
su "${USERNAME}" -c "/usr/local/share/rbenv/bin/rbenv install $VERSION"
34-
su "${USERNAME}" -c "/usr/local/share/rbenv/bin/rbenv global $VERSION"
104+
su "$_user" -c "$_home_dir/.local/bin/mise install ruby@$_version"
105+
su "$_user" -c "$_home_dir/.local/bin/mise use -g ruby@$_version"
106+
su "$_user" -c "$_home_dir/.local/bin/mise settings add idiomatic_version_file_enable_tools ruby"
107+
}
108+
109+
install_dependencies
110+
111+
# Setup version manager and install Ruby based on user choice
112+
if [ "$VERSION_MANAGER" = "rbenv" ]; then
113+
setup_rbenv "$USERNAME"
114+
install_ruby_rbenv "$USERNAME" "$VERSION"
115+
else
116+
setup_mise "$USERNAME"
117+
install_ruby_mise "$USERNAME" "$VERSION"
118+
fi
35119

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

features/test/ruby/scenarios.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@
66
"version": "3.3.0"
77
}
88
}
9+
},
10+
"with_rbenv": {
11+
"image": "mcr.microsoft.com/devcontainers/base:1-bookworm",
12+
"features": {
13+
"ruby": {
14+
"versionManager": "rbenv"
15+
}
16+
}
917
}
1018
}

features/test/ruby/test.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ set -e
44
# shellcheck source=/dev/null
55
source dev-container-features-test-lib
66

7-
check "PATH contains rbenv" bash -c "echo $PATH | grep rbenv"
8-
check "rbenv is installed" bash -c "rbenv --version"
9-
check "ruby-build is installed" bash -c "ls -l $HOME/.rbenv/plugins/ruby-build | grep '\-> /usr/local/share/ruby-build'"
10-
eval "$(rbenv init -)"
7+
check "mise is installed" bash -c "mise --version"
8+
check "mise init is sourced in the bashrc" bash -c "grep 'eval \"\$(~/.local/bin/mise activate bash)\"' $HOME/.bashrc"
9+
check "mise idiomatic version file is enabled for ruby" bash -c "mise settings | grep idiomatic_version_file_enable_tools | grep ruby"
1110
check "Ruby is installed with YJIT" bash -c "RUBY_YJIT_ENABLE=1 ruby -v | grep +YJIT"
12-
check "Ruby version is set to 3.4.4" bash -c "rbenv global | grep 3.4.4"
11+
check "Ruby version is set to 3.4.4" bash -c "mise use -g ruby | grep 3.4.4"
1312

1413
reportResults

features/test/ruby/version_3_3_0.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ set -e
44
# shellcheck source=/dev/null
55
source dev-container-features-test-lib
66

7-
check "PATH contains rbenv" bash -c "echo $PATH | grep rbenv"
8-
check "rbenv is installed" bash -c "rbenv --version"
9-
check "ruby-build is installed" bash -c "ls -l $HOME/.rbenv/plugins/ruby-build | grep '\-> /usr/local/share/ruby-build'"
10-
check "rbenv init is sourced in the bashrc" bash -c "grep 'eval \"\$(rbenv init -)\"' $HOME/.bashrc"
11-
check "rbenv init is sourced in the zshrc" bash -c "grep 'eval \"\$(rbenv init -)\"' $HOME/.zshrc"
7+
check "mise is installed" bash -c "mise --version"
8+
check "mise init is sourced in the bashrc" bash -c "grep 'eval \"\$(~/.local/bin/mise activate bash)\"' $HOME/.bashrc"
9+
check "mise init is sourced in the zshrc" bash -c "grep 'eval \"\$(~/.local/bin/mise activate zsh)\"' $HOME/.zshrc"
10+
check "mise idiomatic version file is enabled for ruby" bash -c "mise settings | grep idiomatic_version_file_enable_tools | grep ruby"
1211
check "Ruby is installed with YJIT" bash -c "RUBY_YJIT_ENABLE=1 ruby -v | grep +YJIT"
13-
check "Ruby version is set to 3.3.0" bash -c "rbenv global | grep 3.3.0"
12+
check "Ruby version is set to 3.3.0" bash -c "mise use -g ruby | grep 3.3.0"
1413

1514
reportResults

features/test/ruby/with_rbenv.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# shellcheck source=/dev/null
5+
source dev-container-features-test-lib
6+
7+
check "PATH contains rbenv" bash -c "echo $PATH | grep rbenv"
8+
check "rbenv is installed" bash -c "rbenv --version"
9+
check "ruby-build is installed" bash -c "ls -l $HOME/.rbenv/plugins/ruby-build | grep '\-> /usr/local/share/ruby-build'"
10+
eval "$(rbenv init -)"
11+
check "Ruby is installed with YJIT" bash -c "RUBY_YJIT_ENABLE=1 ruby -v | grep +YJIT"
12+
check "Ruby version is set to 3.4.4" bash -c "rbenv global | grep 3.4.4"
13+
14+
reportResults

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)