-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.sh
executable file
·118 lines (97 loc) · 2.96 KB
/
run.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh
set -u
BASH_NO_DESCRIPTIONS="${BASH_NO_DESCRIPTIONS:-0}"
BASH_USE_SELECTOR="${BASH_USE_SELECTOR:-0}"
SELECTOR="${SELECTOR-fzf}"
SELECTOR_QUERY="${SELECTOR_QUERY-'-q'}"
export BASH_NO_DESCRIPTIONS
export BASH_USE_SELECTOR
export SELECTOR
export SELECTOR_QUERY
PYTHON_CMD=python3
if ! command -v "$PYTHON_CMD" > /dev/null 2>&1; then
PYTHON_CMD=python
fi
export PYTHON_CMD
./dependencies.sh || exit 1
usage() {
echo "Usage: $0 man_file"
echo "Example: $0 /usr/share/man/man1/man.1.gz"
exit
}
file=$1
[ -f "$1" ] || usage
name=$(echo "$file" | sed 's/.*\/\([^.]*\).*/\1/g')
[ -n "$name" ] || usage
# change working directory to the repository's root where this file should be
cd "$(dirname "$0")" || exit 1
mkdir -p completions/fish
fish_file=completions/fish/"$name".fish
if [ ! -f "$fish_file" ]; then
echo "Generating fish completion..."
"$PYTHON_CMD" fish-tools/create_manpage_completions.py "$file" -s > "$fish_file"
fi
echo "Building scanner..."
make || exit 1
echo "Running scanner..."
./scanner < "$fish_file" > /dev/null
process_completions() {
shell="$1"
mkdir -p completions/"$shell"
shell_file=completions/"$shell"/_"$name"
scanner_out_file="$shell"-converter.out
echo "Generating $shell completion..."
completions=""
begin_line=""
end_line='\n'
if echo "$shell" | grep "zsh" > /dev/null; then
begin_line='\t\t'
end_line=' \\\n'
fi
while IFS= read -r line; do
completions=$completions$begin_line$line$end_line
done < "$scanner_out_file"
if { echo "$shell" | grep "zsh" > /dev/null; } && [ "${#completions}" -ge 4 ]; then
completions=$(printf '%s\n' "$completions" | head -c -4)
fi
template_file=templates/"$shell"
if echo "$shell" | grep "bash" > /dev/null; then
if [ "$BASH_NO_DESCRIPTIONS" -eq 1 ]; then
template_file=templates/"$shell"_no_descriptions
elif [ "$BASH_USE_SELECTOR" -eq 1 ]; then
template_file=templates/"$shell"_use_selector
fi
fi
cp "$template_file" "$shell_file"
sed -i "s/COMMAND/$name/g" "$shell_file"
tmp_file=$(mktemp)
awk -v r="$completions" \
"{gsub(/ARGUMENTS/,r)}1" \
"$shell_file" > \
"$tmp_file"
mv "$tmp_file" "$shell_file"
if { echo "$shell" | grep "bash" > /dev/null; } && [ "$BASH_NO_DESCRIPTIONS" -eq 0 ]; then
descriptions=""
while IFS= read -r line; do
descriptions=$descriptions$begin_line$line$end_line
done < "$shell"-converter-descriptions.out
if [ -n "$descriptions" ]; then
tmp_file=$(mktemp)
awk -v r="$descriptions" \
"{gsub(/DESCRIPTIONS/,r)}1" \
"$shell_file" > \
"$tmp_file"
mv "$tmp_file" "$shell_file"
fi
if [ "$BASH_USE_SELECTOR" -eq 1 ]; then
if [ -n "$SELECTOR_QUERY" ]; then
sed -i "s/SELECTOR/$SELECTOR $SELECTOR_QUERY \"\$cur\"/g" "$shell_file"
else
sed -i "s/SELECTOR/$SELECTOR/g" "$shell_file"
fi
fi
fi
echo "Completion file available at $shell_file."
}
process_completions bash
process_completions zsh