lean4-htt/nix/lean-dev.in
2020-11-28 17:36:20 +01:00

44 lines
1.3 KiB
Bash

#!@bash@/bin/bash
set -euo pipefail
PATH=@nix@/bin:$PATH
call() {
if [[ $json == 1 ]]; then
$@ 2>&1 | awk '
/{/ { print $0; next }
# Hide some Nix warnings. You will still see them with `nix build` etc., but they are pretty annoying in the editor.
/warning: ignoring/ { next }
{
gsub(/"/, "\\\"", $0);
gsub(/\n/, "\\n", $0);
printf "{\"severity\": \"error\", \"pos_line\": 0, \"pos_col\": 0, \"file_name\": \"<stdin>\", \"text\": \"%s\"}\n", $0 }'
else
$@
fi
}
json=0
input=
for p in "$@"; do
[[ "$p" == --json ]] && json=1
[[ "$p" != -* ]] && input="$(realpath "$p")"
done
root="$(dirname "${input:-/}")"
while [[ "$root" != / ]]; do
[ -f "$root/flake.nix" ] && break
root="$(realpath "$root/..")"
done
if [[ "$root" == / ]]; then
# if we're outside of any project, just call Lean without any packages
call @lean@/bin/lean $@
elif [[ "$input" != "$root@srcDir@/"* ]]; then
# otherwise, if we're inside the package but not its source directory, call Lean with the full package available
call nix run "$root#lean-package" -- $@
else
# otherwise call Lean with the file's dependencies available
input="$(realpath --relative-to="$root@srcDir@" "$input")"
input="${input%.lean}"
input="${input//\//.}"
call nix develop "$root#mods.\"$input\"" -c lean $@
fi