Compare commits
9 Commits
a498992c2a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fb279eca0 | |||
| 0c8db23a3b | |||
| 14526f5419 | |||
| c767762ee0 | |||
| ba07678b31 | |||
| 2b69fa6622 | |||
| 0976f04505 | |||
| f6fc817e84 | |||
| ac83f17b77 |
9
Makefile
Normal file
9
Makefile
Normal 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
|
||||
22
default.nix
22
default.nix
@@ -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;
|
||||
}
|
||||
|
||||
34
direnv-cache
34
direnv-cache
@@ -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
|
||||
|
||||
2
shell.nix
Normal file
2
shell.nix
Normal file
@@ -0,0 +1,2 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.callPackage ./. {}
|
||||
33
tests/test_direnv_stable_behavior
Normal file
33
tests/test_direnv_stable_behavior
Normal 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"
|
||||
}
|
||||
Reference in New Issue
Block a user