-
Notifications
You must be signed in to change notification settings - Fork 71
/
Makefile
222 lines (155 loc) · 4.83 KB
/
Makefile
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
###############################
# Common defaults/definitions #
###############################
comma := ,
# Checks two given strings for equality.
eq = $(if $(or $(1),$(2)),$(and $(findstring $(1),$(2)),\
$(findstring $(2),$(1))),1)
###########
# Aliases #
###########
book: book.build
docs: cargo.doc
fmt: cargo.fmt
lint: cargo.lint
record: record.gif
test: test.cargo
##################
# Cargo commands #
##################
# Generate crates documentation from Rust sources.
#
# Usage:
# make cargo.doc [crate=<crate-name>]
# [private=(yes|no)] [docsrs=(no|yes)]
# [open=(yes|no)] [clean=(no|yes)]
cargo.doc:
ifeq ($(clean),yes)
@rm -rf target/doc/
endif
$(if $(call eq,$(docsrs),yes),RUSTDOCFLAGS='--cfg docsrs',) \
cargo $(if $(call eq,$(docsrs),yes),+nightly,) doc \
$(if $(call eq,$(crate),),--workspace,-p $(crate)) \
--all-features \
$(if $(call eq,$(private),no),,--document-private-items) \
$(if $(call eq,$(open),no),,--open)
# Format Rust sources with rustfmt.
#
# Usage:
# make cargo.fmt [check=(no|yes)]
cargo.fmt:
cargo +nightly fmt --all $(if $(call eq,$(check),yes),-- --check,)
# Lint Rust sources with Clippy.
#
# Usage:
# make cargo.lint
cargo.lint:
cargo clippy --workspace -- -D warnings
cargo clippy --workspace --all-features -- -D warnings
####################
# Testing commands #
####################
# Run Rust tests of project crates.
#
# Usage:
# make test.cargo [crate=<crate-name>] [careful=(no|yes)]
test.cargo:
ifeq ($(careful),yes)
ifeq ($(shell cargo install --list | grep cargo-careful),)
cargo install cargo-careful
endif
ifeq ($(shell rustup component list --toolchain=nightly \
| grep 'rust-src (installed)'),)
rustup component add --toolchain=nightly rust-src
endif
endif
cargo $(if $(call eq,$(careful),yes),+nightly careful,) test \
$(if $(call eq,$(crate),),--workspace,-p $(crate)) --all-features
# Run Rust tests of Book.
#
# Usage:
# make test.book [chapter=<name>] [clean=(no|yes)]
test.book:
ifeq ($(clean),yes)
cargo clean
endif
$(eval target := $(strip $(shell cargo -vV | sed -n 's/host: //p')))
cargo build --all-features --tests
OUT_DIR='$(realpath .)/target' \
mdbook test book $(if $(call eq,$(chapter),),,-c '$(chapter)') \
-L target/debug/deps $(strip \
$(if $(call eq,$(findstring windows,$(target)),),,\
$(shell cargo metadata -q \
| jq -r '.packages[] | select(.name == "windows_$(word 1,$(subst -, ,$(target)))_$(word 4,$(subst -, ,$(target)))") | .manifest_path' \
| sed -e "s/^/-L '/" -e 's/Cargo.toml/lib/' -e "s/$$/'/" )))
#################
# Book commands #
#################
# Build Book.
#
# Usage:
# make book.build [out=<dir>]
book.build:
mdbook build book/ $(if $(call eq,$(out),),,-d $(out))
# Build `highlight.js` library with Gherkin syntax support for Book.
#
# Usage:
# make book.highlight.js [ver=(10.7.3|<version>)]
book-highlight-js-ver = $(or $(ver),10.7.3)
book-highlight-js-tmp-dir := book/highlight.js
book.highlight.js:
@rm -rf $(book-highlight-js-tmp-dir)
git clone https://github.com/highlightjs/highlight.js \
$(book-highlight-js-tmp-dir)/
cd $(book-highlight-js-tmp-dir)/ && \
git checkout $(book-highlight-js-ver)
cd $(book-highlight-js-tmp-dir)/ && \
npm install
cd $(book-highlight-js-tmp-dir)/ && \
node tools/build.js :common gherkin
cp -f $(book-highlight-js-tmp-dir)/build/highlight.min.js \
book/theme/highlight.js
rm -rf $(book-highlight-js-tmp-dir)
# Serve Book on some port.
#
# Usage:
# make book.serve [port=(3000|<port>)]
book.serve:
mdbook serve book/ -p=$(or $(port),3000)
book.test: test.book
book.tests: test.book
######################
# Recording commands #
######################
# Record GIF image of terminal with asciinema.
#
# Requires `asciinema` and `agg` tools being installed:
# https://asciinema.org/docs/installation
# https://github.com/asciinema/agg#building
#
# Usage:
# make record.gif [name=(<current-datetime>|<file-name>)]
record-gif-dir := book/src/rec
record-gif-name := $(or $(name),$(shell date +%y"-"%m"-"%d"_"%H"-"%M"-"%S))
record-gif-file = $(record-gif-dir)/$(record-gif-name).gif
record.gif:
asciinema rec --overwrite rec.cast.json
@mkdir -p $(record-gif-dir)/
@rm -f $(record-gif-file)
agg --speed 2 rec.cast.json $(record-gif-file)
git add $(record-gif-file)
@rm -f rec.cast.json
ifeq ($(record-gif-name),readme)
head -n $$(($$(wc -l < README.md)-1)) README.md > README.tmp.md
mv README.tmp.md README.md
printf "[asciicast]: data:image/gif;base64," >> README.md
base64 -i $(record-gif-file) >> README.md
endif
##################
# .PHONY section #
##################
.PHONY: book docs fmt lint record test \
cargo.doc cargo.fmt cargo.lint \
book.build book.highlight.js book.serve book.test book.tests \
record.gif \
test.cargo test.book