lean4-htt/nix/lean-dev.in
2020-12-23 20:00:36 +01:00

71 lines
1.7 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=
inputFile=
server=0
worker=0
dir="$PWD"
deps=
args=(-- "$@")
for p in "$@"; do
case "$p" in
--json) json=1;;
--stdin) input="$(< /dev/stdin)";;
--server)
server=1
export LEAN_WORKER_PATH="$0"
;;
--worker) worker=1;;
-*) ;;
*)
if [[ $worker == 1 ]]; then
deps="$deps $p"
else
inputFile="$p"
input="$(< "$inputFile")"
dir="$(dirname "$inputFile")"
fi
;;
esac
done
root="$dir"
# find package root
while [[ "$root" != / ]]; do
[ -f "$root/flake.nix" ] && break
root="$(realpath "$root/..")"
done
# fall back to current package
[[ "$root" == / ]] && root="@srcRoot@"
if [[ -n "$input" || -n "$inputFile" ]]; then
deps="$(echo -n "$input" | nix run "$root#print-lean-deps" 2> /dev/null)"
fi
target="$root#check-mod"
# HACK: use stage 0 instead of 1 inside Lean's own `src/`
[[ -d "$root/src/Lean" && "$dir" == "$root/src"* ]] && target="@srcCheckTarget@" && args=@srcCheckArgs@
for dep in $deps; do
target="$target.\"$dep\""
done
if [[ -z "$input" ]]; then
call nix run "$target" ${args[*]}
else
echo -n "$input" | call nix run "$target" ${args[*]}
fi