forked from Viv022/code-wizard-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·118 lines (98 loc) · 2.34 KB
/
build
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
#!/usr/bin/env bash
read -r -d '' helptxt <<EOF
This script is used for building this book.
help - print this help message
epub - build the dist/code-mage-book-<version>.epub
pdf - build the dist/code-mage-book-<version>.pdf
html - build the docs/index-<version>.html
cm - build and install cm command
all - build epub, pdf, html (default)
watch - build on any change
For more information, see the README.md file.
EOF
# Read the version from the VERSION file
if [[ -z "$VERSION" && -f VERSION ]]; then
VERSION=$(head -1 VERSION)
fi
x-help() {
echo "$helptxt"
}
x-all() {
x-html
x-pdf
x-epub
}
x-epub() {
asciidoctor-epub3 \
-D "dist" \
-o "code-mage-book-v${VERSION}.epub" \
-a pdf-fontsdir="./book/assets/fonts" \
"./book/index.adoc"
}
#-a pdf-themesdir="./book/assets/themes" \
#-a pdf-theme="dark-theme.yml" \
x-pdf() {
asciidoctor-pdf \
-D "dist" \
-o "code-mage-book-v${VERSION}.pdf" \
-a pdf-themesdir="./book/assets/themes" \
-a pdf-fontsdir="./book/assets/fonts" \
"./book/index.adoc"
}
x-html() {
asciidoctor \
-D "docs" \
-o "index.html" \
-a copy_script=./book/assets/scripts/script.js \
"./book/index.adoc"
}
x-cm() {
go install ./cmd/cm
}
x-clean() {
rm -rf docs
mkdir -p docs
}
x-watch() {
if [[ ! -d book ]]; then
echo "no book found, wrong directory?" && exit 1
fi
tmux rename-window "watching $(wd)/book"
# while true; do
entr bash -c '
echo "Change detected. Rebuilding..."
./build html
' < <(find .)
# sleep 1
# done
}
# --------------------- completion and delegation --------------------
# The following provides bash completion with `complete -C build build`
# by deriving the names of the verbs for tab completion from those
# beginning with x- above. No other argument completion is provided.
while IFS= read -r line; do
[[ $line =~ ^declare\ -f\ x\- ]] || continue
commands+=("${line##declare -f x-}")
done < <(declare -F)
mapfile -t commands < \
<(LC_COLLATE=C sort < <(printf "%s\n" "${commands[@]}"))
if [[ -n $COMP_LINE ]]; then
line=${COMP_LINE#* }
for c in "${commands[@]}"; do
[[ ${c:0:${#line}} == "${line,,}" ]] && echo "$c"
done
exit
fi
if [[ -n "$1" ]]; then
declare action="$1"
shift
for c in "${commands[@]}"; do
declare cmd
cmd=$(command -v "x-$c")
if [[ $c == "$action" && -n "$cmd" ]]; then
"x-$action" "$@"
exit $?
fi
done
fi
x-all