Skip to content

Instantly share code, notes, and snippets.

@mikeplus64
Last active March 7, 2019 05:24
Show Gist options
  • Save mikeplus64/81459f85f921f4e67394e842f22b88e9 to your computer and use it in GitHub Desktop.
Save mikeplus64/81459f85f921f4e67394e842f22b88e9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
CURDIR="$PWD"
findconfig() {
# via https://unix.stackexchange.com/questions/293393/find-a-specific-file-in-the-nearest-ancestor-of-the-current-working-directory
# from: https://www.npmjs.com/package/find-config#algorithm
# 1. If X/file.ext exists and is a regular file, return it. STOP
# 2. If X has a parent directory, change X to parent. GO TO 1
# 3. Return NULL.
if [ -f "$1" ]; then
printf '%s\n' "${PWD%/}/$1"
elif [ "$PWD" = / ]; then
false
else
# a subshell so that we don't affect the caller's $PWD
(cd .. && findconfig "$1")
fi
}
if [ "$1" != "--really" ]; then
if (which direnv && direnv exec . true) 2>&1 >/dev/null && ROOT="${DIRENV_DIR:1}" && [ -n "$ROOT" ]; then
echo wrapping using direnv
exec direnv exec . "$0" --really "$@"
elif which nix-shell 2>&1 >/dev/null; then
NIX_SHELL="$(findconfig shell.nix)"
NIX_DEFAULT="$(findconfig default.nix)"
USE_SHELL_NIX=false
if [ -n "$NIX_SHELL" -a -n "$NIX_DEFAULT" ]; then
# choose the longer path
if [ ${#NIX_SHELL} -gt ${#NIX_DEFAULT} ]; then
USE_SHELL_NIX=true
fi
else
if [ -n "$NIX_SHELL" ]; then
USE_SHELL_NIX=true
fi
fi
if $USE_SHELL_NIX; then
echo wrapping using nix-shell "$NIX_SHELL"
export ROOT="$(dirname "$NIX_SHELL")"
exec nix-shell "$NIX_SHELL" --run "$0 --really $@"
else
echo wrapping using nix-shell -A env "$NIX_DEFAULT"
export ROOT="$(dirname "$NIX_DEFAULT")"
exec nix-shell "$NIX_DEFAULT" -A env --run "$0 --really $@"
fi
fi
fi
shift
OUTDIR="/tmp/dante/$(echo ${ROOT} | md5sum | cut -d' ' -f1)"
echo "ROOT=$ROOT"
echo "OUTDIR=$OUTDIR"
echo "USE_CABAL=$USE_CABAL"
if [ "$USE_CABAL" ]; then
echo using cabal
cabal new-repl --builddir="$OUTDIR" "$@"
else
echo using ghci
mkdir -p "$OUTDIR"
GHCI_SCRIPT="${OUTDIR}/.ghci"
echo ":cd ${ROOT}" > "$GHCI_SCRIPT"
shift
ghci -O0 \
-ghci-script "$GHCI_SCRIPT" \
-ghci-script "${ROOT}/.ghci" \
-outputdir="${OUTDIR}" \
-Wall \
"$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment