forked from CanerBudak/reach-NFT-LoserX
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reach
1481 lines (1315 loc) · 37.7 KB
/
reach
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
#!/bin/sh
# OS X doesn't come with realpath, so we use
# https://github.com/mkropat/sh-realpath/blob/master/realpath.sh
# if we can't find realpath
command -v realpath >/dev/null 2>&1 || realpath() {
canonicalize_path "$(resolve_symlinks "$1")"
}
resolve_symlinks() {
_resolve_symlinks "$1"
}
_resolve_symlinks() (
_assert_no_path_cycles "$@" || return
path=$(readlink -- "$1")
res=$?
if [ "$res" -eq 0 ]; then
dir_context=$(dirname -- "$1")
_resolve_symlinks "$(_prepend_dir_context_if_necessary "$dir_context" "$path")" "$@"
else
printf '%s\n' "$1"
fi
)
_prepend_dir_context_if_necessary() {
if [ "$1" = . ]; then
printf '%s\n' "$2"
else
_prepend_path_if_relative "$1" "$2"
fi
}
_prepend_path_if_relative() {
case "$2" in
/* ) printf '%s\n' "$2" ;;
* ) printf '%s\n' "$1/$2" ;;
esac
}
_assert_no_path_cycles() (
target=$1
shift
for path in "$@"; do
if [ "$path" = "$target" ]; then
return 1
fi
done
)
canonicalize_path() {
if [ -d "$1" ]; then
_canonicalize_dir_path "$1"
else
_canonicalize_file_path "$1"
fi
}
_canonicalize_dir_path() {
(cd "$1" 2>/dev/null && pwd -P)
}
_canonicalize_file_path() (
dir=$(dirname -- "$1")
file=$(basename -- "$1")
(cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
)
# Real Reach follows
# Rules for how reach interacts with your Makefile,
# if you have *all* of the expected scaffolded files.
# ------------------------------------------------
#
# reach run
#
# --> must call exactly once -->
# make run
#
# * must not call bare reach run
#
# --> may call many times -->
# REACH_CONNECTOR_MODE=$M reach run $T $ARGS...
#
# * must always export REACH_CONNECTOR_MODE in a canonical form
#
# --> must call exactly once (per called) -->
# make run-target ARGS="$T $(ARGS)"
#
# * must not call reach run at all
#
# --> expected to be something like -->
# docker-compose run --rm reach-app-$${REACH_CONNECTOR_MODE} $T $(ARGS)
REACH="$(realpath "$0")"
export REACH
HERE="$(dirname "$REACH")"
# XXX Get from VERSION in Haskell version
REACH_DEFAULT_VERSION=0.1
# REACH_FULL_VERSION=0.1.2
# XXX RV_TAG* is no longer used b/c
# the reach version is no longer embedded into package.json
# When breaking changes occur between RCs,
# it may help to bump the TAGMIN
# so that users are less likely to end up with old RCs.
# When we stop making breaking changes on patch/rc versions
# then this probably won't be necessary
# RV_TAG='rc'
# RV_TAGMIN=4
# RV_TAGMAX=9999
if [ "x${REACH_VERSION}" = "x" ] ; then
REACH_VERSION="${REACH_DEFAULT_VERSION}"
else
# XXX this is wrong if specified version is not full
# REACH_FULL_VERSION="${REACH_VERSION}"
# RV_TAG='release'
:
fi
if [ "${REACH_VERSION}" = "stable" ] ; then
REACH_VERSION_SHORT="${REACH_DEFAULT_VERSION}"
else
REACH_VERSION_SHORT=$(echo "$REACH_VERSION" | sed 's/^v//' | awk -F. '{print $1"."$2}')
fi
if ! (which make docker docker-compose > /dev/null 2>&1) ; then
echo "Reach relies on an installation of make, docker, and docker-compose"
exit 1
fi
# shellcheck disable=SC2016
fatal_infinite_reach_run_loop () {
echo 'reach run has detected an infinite loop'
echo '`make run` may not call `reach run` with no arguments'
echo 'instead try something like `reach run index` from `make run`'
exit 1
}
fatal_connector_mode_coming_soon () {
echo "Sorry. Support for $REACH_CONNECTOR_MODE is coming soon!"
exit 1
}
fatal_pls_report () {
echo "Please report this as an issue with the reach command at:"
echo " https://github.com/reach-sh/reach-lang/issues"
exit 1
}
fatal_impossible_connector_mode() {
echo "impossible: unsupported REACH_CONNECTOR_MODE=$REACH_CONNECTOR_MODE"
fatal_pls_report
}
fatal_unrecognized_connector_mode() {
echo "Unrecognized REACH_CONNECTOR_MODE=$REACH_CONNECTOR_MODE"
exit 1
}
ensure_connector_mode () {
# Makes sure to set REACH_CONNECTOR_MODE to one of:
# * ETH-test-dockerized-geth
# * ETH-live
# * ALGO-test-dockerized-algod
# $WHAT-$WHERE-$HOW-$HOW_WHERE
# (or error)
# This can be derived from REACH_TESTNET and possibly REACH_ETH_MODE
# Expand defaults
REACH_CONNECTOR_MODE=${REACH_CONNECTOR_MODE:-ETH}
case "$REACH_CONNECTOR_MODE" in
"ETH")
REACH_CONNECTOR_MODE="ETH-test-dockerized-geth"
;;
"ETH-live")
REACH_CONNECTOR_MODE="ETH-live"
;;
"ETH-test")
REACH_CONNECTOR_MODE="ETH-test-dockerized-geth"
;;
"ETH-test-dockerized")
REACH_CONNECTOR_MODE="ETH-test-dockerized-geth"
;;
"ALGO")
REACH_CONNECTOR_MODE="ALGO-test-dockerized-algod"
;;
"ALGO-live")
REACH_CONNECTOR_MODE="ALGO-live"
;;
"ALGO-test")
REACH_CONNECTOR_MODE="ALGO-test-dockerized-algod"
;;
"ALGO-test-dockerized")
REACH_CONNECTOR_MODE="ALGO-test-dockerized-algod"
;;
esac
# ensure it is one of the supported things
case "$REACH_CONNECTOR_MODE" in
"ETH-live")
;;
"ETH-test-dockerized-geth")
;;
"ALGO-live")
;;
"ALGO-test-dockerized-algod")
;;
*)
fatal_unrecognized_connector_mode
;;
esac
# make sure sub-commands receive this
export REACH_CONNECTOR_MODE
}
do_whoami () {
docker info --format '{{.ID}}' 2>/dev/null
}
do_compile () {
HS=${HERE}/hs
# Note: shellcheck says splatting is dangerous,
# (because what if file names have spaces),
# but also, sh doesn't have array splicing, so... this.
# It's a little less mix-and-match
reachc_release () {
stack build && \
stack exec -- \
reachc "$@"
}
reachc_prof () {
stack build --profile --fast && \
stack exec --profile -- \
reachc --disable-reporting --intermediate-files "$@" +RTS -p
}
reachc_dev () {
stack build --fast && \
stack exec -- \
reachc --disable-reporting --intermediate-files "$@"
}
ID=$(do_whoami)
if [ "$CIRCLECI" = "true" ] && [ -x ~/.local/bin/reachc ]; then
~/.local/bin/reachc --disable-reporting --intermediate-files "$@"
elif [ -z "${REACH_DOCKER}" ] && [ -d "${HS}/.stack-work" ] && (which stack > /dev/null 2>&1) ; then
export STACK_YAML="${HS}/stack.yaml"
export REACHC_ID=${ID}
REACHC_HASH="$("${HS}/../scripts/git-hash.sh")"
export REACHC_HASH
(cd "$HS" && make stack)
if [ "x${REACHC_RELEASE}" = "xY" ] ; then
reachc_release "$@"
elif [ "x${REACHC_PROFILE}" = "xY" ] ; then
reachc_prof "$@"
else
reachc_dev "$@"
fi
else
docker run \
--rm \
--volume "$PWD:/app" \
-e "REACHC_ID=${ID}" \
reachsh/reach:${REACH_VERSION} \
"$@"
fi
}
do_clean () {
# TODO: add `make clean` to scaffolded makefile,
# and just scaffold & make clean instead?
# This implementation was simpler, though.
MODULE="$1"
if [ "x$MODULE" = "x" ] ; then
MODULE="index"
else
shift
if [ -d "$MODULE" ] ; then
cd "$MODULE" || exit 1
MODULE="index"
fi
fi
IDENT="$1"
if [ "x$IDENT" = "x" ] ; then
IDENT="main"
else
shift
fi
rm -f "build/$MODULE.$IDENT.mjs"
}
do_init () {
# reach init [APP]
# ----------
# APP defaults to index
# fail if $APP.mjs or $APP.rsh exist
# write $APP.mjs and $APP.rsh
APP="$1"
if [ "x$APP" = "x" ] ; then
APP=index
else
shift
fi
RSH="$APP.rsh"
MJS="$APP.mjs"
if [ -f "$RSH" ] ; then
echo "$RSH already exists"
exit 1
fi
if [ -f "$MJS" ] ; then
echo "$MJS already exists"
fi
echo writing "$RSH"
cat >"${RSH}" <<EOF
'reach ${REACH_VERSION_SHORT}';
export const main = Reach.App(
{}, [Participant('Alice', {}), Participant('Bob', {})], (Alice, Bob) => {
// ...
}
);
EOF
echo writing "$MJS"
cat >"${MJS}" <<EOF
import {loadStdlib} from '@reach-sh/stdlib';
import * as backend from './build/${APP}.main.mjs';
(async () => {
const stdlib = await loadStdlib();
const startingBalance = stdlib.parseCurrency(100);
const alice = await stdlib.newTestAccount(startingBalance);
const bob = await stdlib.newTestAccount(startingBalance);
const ctcAlice = alice.deploy(backend);
const ctcBob = bob.attach(backend, ctcAlice.getInfo());
await Promise.all([
backend.Alice(ctcAlice, {
...stdlib.hasRandom
}),
backend.Bob(ctcBob, {
...stdlib.hasRandom
}),
]);
console.log('Hello, Alice and Bob!');
})();
EOF
}
do_scaffold () {
# reach scaffold [--isolate] [--quiet] [APP]
# --------------
# if next arg is --isolate, set ISOLATE flag & shift
# if next arg is --quiet, disable VERBOSE flag & shift
# APP is next arg, defaults to index
# write each of the below if they do not exist
# * .gitignore
# * .dockerignore
# Suffix the following file names with .$APP if ISOLATE:
# Error if any of the below exist:
# Write each of the below:
# * package.json
# * Dockerfile
# * docker-compose.yml
# * Makefile
# lol I hope you didn't misspell --isolate or --quiet
# TODO: better arg parsing
ensure_connector_mode
ISOLATE=false
if [ "x$1" = "x--isolate" ] ; then
ISOLATE=true
shift
fi
VERBOSE=true
if [ "x$1" = "x--quiet" ] ; then
VERBOSE=false
shift
fi
APP="$1"
if [ "x$APP" = "x" ] ; then
APP="index"
else
shift
fi
PROJ="$(basename "$(pwd)" | tr '[:upper:]' '[:lower:]')"
DOCKERFILE="Dockerfile"
PACKAGE_JSON="package.json"
DOCKER_COMPOSE_YML="docker-compose.yml"
MAKEFILE="Makefile"
if $ISOLATE ; then
PROJ="$PROJ-$APP"
DOCKERFILE="$DOCKERFILE.$APP"
PACKAGE_JSON="$PACKAGE_JSON.$APP"
DOCKER_COMPOSE_YML="$DOCKER_COMPOSE_YML.$APP"
MAKEFILE="$MAKEFILE.$APP"
fi
MJS="$APP.mjs"
RSH="$APP.rsh"
if $ISOLATE ; then
CPLINE="RUN cp /app/${PACKAGE_JSON} /app/package.json"
else
# Omit this line of the file if not --isolate
CPLINE=""
fi
if $VERBOSE ; then echo writing $DOCKERFILE; fi
cat >"${DOCKERFILE}" <<EOF
FROM reachsh/runner:${REACH_VERSION}
# If your project needs more node dependencies:
# COPY ${PACKAGE_JSON} /app/package.json
# RUN npm install
# RUN npm link @reach-sh/stdlib
COPY . /app
${CPLINE}
CMD ["${APP}"]
EOF
# TODO: s/lint/preapp. It's disabled because sometimes our
# generated code trips the linter
# TODO: ^ The same goes for js/runner_package.template.json
if $VERBOSE ; then echo writing $PACKAGE_JSON; fi
# XXX We could optimize this by making reachsh/stdlib-app with everything except the MJS files and make the package linking/install go faster.
# if [ "$RV_TAG" = "release" ] ; then
# echo "$RV_TAG"
# REACH_VERSION_EXPR="$REACH_FULL_VERSION"
# else
# REACH_VERSION_EXPR=">=$REACH_FULL_VERSION-$RV_TAG.$RV_TAGMIN <$REACH_FULL_VERSION-$RV_TAG.$RV_TAGMAX"
# fi
# XXX: Is REACH_VERSION_EXPR still useful somehow?
# It seems wasteful to just throw it away
cat >"${PACKAGE_JSON}" <<EOF
{
"name": "@reach-sh/${PROJ}",
"type": "module",
"dependencies": {
},
"author": "reach.sh",
"license": "Apache-2.0",
"scripts": {
"lint": "eslint --ignore-path .gitignore --ext .mjs .",
"${APP}": "node --experimental-modules --unhandled-rejections=strict ${MJS}"
}
}
EOF
SERVICE="reach-app-${PROJ}"
IMAGE="reachsh/${SERVICE}"
IMAGE_TAG="${IMAGE}:latest"
if $VERBOSE ; then echo writing $DOCKER_COMPOSE_YML; fi
cat >"${DOCKER_COMPOSE_YML}" <<EOF
version: '3.4'
x-app-base: &app-base
image: ${IMAGE_TAG}
services:
ethereum-devnet:
image: reachsh/ethereum-devnet:${REACH_VERSION}
algorand-devnet:
image: reachsh/algorand-devnet:${REACH_VERSION}
depends_on:
- algorand-postgres-db
environment:
- REACH_DEBUG
- POSTGRES_HOST=algorand-postgres-db
- POSTGRES_USER=algogrand
- POSTGRES_PASSWORD=indexer
- POSTGRES_DB=pgdb
ports:
- 9392
algorand-postgres-db:
image: postgres:11-alpine
environment:
- POSTGRES_USER=algogrand
- POSTGRES_PASSWORD=indexer
- POSTGRES_DB=pgdb
${SERVICE}-ETH-live:
<<: *app-base
environment:
- REACH_DEBUG
- REACH_CONNECTOR_MODE=ETH-live
- REACH_ISOLATED_NETWORK
- ETH_NODE_URI
- ETH_NODE_NETWORK
${SERVICE}-ETH-test-dockerized-geth: &default-app
<<: *app-base
depends_on:
- ethereum-devnet
environment:
- REACH_DEBUG
- REACH_CONNECTOR_MODE=ETH-test-dockerized-geth
- ETH_NODE_URI=http://ethereum-devnet:8545
${SERVICE}-ALGO-live:
<<: *app-base
environment:
- REACH_DEBUG
- REACH_CONNECTOR_MODE=ALGO-live
- REACH_ISOLATED_NETWORK
- ALGO_TOKEN
- ALGO_SERVER
- ALGO_PORT
- ALGO_INDEXER_TOKEN
- ALGO_INDEXER_SERVER
- ALGO_INDEXER_PORT
- ALGO_FAUCET_PASSPHRASE
${SERVICE}-ALGO-test-dockerized-algod:
<<: *app-base
depends_on:
- algorand-devnet
environment:
- REACH_DEBUG
- REACH_CONNECTOR_MODE=ALGO-test-dockerized-algod
- ALGO_SERVER=http://algorand-devnet
- ALGO_PORT=4180
- ALGO_INDEXER_SERVER=http://algorand-devnet
- ALGO_INDEXER_PORT=8980
${SERVICE}-: *default-app
${SERVICE}: *default-app
EOF
if $VERBOSE ; then echo writing "$MAKEFILE"; fi
# TODO: a better makefile
cat >"${MAKEFILE}" <<EOF
REACH = reach
.PHONY: clean
clean:
rm -rf build/*.main.mjs
build/%.main.mjs: %.rsh
\$(REACH) compile $^
.PHONY: build
build: build/${APP}.main.mjs
docker build -f ${DOCKERFILE} --tag=${IMAGE_TAG} .
.PHONY: run
run:
\$(REACH) run ${APP}
.PHONY: run-target
run-target: build
docker-compose -f "${DOCKER_COMPOSE_YML}" run --rm ${SERVICE}-\$\${REACH_CONNECTOR_MODE} \$(ARGS)
.PHONY: down
down:
docker-compose -f "${DOCKER_COMPOSE_YML}" down --remove-orphans
EOF
GITIGNORE=".gitignore"
if [ ! -f "$GITIGNORE" ] ; then
if $VERBOSE ; then echo writing "$GITIGNORE"; fi
cat >"$GITIGNORE" <<EOF
build/
node_modules/
EOF
fi
DOCKERIGNORE=".dockerignore"
if [ ! -f "$DOCKERIGNORE" ] ; then
if $VERBOSE ; then echo writing "$DOCKERIGNORE" ; fi
cat >"$DOCKERIGNORE" <<EOF
node_modules/
EOF
fi
}
do_unscaffold () {
ISOLATE=false
if [ "x$1" = "x--isolate" ] ; then
ISOLATE=true
shift
fi
VERBOSE=true
if [ "x$1" = "x--quiet" ] ; then
VERBOSE=false
shift
fi
APP="$1"
if [ "x$APP" = "x" ] ; then
APP="index"
else
shift
fi
DOCKERFILE="Dockerfile"
PACKAGE_JSON="package.json"
DOCKER_COMPOSE_YML="docker-compose.yml"
MAKEFILE="Makefile"
if $ISOLATE ; then
DOCKERFILE="$DOCKERFILE.$APP"
PACKAGE_JSON="$PACKAGE_JSON.$APP"
DOCKER_COMPOSE_YML="$DOCKER_COMPOSE_YML.$APP"
MAKEFILE="$MAKEFILE.$APP"
fi
for file in $DOCKERFILE $PACKAGE_JSON $DOCKER_COMPOSE_YML $MAKEFILE ; do
if $VERBOSE ; then echo deleting $file ; fi
rm -f $file
done
}
do_down () {
# TODO: not duplicate so much from do_run
MAKEFILE=Makefile
DOCKERFILE=Dockerfile
PACKAGE_JSON=package.json
DOCKER_COMPOSE_YML=docker-compose.yml
# Note: Makefile excluded from this check
NONE_EXIST=true
if [ -f "$DOCKERFILE" ] || [ -f "$PACKAGE_JSON" ] || [ -f "$DOCKER_COMPOSE_YML" ] ; then
NONE_EXIST=false
fi
if $NONE_EXIST ; then
do_scaffold --isolate --quiet "$APP"
fi
cleanup () {
do_unscaffold --isolate --quiet "$APP"
}
reach_make () {
RUN_FROM_REACH=true make "$@" REACH="${REACH}"
MAKE_EXIT=$?
if [ $MAKE_EXIT -ne 0 ] ; then
cleanup
exit $MAKE_EXIT
fi
}
# if do_scaffold ran, it changed $MAKEFILE
reach_make_f () {
reach_make -f "$MAKEFILE" "$@"
}
reach_make_f down
cleanup
}
run_ () {
F="$1"
shift
PROJ="$1"
shift
docker-compose -f "$F" run --rm "reach-app-${PROJ}" "$@"
}
do_run () {
# reach run args
# check state of scaffolded files
# * if none exist: scaffold in --isolate --quiet mode, set flag UNSCAFFOLD
# * if all exist: just use the existing things
# * if some exist: error
# make build run
# unscaffold if UNSCAFFOLD
ANY_CUSTOMIZATION=false
if ! [ "x$REACH_CONNECTOR_MODE" = "x" ] ; then
ANY_CUSTOMIZATION=true
fi
ensure_connector_mode
export RUN_FROM_REACH=${RUN_FROM_REACH:-false}
if [ "x$1" = "x" ] ; then
BARE_REACH_RUN=true
APP="index"
else
BARE_REACH_RUN=false
ANY_CUSTOMIZATION=true
ARG=$1
shift
# TODO: better arg parsing at some point
if [ "x$ARG" = "x--" ] ; then
ARG="index"
fi
if [ -d "$ARG" ] ; then
ARG="$ARG/index"
fi
cd "$(dirname "$ARG")" || exit
APP="$(basename "$ARG")"
fi
RSH="${APP}.rsh"
MJS="${APP}.mjs"
if [ "x$APP" = "x" ] ||
! [ -f "${RSH}" ] ||
! [ -f "${MJS}" ]; then
echo "Usage: reach-run APP"
echo " where APP.rsh"
echo " and APP.mjs"
echo " exists in current directory."
exit 1
fi
# XXX Can we add eslint on the JS?
MAKEFILE=Makefile
DOCKERFILE=Dockerfile
PACKAGE_JSON=package.json
DOCKER_COMPOSE_YML=docker-compose.yml
NONE_EXIST=true
# Note: Makefile excluded from this check
if [ -f "$DOCKERFILE" ] || [ -f "$PACKAGE_JSON" ] || [ -f "$DOCKER_COMPOSE_YML" ] ; then
NONE_EXIST=false
fi
if $NONE_EXIST ; then
do_scaffold --isolate --quiet "$APP"
cleanup () {
do_unscaffold --isolate --quiet "$APP"
}
# Note: do_scaffold --isolate has mutated these vars like so:
# MAKEFILE=$MAKEFILE.${APP}
# DOCKERFILE=$Dockerfile.${APP}
# PACKAGE_JSON=$PACKAGE_JSON.${APP}
# DOCKER_COMPOSE_YML=$DOCKER_COMPOSE_YML.${APP}
else
cleanup () {
:
}
ALL_EXIST=false
if [ -f "$MAKEFILE" ] && [ -f "$DOCKERFILE" ] && [ -f "$PACKAGE_JSON" ] && [ -f "$DOCKER_COMPOSE_YML" ] ; then
ALL_EXIST=true
fi
# We trust our scaffolded makefiles,
# so we only need to check for infinite recurrsion on:
# * reach run ($BARE_REACH_RUN), since this is the only potential avenue for inf recursion
# * a proj with customized scaffolding. ($ALL_EXIST)
# * running from inside another reach run ($RUN_FROM_REACH)
if $BARE_REACH_RUN && $ALL_EXIST && $RUN_FROM_REACH ; then
fatal_infinite_reach_run_loop
fi
if ! $ALL_EXIST ; then
# TODO: more description on err
echo "I'm confused, some scaffolded files exist, but not all"
exit 1
fi
fi
reach_make () {
RUN_FROM_REACH=true make "$@" REACH="${REACH}"
MAKE_EXIT=$?
if [ $MAKE_EXIT -ne 0 ] ; then
cleanup
exit $MAKE_EXIT
fi
}
reach_make_f () {
reach_make -f "$MAKEFILE" "$@"
}
if $BARE_REACH_RUN ; then
# Always build from "scaffolded" file
reach_make_f build
# Run from Makefile if present and not "run from reach"
if [ -f Makefile ] && ! $RUN_FROM_REACH && ! $ANY_CUSTOMIZATION; then
reach_make run
else
reach_make_f run
fi
else
# It is assumed that if this is being called from within reach run,
# then the build step has already been handled.
# TODO: better use of makefiles so that we just call make build anyway,
# and it is a noop if nothing needs to be done.
if ! $RUN_FROM_REACH ; then
reach_make_f build
fi
# This is nuts and possibly a little bit wrong.
# Easier methods exist but they are outside of POSIX standard.
escape_args () {
for arg in "$APP" "$@" ; do
escaped_arg=""
for word in $arg ; do
escaped_arg="$escaped_arg$(printf "%s\ " "$word")"
done
echo "${escaped_arg%??}"
done
}
ARGS=$(escape_args "$@")
# Yes it apparently has to be exactly "$(echo $ARGS)" because reasons.
# shellcheck disable=SC2116,SC2086
reach_make_f run-target ARGS="$(echo $ARGS)"
fi
cleanup
}
react_compose () {
if $USE_EXISTING_DEVNET ; then
echo 'using existing devnet'
DEPENDS_ON=''
else
case "$REACH_CONNECTOR_MODE" in
ALGO*)
DEPENDS_ON='
# Does not technically depend on algorand-devnet.
# This is just to get it to run both.
depends_on:
- algorand-devnet
'
;;
ETH*)
DEPENDS_ON='
# Does not technically depend on ethereum-devnet.
# This is just to get it to run both.
depends_on:
- ethereum-devnet
'
;;
esac
fi
cat<<EOF > docker-compose.yml.react
version: '3.4'
services:
dev-server:
image: reachsh/react-runner:$REACH_VERSION
volumes:
- .:/app/src
ports:
- "3000:3000"
stdin_open: true
tty: true
environment:
- REACH_DEBUG
- REACH_CONNECTOR_MODE
- REACH_ISOLATED_NETWORK
- REACT_APP_REACH_DEBUG=${REACH_DEBUG}
- REACT_APP_REACH_CONNECTOR_MODE=${REACH_CONNECTOR_MODE}
- REACT_APP_REACH_ISOLATED_NETWORK=${REACH_ISOLATED_NETWORK}
$DEPENDS_ON
ethereum-devnet:
image: reachsh/ethereum-devnet:${REACH_VERSION}
ports:
- '8545:8545'
algorand-devnet:
image: reachsh/algorand-devnet:${REACH_VERSION}
depends_on:
- algorand-postgres-db
environment:
- REACH_DEBUG
- POSTGRES_HOST=algorand-postgres-db
- POSTGRES_USER=algogrand
- POSTGRES_PASSWORD=indexer
- POSTGRES_DB=pgdb
ports:
- '4180:4180'
- '8980:8980'
- '9392:9392'
algorand-postgres-db:
image: postgres:11-alpine
environment:
- POSTGRES_USER=algogrand
- POSTGRES_PASSWORD=indexer
- POSTGRES_DB=pgdb
EOF
# /bin/sh run.sh.react || :
docker-compose -f docker-compose.yml.react "$@" || :
rm docker-compose.yml.react # run.sh.react
}
fatal_unrecognized_connector_mode_react () {
echo "Unrecognized REACH_CONNECTOR_MODE=$REACH_CONNECTOR_MODE"
echo "Supported modes for reach react are: ETH, ALGO"
exit 1
}
ensure_connector_mode_browser () {
# Makes sure to set REACH_CONNECTOR_MODE to one of:
# * ETH-browser
# * ALGO-browser
# $WHAT-$WHERE
# TODO: distinguish between BLAH-browser-dockerized and browser-remote
# Expand defaults
REACH_CONNECTOR_MODE=${REACH_CONNECTOR_MODE:-ETH}
case "$REACH_CONNECTOR_MODE" in
"ETH")
REACH_CONNECTOR_MODE="ETH-browser"
;;
"ALGO")
REACH_CONNECTOR_MODE="ALGO-browser"
;;
esac
# ensure it is one of the supported things
case "$REACH_CONNECTOR_MODE" in
"ETH-browser")
;;
"ALGO-browser")
;;
*)
fatal_unrecognized_connector_mode_react
;;
esac
}
do_react () {
ensure_connector_mode_browser
# TODO: make better use of REACH_CONNECTOR_MODE for branching here
USE_EXISTING_DEVNET=false
if [ "x$1" = "x--use-existing-devnet" ]; then
shift
USE_EXISTING_DEVNET=true
fi
case "$REACH_CONNECTOR_MODE" in
ALGO*)
if command -v lsof >/dev/null && lsof -i tcp:4180 | grep LISTEN >/dev/null ; then
USE_EXISTING_DEVNET=true
fi
;;
ETH*)
if command -v lsof >/dev/null && lsof -i tcp:8545 | grep LISTEN >/dev/null ; then
USE_EXISTING_DEVNET=true
fi
;;
esac
# All args are just forwarded to do_compile
# TODO: support args comparable to do_run?
do_compile "$@"
react_compose run --service-ports --rm dev-server
exit 0
}
do_react_down () {
USE_EXISTING_DEVNET=false
react_compose down
exit 0
}
do_devnet () {
ensure_connector_mode
case "$REACH_CONNECTOR_MODE" in
ALGO*)
cat<<EOF > docker-compose.yml.devnet
version: '3.4'
services:
algorand-devnet:
image: reachsh/algorand-devnet:latest
depends_on:
- algorand-postgres-db
environment:
- REACH_DEBUG
- POSTGRES_HOST=algorand-postgres-db
- POSTGRES_USER=algogrand
- POSTGRES_PASSWORD=indexer
- POSTGRES_DB=pgdb
ports:
- '4180:4180'
- '8980:8980'