Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contib: add direnv implementation #4

Merged
merged 10 commits into from
Jul 11, 2023
100 changes: 100 additions & 0 deletions contrib/direnv
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash

# Discover PRJ_ROOT if not found via git (i.e. not in a git repository).
# Look for the .config folder in the current directory and up
find_prj_config() (
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
local old_pwd
while [[ $old_pwd != $PWD ]]; do
if [[ -d .config && "$HOME" != "$PWD" ]]; then
echo "$PWD"
return 0
fi
old_pwd=$PWD
cd ..
done
# We're at the top and didn't find anything
log_error "ERROR: could not find project root" >&2
return 1
)

# Discover PRJ_ROOT if in a git repository.
find_prj_root_with_git (
# remove everything after .git in the path because worktrees
# have the following path: /path/to/main/.git/worktrees/<name>
echo "${GIT_DIR%/.git*}"
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
)

{
: "${XDG_CONFIG_HOME:=${HOME}/.config}"
: "${XDG_CACHE_HOME:=${HOME}/.cache}"
: "${XDG_DATA_HOME:=${HOME}/.local/share}"

: "${GIT_DIR:=$(git rev-parse --absolute-git-dir 2>/dev/null)}"
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
blaggacao marked this conversation as resolved.
Show resolved Hide resolved

# PRJ_ROOT
_prj_root="$(find_prj_root_with_git)"
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
: "${PRJ_ROOT:=${_prj_root:=$(find_prj_config)}}"

# PRJ_CONFIG_HOME - always local to the project
: "${PRJ_CONFIG_HOME:=${PRJ_ROOT}/.config}"
mkdir -p "${PRJ_CONFIG_HOME}"
blaggacao marked this conversation as resolved.
Show resolved Hide resolved

if [[ -z "${PRJ_ID:-}" && -f "${PRJ_CONFIG_HOME}/prj_id" ]]; then
PRJ_ID=$(<"${PRJ_CONFIG_HOME}/prj_id")
fi

# PRJ_RUNTIME_DIR - always local to the project
: "${PRJ_RUNTIME_DIR:=${PRJ_ROOT}/.run}"
mkdir -p "${PRJ_RUNTIME_DIR}"

# PRJ_CACHE_HOME - shared if PRJ_ID is set
if [[ -z "${PRJ_CACHE_HOME:-}" ]]; then
if [[ -n "${PRJ_ID:-}" ]]; then
PRJ_CACHE_HOME="${XDG_CACHE_HOME}/prj/${PRJ_ID}"
else
PRJ_CACHE_HOME="${PRJ_ROOT}/.cache"
fi
fi
mkdir -p "${PRJ_CACHE_HOME}"

# PRJ_DATA_HOME - shared if PRJ_ID is set
if [[ -z "${PRJ_DATA_HOME:-}" ]]; then
if [[ -n "${PRJ_ID:-}" ]]; then
PRJ_DATA_HOME="${XDG_DATA_HOME}/prj/${PRJ_ID}"
else
PRJ_DATA_HOME="${PRJ_ROOT}/.data"
fi
fi
mkdir -p "${PRJ_DATA_HOME}"

# PRJ_PATH - shared if PRJ_ID is set
if [[ -z "${PRJ_PATH:-}" ]]; then
if [[ -n "${PRJ_ID:-}" ]]; then
PRJ_PATH="${HOME}/.local/bin/prj/${PRJ_ID}"
else
PRJ_PATH="${PRJ_ROOT}/.bin"
fi
fi
mkdir -p "${PRJ_PATH}"
}

unset -f find_prj_config

PATH_add "${PRJ_PATH}"
export PRJ_ROOT
export PRJ_ID
export PRJ_PATH
blaggacao marked this conversation as resolved.
Show resolved Hide resolved
export PRJ_CONFIG_HOME
export PRJ_CACHE_HOME
export PRJ_DATA_HOME
export PRJ_RUNTIME_DIR
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_ROOT: ${PRJ_ROOT}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_ID: ${PRJ_ID=none}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_CONFIG_HOME: ${PRJ_CONFIG_HOME#${PRJ_ROOT}/}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_RUNTIME_DIR: ${PRJ_RUNTIME_DIR#${PRJ_ROOT}/}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_CACHE_HOME: ${PRJ_CACHE_HOME#${PRJ_ROOT}/}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_DATA_HOME: ${PRJ_DATA_HOME#${PRJ_ROOT}/}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_STATE_HOME: ${PRJ_STATE_HOME#${PRJ_ROOT}/}"
[ -z ${DIRENV_PRJ_SILENCE+x} ] && log_status "PRJ_PATH: ${PRJ_PATH#${PRJ_ROOT}/}"
blaggacao marked this conversation as resolved.
Show resolved Hide resolved

direnv_layout_dir="${PRJ_DATA_HOME}/direnv"