-
Notifications
You must be signed in to change notification settings - Fork 3
/
zinit-pkg-gen.sh
executable file
·1130 lines (1001 loc) · 27.4 KB
/
zinit-pkg-gen.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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Regenerate this list with
# ./zinit-gen-pkg.sh --update-ices
ZINIT_ICES=(
"aliases"
"as"
"atclone"
"atdelete"
"atinit"
"atload"
"atpull"
"autoload"
"!bash"
"bash"
"binary"
"bindmap"
"blockf"
"bpick"
"cargo"
"cloneonly"
"cloneopts"
"compile"
"countdown"
"cp"
"!csh"
"csh"
"debug"
"depth"
"dl"
"dlink"
"eval"
"extract"
"fbin"
"ferc"
"fmod"
"from"
"fsrc"
"gem"
"git"
"has"
"id-as"
"if"
"install"
"is-snippet"
"!ksh"
"ksh"
"light-mode"
"load"
"lucid"
"make"
"multisrc"
"mv"
"nocd"
"nocompile"
"nocompletions"
"node"
"notify"
"null"
"on-update-of"
"opts"
"pack"
"param"
"patch"
"pick"
"pip"
"proto"
"ps-on-unload"
"ps-on-update"
"pullopts"
"reset"
"reset-prompt"
"run-atpull"
"rustup"
"sbin"
"service"
"!sh"
"sh"
"silent"
"src"
"subscribe"
"subst"
"svn"
"trackbinds"
"trigger-load"
"unload"
"ver"
"verbose"
"wait"
"wrap"
)
PACKAGE_VARS=(
AUTHOR
DESCRIPTION
LICENSE
MESSAGE
NAME
PARAM_DEFAUT
REQUIREMENTS
URL
VERSION
)
# Note: this function needs to be defined below ZINIT_ICES (not above)
# Otherwise perl will update the regex below instead of the actual
# ZINIT_ICES array
update_zinit_ice_list() {
local file
file="${1:-$(basename "$0")}"
if [[ "$file" == "bash" ]]
then
echo_err "Cannot determine path to self."
echo_err "Please provide filepath: $0 --update-ices FILEPATH"
return 1
fi
# Note: We purposefully grep -v'd the pack ice here
# The first sed add quotes around the ices and replaces " " with "\n"
# The second one indents the values by 2 spaces
local ices
ices="$(zsh -ic "zinit --help" | tail -1 | \
sed -r -e 's/([^ ]+)/"\1"/g' -e $'s/ /\\\n/g' | \
sed -e 's/^/ /g' | \
grep -vE '^"pack"$' | \
sort -u)"
if [[ -z "$ices" ]]
then
echo_err "Failed to build ice list. Is zinit installed correctly?"
return 1
fi
echo_info "Updating ice list with:\n$ices"
echo_info "File to update (self): $file"
perl -i -p0e \
's/(ZINIT_ICES)=([^)]*)/\1=(\n'"${ices}"'\n/s' \
"$file"
}
echo_debug() {
[[ -z "$DEBUG" ]] && return
echo -e "\e[35m🐝 ${*}\e[0m" >&2
}
echo_info() {
echo -e "\e[34m🫐 ${*}\e[0m" >&2
}
echo_success() {
echo -e "\e[32m✅ ${*}\e[0m" >&2
}
echo_warn() {
echo -e "\e[33m🚸 ${*}\e[0m" >&2
}
echo_err() {
echo -e "\e[31m❌ ${*}\e[0m" >&2
}
zinit_parse_ice() {
local ice="$1" arg="$2" tmp
# Remove the atclone=? prefix
tmp="${arg##${ice}?(=)}"
# Remove quotes
tmp="${tmp#\"}"
tmp="${tmp%\"}"
tmp="${tmp#\'}"
tmp="${tmp%\'}"
# Replace newlines with spaces (the first sed expr)
# Then: replace tabs chars with spaces, remove duplicate spaces, and trim
sed -rz \
-e 's/\n/ /g' \
-e 's/\t/ /g' \
-e 's/ +/ /g' \
-e 's/ *$//' \
<<< "$tmp"
}
# Fake zinit function that outputs the ices as JSON
zinit() {
echo_debug "zinit invoked with: ${*@Q}"
local -A ices
local arg ice matched
local for repo plugin_org plugin_name
shopt -s extglob
for arg in "$@"
do
case "$arg" in
for)
# for is special, the next argument will be the repo
for=1
;;
*)
if [[ -n "$for" ]]
then
# remove leading '@', as in 'zinit xxx for @direnv/direnv'
repo="${arg#@}"
plugin_org="${repo%%/*}"
plugin_name="${repo##*/}"
# Ignore remaining args
break
else
matched=""
for ice in "${ZINIT_ICES[@]}"
do
if [[ "$arg" =~ ^${ice} ]]
then
matched="$arg"
break
fi
done
if [[ -z "$matched" ]]
then
echo_err "Unknown ice: $arg"
return 1
fi
ices["$ice"]="$(zinit_parse_ice "$ice" "$arg")"
fi
;;
esac
done
# Display PACKAGE_VARS
local var
for var in "${PACKAGE_VARS[@]}"
do
echo_debug "${var}=${!var}"
done
echo_debug "repo: $repo (org: $plugin_org - name: $plugin_name)"
# shellcheck disable=2030
echo_debug "ices: $(typeset -p ices)"
if [[ -z "$repo" ]]
then
echo_err "Missing repo name"
return 1
fi
# JSON output
local key
# shellcheck disable=2031
for key in "${!ices[@]}"
do
echo "$key"
echo "${ices[$key]}"
done | jq -e -n -R \
--arg author "$AUTHOR" \
--arg description "$DESCRIPTION" \
--arg license "$LICENSE" \
--arg message "$MESSAGE" \
--arg name "$NAME" \
--arg param_default "$PARAM_DEFAUT" \
--arg plugin_name "$plugin_name" \
--arg plugin_org "$plugin_org" \
--arg repo "$repo" \
--arg requirements "$REQUIREMENTS" \
--arg url "$URL" \
--arg version "$VERSION" \
'{
"author": $author,
"description": $description,
"ices": ({"requires": $requirements} + reduce inputs as $i ({}; . + {
($i): (input | (tonumber? // .))
})),
"license": $license,
"message": $message,
"name": $name,
"param_default": $param_default,
"plugin_name": $plugin_name,
"plugin_org": $plugin_org,
"repo": $repo,
"url": $url,
"version": $version,
}'
}
get_zinit_json_data() {
local pkg="$1" profile="$2"
local srcfile="${pkg}/${profile}.ices.zsh"
if ! [[ -e "$srcfile" ]]
then
echo_err "source file does not exist: $srcfile"
return 1
fi
# call our fake zinit func
# shellcheck disable=1090
source "$srcfile"
}
list_packages() {
local dir="$1"
while IFS= read -r -d '' pkg
do
basename "$pkg"
done < <(find "$dir" -maxdepth 1 -mindepth 1 -type d \
-and -not -path "${dir}/.git" -print0)
}
list_profiles() {
local package="$1"
find "$package" -iname '*.ices.zsh' -print0 | \
xargs -0 -L1 basename | \
sed -nr 's#(.+).ices.zsh#\1#p'
}
update_ices() {
cd "$(cd "$(dirname "$0")" >/dev/null 2>&1; pwd -P)" || exit 9
local pkg="$1"
local profile="$2"
local zinit_json="$3"
local srcfile="${pkg}/${profile}.ices.zsh"
local pkgfile="${pkg}/package.json"
echo_debug "Updating $pkgfile with:\n$(jq -c <<< "$zinit_json")"
# If the target package does not already have a package.json file,
# we need to use the template to get the min boilerplate
local input_file="$pkgfile"
if ! [[ -e "$pkgfile" ]]
then
input_file="$PWD/package.template.json"
fi
local tmpfile
tmpfile="$(mktemp)"
trap 'rm -f $tmpfile' EXIT INT
local generated_by generated_date
generated_by="$(extract_generated_by "$srcfile")"
generated_date="$(extract_creation_date "$srcfile")"
# strip the param ice if it is set to the same value as param-default
local param_default="$(extract_param_default "$srcfile")"
if [[ -n "$param_default" ]] && \
jq -er --arg default "$param_default" '.ices.param == $default' \
<<< "$zinit_json" >/dev/null
then
zinit_json="$(jq -er 'del(.ices.param)' <<< "$zinit_json")"
fi
# shellcheck disable=2153
jq -e \
--arg profile "$profile" \
--arg generated_by "$generated_by" \
--arg generated_date "$generated_date" \
--arg param_default "$param_default" \
--argjson data "$zinit_json" \
'.author = $data.author |
.description = $data.description |
.homepage = $data.url |
.license = $data.license |
.name = ($data.name // $data.repo) |
.version = $data.version |
.["zsh-data"]["plugin-info"]["generated-by"] = $generated_by |
.["zsh-data"]["plugin-info"]["generation-date"] = $generated_date |
.["zsh-data"]["plugin-info"].message = $data.message |
.["zsh-data"]["plugin-info"].plugin = $data.plugin_name |
.["zsh-data"]["plugin-info"]["param-default"] = $param_default |
.["zsh-data"]["plugin-info"].user = $data.plugin_org |
.["zsh-data"]["plugin-info"].version = $data.version |
.["zsh-data"]["zinit-ices"][$profile] = $data.ices' \
"$input_file" > "$tmpfile" || return 1
local data
data="$(jq -e . "$tmpfile")"
local rc="$?"
if [[ -n "$CHECK" ]]
then
local diff_msg
if diff_msg="$(diff \
<(jq --sort-keys . <<< "$data") \
<(jq --sort-keys . $pkgfile))"
then
echo_success "$pkgfile [$profile]: No change."
return
else
echo_warn "$pkgfile [$profile]: Files differ!"
echo "$diff_msg" >&2
return 1
fi
fi
if [[ -n "$DRY_RUN" ]]
then
jq <<< "$data"
return "$rc"
fi
if [[ "$rc" -eq 0 ]]
then
mv "$tmpfile" "$pkgfile" && {
echo_success "Updated ${package}/package.json [${profile}]"
} || {
echo_err "Failed to write ${package}/package.json"
return 1
}
fi
}
extract_generated_by() {
local srcfile="$1" # .ice.zsh file path
awk -F "by " '/Generated by/ {print $2}' "$srcfile"
}
extract_creation_date() {
local srcfile="$1" # .ice.zsh file path
awk '/^# [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{2}:[0-9]{2}/ { print $2; exit }' \
"$srcfile" 2>/dev/null
}
extract_param_default() {
local srcfile="$1" # .ice.zsh file path
local PARAM_DEFAULT
eval "$(awk '/^PARAM_DEFAULT=/' "$srcfile")"
echo "$PARAM_DEFAULT"
unset PARAM_DEFAULT
}
generate_package_json_profile() {
local package="$1"
local profile="$2"
local zinit_json_data
echo_debug "Processing package $package - profile: $profile"
if ! zinit_json_data="$(get_zinit_json_data "$package" "$profile")"
then
return "$?"
fi
update_ices "$package" "$profile" "$zinit_json_data"
}
generate_package_json() {
local package="$1"; shift
local -a profiles=("$@")
local filename
local filepath
local ice_file
local profile
echo_debug "Processing package $package"
# Check if we were provided with a file path
# eg: zinit-gen-pkg.sh null/default.ices.zsh
if [[ -f "$package" ]]
then
filepath="$(realpath "$package")"
filename="$(basename "$filepath")"
package="$(basename "$(dirname "$filepath")")"
profile="${filename//.ices.zsh}"
fi
# No profile provide, assume all
if [[ -z "${profiles[*]}" ]]
then
while IFS= read -r -d '' ice_file
do
profiles+=("$(basename "${ice_file//.ices.zsh}")")
done < <(find "$package" -type f -iname '*.ices.zsh' -print0)
fi
if [[ "${#profiles[@]}" -eq 0 ]]
then
echo_warn "Package $package doesn't contain any *.ices.zsh file"
return 1
fi
for profile in "${profiles[@]}"
do
generate_package_json_profile "$package" "$profile"
# Unset vars which may be set from the .ices.zsh files
eval unset "${PACKAGE_VARS[*]}"
done
}
generate_ices_zsh_files() {
local package="$1"; shift
local -a profiles=("$@")
echo_info "Generating ices.zsh for package $package"
local srcfile pkgfile="$package/package.json"
local content ice ice_data ice_val metadata plugin
local author description license message param_default plugin_url requirements \
url version
local -a ices
# Constants
local default_plugin="zdharma-continuum/null"
local modeline='# vim: set ft=zsh et ts=2 sw=2 :'
if ! [[ -f "$pkgfile" ]]
then
echo_err "Missing package.json in ${package}/"
return 1
fi
if ! jq -e . "$pkgfile" >/dev/null
then
echo_err "$pkgfile: Invalid JSON"
return 1
fi
if [[ -z "${profiles[*]}" ]]
then
readarray -t profiles < <(\
jq -e -r -c '.["zsh-data"]["zinit-ices"] | keys[]' "$pkgfile")
fi
echo_info "[${package}] Selected profiles: ${profiles[*]:-default}"
local profile
for profile in "${profiles[@]}"
do
echo_debug "Processing profile $profile"
srcfile="${package}/${profile}.ices.zsh"
metadata="$(jq -e -r '.["zsh-data"]["plugin-info"]' "$pkgfile")"
ice_data="$(jq -e -r --arg profile "$profile" \
'.["zsh-data"]["zinit-ices"][$profile]' "$pkgfile")"
# Metadata
# TODO Add more/better fallback logic here? For snippets for example
# we need to use the snippet url, or the $package name
plugin="$(jq -er --arg default "$default_plugin" '
(.user + "/" + .plugin) as $n |
if ($n != "/") then
$n
else
$default
end' <<< "$metadata")"
# items in root
author="$(jq -er '.author // ""' "$pkgfile")"
description="$(jq -er '.description // ""' "$pkgfile")"
license="$(jq -er '.license // ""' "$pkgfile")"
name="$(jq -er '.name // ""' "$pkgfile")"
url="$(jq -er '.homepage // ""' "$pkgfile")"
version="$(jq -er '.version // ""' "$pkgfile")"
# items in plugin-info
message="$(jq -er '.message // ""' <<< "$metadata")"
param_default="$(jq -er '.["param-default"] // ""' <<< "$metadata")"
plugin_url="$(jq -er '.url // ""' <<< "$metadata")"
# FIXME The requirements field really shouldn't be in the ices array...
requirements="$(jq -er '.requires // ""' <<< "$ice_data")"
local now
if [[ -n "$REPRODUCIBLE" ]]
then
# Re-use previous timestamp, fall back to UNIX ts 0
now="$(extract_creation_date "$srcfile")"
if [[ -z "$now" ]]
then
now="$(date -d @0 -Iseconds --utc)"
fi
else
now="$(date -Iseconds)"
fi
# Sanitize metadata
local var val
for var in author description license message name param_default \
requirements url version
do
# FIXME the lines below feel wrong
val="${!var}"
[[ -z "$val" ]] && continue
val="$(sed -z "s/'/\\\'/g; s/\n/\\\\\\\\\\\\\\\\n/g" <<< "$val")"
val="${val%$'\\\\\\\\\\\\\\\\n'}"
val="$(sed -z "s/\\\'/\\\\\'\\\\\\\\\\\\'\\\\\'/g" <<< "$val")"
eval "${var}=\$'${val}'"
done
content="# zinit package for ${package} [${profile}]\n"
content+="# Generated by $(basename "$0")\n"
content+="# ${now}\n"
content+="AUTHOR='${author}'\n"
content+="DESCRIPTION='${description}'\n"
content+="LICENSE='${license}'\n"
content+="MESSAGE='${message}'\n"
content+="NAME='${name}'\n"
content+="PARAM_DEFAULT='${param_default}'\n"
content+="REQUIREMENTS='${requirements}'\n"
content+="URL='${url}'\n"
content+="VERSION='${version}'\n"
# Add zinit call to output (static)
content+='\nzinit \\\n'
# Process ices
readarray -t ices < <(\
jq -e -r -c 'keys[] | select(
(contains("requires") | not) and
(contains("plugin") | not))' <<< "$ice_data")
# Prepend id-as, if not already in the ices array
if ! [[ " ${ices[*]} " =~ " id-as " ]]
then
ices=(id-as "${ices[@]}")
fi
# Add param ice
if ! [[ " ${ices[*]} " =~ " param " ]] && [[ -n "$param_default" ]]
then
ices+=("param")
fi
for ice in "${ices[@]}" # note: $ices holds the ice names only
do
# 1st sed: escape '\n' to '\\n' to avoid it getting mangled by echo -e
# (escapes '\\n' to '\\\n' too)
# 2nd sed -> We need to properly encode single quotes since we are
# using these to quote the ice values below
ice_val="$(jq -e -r --arg ice "$ice" '.[$ice] // ""' <<< "$ice_data" | \
sed -r 's#(\\+n)#\\\\\\\1#g' | \
sed "s/'/'\\\''/g")"
case "$ice" in
is-snippet)
is_snippet=1
;;
id-as)
# if the plugin does not have a propper id then use the package name
# 1. Use plugin name (if != zdharma-continuum/null)
# 2. Default to NAME
# 3. If NAME is not set, use zinit-package-$package
if [[ "$plugin" != "$default_plugin" ]]
then
ice_val="${ice_val:-${plugin}}"
else
ice_val="${ice_val:-${name:-zinit-package-${package}}}"
fi
;;
param)
[[ -z "$ice_val" ]] && ice_val="$param_default"
;;
esac
echo_debug "$ice value: ${ice_val:-\"\"}"
content+=" $ice"
# 1st expr: Add newlines after && and ; (should only occur within
# atclone/atpull)
# 2nd expr: Remove trailing whitespace
ice_val="$(sed -r \
-e 's#(&&|;) #\1\n #g' \
<<< "$ice_val")"
# Append the quoted value of the ice
if [[ -n "$ice_val" ]]
then
content+="'${ice_val}'"
fi
content+=' \\\n'
done
if [[ -n "$is_snippet" ]] && [[ -n "$plugin_url" ]]
then
# Use single quotes here since the url might contain vars with expansions
# that bash is incapable of handling (eg: ${(M)OSTYPE#(linux|darwin)})
# It's safe to use single quotes here. zinit will expand vars in the
# plugin url.
content+=" for '${plugin_url}'"
else
content+=" for @${plugin}"
fi
content+="\n\n$modeline"
echo_debug "Generated content for ${srcfile}:\n${content}"
if [[ -n "$CHECK" ]]
then
if echo -e "$content" | diff "$srcfile" -
then
echo_success "$srcfile: No change."
return
else
echo_warn "$srcfile: Files differ!"
return 1
fi
fi
if [[ -n "$DRY_RUN" ]]
then
echo -e "$content"
echo
else
echo -e "$content" > "$srcfile" && {
echo_success "Generated $srcfile"
} || {
echo_err "Failed to generate $srcfile"
return 1
}
fi
done
}
create_package() {
local package="$1"; shift
local profiles=("$@")
local script_dir
script_dir="$(cd "$(dirname "$0")" >/dev/null 2>&1 || return 1; pwd -P)"
local dest="${script_dir}/${package}"
local pkgfile="${dest}/package.json"
echo_info "Creating new package: ${package} (${dest})"
mkdir -pv "${dest}"
if ! [[ -e ${pkgfile} ]]
then
cp -v "${script_dir}/package.template.json" "${pkgfile}"
fi
local pkgdata
pkgdata="$(cat "$pkgfile")"
# Create additional profiles
local profile
for profile in "${profiles[@]}"
do
echo_debug "Processing profile $profile"
if [[ -e "${dest}/${profile}.ices.zsh" ]] && [[ -z "$FORCE" ]]
then
echo_err "Package profile $profile already exists for $package."
return 1
fi
pkgdata="$(jq -e -r --arg profile "$profile" \
'.["zsh-data"]["zinit-ices"][$profile].requires = ""' <<< "$pkgdata")"
done
echo "$pkgdata" > "$pkgfile"
# Generate the .ices.zsh files
generate_ices_zsh_files "$package"
}
fetch_zinit_docker_run() {
if [[ -x docker-run.sh ]]
then
realpath docker-run.sh
return
fi
local url="https://raw.githubusercontent.com/zdharma-continuum/zinit/main/scripts/docker-run.sh"
cd "$(cd "$(dirname "$0")" >/dev/null 2>&1; pwd -P)" || exit 9
rm -fv docker-run.sh
echo_info "Fetching docker-run.sh from $url"
if ! curl -fsSL "$url" > docker-run.sh
then
rm -f docker-run.sh
echo_err "Failed to download docker-run.sh"
return 1
fi
chmod +x docker-run.sh
realpath docker-run.sh
}
run_package() {
local package="$1"
local profile="${2:-default}"
shift 2
if [[ "$profile" =~ -|-- ]]
then
profile="default"
fi
local -a cmd
cmd=("$(fetch_zinit_docker_run)" --env QUIET=1)
if [[ -n "$DEBUG" ]]
then
cmd=(bash -x "${cmd[0]}")
fi
local ices_file="${package}/${profile}.ices.zsh"
local pkgjson="${package}/package.json"
if [[ -n "$REGENERATE" ]]
then
if [[ -n "$RUN_PACKAGE" ]]
then
if ! generate_package_json_profile "$package" "$profile"
then
echo_err "Failed to generate package.json"
return 1
fi
else
if ! generate_ices_zsh_files "$package" "$profile"
then
echo_err "Failed to generate ${profile}.ices.zsh"
return 1
fi
fi
fi
if ! [[ -r "$ices_file" ]]
then
echo_err "Unable to read from ${ices_file}"
return 2
fi
local args=()
if [[ -n "$NON_INTERACTIVE" ]]
then
args=(zsh -ilsc '@zinit-scheduler burst')
# args=(zsh -ilsc 'exit $?')
fi
if [[ -n "$RUN_PACKAGE" ]]
then
echo_info "🐳 Running zinit pack'${pkgjson}:${profile}' for @${package}"
"${cmd[@]}" --volume "$PWD:/devel" \
--config "zinit pack'/devel/${pkgjson}:${profile}' for @${package}" \
"${args[@]}"
else
echo_info "🐳 Running with file: $ices_file"
"${cmd[@]}" --file "$ices_file" "${args[@]}"
fi
}
usage() {
echo "Usage: $(basename "$0") ACTION [ARGS] [PACKAGE] [PROFILES...]"
echo "ACTIONS: create|gen-json|gen-ices|run|update-ices"
echo
echo "Global flags:"
echo " --check Check if generated files are different"
echo " --debug Debug mode"
echo " --dry-run Don't write files, just display what was generated to stdout"
echo " --parallel Async generation of ices.zsh/package.json"
echo
echo "Actions:"
echo " create PACKAGE [PROFILES...] Create new packages or profiles"
echo " --force Force creation of files regardless if they already exist"
echo " gen-json [PACKAGE] [PROFILES...] Generate package.json files from source ices.zsh"
echo " gen-ices [PACKAGE] [PROFILES...] Generate ices.zsh from package.json"
echo " --reproducible Re-use generation timestamps (defaults to UNIX time 0 if JSON/ices.zsh file does not exist yet)"
echo " run PACKAGE [PROFILE] Run a given package inside a container"
echo " --non-interactive Don't keep the container running but exit right away"
echo ' --pack Run zinit pack"PROFILE" PACKAGE instead of sourcing the ices.zsh file'
echo " --regenerate Regenerate ices.zsh (or package.json if --pack) prior to running"
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
CHECK="${CHECK:-}"
DEBUG="${DEBUG:-}"
DRY_RUN="${DRY_RUN:-}"
FAST="${FAST:-}"
FORCE="${FORCE:-}"
NON_INTERACTIVE="${NON_INTERACTIVE:-}"
RUN_PACKAGE="${RUN_PACKAGE:-}"
REPRODUCIBLE="${REPRODUCIBLE:-}"
REGENERATE="${REGENERATE:-}"
ACTION="${ACTION:-generate-json}"
ARGS=("$@")
for arg in "${ARGS[@]}"
do
case "$arg" in
-c|--check)
CHECK=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-d|--debug)
DEBUG=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-f|--force)
FORCE=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-F|--fast|--parallel)
FAST=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-k|--dry-run)
DRY_RUN=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-n|--non-interactive|-ni|--exit|-e)
NON_INTERACTIVE=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-p|--pack|--package)
RUN_PACKAGE=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-R|-rp|--rep|--repro|--reproducible)
REPRODUCIBLE=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
--regen*|-rr)
REGENERATE=1
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
# Action flags
-C|--create|-i|--init)
ACTION=create
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-r|--rev|--reverse)
ACTION=generate-ices-zsh
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
--run)
ACTION=run
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
--update-ices)
ACTION=update-ices
IFS=" " read -r -a ARGS <<< "${ARGS[@]/$arg}"
;;
-h|--help)
usage
exit 0
;;
esac
done
set -- "${ARGS[@]}"
# Alternative usage: $0 create|gen-json|gen-ices|run|update-ices
case "$1" in
create|init|c|i)
ACTION=create
shift
;;
json|generate|gen|gen-json|g|j)
ACTION=generate-json
shift
;;
gen-ices|reverse|rev|r)
ACTION=generate-ices-zsh
shift
;;
run|R)
ACTION=run
shift
;;
update-self|update-ices|u)
ACTION=update-ices
shift
;;
esac
# Special snowflake, let's execute this right away.
if [[ "$ACTION" == update-ices ]]
then
update_zinit_ice_list "$1"
exit "$?"
fi
PACKAGE="$1"; shift
PROFILES=("$@")
if [[ -z "$PACKAGE" ]]
then
echo_err "Missing PACKAGE name"
usage >&2
exit 2
fi
PACKAGES=()
# Check if we were provided with a file
# eg: zinit-gen-pkg.sh null/default.ices.zsh
if [[ -f "$PACKAGE" ]]
then
# Assume the user wants to test/run/generate a package.json file directly if
# providing a path to a package.json
if [[ "${PACKAGE}" =~ package.json ]]
then
RUN_PACKAGE=1