-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_help.sh
executable file
·66 lines (55 loc) · 1.46 KB
/
gen_help.sh
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
#!/bin/bash
set -eufo pipefail
default_out=src/help.json
usage() {
cat <<EOS
Usage: $(basename "$0") [-c] BQN_REPO_DIR [OUT_FILE]
Generates $default_out from Markdown files in the BQN repo.
Also checks that all URLs give 200 when -c is provided.
Used to produce help messages on hover in the VS Code extension.
EOS
}
check_urls=false
case ${1-} in
-h|--help|help) usage; exit ;;
-c) check_urls=true; shift ;;
esac
if [[ $# -eq 0 ]]; then
usage >&2
exit 1
fi
bqn_repo=$1
out_file=${2-$default_out}
# Useful when adding debug prints in the Lua file.
[[ "$out_file" == - ]] && out_file=/dev/stdout
if ! command -v pandoc &>/dev/null; then
echo >&2 "error: pandoc must be installed"
exit 1
fi
url_file=$(mktemp)
trap 'rm -f "$url_file"' EXIT
find "$bqn_repo/help" -maxdepth 1 -name "*.md" -not -name "README.md" \
| sort \
| xargs pandoc -M bqn_repo="$bqn_repo" -M url_file="$url_file" \
-f commonmark -t gen_help_writer.lua \
| npx prettier --parser json --no-config --tab-width 4 \
> "$out_file"
if [[ $check_urls == true ]]; then
echo >&2 "checking urls"
n=0
while read -r url; do
{
if ! curl --head --silent --show-error --fail "$url" >/dev/null; then
echo >&2 "$url: not found"
exit 1
fi
echo -n >&2 .
} &
((n++))
if [[ n -eq 10 ]]; then
wait
fi
done < "$url_file"
wait
echo >&2
fi