-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustfile
More file actions
357 lines (295 loc) · 12 KB
/
justfile
File metadata and controls
357 lines (295 loc) · 12 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Justfile (Convenience Command Runner)
# rust vars
J_CARGO_NO_WARN := '-Awarnings'
J_RUST_LOG:= 'debug'
J_RUST_BACKTRACE:= '1'
J_RUSTFLAGS:='--cfg tokio_unstable'
# just path vars
J_HOME_DIR := env_var('HOME')
J_LOCAL_ROOT := justfile_directory()
J_INVOCD_FROM := invocation_directory()
J_INVOC_IS_ROOT := if J_INVOCD_FROM == J_LOCAL_ROOT { "true" } else { "false" }
# custom vars
J_FROZE_SHA_REGEX := 'FROZE_[a-fA-F0-9]{64}_FROZE-'
J_VAR_OR_ENV_REGEX := '[A-Z][A-Z_0-9]{3}+'
# ANSI Color Codes for use with echo command
NC := '\033[0m' # No Color
CYN := '\033[0;36m' # Cyan
BLU := '\033[0;34m' # Blue
GRN := '\033[0;32m' # Green
PRP := '\033[0;35m' # Purple
RED := '\033[0;31m' # Red
YLW := '\033[0;33m' # Yellow
BRN := '\033[0;33m' # Brown
# Default, lists commands.
_default:
@just --list --unsorted
alias cs:= cargo-script
alias csa:= cargo-script-all
alias wcr:= watch-check-run
alias wr:= watch-run
alias wc:= watch-check
######## init ########
# Ready all local `.rs` files.
[confirm(
'This will:
(1) Give user executable permissions to all `.rs` files in current directory level.
(`chmod u+x`)
(2) Run `cargo` clean, build, and doc on those files.
Commands can be inspected in the currently invoked `justfile`.
-- Confirm initialization?'
)]
[group('init')]
init: _permit-all (cargo-script-all 'clean') _compile-debug _compile-release (cargo-script-all 'doc') && _list-external-deps _gen-env _gen-git-hooks
######## general ########
# Cargo _ on script file.
[group('general')]
cargo-script command file *args:
cargo +nightly {{command}} {{args}} --manifest-path {{file}} -Zscript
# Cargo _ on ALL `.rs` files at current directory level.
[group('general')]
cargo-script-all command *args:
fd . --extension rs --max-depth 1 \
| xargs -I _ \
cargo +nightly {{command}} {{args}} --manifest-path _ -Zscript;
######## create ########
# New script, with executable user privileges.
[group('create')]
new name:
cat .support/_template-script-basic_rs \
| sd '\{\{sd_me:(.*?)\}\}' '{{name}}' \
> {{name}}.rs ;
chmod u+x {{name}}.rs
# New script, with executable user privileges.
[group('create')]
new-clap name:
cat .support/_template-script-clap_rs \
| sd '\{\{sd_me:(.*?)\}\}' '{{name}}' \
> {{name}}.rs ;
chmod u+x {{name}}.rs
######## common ########
# Linting, formatting, typo checking, etc.
[group('common')]
check file:
@echo '-- clippy @ {{file}} --'
just cargo-script clippy {{file}} --all-targets --all-features
@echo '-- tests @ {{file}} --'
RUSTFLAGS={{J_CARGO_NO_WARN}} just cargo-script test {{file}} --all-targets --all-features --quiet
@echo '-- typos @ {{file}} --'
typos ./{{file}}
# Show general use docs.
[group('common')]
docs-gen:
rustup doc
rustup doc --std
# Show docs for a script.
[group('common')]
docs file:
just cargo-script doc {{file}} --open --document-private-items --all-features
# Run performance analysis on a package.
[group('common')]
perf-script file *args:
hyperfine './{{file}} {{args}}' --warmup=3 --shell=none;
@echo 'Not run: {{GRN}}samply{{NC}} {{PRP}}record --iteration-count=3 ./{{file}} {{args}};{{NC}}'
@echo 'samply would respond: "{{BRN}}Profiling failed: Could not obtain the root task.{{NC}}"'
# Run performance analysis on a package.
[group('profile')]
_perf-compare-release file *args:
hyperfine 'cargo +nightly run --manifest-path={{file}} -Zscript {{args}}' 'cargo +nightly run --release --manifest-path={{file}} -Zscript {{args}}' --warmup=3 --shell=none;
@echo 'Not run: {{GRN}}samply{{NC}} {{PRP}}record --iteration-count=3 ./{{file}} {{args}};{{NC}}'
@echo 'samply would respond: "{{BRN}}Profiling failed: Could not obtain the root task.{{NC}}"'
# ######## modify ########
# # This turns out to be sub-optimal.
# # Setting Cargo.toml options in the actual script seems clearer and more robust.
# # e.g.
# # ```
# # ---
# # ...
# # profile.dev.opt-level = 2
# # profile.dev.package."*".opt-level = 2
# # ...
# # ---
# # ```
# # Modify shebang: run without flags. (default)
# [group('modify')]
# simple-script file:
# sd '\#!/usr/bin/env -S cargo .*$' \
# '#!/usr/bin/env -S cargo +nightly -Zscript' \
# {{file}}.rs ;
# # Modify shebang: use`--quiet` when called directly.
# [group('modify')]
# quiet-script file:
# sd '\#!/usr/bin/env -S cargo .*$' \
# '#!/usr/bin/env -S cargo +nightly --quiet -Zscript' \
# {{file}} ;
# # Modify shebang: use `--release` when called directly.
# [group('modify')]
# heavy-script file:
# sd '\#!/usr/bin/env -S cargo .*$' \
# '#!/usr/bin/env -S cargo +nightly -Zscript run --release --manifest-path' \
# {{file}} ;
# # Modify shebang: use `--release` & `--quiet` when called directly.
# [group('modify')]
# stable-script file:
# sd '\#!/usr/bin/env -S cargo .*$' \
# '#!/usr/bin/env -S cargo +nightly --quiet -Zscript run --release --manifest-path' \
# {{file}} ;
######## watch ########
# Run a file when it changes.
[group('watch')]
watch-run file *args:
watchexec --filter {{file}} \
'clear; ./{{file}} {{args}}';
# Run a file, without warnings, when it changes.
[group('watch')]
watch-quiet file *args:
watchexec --filter {{file}} \
'clear; RUSTFLAGS={{J_CARGO_NO_WARN}} ./{{file}} {{args}}';
# Lint & test a file when it changes.
[group('watch')]
watch-check file:
watchexec --filter {{file}} \
'clear; just check {{file}}';
# Lint & test then run a file when it changes.
[group('watch')]
watch-check-run file *args:
watchexec --filter {{file}} \
'clear; just check {{file}}; \
echo '-- run ./{{file}} --'; \
RUSTFLAGS={{J_CARGO_NO_WARN}} ./{{file}} {{args}}';
# `chmod u+x` on ALL `.rs` files at current directory level.
_permit-all:
fd . --extension rs --max-depth 1 \
| xargs -I _ \
chmod u+x _ ;
# `chmod u-x` on ALL `.rs` files at current directory level.
_depermit-all:
fd . --extension rs --max-depth 1 \
| xargs -I _ \
chmod a-x _ ;
# Compile in debug mode if NO `--release` in shebang
_compile-debug:
just _has-shebang-no-release \
| xargs -I _ \
just cargo-script build _;
# Compile in release mode if `--release` in shebang
_compile-release:
just _has-shebang-release \
| xargs -I _ \
just cargo-script build _ --release;
# List files withOUT release in the sehbang.
_has-shebang-no-release:
-@just _has-rs \
| xargs -I _ \
rg '^#!.*cargo' --files-with-matches _ \
| xargs -I _ \
rg '^(#!.*\-\-release|[^#]|$)' -vm 1 --files-with-matches _ ;
# List files with `--release` in shebang.
_has-shebang-release:
-@just _has-rs \
| xargs -I _ \
rg '^#!.*cargo' --files-with-matches _ \
| xargs -I _ \
rg '^#!.*\-\-release' --files-with-matches _ ;
# List `.rs` files
_has-rs:
-@fd . --extension rs --max-depth 1
# List dependencies. (This command has dependencies.)
[group('meta')]
_list-external-deps:
@echo "{{CYN}}List of external dependencies for this command runner and repo:"
xsv table ad_deps.csv
# Info about Rust-Compiler, Rust-Analyzer, Cargo-Clippy, and Rust-Updater.
[group('meta')]
_rust-meta-info:
rustc --version
rust-analyzer --version
cargo-clippy --version
rustup --version
# ######################################################################## #.
# Count all `{{_}}` vs `{{pat_}}`, show diff.
[group('meta-tests')]
_bracket-diff pat_prefix='sd_me:' file_globs='.support/_template*':
@echo "{{{{"{{pat_prefix}}".*}}:"
@rg '\{\{''{{pat_prefix}}''.*\}\}' {{file_globs}} \
| wc -l ;
@echo "{{{{".*"}}:"
@rg '\{\{.*\}\}' {{file_globs}} \
| wc -l ;
@echo "Difference:"
@-rg '\{\{.*\}\}' {{file_globs}} \
| rg {{pat_prefix}} --invert-match \
| uniq -c ;
# Show contents of `{{pat_}}`.
[group('meta-tests')]
_bracket-show pat_prefix='sd_me:' file_globs='.support/_template*':
@echo '{{{{'{{pat_prefix}}'_}} in files {{file_globs}}:'
@rg '\{\{''{{pat_prefix}}''.*\}\}' {{file_globs}} \
| sd '.*\{\{''{{pat_prefix}}''(.*)\}\}.*' '$1' \
| sort \
| uniq -c ;
# Inspect counts, ensure partitioning.
[group('meta-tests')]
_check-release-counts:
just _has-shebang-release | wc -l
just _has-shebang-no-release | wc -l
just _has-cargo-shebang | wc -l
just _has-rs | wc -l
# List files with `--release` in shebang.
_has-cargo-shebang:
-@just _has-rs \
| xargs -I _ \
rg '^#!.*cargo' --files-with-matches _ ;
# ######################################################################## #.
# Generate .env file from template, if .env file not present.
_gen-env:
@ if [ -f '.env' ]; \
then \
echo '`{{BRN}}.env{{NC}}` exists, {{PRP}}skipping creation{{NC}}...' && exit 0; \
else \
cp -n .support/_template.env .env; \
echo "{{BLU}}.env{{NC}} created from template with {{GRN}}example{{NC}} values."; \
fi
# Attempt to add all git-hooks. (no overwrite)
_gen-git-hooks: _gen-precommit-hook _gen-commitmsg-hook
# Attempt to add `pre-commit` git-hook. (no overwrite)
_gen-precommit-hook:
@ if [ -f '.git/hooks/pre-commit' ]; \
then \
echo '`.git/hooks/{{BRN}}pre-commit{{NC}}` exists, {{PRP}}skipping creation{{NC}}...' && exit 0; \
else \
cp -n .support/git_hooks/pre-commit .git/hooks/pre-commit; \
chmod u+x .git/hooks/pre-commit; \
echo live "{{BLU}}pre-commit{{NC}} hook added to {{GRN}}.git/hooks{{NC}} and set as executable"; \
fi
# Attempt to add `commit-msg` git-hook. (no overwrite)
_gen-commitmsg-hook:
@ if [ -f '.git/hooks/commit-msg' ]; \
then \
echo '`.git/hooks/{{BRN}}commit-msg{{NC}}` exists, {{PRP}}skipping creation{{NC}}...' && exit 0; \
else \
cp -n .support/git_hooks/commit-msg .git/hooks/commit-msg; \
chmod u+x .git/hooks/commit-msg; \
echo live "{{BLU}}commit-msg{{NC}} hook added to {{GRN}}.git/hooks{{NC}} and set as executable"; \
fi
# ######################################################################## #.
# Freeze! For your safety.
_freeze file:
mv -iv {{file}} FROZE_{{sha256(file)}}_FROZE-{{file}} | rg {{file}}
# Unfreeze a file. (removes 'FROZE...FROZE-' tag from filename).
_thaw file:
echo {{file}} | sd '{{J_FROZE_SHA_REGEX}}' '' | xargs mv -iv {{file}}
# Search local files through ice.
_arctic-recon iceless_name:
fd --max-depth 1 '{{J_FROZE_SHA_REGEX}}{{iceless_name}}' | rg {{iceless_name}}
# ######################################################################## #.
# Speak Funny to Me!
_uu:
echo {{uuid()}}
# Say my name.
_sha file:
echo {{sha256_file(file)}}
# Example function for syntax reference.
_example-file-exists-test file:
echo {{ if path_exists(file) == "true" { "hello" } else { "goodbye" } }}
# ######################################################################## #.