-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.hie-bios
executable file
·94 lines (81 loc) · 2.61 KB
/
.hie-bios
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
# hie-bios passes in HIE_BIOS_ARG, but this script accepts an argument for testing alternatively
arg="${HIE_BIOS_ARG-$1}"
ws=$(bazel info workspace)
execroot=$(bazel info execution_root)
# make arg relative to workspace
source="${arg#"$ws"/}"
# determine the haskell_binary or haskell_library target of the source file
declare -a target
mapfile -t target < <(
bazel query "let uses = rdeps(//..., $source, 2) in kind(_haskell_library, \$uses) union kind(_haskell_binary, \$uses)"
)
function die() {
echo "$@" >&2
exit 1
}
case "${#target[@]}" in
1) ;;
0)
die "could not determine haskell target for $arg"
;;
*)
die "matched multiple targets for $arg"
;;
esac
# build all dependent targets
declare -a dep_targets
mapfile -t dep_targets < <(bazel query "kind(rule, deps(${target[0]}, 1))")
if [[ "${#dep_targets[@]}" -ge 1 ]]; then
bazel build "${dep_targets[@]}"
fi
hie_bios_flags() {
echo -i
bazel run "${target[0]}@repl@bios" | while read -r line; do
# The bios script references files in the execroot bazel folder.
#
# We need to re-map the files back to to the workspace directory since
# the IDE should not try to open files inside of the execroot (and the
# hls would not work there).
#
# Also, hie-bios / ghcide needs to be able to locate files in the search
# path (given with -i flags). This is how it learns about the source files
# it should track, otherwise it will not be able to load modules from the
# same component. So we also have to map the search paths referencing
# the execroot back to the workspace too.
if [[ "$line" = "$execroot/"* ]]; then
relpath="${line#"$execroot/"}"
if [[ -f "$relpath" ]]; then
echo "$relpath"
continue
fi
elif [[ "$line" = "-i$execroot/"* ]]; then
relpath="${line#-i"$execroot"/}"
if [[ -d "$relpath" ]]; then
echo "-i$relpath"
continue
fi
fi
echo "$line"
done
# Make warnings non-fatal
echo -Wwarn
}
hie_bios_deps() {
bazel query "filter(\"^//\", buildfiles(deps(${target[0]})))" | while read -r label; do
package="${label#//}"
package="${package%:*}"
file="${label#*:}"
echo "$package/$file"
done
}
if [[ -z "${HIE_BIOS_DEPS-}" ]]; then
hie_bios_deps >&2
else
hie_bios_deps >"$HIE_BIOS_DEPS"
fi
if [[ -z "${HIE_BIOS_OUTPUT-}" ]]; then
hie_bios_flags
else
hie_bios_flags >"$HIE_BIOS_OUTPUT"
fi