diff --git a/containers/ruby/.devcontainer/devcontainer.json b/containers/ruby/.devcontainer/devcontainer.json index 9d75e9bf4b..bae30e2731 100644 --- a/containers/ruby/.devcontainer/devcontainer.json +++ b/containers/ruby/.devcontainer/devcontainer.json @@ -12,6 +12,11 @@ } }, + "containerEnv": { + "RBENV_ENABLED": "true", + "RVM_ENABLED": "true" + }, + // Set *default* container specific settings.json values on container create. "settings": {}, diff --git a/containers/ruby/.devcontainer/library-scripts/ruby-debian.sh b/containers/ruby/.devcontainer/library-scripts/ruby-debian.sh index 9908510012..bb22596e65 100755 --- a/containers/ruby/.devcontainer/library-scripts/ruby-debian.sh +++ b/containers/ruby/.devcontainer/library-scripts/ruby-debian.sh @@ -225,7 +225,7 @@ else # Install rvm curl -sSL https://get.rvm.io | bash -s stable --ignore-dotfiles ${RVM_INSTALL_ARGS} --with-default-gems="${DEFAULT_GEMS}" 2>&1 usermod -aG rvm ${USERNAME} - su ${USERNAME} -c ". /usr/local/rvm/scripts/rvm && rvm fix-permissions system" + su ${USERNAME} -c "/usr/local/rvm/scripts/rvm fix-permissions system" rm -rf ${GNUPGHOME} fi @@ -233,11 +233,11 @@ if [ "${INSTALL_RUBY_TOOLS}" = "true" ]; then # Non-root user may not have "gem" in path when script is run and no ruby version # is installed by rvm, so handle this by using root's default gem in this case ROOT_GEM='$(which gem || echo "")' - su ${USERNAME} -c ". /usr/local/rvm/scripts/rvm && \"$(which gem || echo ${ROOT_GEM})\" install ${DEFAULT_GEMS}" + su ${USERNAME} -c "\"$(which gem || echo ${ROOT_GEM})\" install ${DEFAULT_GEMS}" fi # VS Code server usually first in the path, so silence annoying rvm warning (that does not apply) and then source it -updaterc "if ! grep rvm_silence_path_mismatch_check_flag \$HOME/.rvmrc > /dev/null 2>&1; then echo 'rvm_silence_path_mismatch_check_flag=1' >> \$HOME/.rvmrc; fi\nsource /usr/local/rvm/scripts/rvm > /dev/null 2>&1" +updaterc "if ! grep rvm_silence_path_mismatch_check_flag \$HOME/.rvmrc > /dev/null 2>&1; then echo 'rvm_silence_path_mismatch_check_flag=1' >> \$HOME/.rvmrc; fi" # Install rbenv/ruby-build for good measure git clone --depth=1 \ @@ -247,8 +247,7 @@ git clone --depth=1 \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ https://github.com/rbenv/rbenv.git /usr/local/share/rbenv -ln -s /usr/local/share/rbenv/bin/rbenv /usr/local/bin -updaterc 'eval "$(rbenv init -)"' + git clone --depth=1 \ -c core.eol=lf \ -c core.autocrlf=false \ @@ -256,16 +255,28 @@ git clone --depth=1 \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ https://github.com/rbenv/ruby-build.git /usr/local/share/ruby-build + mkdir -p /root/.rbenv/plugins ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build + if [ "${USERNAME}" != "root" ]; then mkdir -p /home/${USERNAME}/.rbenv/plugins chown -R ${USERNAME} /home/${USERNAME}/.rbenv ln -s /usr/local/share/ruby-build /home/${USERNAME}/.rbenv/plugins/ruby-build fi +# Update bashrc and zshrc with conditionals +# We use "not false" so that the default state is true, i.e., enabled +updaterc 'if [ "${RVM_ENABLED}" != "false" ]; then + source /usr/local/rvm/scripts/rvm +fi' + +updaterc 'if [ "${RBENV_ENABLED}" != "false" ]; then + export PATH=$PATH:/usr/local/share/rbenv/bin + eval "$(rbenv init -)" + fi' + # Clean up -source /usr/local/rvm/scripts/rvm -rvm cleanup all +/usr/local/rvm/scripts/rvm cleanup all gem cleanup echo "Done!" diff --git a/containers/ruby/README.md b/containers/ruby/README.md index 13f84bba4e..eee9af8a26 100644 --- a/containers/ruby/README.md +++ b/containers/ruby/README.md @@ -61,6 +61,33 @@ Given JavaScript front-end web client code written for use in conjunction with a } ``` +### Enabling and Disabling Ruby Version Managers (RVM and RBENV) + +This Dev Container comes pre-packaged with a `ruby-debian.sh` shell script which installs `rbenv` and `rvm`. Both tools and their configuration files are enabled in the devcontainer by default. If you want to use them, simply start the container as normal. You can test the `rbenv` installation by running: + +```bash +rbenv -v +``` + +And the `rvm` installation by running: + +```bash +rvm -v +``` + +These commands can be run in the integrated terminal. + +To disable these tools, change the `containerEnv` variables in `devcontainer.json`: + +```jsonc +"containerEnv": { + "RBENV_ENABLED": "false", + "RVM_ENABLED": "false" +}, +``` + +These variables need to be explicitly set to `false` for the tools to be disabled. Any other input will result in `rvm` and `rbenv` being enabled for the container. + ### Adding the definition to your folder 1. If this is your first time using a development container, please see getting started information on [setting up](https://aka.ms/vscode-remote/containers/getting-started) Remote-Containers or [creating a codespace](https://aka.ms/ghcs-open-codespace) using GitHub Codespaces. diff --git a/script-library/ruby-debian.sh b/script-library/ruby-debian.sh index 9908510012..bb22596e65 100755 --- a/script-library/ruby-debian.sh +++ b/script-library/ruby-debian.sh @@ -225,7 +225,7 @@ else # Install rvm curl -sSL https://get.rvm.io | bash -s stable --ignore-dotfiles ${RVM_INSTALL_ARGS} --with-default-gems="${DEFAULT_GEMS}" 2>&1 usermod -aG rvm ${USERNAME} - su ${USERNAME} -c ". /usr/local/rvm/scripts/rvm && rvm fix-permissions system" + su ${USERNAME} -c "/usr/local/rvm/scripts/rvm fix-permissions system" rm -rf ${GNUPGHOME} fi @@ -233,11 +233,11 @@ if [ "${INSTALL_RUBY_TOOLS}" = "true" ]; then # Non-root user may not have "gem" in path when script is run and no ruby version # is installed by rvm, so handle this by using root's default gem in this case ROOT_GEM='$(which gem || echo "")' - su ${USERNAME} -c ". /usr/local/rvm/scripts/rvm && \"$(which gem || echo ${ROOT_GEM})\" install ${DEFAULT_GEMS}" + su ${USERNAME} -c "\"$(which gem || echo ${ROOT_GEM})\" install ${DEFAULT_GEMS}" fi # VS Code server usually first in the path, so silence annoying rvm warning (that does not apply) and then source it -updaterc "if ! grep rvm_silence_path_mismatch_check_flag \$HOME/.rvmrc > /dev/null 2>&1; then echo 'rvm_silence_path_mismatch_check_flag=1' >> \$HOME/.rvmrc; fi\nsource /usr/local/rvm/scripts/rvm > /dev/null 2>&1" +updaterc "if ! grep rvm_silence_path_mismatch_check_flag \$HOME/.rvmrc > /dev/null 2>&1; then echo 'rvm_silence_path_mismatch_check_flag=1' >> \$HOME/.rvmrc; fi" # Install rbenv/ruby-build for good measure git clone --depth=1 \ @@ -247,8 +247,7 @@ git clone --depth=1 \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ https://github.com/rbenv/rbenv.git /usr/local/share/rbenv -ln -s /usr/local/share/rbenv/bin/rbenv /usr/local/bin -updaterc 'eval "$(rbenv init -)"' + git clone --depth=1 \ -c core.eol=lf \ -c core.autocrlf=false \ @@ -256,16 +255,28 @@ git clone --depth=1 \ -c fetch.fsck.zeroPaddedFilemode=ignore \ -c receive.fsck.zeroPaddedFilemode=ignore \ https://github.com/rbenv/ruby-build.git /usr/local/share/ruby-build + mkdir -p /root/.rbenv/plugins ln -s /usr/local/share/ruby-build /root/.rbenv/plugins/ruby-build + if [ "${USERNAME}" != "root" ]; then mkdir -p /home/${USERNAME}/.rbenv/plugins chown -R ${USERNAME} /home/${USERNAME}/.rbenv ln -s /usr/local/share/ruby-build /home/${USERNAME}/.rbenv/plugins/ruby-build fi +# Update bashrc and zshrc with conditionals +# We use "not false" so that the default state is true, i.e., enabled +updaterc 'if [ "${RVM_ENABLED}" != "false" ]; then + source /usr/local/rvm/scripts/rvm +fi' + +updaterc 'if [ "${RBENV_ENABLED}" != "false" ]; then + export PATH=$PATH:/usr/local/share/rbenv/bin + eval "$(rbenv init -)" + fi' + # Clean up -source /usr/local/rvm/scripts/rvm -rvm cleanup all +/usr/local/rvm/scripts/rvm cleanup all gem cleanup echo "Done!"