mirror of
https://github.com/hcartiaux/dotfiles.git
synced 2024-10-18 17:25:23 +02:00
[direnv] Configuration imported from falkor/dotfiles
This commit is contained in:
parent
a44f4a9257
commit
9549358bc7
3 changed files with 158 additions and 0 deletions
21
direnv/direnv.sh
Normal file
21
direnv/direnv.sh
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# Custom load for direnv
|
||||||
|
# See https://direnv.net/
|
||||||
|
|
||||||
|
if [ -n "$(which direnv 2>/dev/null)" ]; then
|
||||||
|
eval "$(direnv hook $(basename $SHELL))"
|
||||||
|
# export DIRENV_WARN_TIMEOUT=100s
|
||||||
|
|
||||||
|
# See https://github.com/direnv/direnv/wiki/Python#restoring-the-ps
|
||||||
|
show_virtual_env() {
|
||||||
|
if [[ -n "$VIRTUAL_ENV" && -n "$DIRENV_DIR" ]]; then
|
||||||
|
echo "($(basename $VIRTUAL_ENV))"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
if [[ -n ${ZSH_VERSION-} ]]; then
|
||||||
|
setopt PROMPT_SUBST
|
||||||
|
else
|
||||||
|
export -f show_virtual_env
|
||||||
|
fi
|
||||||
|
|
||||||
|
PS1='$(show_virtual_env) '$PS1
|
||||||
|
fi
|
116
direnv/direnvrc
Normal file
116
direnv/direnvrc
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
# -*- mode: sh; -*-
|
||||||
|
# Custom global configuration for [direnv](https://direnv.net/)
|
||||||
|
# i.e. override/complete the direnv-stdlib:
|
||||||
|
# https://github.com/direnv/direnv/blob/master/stdlib.sh
|
||||||
|
#
|
||||||
|
# Quick installation of this file:
|
||||||
|
# mkdir -p ~/.config/direnv
|
||||||
|
# cd ~/.config/direnv
|
||||||
|
# curl -o direnvrc https://raw.githubusercontent.com/Falkor/dotfiles/master/direnv/direnvrc
|
||||||
|
#
|
||||||
|
# Sample .envrc you can use for Python projects based on the
|
||||||
|
# layouts defined in this file:
|
||||||
|
# https://github.com/Falkor/dotfiles/blob/master/direnv/envrc
|
||||||
|
#
|
||||||
|
############################ Python ############################
|
||||||
|
# Workfow based on:
|
||||||
|
# - 'pyenv' to easily switch to a special version of python
|
||||||
|
# - 'pyenv-virtualenv' to manage python versions AND virtualenvs
|
||||||
|
#
|
||||||
|
# Typical .envrc for your python project using the below functions:
|
||||||
|
# if [ -f ".python-version" ]; then
|
||||||
|
# pyversion=$(head .python-version)
|
||||||
|
# else
|
||||||
|
# pyversion=2.7.16
|
||||||
|
# fi
|
||||||
|
# pvenv=$(basename $PWD)
|
||||||
|
#
|
||||||
|
# use python ${pyversion}
|
||||||
|
# layout virtualenv ${pyversion} ${pvenv}
|
||||||
|
# layout activate ${pvenv}
|
||||||
|
#
|
||||||
|
# Adapted from
|
||||||
|
# - https://github.com/direnv/direnv/wiki/Python#-pyenv and
|
||||||
|
# - https://github.com/direnv/direnv/wiki/Python#-virtualenvwrapper
|
||||||
|
# - https://github.com/direnv/direnv/wiki/Python#venv-stdlib-module
|
||||||
|
#
|
||||||
|
# Side note:
|
||||||
|
# It appeared required to reload the pyenv [virtualenv-]init as for
|
||||||
|
# It May be due to the fact that direnv is creating a new bash
|
||||||
|
# sub-process to load the stdlib, direnvrc and .envrc
|
||||||
|
###
|
||||||
|
|
||||||
|
# === Use a specific python version (with pyenv) ===
|
||||||
|
# Usage in .envrc:
|
||||||
|
# use python <version>
|
||||||
|
use_python() {
|
||||||
|
if has pyenv; then
|
||||||
|
local pyversion=$1
|
||||||
|
eval "$(pyenv init -)"
|
||||||
|
pyenv local ${pyversion} || log_error "Could not find pyenv version '${pyversion}'. Consider running 'pyenv install ${pyversion}'"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# === Support for Python >=3.3 venv ===
|
||||||
|
# See https://github.com/direnv/direnv/wiki/Python#venv-stdlib-module
|
||||||
|
# Python 3.3 and later provide built-in support for virtual environments via the venv module in the standard library.
|
||||||
|
# Usage in .envrc:
|
||||||
|
# export VIRTUAL_ENV_HOME=.venv # Default: ~/venv
|
||||||
|
# layout python-venv <name> # creates ${VIRTUAL_ENV_HOME}/<name> venv
|
||||||
|
realpath() {
|
||||||
|
[[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
|
||||||
|
}
|
||||||
|
layout_python-venv() {
|
||||||
|
local pvenv=$1
|
||||||
|
|
||||||
|
VIRTUAL_ENV_HOME=${VIRTUAL_ENV_HOME:-~/venv}
|
||||||
|
VIRTUAL_ENV=${VIRTUAL_ENV:-${VIRTUAL_ENV_HOME}/${pvenv}}
|
||||||
|
unset PYTHONHOME
|
||||||
|
export VIRTUAL_ENV
|
||||||
|
if [[ ! -d $VIRTUAL_ENV ]]; then
|
||||||
|
log_status "no venv found; creating $VIRTUAL_ENV"
|
||||||
|
python3 -m venv "$VIRTUAL_ENV"
|
||||||
|
fi
|
||||||
|
PATH_add "${VIRTUAL_ENV}/bin"
|
||||||
|
}
|
||||||
|
|
||||||
|
# === Create a new virtualenv ===
|
||||||
|
# Usage in .envrc:
|
||||||
|
# layout virtualenv <version> <name>
|
||||||
|
layout_virtualenv() {
|
||||||
|
local pyversion=$1
|
||||||
|
local pvenv=$2
|
||||||
|
if has pyenv; then
|
||||||
|
pyenv local ${pyversion}
|
||||||
|
if [ -n "$(which pyenv-virtualenv)" ]; then
|
||||||
|
eval "$(pyenv virtualenv-init -)"
|
||||||
|
pyenv virtualenv --force --quiet ${pyversion} ${pvenv}
|
||||||
|
else
|
||||||
|
log_error "pyenv-virtualenv is not installed."
|
||||||
|
fi
|
||||||
|
elif has python3; then
|
||||||
|
# Use venv by default
|
||||||
|
layout_python-venv ${pvenv}
|
||||||
|
else
|
||||||
|
log_error "pyenv or python3 venv not found."
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# === Activate a virtualenv ===
|
||||||
|
# Note that pyenv-virtualenv uses 'python -m venv' if it is
|
||||||
|
# available (CPython 3.3 and newer) and 'virtualenv' otherwise
|
||||||
|
# Usage in .envrc:
|
||||||
|
# layout activate <name>
|
||||||
|
layout_activate() {
|
||||||
|
if has pyenv; then
|
||||||
|
local pyenvprefix=$(pyenv prefix)
|
||||||
|
local pyversion=$(pyenv version-name)
|
||||||
|
local pvenv="$1"
|
||||||
|
# Below initialization is necessary to recall ;(
|
||||||
|
|
||||||
|
pyenv activate ${pvenv}
|
||||||
|
else
|
||||||
|
source ${VIRTUAL_ENV}/bin/activate
|
||||||
|
fi
|
||||||
|
}
|
21
direnv/envrc
Normal file
21
direnv/envrc
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# -*- mode: sh; -*-
|
||||||
|
# (rootdir)/.envrc : local direnv configuration file
|
||||||
|
# Assumes the presence of '~/.config/direnv/direnvrc' from
|
||||||
|
# https://github.com/Falkor/dotfiles/blob/master/direnv/direnvrc
|
||||||
|
#
|
||||||
|
# Grab the latest version of this file from
|
||||||
|
# https://github.com/Falkor/dotfiles/blob/master/direnv/envrc
|
||||||
|
#
|
||||||
|
# Quick installation of this file for your project:
|
||||||
|
# cd /path/to/projectdir
|
||||||
|
# curl -o .envrc https://raw.githubusercontent.com/Falkor/dotfiles/master/direnv/envrc
|
||||||
|
|
||||||
|
# Default python version and virtualenv (basename of the root project directory)
|
||||||
|
[ -f ".python-version" ] && pyversion=$(head .python-version) || pyversion=3.7.4
|
||||||
|
[ -f ".python-virtualenv" ] && pvenv=$(head .python-virtualenv) || pvenv=$(basename $PWD)
|
||||||
|
|
||||||
|
use python ${pyversion}
|
||||||
|
# Create the virtualenv if not yet done
|
||||||
|
layout virtualenv ${pyversion} ${pvenv}
|
||||||
|
# activate it
|
||||||
|
layout activate ${pvenv}
|
Loading…
Reference in a new issue