Skip to content

Instantly share code, notes, and snippets.

@alexhayes
Last active August 25, 2021 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhayes/5227ffd39a314630f481474c454a9df4 to your computer and use it in GitHub Desktop.
Save alexhayes/5227ffd39a314630f481474c454a9df4 to your computer and use it in GitHub Desktop.
My custom .direnvrc
# See https://github.com/direnv/direnv/wiki/Python#-pyenv
use_flutter() {
PATH_add "$HOME/workspace/flutter/bin"
}
# Python using pyenv
#
# Usage:
# Install python with:
# pyenv install 3.9.0
# Add to .envrc
# use python 3.9.0
#
layout_python() {
local python=${1:-python}
[[ $# -gt 0 ]] && shift
local old_env=$(direnv_layout_dir)/virtualenv
unset PYTHONHOME
if [[ -d $old_env && $python = python ]]; then
export VIRTUAL_ENV=$old_env
else
local python_version
python_version=$("$python" -c "import platform as p;print(p.python_version())")
if [[ -z $python_version ]]; then
log_error "Could not find python's version"
return 1
fi
export VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
if [[ ! -d $VIRTUAL_ENV ]]; then
# virtualenv "--python=$python" "$@" "$VIRTUAL_ENV"
python -m venv "$@" "$VIRTUAL_ENV"
fi
fi
PATH_add "$VIRTUAL_ENV/bin"
}
use_python() {
local python_root=$HOME/.pyenv/versions/$1
load_prefix "$python_root"
if [[ -x "$python_root/bin/python3" ]]; then
layout python3
else
layout python
fi
}
# nvm
#
# See https://gist.github.com/gudleik/7ae3ef00f42440671f16
#
# Usage:
# Add to .envrc
# use node 10.16.3
#
use_node() {
layout node
local node_version=$1
if [ "$node_version" == "package.json" ]; then
# Requires jq: `brew install jq`
node_version=`jq --raw-output .engines.node package.json | tr -d "<=> "`
fi
local node_path=${NODE_DIR:-$HOME/.nvm/versions/node}
local nvm_fish="$HOME/.config/nvm"
if [ -x "$node_path/$node_version/bin/node" ]; then
PATH_add $node_path/$node_version/bin
elif [ -x "$node_path/v$node_version/bin/node" ]; then
# Support nvm users (nvm prefixes versions with 'v')
PATH_add $node_path/v$node_version/bin
elif [ -x "$nvm_fish/$node_version/bin/node" ]; then
# Support nvm.fish - https://github.com/jorgebucaran/nvm.fish
PATH_add $nvm_fish/$node_version/bin
else
echo "!!! Node.js version $node_version is not installed"
fi
export NODE_PATH=./node_modules
}
# Ruby
# See https://github.com/direnv/direnv/wiki/Ruby#-chruby
#
# Usage:
# Install ruby with:
# ruby-install 2.6.8
#
# Add to .envrc file:
# use ruby 2.6.8
#
use_ruby()
{
local version
version="${1}"
[[ "${version}" == --auto ]] && version="$(read_version_file .ruby-version)"
[[ -z "${version}" ]] && return
local chruby
if has brew; then
local brew_prefix
brew_prefix="$(brew --prefix)"
if [[ -e "${brew_prefix}/opt/chruby/share/chruby/chruby.sh" ]]; then
chruby="${brew_prefix}/opt/chruby/share/chruby/chruby.sh"
fi
fi
[[ -z "${chruby}" ]] && [[ -e /usr/local/share/chruby/chruby.sh ]] &&
chruby=/usr/local/share/chruby/chruby.sh
[[ -z "${chruby}" ]] && return
echo "${chruby}"
source "${chruby}"
chruby "${version}"
}
# Java
#
# See: https://gist.github.com/rnorth/0fd5048da85957da39c17bd49c4ca922
#
# Usage:
# Add to .envrc file:
# use java adopt@1.11.0-6
#
use_java() {
source ~/.jabba/jabba.sh
# 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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment