-
Notifications
You must be signed in to change notification settings - Fork 0
/
epm
executable file
·1100 lines (988 loc) · 36.5 KB
/
epm
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
# safety function to avoid executing anything in case this script is not downloaded properly
function main() {
##########################
#--[ Global Variables ]--#
##########################
# @VARIABLE: VERSION
# @DESCRIPTION:
# Version of this script.
VERSION=0.3.9
# Default base URL for short package names
BASE_URL=${EPM_REPO_BASE:-"https://github.com/e-wrks"}
# @VARIABLE: SCRIPT
# @DESCRIPTION:
# Name of this script. This will be the
# shell name if this script is sourced, so
# only rely on this for echos and trivial things.
SCRIPT="$(basename "$0")"
# the nearest home directory for local packages
EPM_HOME="Yet-Discovered"
# all effective epm home's bin directory separated by colon (:)
EPM_PATH=""
init_epm_home() {
(
[ -w "${EPM_HOME}/edh_modules" ] || edo mkdir "${EPM_HOME}/edh_modules"
[ -w "${EPM_HOME}/edh-universe/bin" ] || edo mkdir -p "${EPM_HOME}/edh-universe/bin"
[ -w "${EPM_HOME}/python_modules" ] || edo mkdir "${EPM_HOME}/python_modules"
[ -w "${EPM_HOME}/node_modules" ] || edo mkdir "${EPM_HOME}/node_modules"
) || die "Can NOT create EPM home artifacts in [${EPM_HOME}]"
}
locate_epm_home() {
locate_home_at() { (
parent_dir=$(dirname "$1")
if [ "${parent_dir}" == "$1" ]; then
ppath=""
else
ppath=$(locate_home_at "${parent_dir}")
fi
if [ -x "$1"/edh-universe/bin ]; then
if [ -n "${ppath}" ]; then
echo "$1"/edh-universe/bin:"${ppath}"
else
echo "$1"/edh-universe/bin
fi
else
echo "${ppath}"
fi
); }
EPM_PATH=$(locate_home_at "$(pwd)")
EPM_HOME="${EPM_PATH%%/edh-universe/bin*}"
if [ -e "${EPM_HOME}" ]; then
if [ ! -d "${EPM_HOME}" ]; then
die "Not a directory: ${EPM_HOME}"
elif [ -w "${EPM_HOME}" ]; then
status_message " >> Managing packages at EPM home [${EPM_HOME}] <<"
elif ${MODIFYING_EPM_HOME}; then
red_message " >> Readonly EPM home at [${EPM_HOME}] <<"
return 1
fi
elif ${CREATING_EPM_HOME}; then
EPM_HOME=$(pwd)
warning_message "Creating new EPM home at [${EPM_HOME}] ..."
init_epm_home
if [ -n "${EPM_PATH}" ]; then
EPM_PATH="${EPM_HOME}/edh-universe/bin:${EPM_PATH}"
else
EPM_PATH="${EPM_HOME}/edh-universe/bin"
fi
else
warning_message "No effective EPM home for directory [$(pwd)]"
return 1
fi
export EPM_PATH EPM_HOME
}
add_cabal_packages() { (
find -L . -name \*.cabal |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r fn; do
relfn=${fn#./}
shadowed=0
for dhome in ${DESCENDANT_HOMES}; do
[ -e "${dhome}"/edh-universe/"${relfn}" ] && shadowed=1
done
if [ "${shadowed}" == "0" ]; then
echo " ${rel_univ}/${relfn}"
fi
done
); }
add_stack_packages() { (
find -L . -name \*.cabal -o -name package.yaml |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r fn; do
pkg_dir="$(dirname "${fn}")"
if [[ "$fn" =~ package\.yaml$ ]] && [ -e "${pkg_dir}"/*.cabal ]; then
continue # should have been added due to the .cabal file
fi
relfn=${fn#./}
shadowed=0
for dhome in ${DESCENDANT_HOMES}; do
[ -e "${dhome}"/edh-universe/"${relfn}" ] && shadowed=1
done
if [ "${shadowed}" == "0" ]; then
echo " - ${rel_univ}/${pkg_dir#./}"
fi
done
); }
update_project_packages() {
cd "${EPM_HOME}/edh-universe" || die "Bad EPM home [${EPM_HOME}]"
if [ ! -e "avoid-cabal.project" ]; then
(
if [ -f "cabal.project" ]; then
while IFS= read -r line; do
[ "${line}" == '-- FOLLOWING CONTENTS GONNA BE OVERWRITTEN BY EPM --' ] && break
echo "$line"
done <"cabal.project"
else
echo ''
echo "-- Please change Cabal project settings per your needs"
echo ''
fi
echo '-- FOLLOWING CONTENTS GONNA BE OVERWRITTEN BY EPM --'
echo ''
echo "-- * this file should be:"
echo "-- ${EPM_HOME}/edh-universe/cabal.project"
echo ''
echo 'packages:'
export DESCENDANT_HOMES=''
export rel_univ='.'
add_cabal_packages
export DESCENDANT_HOMES="${EPM_HOME}"
process_parent_homes_from() { (
parent_dir=$(dirname "$1")
if [ "${parent_dir}" == "$1" ]; then
return 0
fi
rel_univ="${parent_dir}"/edh-universe
if [ -x "${rel_univ}" ]; then
cd "${rel_univ}"
EPM_HOME="${parent_dir}" add_cabal_packages
export DESCENDANT_HOMES="${DESCENDANT_HOMES} ${parent_dir}"
fi
process_parent_homes_from "${parent_dir}"
); }
process_parent_homes_from "${EPM_HOME}"
) >"_new_cabal.project"
edo mv -f "_new_cabal.project" "cabal.project"
fi
if [ ! -e "avoid-stack.yaml" ]; then
(
if [ -f "stack.yaml" ]; then
while IFS= read -r line; do
[ "${line}" == '## FOLLOWING CONTENTS GONNA BE OVERWRITTEN BY EPM ##' ] && break
echo "$line"
done <"stack.yaml"
else
echo ''
echo "# Please change the resolver and other Stack settings per your needs"
echo 'resolver: lts-22.24'
echo ''
fi
echo '## FOLLOWING CONTENTS GONNA BE OVERWRITTEN BY EPM ##'
echo ''
echo "## * this file should be:"
echo "## ${EPM_HOME}/edh-universe/stack.yaml"
echo ''
echo "local-bin-path: ${EPM_HOME}/edh-universe/bin"
echo 'packages:'
export DESCENDANT_HOMES=''
export rel_univ='.'
add_stack_packages
export DESCENDANT_HOMES="${EPM_HOME}"
process_parent_homes_from() { (
parent_dir=$(dirname "$1")
if [ "${parent_dir}" == "$1" ]; then
return 0
fi
rel_univ="${parent_dir}"/edh-universe
if [ -x "${rel_univ}" ]; then
cd "${rel_univ}"
EPM_HOME="${parent_dir}" add_stack_packages
export DESCENDANT_HOMES="${DESCENDANT_HOMES} ${parent_dir}"
fi
process_parent_homes_from "${parent_dir}"
); }
process_parent_homes_from "${EPM_HOME}"
) >"_new_stack.yaml"
edo mv -f "_new_stack.yaml" "stack.yaml"
fi
}
# whether we tend to create an EPM home
CREATING_EPM_HOME=false
# whether we tend to modify an EPM home
MODIFYING_EPM_HOME=false
# @VARIABLE: VERBOSE
# @DESCRIPTION:
# Whether to print verbose messages in this script.
VERBOSE=false
# @VARIABLE: FORCE
# @DESCRIPTION:
# Whether to force installation and overwrite files.
FORCE=false
# options in list subcommand
SHOW_REPO=false
SHOW_BRANCH=false
SHOW_MODULES=false
SHOW_HS_PKGS=false
# options in update subcommand
PULL_UPSTREAM=true
####################
#--[ Print Help ]--#
####################
# @FUNCTION: usage
# @DESCRIPTION:
# Print the help message for 'epm' to STDERR
# and exit the script with status code 1.
usage() {
(echo >&2 "epm ${VERSION} >> Edh Package Manager <<
USAGE:
${SCRIPT} [FLAGS] <SUBCOMMAND>
FLAGS:
-v, --verbose Enable verbose output
-h, --help Prints help information
-V, --version Prints version information
-B, --base <URL> URL prefix for upstream package repositories
default: ${BASE_URL}
environment variable EPM_REPO_BASE overrides above
SUBCOMMANDS:
init Initialize current working directory as an EPM home
install | i Install new, or change branches of existing packages
list | l List homes and packages
update | up | u Pull upstream changes of packages from tracked branches
run | exec | x Run command with all effective EPM home's bin on \$PATH
rm Remove specified package(s) from nearest EPM home
")
exit 1
}
# @FUNCTION: init_usage
# @DESCRIPTION:
# Print the help message for 'epm init' to STDERR
# and exit the script with status code 1.
init_usage() {
(echo >&2 "epm-init
Initialize current working directory as an EPM home, with packages as specified
USAGE:
${SCRIPT} init [FLAGS] [ -B <repo-base> ] [ -b <branch> ] [ <PKGn> | <URLn> ] ...
FLAGS:
-h, --help Prints help information
ARGS:
-b <branch> Branch to checkout and track
only applies to the immediately following package
-B, --base <URL> URL prefix for upstream package repositories
default: ${BASE_URL}
environment variable EPM_REPO_BASE overrides above
<PKGn> E.g. nedh or sedh
<URLn> E.g. \"https://github.com/e-wrks/hasdb\"
EXAMPLES:
epm init -b 0.3 edh -b 0.1 nedh \\
-B https://git.corporate.lan -b golden esb/cord -b qa esb/mart
")
exit 1
}
# @FUNCTION: install_usage
# @DESCRIPTION:
# Print the help message for 'epm install' to STDERR
# and exit the script with status code 1.
install_usage() {
(echo >&2 "epm-install
Install packages as specified
USAGE:
${SCRIPT} install [FLAGS] [ -B <repo-base> ] [ -b <branch> ] [ <PKGn> | <URLn> ] ...
FLAGS:
-h, --help Prints help information
ARGS:
-b <branch> Branch to checkout and track
only applies to the immediately following package
-B, --base <URL> URL prefix for upstream package repositories
default: ${BASE_URL}
environment variable EPM_REPO_BASE overrides above
<PKGn> E.g. nedh or sedh
<URLn> E.g. \"https://github.com/e-wrks/hasdb\"
EXAMPLES:
epm install -b 0.3 edh -b 0.1 nedh \\
-B https://git.corporate.lan -b golden esb/cord -b qa esb/mart
")
exit 1
}
# @FUNCTION: epm_exec_usage
# @DESCRIPTION:
# Print the help message for 'epm run' to STDERR
# and exit the script with status code 1.
epm_exec_usage() {
(echo >&2 "epm-run
Run command with all effective EPM home's bin on \$PATH
USAGE:
${SCRIPT} run [FLAGS] <CMD> [ <ARGn> ... ]
FLAGS:
-h, --help Prints help information
ARGS:
<CMD> [ <ARGn> ... ] E.g. \"edhm\" or \"stack run hski\"
")
exit 1
}
# @FUNCTION: rm_usage
# @DESCRIPTION:
# Print the help message for 'epm rm' to STDERR
# and exit the script with status code 1.
rm_usage() {
(echo >&2 "epm-rm
Remove the specified packages installed by epm
USAGE:
${SCRIPT} rm [FLAGS] <PKG1> [ <PKG2> ... ]
FLAGS:
-h, --help Prints help information
ARGS:
<PKGn> E.g. \"nedh\" or \"sedh\"
")
exit 1
}
# @FUNCTION: list_usage
# @DESCRIPTION:
# Print the help message for 'epm list' to STDERR
# and exit the script with status code 1.
list_usage() {
(echo >&2 "epm-list
Show locally installed packages
USAGE:
${SCRIPT} list [FLAGS]
FLAGS:
-h, --help Prints help information
-u, --upstream Show repository URL
-b, --branch Show branches
-m, --modules List Edh modules
-H, --haskell List Haskell packages
")
exit 1
}
# @FUNCTION: update_usage
# @DESCRIPTION:
# Print the help message for 'epm update' to STDERR
# and exit the script with status code 1.
update_usage() {
(echo >&2 "epm-update
Pull upstream changes of packages from tracked branches
USAGE:
${SCRIPT} update [FLAGS]
FLAGS:
-h, --help Prints help information
-e, --edit Only per local edit, i.e. no pull from upstream
")
exit 1
}
############################
#--[ Subcommand install ]--#
############################
install_pkgs() { (
cd "${EPM_HOME}"
while [ $# -gt 0 ]; do
case $1 in
-B | --base)
if [ $# -lt 2 ]; then
install_usage
fi
BASE_URL="$2"
shift 2
;;
-b)
if [ $# -lt 2 ]; then
install_usage
fi
BRANCH="$2"
shift 2
;;
*)
install_pkg "$1"
# -b has to be specified per pkg
unset BRANCH
shift 1
;;
esac
done
update_project_packages
); }
# @FUNCTION: install_pkg
# @USAGE: <PKG>
# @DESCRIPTION:
# Installs the specified package
install_pkg() {
if [ "${1#*://}" == "$1" ]; then
full_url="${BASE_URL}/$1"
else
full_url="$1"
fi
full_url=${full_url%%.git}
if [ "${full_url#*://}" == "${full_url}" ]; then
pkg_id="${1%%.git}"
else
pkg_id=${full_url#*://*/}
fi
pkg_dir=edh-universe/"${pkg_id}"
status_message "Installing $1 to ${pkg_dir} ..."
(
if [ -d "${pkg_dir}" ]; then
edo pushd "${pkg_dir}" >/dev/null
edo git fetch --all
if [ -z "${BRANCH}" ]; then
edo git checkout -t $(git symbolic-ref refs/remotes/origin/HEAD)
else
edo git checkout -B "${BRANCH}" -t origin/"${BRANCH}"
fi
edo popd >/dev/null
else
if [ -z "${BRANCH}" ]; then
edo git clone "${full_url}" "${pkg_dir}"
else
edo git clone -b "${BRANCH}" "${full_url}" "${pkg_dir}"
fi
fi
) || die "Failed cloning ${full_url} to ${pkg_dir}"
(
edo pushd edh-universe/bin >/dev/null
for a in ../../"${pkg_dir}"/bin/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed linking scripting artifacts"
(
edo pushd edh_modules >/dev/null
for a in ../"${pkg_dir}"/edh_modules/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed linking Edh module artifacts"
(
edo pushd python_modules >/dev/null
for a in ../"${pkg_dir}"/host.py/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed linking Python module artifacts"
(
edo pushd node_modules >/dev/null
for a in ../"${pkg_dir}"/host.js/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed linking NodeJS module artifacts"
status_message "Installed $1 ."
unset full_url pkg_id pkg_dir a b
}
#########################
#--[ Subcommand list ]--#
#########################
# @FUNCTION: list
# @USAGE: <tool> <raw-format> <criteria>
# @DESCRIPTION:
# List local packages installed by epm
list_pkgs() { (
IFS=: ps=(${EPM_PATH})
for p in ${ps[@]}; do
EPM_HOME=${p%%/edh-universe/bin}
status_message " > EPM home at [${EPM_HOME}] <"
cd "${EPM_HOME}"
find -L edh-universe -name edh_modules |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r pkg_dir1; do
pkg_dir="${pkg_dir1%/edh_modules}"
pkg_id="${pkg_dir#edh-universe/}"
(
edo pushd "${pkg_dir}" >/dev/null
if [ -d .git ]; then
git update-index --refresh
echo "${pkg_id} - $(git describe --all --dirty --broken)"
if ${SHOW_REPO}; then
echo " - From: $(
git config "remote.$(git config branch.$(git rev-parse --abbrev-ref HEAD).remote).url"
)"
fi
if ${SHOW_BRANCH}; then
echo " - Branch: $(edo git branch -vv)"
fi
else
echo "${pkg_id} - <no git>"
fi
if ${SHOW_MODULES}; then
echo " - Modules:"
find -L edh_modules -type f | while read -r modu; do
echo " ${modu}"
done
fi
edo popd >/dev/null
) || die "Failed browsing package ${pkg_id}"
done
if ${SHOW_HS_PKGS}; then
echo ""
echo " * Haskell packages:"
echo ""
find -L edh-universe -name '*.cabal' -o -name 'package.yaml' |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r cabal_file; do
pkg_id="${cabal_file#edh-universe/}"
pkg_dir=$(dirname "${cabal_file}")
if [[ "$fn" =~ package\.yaml$ ]] && [ -e "${pkg_dir}"/*.cabal ]; then
continue # should have been displayed due to the .cabal file
fi
(
edo pushd "${pkg_dir}" >/dev/null
git update-index --refresh
echo "${pkg_id} - $(git describe --all --dirty --broken)"
if ${SHOW_REPO}; then
echo " - From: $(
git config "remote.$(git config branch.$(git rev-parse --abbrev-ref HEAD).remote).url"
)"
fi
if ${SHOW_BRANCH}; then
echo " - Branch: $(edo git branch -vv)"
fi
edo popd >/dev/null
) || die "Failed browsing package ${pkg_id}"
done
fi
done
); }
#########################
#--[ Subcommand run ]--#
#########################
# Run command with all effective EPM home's bin on $PATH
epm_exec() {
if [ "$1" == "cabal" -a "$2" == "install" ]; then
shift 2
# implant `--overwrite-policy=always --installdir=${EPM_HOME}/edh-universe/bin`
# to `cabal install` cmdl, magically !
set -- cabal install --overwrite-policy=always --installdir=${EPM_HOME}/edh-universe/bin "$@"
fi
PYP=$(
PYP=""
IFS=: ps=(${EPM_PATH})
for p in ${ps[@]}; do
EPM_HOME=${p%%/edh-universe/bin}
if [ -n "${PYP}" ]; then
PYP="${PYP}":"${EPM_HOME}"/python_modules
else
PYP="${EPM_HOME}"/python_modules
fi
done
echo "${PYP}"
)
if [ -n "${PYTHONPATH}" ]; then
export PYTHONPATH="${PYP}":"${PYTHONPATH}"
else
export PYTHONPATH="${PYP}"
fi
export PATH="${EPM_PATH}":"${PATH}"
exec "$@"
red_message "You've been kicked back."
}
#########################
#--[ Subcommand update ]--#
#########################
# Update local packages installed by epm
update_pkgs() { (
cd "${EPM_HOME}"
if ${PULL_UPSTREAM}; then
find -L edh-universe -type d -name .git |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r git_dir; do
repo_dir=$(dirname "${git_dir}")
(
edo pushd "${repo_dir}" >/dev/null
status_message "Updating repository ${repo_dir}"
if [ -n "$(git config branch.$(git rev-parse --abbrev-ref HEAD).merge)" ]; then
git pull
fi
edo popd >/dev/null
) || die "Failed updating repository ${repo_dir}"
done
fi
find -L edh-universe -name edh_modules |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r pkg_dir1; do
pkg_dir="${pkg_dir1%/edh_modules}"
pkg_id="${pkg_dir#edh-universe/}"
status_message "Updating package ${pkg_id}"
(
find edh-universe/bin -type l | while read -r script_art; do
mr_path=$(readlink "${script_art}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${script_art}" || die "Failed unlink ${script_art}"
fi
done
edo pushd edh-universe/bin >/dev/null
for a in ../../"${pkg_dir}"/bin/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed re-linking Edh module artifacts"
(
find edh_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
edo pushd edh_modules >/dev/null
for a in ../"${pkg_dir}"/edh_modules/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed re-linking Edh module artifacts"
(
find python_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
edo pushd python_modules >/dev/null
for a in ../"${pkg_dir}"/host.py/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed re-linking Python module artifacts"
(
find node_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
edo pushd node_modules >/dev/null
for a in ../"${pkg_dir}"/host.js/*; do
# deal with empty dir without nullglob
[ ! -e "$a" ] && continue
b=$(basename "$a")
edo rm -rf "./$b" 2>/dev/null
edo ln -s "$a" "$b"
done
edo popd >/dev/null
) || die "Failed re-linking NodeJS module artifacts"
done
); }
#######################
#--[ Subcommand rm ]--#
#######################
# @FUNCTION: rm_pkgs
# @USAGE: <ghcversion>
# @DESCRIPTION:
# Removes the specified package(s) installed by epm.
rm_pkgs() { (
cd "${EPM_HOME}"
find -L edh-universe -name edh_modules |
fgrep -v '/dist-newstyle/' | fgrep -v '/.stack-work/' |
while read -r pkg_dir1; do
pkg_dir="${pkg_dir1%/edh_modules}"
pkg_id="${pkg_dir#edh-universe/}"
edo pushd "${pkg_dir}" >/dev/null
pkg_repo="$(
git config remote.$(git config branch.$(git branch --format '%(refname:short)').remote).url
)"
edo popd >/dev/null
for name2rm in "$@"; do
if [ "${pkg_id%/${name2rm}}" != "${pkg_id}" ]; then
warning_message "Package [${pkg_id}] has upstream ${pkg_repo}"
if ask_for_confirmation "Remove it from [${EPM_HOME}] ?"; then
status_message "Removing ${pkg_dir} ..."
find edh_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
find python_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
find node_modules -type l | while read -r modu_root; do
mr_path=$(readlink "${modu_root}")
if [ "${mr_path#../${pkg_dir1}/}" != "${mr_path}" ]; then
edo rm "${modu_root}" || die "Failed unlink ${modu_root}"
fi
done
edo rm -rf "${pkg_dir}"
fi
fi
done
done
update_project_packages
); }
###########################
#--[ Utility functions ]--#
###########################
# @FUNCTION: die
# @USAGE: [msg]
# @DESCRIPTION:
# Exits the shell script with status code 2
# and prints the given message in red to STDERR, if any.
die() {
(red_message "$1")
exit 2
}
# @FUNCTION: edo
# @USAGE: <command>
# @DESCRIPTION:
# Executes the given command. Also prints what
# command that is (in blue) if verbosity is enabled.
# Exits with status code 2 if the command failed.
edo() {
if ${VERBOSE}; then
printf >&2 ' 🐞 %s\n' "$*"
fi
"$@" || exit 2
}
# @FUNCTION: debug_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a blue debug message if verbosity is enabled.
debug_message() {
if ${VERBOSE}; then
printf >&2 ' 🐞 %s\n' "$1"
fi
}
# @FUNCTION: optionv
# @USAGE: <arg1> [arg2]
# @DESCRIPTION:
# If verbosity is enabled, echo the first argument, otherwise
# the second (if any).
# @STDOUT: first or second argument
optionv() {
if ${VERBOSE}; then
echo "$1"
else
if [ -n "$2" ]; then
echo "$2"
fi
fi
}
# @FUNCTION: status_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a green status message.
status_message() {
printf >&2 ' ℹ️ %s\n' "$1"
}
# @FUNCTION: warning_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a yellow warning message.
warning_message() {
printf >&2 ' ⚠️ %s\n' "$1"
}
# @FUNCTION: red_message
# @USAGE: <msg>
# @DESCRIPTION:
# Print a red message.
red_message() {
printf >&2 ' ❗ %s\n' "$1"
}
# @FUNCTION: command_exists
# @USAGE: <command>
# @DESCRIPTION:
# Check if a command exists (no arguments).
# @RETURNS: 0 if the command exists, non-zero otherwise
command_exists() {
[ -z "$1" ] && die "Internal error: no argument given to command_exists"
command -V "$1" >/dev/null 2>&1
return $?
}
# @FUNCTION: check_required_commands
# @USAGE: [additional-commands]
# @DESCRIPTION:
# Check that all required commands for this script exist.
# @STDOUT: The commands that do not exist
# @RETURNS: 0 if all command exists, non-zero otherwise
check_required_commands() {
_missing_commands=
for com in "$@" basename dirname find fgrep git; do
command_exists "${com}" || {
_missing_commands="${_missing_commands} ${com}"
}
done
unset com
if [ -n "${_missing_commands}" ]; then
printf >&2 '%s\n' "${_missing_commands}"
unset _missing_commands
return 1
else
unset _missing_commands
return 0
fi
}
# @FUNCTION: ask_for_confirmation
# @USAGE: [confirmation-msg]
# @DESCRIPTION:
# Asks the user for confirmation and returns 0 for yes, 1 for no.
# @RETURN: 0 if user confirmed, 1 otherwise
ask_for_confirmation() {
confirmation_msg=$1
if [ -n "${confirmation_msg}" ]; then
printf >&2 '%s\n(y/n and press Enter)\n' "${confirmation_msg}"
else
printf >&2 'Confirm action: (y/n and press Enter)\n'
fi
read -r answer </dev/tty
if [ "${answer}" != "${answer#[Yy]}" ]; then
return 0
else
return 1
fi
unset confirmation_msg answer
}
##############################################
#--[ Command line parsing and entry point ]--#
##############################################
[ $# -lt 1 ] && usage
while [ $# -gt 0 ]; do
case $1 in
-v | --verbose)
VERBOSE=true
shift 1
if [ $# -lt 1 ]; then
usage
fi
;;
-V | --version)
printf '%s\n' "${VERSION}"
exit 0
;;
-B | --base)
if [ $# -lt 3 ]; then
usage
fi
BASE_URL="$2"
shift 2
;;
--list-commands)
echo "exec
init
install
list
rm
run
update"
exit 0
;;
-h | --help)
usage
;;
*)
# check for available commands
missing_commands="$(check_required_commands)"
if [ -n "${missing_commands}" ]; then
die "Following commands are required, but missing, please install: ${missing_commands}"
fi
unset missing_commands
case $1 in
init)
shift 1
[ "$1" == "-h" -o "$1" == "--help" ] && init_usage
CREATING_EPM_HOME=true
MODIFYING_EPM_HOME=true
EPM_HOME=$(pwd)
init_epm_home