-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpr.fish
572 lines (439 loc) · 18.3 KB
/
tpr.fish
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
function __tpr_FAIL --argument message
set_color red
echo -n "Error: " >&2
set_color normal
echo $message >&2
return 1
end
function __tpr_missing_arg --argument arg_name
__tpr_FAIL "Missing positional argument $arg_name."
return 1
end
function __tpr_WARN --argument message
set_color yellow
echo -n "Warning: " >&2
set_color normal
echo $message >&2
end
function __tpr_main_tex --argument tpr_working_dir
# get the main tex file, and ensure it exists and has the expected extension
set --local main_tex_relative (path change-extension '' $tpr_working_dir/*.latexmain | head -n 1)
set --local main_tex (path basename $main_tex_relative)
set --local main_tex_extension (path extension $main_tex)
if not test -f $main_tex_relative
or not test "$main_tex_extension" = ".tex"
__tpr_FAIL "no valid tex file specified with .latexmain"
return 1
end
echo $main_tex
end
function __tpr_compile --argument texfile
latexmk -pdf -interaction=nonstopmode -silent -Werror -file-line-error -cd $texfile >/dev/null
end
function __tpr_compile_force --argument texfile
latexmk -pdf -f -interaction=nonstopmode -silent -cd $texfile >/dev/null
end
function __tpr_tar --description "create an uncompressed tarfile from `source_dir`" --argument source_dir tarfile
if test -f "$source_dir/.gitignore"
set --function ignore_file --ignore-file $source_dir/.gitignore
end
fd -H --exclude '.git' $ignore_file --base-directory $source_dir --exec-batch tar -rf $tarfile -C $source_dir
end
function __tpr_populate_tempdir --description "archive the current directory to a temporary tarfile" --argument temp_dir tpr_working_dir commit
# get and validate main.tex
set --local main_tex (__tpr_main_tex $tpr_working_dir)
or return 1
if test -n "$commit" >/dev/null
# use `git archive` to dump to $tarfile if commit specified
git -C $tpr_working_dir archive --format=tar $commit --output $temp_dir/source.tar
or return 1
else
# otherwise, populate $tarfile with current contents
__tpr_tar $tpr_working_dir $temp_dir/source.tar
or return 1
end
# untar
mkdir $temp_dir/source
tar -xf $temp_dir/source.tar -C $temp_dir/source
# check that archive was generated properly
if not test -f "$temp_dir/source/$main_tex"
__tpr_FAIL "failed to generate archive"
return 1
end
# if so, return
echo "$main_tex"
end
function __tpr_list_templates --argument template_directory
path basename $template_directory/*
or return 0
end
function tpr --description 'Manage LaTeX project repositories' --argument command
set --local options (fish_opt --short=h --long=help)
set --local options $options (fish_opt --short=v --long=version)
set --local options $options (fish_opt --short=C --long=directory --required-val)
argparse --stop-nonopt $options -- $argv
or return 1
set --function tpr_version 1.2
# catch help and version flags
if set --query _flag_help
tpr_help
return 0
end
if set --query _flag_version
echo "tpr (version $tpr_version)"
return 0
end
# set main directory locations
if set --query XDG_DATA_HOME
set --function tpr_data_dir $XDG_DATA_HOME/tpr
else
set --function tpr_data_dir $HOME/.local/share/tpr
end
if set --query XDG_CONFIG_HOME
set --function tpr_config_dir $XDG_CONFIG_HOME/tpr
else
set --function tpr_config_dir $HOME/.config/tpr
end
if set --query _flag_directory
set --function tpr_working_dir $_flag_directory
else
set --function tpr_working_dir (pwd)
end
mkdir --parents $tpr_data_dir
mkdir --parents $tpr_config_dir
set --function tpr_resource_dir $tpr_data_dir/resources
set --function tpr_template_dir $tpr_data_dir/templates
set --function tpr_config_file $tpr_config_dir/config.toml
if test (count $argv) -eq 0
tpr_help
return 1
end
set --local temp_dir (mktemp --directory)
and trap "rm -rf $temp_dir" INT TERM HUP EXIT
set --local options (fish_opt --short=h --long=help)
switch "$argv[1]"
case template
if not type -q copier
__tpr_FAIL "Dependency `copier` missing: command `tpr template` not supported"
return 1
end
# parse options and catch help
argparse --name "tpr template" --stop-nonopt $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help template
return 0
end
if test (count $argv) -eq 0
tpr_help template
return 1
end
switch $argv[1]
case install
# parse options and catch help
argparse --name "tpr template install" --max-args 2 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help template-install
return 0
end
set --local NAME $argv[1]
if not set --query NAME
__tpr_missing_arg NAME
return 1
end
set --local GIT $argv[2]
if not set --query GIT
__tpr_missing_arg GIT
return 1
end
# validate template name
set --local matched_name (string match --regex '[a-zA-Z0-9_\-]+' $NAME)
if not test "$matched_name" = "$NAME"
__tpr_FAIL "Invalid template name!"
return 1
end
if test -e "$tpr_template_dir/$NAME"
__tpr_FAIL "Template with name $NAME already installed!"
return 1
end
# install to directory
git clone $GIT "$tpr_template_dir/$NAME" >/dev/null
case uninstall
# parse options and catch help
argparse --name "tpr template uninstall" --max-args 1 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help template-uninstall
return 0
end
set --local NAME $argv[1]
if not set --query NAME
__tpr_missing_arg NAME
return 1
end
# validate template name
set --local matched_name (string match --regex '[a-zA-Z0-9_\-]+' $NAME)
if not test "$matched_name" = "$NAME"
__tpr_FAIL "Invalid template name!"
return 1
end
if test -e "$tpr_template_dir/$NAME"
rm -rf "$tpr_template_dir/$NAME"
end
case update
# parse options and catch help
argparse --name "tpr template update" --max-args 0 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help template-update
return 0
end
for file in $tpr_template_dir/*
fish --command "git -C $file pull --force" &
set --append pid_list (jobs --last --pid)
end
wait $pid_list 2>/dev/null
case list
# parse options and catch help
argparse --name "tpr template list" --max-args 0 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help template-list
return 0
end
__tpr_list_templates $tpr_template_dir
case '*'
__tpr_FAIL "Unknown template subcommand: \"$argv[2]\""
return 1
end
case init
if not type -q copier
__tpr_FAIL "Dependency `copier` missing: command `tpr init` not supported"
return 1
end
set --local options $options (fish_opt --short=F --long=force)
argparse --name "tpr init" --max-args 1 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help init
return 0
end
set --local TEMPLATE $argv[1]
if test -z "$TEMPLATE"
__tpr_missing_arg TEMPLATE
return 1
end
set --function available_templates (__tpr_list_templates $tpr_template_dir)
if not contains $TEMPLATE $available_templates
__tpr_FAIL "Invalid template '$TEMPLATE'"
return 1
end
copier copy $tpr_template_dir/$TEMPLATE $tpr_working_dir
# not a git directory: initialize new repository
if not test -d $tpr_working_dir/.git
git -C $tpr_working_dir init
and git -C $tpr_working_dir add -A
and git -C $tpr_working_dir commit -m "Initialize new project repository."
set --local commit_file $tpr_resource_dir/pre-commit
if test -f "$commit_file"
cp -i $commit_file $tpr_working_dir/.git/hooks/pre-commit
end
end
case remote
if not type -q yq
and not type -q gh
__tpr_FAIL "Dependencies `yq` and `gh` missing: command `tpr remote` not supported"
return 1
end
argparse --name "tpr remote" --max-args 1 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help remote
return 0
end
set --local REPONAME $argv[1]
if test -z "$REPONAME"
__tpr_missing_arg REPONAME
return 1
end
if git -C $tpr_working_dir config --get remote.origin.url
__tpr_FAIL "remote 'origin' already exists"
return 1
end
set --function homepage (yq '.homepage' $tpr_config_file)
if test -n "$homepage"
set --function homepage_opt --homepage $homepage
end
set --function remote (yq '.remote' $tpr_config_file)
if test -z "$homepage"
set --function remote origin
end
gh repo create $REPONAME --remote $remote --source $tpr_working_dir --disable-issues --disable-wiki --private --push $homepage_opt
case archive
set --local options $options (fish_opt --short=I --long=include --multiple-vals)
set --local options $options (fish_opt --short=b --long=bare)
set --local options $options (fish_opt --short=r --long=reference --required-val)
set --local options $options (fish_opt --short=f --long=force)
set --local options $options (fish_opt --short=F --long=format --required-val)
argparse --name "tpr archive" --max-args 1 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help archive
return 0
end
if set --query _flag_format
set --local allowed_formats gz tar dir
set --function FORMAT $_flag_format
if not contains $FORMAT $allowed_formats
__tpr_FAIL "invalid option for --format: $FORMAT"
end
else
set --function FORMAT gz
end
set --function OUT $argv[1]
if test -z "$OUT"
__tpr_missing_arg OUT
return 1
end
# if force, delete OUT
# it is important to do this now in case the file is in the current directory
# in which case the old version would be included in the archive
if set --query _flag_force
rm --recursive --force $OUT
end
set --local main_tex (__tpr_populate_tempdir $temp_dir $tpr_working_dir $_flag_reference)
or return 1
# if --bare: replace tarfile with modified contents
if set --query _flag_bare
if type -q arxiv_latex_cleaner
arxiv_latex_cleaner --keep_bib $temp_dir/source
else
__tpr_WARN "Command `arxiv_latex_cleaner` not found: skipping some cleaning steps"
cp -r $temp_dir/source $temp_dir/source_arXiv
end
# delete all hidden files
fd --hidden --regex "^\." --no-ignore $temp_dir/source_arXiv --exec-batch rm -rf
set --local delete_endings \
aux bcf blg brf fdb_latexmk fls \
gz latexmain log run.xml tar thm toc toml \
yml yaml zip
rm --force $temp_dir/source_arXiv/**/*.{$delete_endings}
rm --force --recursive $temp_dir/source_arXiv/{.gitignore, .git, .github, .copier-answers.yml}
rm --force $temp_dir/source.tar
__tpr_tar $temp_dir/source_arXiv $temp_dir/source.tar
end
# include additional requested files
if set --query _flag_include
__tpr_compile_force "$temp_dir/source/$main_tex"
or __tpr_WARN "Compilation failed: some files may not be included."
for file_end in $_flag_include
set --local include_file (path change-extension $file_end $main_tex)
if test -f $temp_dir/source/$include_file
tar -rf $temp_dir/source.tar -C $temp_dir/source ./$include_file
else
__tpr_WARN "File '$include_file' was not generated before or during compilation."
end
end
end
# compress tarfile and export
switch $FORMAT
case gz
gzip -9 $temp_dir/source.tar
and mv --interactive $temp_dir/source.tar.gz $OUT
case tar
mv --interactive $temp_dir/source.tar $OUT
case dir
if test -e $OUT
__tpr_FAIL "File or directory '$OUT' already exists. Override with --force."
end
mkdir --parents $OUT
and tar -xf $temp_dir/source.tar -C $OUT
end
case diff
argparse --name "tpr diff" --max-args 3 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help diff
return 0
end
set --function PDF $argv[1]
if test -z "$PDF"
__tpr_missing_arg PDF
return 1
end
set --function COMMIT $argv[2]
if not set --query COMMIT
__tpr_missing_arg COMMIT
return 1
end
set --function REV $argv[3]
set --local main_tex (__tpr_populate_tempdir $temp_dir $tpr_working_dir $REV)
or return 1
set --local diff_tex (path change-extension '' $temp_dir/source/$main_tex)-diff.tex
if latexdiff (git show $COMMIT:$main_tex | psub) $main_tex >$diff_tex
and __tpr_compile_force $diff_tex
mv -i (path change-extension pdf $diff_tex) $PDF
else
__tpr_FAIL "Failed to compile diff file"
return 1
end
case validate
set --local options $options (fish_opt --short=r --long=reference --required-val)
argparse --name "tpr validate" --max-args 0 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help validate
return 0
end
set --local main_tex (__tpr_populate_tempdir $temp_dir $tpr_working_dir $_flag_reference)
or return 1
__tpr_compile "$temp_dir/source/$main_tex"
case compile
set --local options $options (fish_opt --short=F --long=format --required-val)
set --local options $options (fish_opt --short=f --long=force)
set --local options $options (fish_opt --short=r --long=reference --required-val)
argparse --name "tpr compile" --max-args 1 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help compile
return 0
end
if set --query _flag_format
set --function FORMAT $_flag_format
else
set --function FORMAT pdf
end
set --local OUT $argv[1]
if test -z "$OUT"
__tpr_missing_arg OUT
return 1
end
set --local main_tex (__tpr_populate_tempdir $temp_dir $tpr_working_dir $_flag_reference)
or return 1
if not __tpr_compile "$temp_dir/source/$main_tex"
__tpr_FAIL "Failed to compile project."
return 1
end
if set --query _flag_force
set --function mv_flags --force
else
set --function mv_flags --interactive
end
if not mv $mv_flags (path change-extension $FORMAT $temp_dir/source/$main_tex 2> /dev/null) $OUT
__tpr_FAIL "Failed to obtain file '$(path change-extension $FORMAT $main_tex)' after compilation."
return 1
end
case update
argparse --name "tpr update" --max-args 0 $options -- $argv[2..]
or return 1
if set --query _flag_help
tpr_help update
return 0
end
copier update $tpr_working_dir
case '*'
__tpr_FAIL "Unknown command: '$argv[1]'"
return 1
end
end