Skip to content

Instantly share code, notes, and snippets.

@rnorth
Created May 2, 2020 20:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnorth/0fd5048da85957da39c17bd49c4ca922 to your computer and use it in GitHub Desktop.
Save rnorth/0fd5048da85957da39c17bd49c4ca922 to your computer and use it in GitHub Desktop.
#
# Python
#
export PYENV_ROOT=${PYENV_ROOT:-$HOME/.pyenv}
# Usage:
# Add to .envrc file:
# use python 3.7.5
use_python() {
# Install pyenv for management of Python versions
if ! has pyenv; then
echo "Installing pyenv"
brew install pyenv
fi
# Install the required version of Python if not already installed
if [[ ! -d $PYENV_ROOT/versions/$1 ]]; then
echo "Python $1 is not installed; attempting to install"
CONFIGURE_OPTS="--with-openssl=$(brew --prefix openssl@1.1)" \
pyenv install -v $1
fi
# Load the required version of Python onto the path
PYTHON_ROOT=$PYENV_ROOT/versions/$1
load_prefix "$PYTHON_ROOT"
if [[ -x "$PYTHON_ROOT/bin/python" ]]; then
# Tell direnv to create/load a virtualenv in the current directory
layout python "$PYTHON_ROOT/bin/python"
# Install pip dependencies
pip3 install virtualenv
else
echo "Error: $PYTHON_ROOT/bin/python can't be executed."
exit
fi
}
# Alternatively, look for a .python-version file, and use that as the Python version instead
python_version="$(find_up .python-version || true)"
if [[ -f "${python_version}" ]]; then
echo "python-version=${python_version}"
watch_file "${python_version}"
use_python "$(cat ${python_version})"
fi
#
# Java
#
# Usage:
# Add to .envrc file:
# use java adopt@1.11.0-6
use_java() {
# Install jabba for management of Java versions
if ! has jabba; then
echo "Installing jabba"
brew install jabba
fi
# Install the required version of Java if not already installed
if [[ "$(jabba link $1)" == "" ]]; then
echo "Java $1 is not installed; attempting to install"
jabba install $1
fi
# Set JAVA_HOME, and load the required version of Java onto the path
export JAVA_HOME=$(jabba link $1)/Contents/Home
echo "JAVA_HOME=$JAVA_HOME"
load_prefix "$JAVA_HOME"
}
# Alternatively, look for a .jabbarc file, and use that as the Java version instead
jabbarc="$(find_up .jabbarc || true)"
if [[ -f "${jabbarc}" ]]; then
watch_file "${jabbarc}"
jabba_version=$(cat ${jabbarc} | grep -v "#" | cut -d " " -f2)
echo "Using ${jabba_version} declared in ${jabbarc}"
use_java "${jabba_version}"
fi
#
# NodeJS
#
export NODE_VERSIONS=${NODE_VERSIONS:-$HOME/.node}
# Usage:
# Add to .envrc file:
# use node 12.16.3
use_node() {
# Install the required version of NodeJS if not already installed
if [[ ! -d $NODE_VERSIONS/$1 ]]; then
mkdir -p $NODE_VERSIONS/$1
curl -fsSL http://nodejs.org/dist/v$1/node-v$1-darwin-x64.tar.gz | tar xvz --strip 1 -C $NODE_VERSIONS/$1 2>/dev/null
fi
load_prefix $NODE_VERSIONS/$1
}
#
# Ruby
#
# Usage:
# Add to .envrc file:
# use ruby 2.3.3
use_ruby() {
if ! has ruby-install; then
echo "Installing ruby-install"
brew install ruby-install
fi
if [[ ! -d $HOME/.rubies/ruby-$1 ]]; then
echo "Ruby $1 is not installed; attempting to install"
mkdir -p $NODE_VERSIONS/$1
ruby-install ruby $1
fi
local ruby_root=$HOME/.rubies/ruby-$1
load_prefix "$ruby_root"
layout_ruby
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment