Compare commits

...

9 Commits

Author SHA1 Message Date
6fb279eca0 Allow calling direnv-cache from subdirectories of $DIRENV_DIR 2020-02-03 11:33:25 +01:00
0c8db23a3b Prefer local nix build 2020-01-23 09:54:42 +01:00
14526f5419 Make term-test dependency optional
checkPhase still requires term-test

Closes #1
2019-12-29 10:55:04 +01:00
c767762ee0 Always recreate recipe by evaluating .envrc
evaluating .envrc (not the cached section) should not be expensive
2019-12-16 14:56:19 +01:00
ba07678b31 Add some logging 2019-12-16 14:55:51 +01:00
2b69fa6622 Add direnv reload after clearing cache 2019-12-04 12:00:32 +01:00
0976f04505 Add tests 2019-12-03 20:43:28 +01:00
f6fc817e84 Ignore variables used by direnv
All variables matching DIRENV_* are excluded to not influence direnv behavior.
2019-11-27 15:05:32 +01:00
ac83f17b77 Fix bugs
* Fix env diff for variables containing leading/trailing whitespace
* Run whole script in 'clean' (non-direnv) environment (fixes prepending/appending)
2019-11-27 14:07:38 +01:00
6 changed files with 102 additions and 19 deletions

9
Makefile Normal file
View File

@@ -0,0 +1,9 @@
build:
install:
mkdir -p $(PREFIX)/bin $(PREFIX)/share
cp direnv-cache $(PREFIX)/bin
cp direnvrc $(PREFIX)/share
check:
term-test -s tests

View File

@@ -1,3 +1,19 @@
{ pkgs ? import <nixpkgs> {} }:
(pkgs.writers.writeBashBin "direnv-cache" ./direnv-cache).overrideAttrs
(o: { passthru = { direnvrc = ./direnvrc; }; })
{ stdenv
, direnv
, term-test ? null
, doCheck ? true
}:
assert doCheck -> term-test != null;
stdenv.mkDerivation {
name = "direnv-cache";
src = ./.;
propagatedBuildInputs = [ direnv ];
installPhase = ''
make install PREFIX=$out
'';
checkInputs = [ term-test ];
inherit doCheck;
passthru = { direnvrc = ./direnvrc; };
preferLocalBuild = true;
}

View File

@@ -30,15 +30,18 @@ diffEnvs() {
local old="$1"
local new="$2"
while read -r -d '' line; do
while IFS= read -r -d '' line; do
name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=')
if ! [[ "$name" = DIRENV_* ]]; then
if ! getByName "$name" "$new" >/dev/null; then
echo "export -n '${name}'"
fi
fi
done <"$old"
while read -rd $'\0' line; do
while IFS= read -rd $'\0' line; do
name=$(tr -d '\0' <<<"$line" | head -n1 | cut -f1 -d'=')
if ! [[ "$name" = DIRENV_* ]]; then
if getByName "$name" "$old" >/dev/null; then
# found in old env, check if values match
if ! grep -z "^${line}\$" "$old" >/dev/null; then
@@ -49,6 +52,7 @@ diffEnvs() {
# variable is new
echo "export ${line@Q}"
fi
fi
done <"$new"
}
@@ -66,9 +70,23 @@ getCacheFilePath() {
echo "${cacheDir}/$(pwd | sha1sum | tr -d ' -')"
}
switchToDirenvDir() {
# Find directory that contains the current .envrc. Allows direnv-cache to
# be called from subdirectories.
wd="$(pwd)"
if [[ -n $DIRENV_DIR ]]; then
wd="${DIRENV_DIR#-}"
fi
cd "$wd"
}
cmd="$1"
if ! [[ -e .envrc ]]; then
switchToDirenvDir
if ! [[ -e ".envrc" ]]; then
echo "Error, no .envrc found in current directory"
exit 1
fi
@@ -77,13 +95,18 @@ cacheFile=$(getCacheFilePath)
case $cmd in
reload|r)
if [[ -n $DIRENV_DIR ]]; then
# run self in clean environment (i.e. outside of the current direnv
# environment)
echo "Switching to clean environment"
exec direnv exec /proc "${BASH_SOURCE[0]}" "$@"
fi
echo "Re-creating cache"
mkdir -p "$(dirname ${cacheFile})"
dumpEnv > "${cacheFile}.pre"
source <(direnv stdlib)
if [[ .envrc -nt ${cacheFile}.recipe ]]; then
echo "Re-creating recipe"
direnv exec . true > /dev/null
fi
source "${cacheFile}.recipe"
dumpEnv > "${cacheFile}.post"
diffEnvs "${cacheFile}.pre" "${cacheFile}.post" > "$cacheFile"
@@ -99,6 +122,7 @@ case $cmd in
if [[ -e "$cacheFile" ]]; then
rm "$cacheFile"
fi
direnv reload
;;
*)
usage

View File

@@ -14,4 +14,3 @@ cache() {
echo "Environment not cached"
fi
}

2
shell.nix Normal file
View File

@@ -0,0 +1,2 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.callPackage ./. {}

View File

@@ -0,0 +1,33 @@
# -*- mode: sh -*-
function initDirenv {
# direnv needs a writable home:
run "export HOME=$(pwd)"
run 'eval "$(direnv hook bash)"'
}
function test_simpleEnvrcWorks {
initDirenv
writeFile .envrc <<EOF
export FOO=bar
EOF
run "direnv allow"
assert "$(getEnv FOO)" == bar
}
function test_direnvExecRunsInCleanEnv {
initDirenv
run "export FOO=baz"
writeFile .envrc <<EOF
export FOO=bar
EOF
run "direnv allow"
assert "$(getEnv FOO)" == bar
#captureShell 'echo $FOO'
#captureShell 'direnv exec /proc env | grep FOO'
assert "$(captureShell 'direnv exec /proc env | grep FOO')" == "FOO=baz"
}