-
Notifications
You must be signed in to change notification settings - Fork 0
/
depends.sh
executable file
·684 lines (568 loc) · 17.4 KB
/
depends.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
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BOLD='\033[1m'
NC='\033[0m' # No Color
MISSING_TOOLS=0
HAS_JAVA=false
HAS_JAVAC=false
HAS_ANT=false
HAS_IVY=false
HAS_NPM=false
HAS_NODE=false
HAS_PYTHON=false
HAS_PIP=false
MISSING_DEPS=0
HAS_REQUESTS=false
HAS_LANGCHAIN=false
HAS_DOTENV=false
HAS_NODE_MODULES=false
DO_INSTALL=false
DO_SILENT=false
INSTALL_REQUIRED=false
IS_WINDOWS=false
IS_DARWIN=false
IS_LINUX=false
DO_INSTALL=true
SYS_OS_NAME="$(uname -s)"
SYS_ARCH="$(uname -m)"
#####################################################################
# OS detection
if [[ "$SYS_OS_NAME" == "Windows_NT" ]]; then
IS_WINDOWS=true
elif [[ "$SYS_OS_NAME" == "Darwin" ]]; then
IS_DARWIN=true
elif [[ "$SYS_OS_NAME" == "Linux" ]]; then
IS_LINUX=true
fi
extract_major_version() {
echo "$1" | awk -F '.' '{print $1}'
}
extract_minor_version() {
echo "$1" | awk -F '.' '{print $2}'
}
clear_line() {
printf "\r%*s\r" "$(tput cols)" ""
}
print_line() {
printf "%*s\n" "$(tput cols)" | tr ' ' -
}
succeed() {
clear_line
printf "Has ${BOLD}$1${NC} - ${GREEN}OK${NC}\n"
}
fail() {
clear_line
printf "Has ${BOLD}$1${NC} - ${RED}FAIL${NC}\n"
}
# hash_file <file>
hash_file() {
if [[ "$IS_DARWIN"=true ]]; then
echo $($md5 -q "$1")
else
echo $($md5sum "$1" | awk '{print $1}')
fi
}
get_os_version() {
if [[ "$IS_WINDOWS"=true ]]; then
echo $(wmic os get Caption,Version | sed -n '2p')
elif [[ "$IS_LINUX"=true ]]; then
echo $(lsb_release -d | awk -F ' ' '{print $2}')
elif [[ "$IS_DARWIN"=true ]]; then
echo $(sw_vers -productVersion)
fi
}
#####################################################################
# Command line arguments
for arg in "$@"; do
if [[ "$arg" == "--no-install" ]]; then
DO_INSTALL=false
elif [[ "$arg" == "--clear-cache" ]]; then
printf "${YELLOW}Clearing dependency cache${NC}\n"
rm -f "client/package.json.md5"
exit 0
elif [[ "$arg" == "--silent" ]]; then
DO_SILENT=true
elif [[ "$arg" == "--help" ]]; then
printf "Usage: ./depends.sh [options...]\n"
printf "Check tools and dependencies for the project and install missing ones\n"
printf "\n"
printf " --help Display this help and exit\n"
printf " --no-install Do not install missing dependencies\n"
printf " --clear-cache Clear the cache of installed dependencies\n"
printf " --silent Do not print anything to the console\n"
exit 0
else
printf "${RED}Unknown argument: $arg${NC}\n"
exit 1
fi
done
if [[ $DO_SILENT = true ]]; then
exec 1>/dev/null
exec 2>/dev/null
fi
#####################################################################
printf "${BOLD}System${NC}\n"
print_line
printf "Operating System ${BOLD}$SYS_OS_NAME${NC}\n"
printf "Architecture ${BOLD}$SYS_ARCH${NC}\n"
printf "OS Version ${BOLD}$(get_os_version)${NC}\n"
#####################################################################
# Check tools
printf "\n${BOLD}Check tools${NC}\n"
print_line
#####################################################################
# Check Java installation
printf "${YELLOW}Check ${BOLD}java${NC}"
if ! command -v java &> /dev/null; then
fail "java"
else
java_version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')
if [[ -z "$java_version" ]]; then
fail "java"
else
java_version_major=$(extract_major_version "$java_version")
if [[ -z "$java_version_major" ]]; then
fail "java"
else
if [[ "$java_version_major" -ge 17 ]]; then
succeed "java"
HAS_JAVA=true
else
fail "java"
fi
fi
fi
fi
if [[ $HAS_JAVA = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check Java Compiler installation
if [[ $HAS_JAVA = true ]]; then
printf "${YELLOW}Check ${BOLD}javac${NC}"
if ! command -v javac &> /dev/null; then
fail "javac"
else
javac_version=$(javac -version 2>&1 | awk -F ' ' '/javac/ {print $2}')
if [[ -z "$javac_version" ]]; then
fail "javac"
else
javac_version_major=$(extract_major_version "$javac_version")
if [[ -z "$javac_version_major" ]]; then
fail "javac"
else
clear_line
if [[ "$javac_version_major" -ge 17 ]]; then
succeed "javac"
HAS_JAVAC=true
else
fail "javac"
fi
fi
fi
fi
else
printf "${YELLOW}Skipping javac installation check${NC}\n"
fi
if [[ $HAS_JAVAC = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check ant installation
printf "${YELLOW}Check ${BOLD}ant${NC}"
if ! command -v ant &> /dev/null; then
fail "ant"
else
ant_version=$(ant -version | awk -F ' ' '/version/ {print $4}')
if [[ -z "$ant_version" ]]; then
fail "ant"
else
ant_version_major=$(extract_major_version "$ant_version")
ant_version_minor=$(extract_minor_version "$ant_version")
if [[ -z "$ant_version_major" || -z "$ant_version_minor" ]]; then
fail "ant"
else
if [[ "$ant_version_major" -eq 1 && "$ant_version_minor" -ge 10 ]]; then
succeed "ant"
HAS_ANT=true
else
fail "ant"
fi
fi
fi
fi
if [[ $HAS_ANT = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check ivy installation
if [[ $HAS_ANT = true ]]; then
printf "${YELLOW}Checking ${BOLD}ivy${NC}"
ivy_jar_matches=$(ant -diagnostics | grep ivy-*.*.*.jar | awk -F ' ' '{print $1}')
found_installation=false
while IFS= read -r line; do
if [[ $line == *ivy-*.*.*.jar ]]; then
found_installation=true
ivy_jar=$line
ivy_version=$(echo "$ivy_jar" | sed -n 's/ivy-\([0-9]*\.[0-9]*\.[0-9]*\).jar/\1/p')
ivy_version_major=$(extract_major_version "$ivy_version")
ivy_version_minor=$(extract_minor_version "$ivy_version")
if [[ -z "$ivy_version_major" || -z "$ivy_version_minor" ]]; then
fail "ivy"
else
if [[ "$ivy_version_major" -ge 2 && "$ivy_version_minor" -ge 5 ]]; then
succeed "ivy"
HAS_IVY=true
break
else
fail "ivy"
fi
fi
fi
done < <(echo "$ivy_jar_matches")
if [[ $found_installation = false ]]; then
fail "ivy"
fi
else
printf "${YELLOW}Skipping Ivy installation check${NC}\n"
fi
if [[ $HAS_IVY = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check npm installation
printf "${YELLOW}Checking ${BOLD}npm${NC}"
if ! command -v npm &> /dev/null; then
fail "npm"
else
npm_version=$(npm -v)
if [[ -z "$npm_version" ]]; then
fail "npm"
else
npm_version_major=$(extract_major_version "$npm_version")
if [[ -z "$npm_version_major" ]]; then
fail "npm"
else
clear_line
if [[ "$npm_version_major" -ge 9 ]]; then
succeed "npm"
HAS_NPM=true
else
fail "npm"
fi
fi
fi
fi
if [[ $HAS_NPM = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check node installation
printf "${YELLOW}Checking ${BOLD}node${NC}"
if ! command -v node &> /dev/null; then
fail "node"
else
node_version=$(node -v)
node_version=${node_version:1}
if [[ -z "$node_version" ]]; then
fail "node"
else
node_version_major=$(extract_major_version "$node_version")
if [[ -z "$node_version_major" ]]; then
fail "node"
else
if [[ "$node_version_major" -ge 18 ]]; then
succeed "node"
HAS_NODE=true
else
fail "node"
fi
fi
fi
fi
if [[ $HAS_NODE = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check python installation
printf "${YELLOW}Checking ${BOLD}python${NC}"
if ! command -v python &> /dev/null; then
fail "python"
else
python_version=$(python -V 2>&1 | awk -F ' ' '/Python/ {print $2}')
if [[ -z "$python_version" ]]; then
fail "python"
else
python_version_major=$(extract_major_version "$python_version")
python_version_minor=$(extract_minor_version "$python_version")
if [[ -z "$python_version_major" || -z "$python_version_minor" ]]; then
fail "python"
else
if [[ "$python_version_major" -ge 3 && "$python_version_minor" -ge 3 ]]; then
succeed "python"
HAS_PYTHON=true
else
fail "python"
fi
fi
fi
fi
if [[ $HAS_PYTHON = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
#####################################################################
# Check pip installation
printf "${YELLOW}Checking ${BOLD}pip${NC}"
if ! command -v pip &> /dev/null; then
fail "pip"
else
pip_version=$(pip -V 2>&1 | awk -F ' ' '/pip/ {print $2}')
if [[ -z "$pip_version" ]]; then
fail "pip"
else
pip_version_major=$(extract_major_version "$pip_version")
if [[ -z "$pip_version_major" ]]; then
fail "pip"
else
if [[ "$pip_version_major" -ge 20 ]]; then
succeed "pip"
HAS_PIP=true
else
fail "pip"
fi
fi
fi
fi
if [[ $HAS_PIP = false ]]; then
MISSING_TOOLS=$((MISSING_TOOLS+1))
fi
if [[ $MISSING_TOOLS -gt 0 ]]; then
printf "\n${RED}One or more tools are missing or incompatible${NC}\n"
fi
#####################################################################
# Check dependencies
printf "\n${BOLD}Check dependencies${NC}\n"
print_line
# requests
printf "${YELLOW}Checking ${BOLD}requests${NC}"
if ! python -c "import requests" &> /dev/null; then
fail "requests"
MISSING_DEPS=$((MISSING_DEPS+1))
else
succeed "requests"
HAS_REQUESTS=true
fi
# langchain
printf "${YELLOW}Checking ${BOLD}langchain${NC}"
if ! python -c "import langchain" &> /dev/null; then
fail "langchain"
MISSING_DEPS=$((MISSING_DEPS+1))
else
succeed "langchain"
HAS_LANGCHAIN=true
fi
# dotenv
printf "${YELLOW}Checking ${BOLD}dotenv${NC}"
if ! python -c "import dotenv" &> /dev/null; then
fail "dotenv"
MISSING_DEPS=$((MISSING_DEPS+1))
else
succeed "dotenv"
HAS_DOTENV=true
fi
# node_modules
printf "${YELLOW}Checking ${BOLD}node_modules${NC}"
current_package_json_hash=$(hash_file "client/package.json")
if [[ ! -d "client/node_modules" ]]; then
# npm install has not been run
fail "node_modules"
MISSING_DEPS=$((MISSING_DEPS+1))
else
prev_hash=""
if [[ -f "client/package.json.md5" ]]; then
prev_hash=$(cat "client/package.json.md5")
fi
if [[ "$current_package_json_hash" != "$prev_hash" ]]; then
# package.json has changed since last install
fail "node_modules"
MISSING_DEPS=$((MISSING_DEPS+1))
else
succeed "node_modules"
HAS_NODE_MODULES=true
fi
fi
if [[ $MISSING_DEPS -gt 0 ]]; then
printf "\n${RED}One or more dependencies are missing or incompatible${NC}\n"
INSTALL_REQUIRED=true
fi
if [[ $INSTALL_REQUIRED = true ]]; then
#####################################################################
# Install missing dependencies
printf "\n${BOLD}Install dependencies${NC}\n"
print_line
if [[ $DO_INSTALL = true ]]; then
printf "Starting installs, this may take a while...\n\n"
if [[ $HAS_PIP = false ]]; then
printf "${RED}${BOLD}pip${NC} ${YELLOW}is missing - Python dependencies will not be installed${NC}\n"
fi
if [[ $HAS_REQUESTS = false && $HAS_PIP = true ]]; then
printf "${YELLOW}Install ${BOLD}requests${NC}${YELLOW}...${NC}\n"
pip install requests
success=$?
if [[ $success -eq 0 ]]; then
printf "${GREEN}Installed ${BOLD}requests${NC}\n"
HAS_REQUESTS=true
MISSING_DEPS=$((MISSING_DEPS-1))
else
printf "${RED}Failed to install ${BOLD}requests${NC}\n"
fi
printf "\n"
fi
if [[ $HAS_LANGCHAIN = false && $HAS_PIP = true ]]; then
printf "${YELLOW}Install ${BOLD}langchain${NC}${YELLOW}...${NC}\n"
pip install langchain
success=$?
if [[ $success -eq 0 ]]; then
printf "${GREEN}Installed ${BOLD}langchain${NC}\n"
HAS_LANGCHAIN=true
MISSING_DEPS=$((MISSING_DEPS-1))
else
printf "${RED}Failed to install ${BOLD}langchain${NC}\n"
fi
printf "\n"
fi
if [[ $HAS_DOTENV = false && $HAS_PIP = true ]]; then
printf "${YELLOW}Install ${BOLD}dotenv${NC}${YELLOW}...${NC}\n"
pip install python-dotenv
success=$?
if [[ $success -eq 0 ]]; then
printf "${GREEN}Installed ${BOLD}dotenv${NC}\n"
HAS_DOTENV=true
MISSING_DEPS=$((MISSING_DEPS-1))
else
printf "${RED}Failed to install ${BOLD}dotenv${NC}\n"
fi
printf "\n"
fi
if [[ $HAS_NODE_MODULES = false && $HAS_NPM = true ]]; then
printf "${YELLOW}Install ${BOLD}node_modules${NC}${YELLOW}...${NC}\n"
cd client
npm install
success=$?
cd ..
if [[ $success -eq 0 ]]; then
printf "${GREEN}Installed ${BOLD}node_modules${NC}\n"
echo "$current_package_json_hash" > "client/package.json.md5"
HAS_NODE_MODULES=true
MISSING_DEPS=$((MISSING_DEPS-1))
else
printf "${RED}Failed to install ${BOLD}node_modules${NC}\n"
fi
printf "\n"
fi
else
printf "\n${YELLOW}Run with ${BOLD}--no-install${NC} ${YELLOW}to install missing dependencies${NC}\n"
fi
fi
#####################################################################
# Summary
printf "\n${BOLD}Summary${NC}\n"
term_width=$(tput cols)
printf "%*s\n" "${term_width}" | tr ' ' -
printf "\n${BOLD}Tools${NC}\n"
# Java
printf " - ${BOLD}java $java_version${NC}\n"
if [[ $HAS_JAVA = false ]]; then
if [[ -z "$java_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires JDK 17 or higher${NC}\n"
fi
# Javac
printf " - ${BOLD}javac $javac_version${NC}\n"
if [[ $HAS_JAVAC = false ]]; then
if [[ -z "$javac_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires JDK 17 or higher${NC}\n"
fi
# Ant
printf " - ${BOLD}ant $ant_version${NC}\n"
if [[ $HAS_ANT = false ]]; then
if [[ -z "$ant_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires Ant 1.10 or higher${NC}\n"
fi
# Ivy
printf " - ${BOLD}ivy $ivy_version${NC}\n"
if [[ $HAS_IVY = false ]]; then
if [[ -z "$ivy_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires Ivy 2.5 or higher${NC}\n"
fi
# NPM
printf " - ${BOLD}npm $npm_version${NC}\n"
if [[ $HAS_NPM = false ]]; then
if [[ -z "$npm_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires NPM 9 or higher${NC}\n"
fi
# Node
printf " - ${BOLD}node $node_version${NC}\n"
if [[ $HAS_NODE = false ]]; then
if [[ -z "$node_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires Node 18 or higher${NC}\n"
fi
# Python
printf " - ${BOLD}python $python_version${NC}\n"
if [[ $HAS_PYTHON = false ]]; then
if [[ -z "$python_version" ]]; then
printf " ${RED}No installation"
fi
printf " ${RED}Requires Python 3.5 or higher${NC}\n"
fi
# Pip
printf " - ${BOLD}pip $pip_version${NC}\n"
if [[ $HAS_PIP = false ]]; then
if [[ -z "$pip_version" ]]; then
printf " ${RED}No installation${NC}\n"
fi
printf " ${RED}Requires Pip 20 or higher${NC}\n"
fi
printf "\n${BOLD}Dependencies${NC}\n"
# Requests
printf " - ${BOLD}requests${NC} (Python)\n"
if [[ $HAS_REQUESTS = false ]]; then
printf " ${RED}Missing${NC}\n"
fi
# Langchain
printf " - ${BOLD}langchain${NC} (Python)\n"
if [[ $HAS_LANGCHAIN = false ]]; then
printf " ${RED}Missing${NC}\n"
fi
# Dotenv
printf " - ${BOLD}dotenv${NC} (Python)\n"
if [[ $HAS_DOTENV = false ]]; then
printf " ${RED}Missing${NC}\n"
fi
# Node Modules
printf " - ${BOLD}node_modules${NC} (Node)\n"
if [[ $HAS_NODE_MODULES = false ]]; then
printf " ${RED}Missing${NC}\n"
fi
# If there are still issues, print a message
if [[ $MISSING_TOOLS -gt 0 || $MISSING_DEPS -gt 0 ]]; then
printf "\n${RED}There are still issues${NC}\n"
printf "${YELLOW}Please resolve them and try again${NC}\n"
printf "See ${BOLD}README.md${NC} for more information\n"
exit 1
fi
printf "\n${GREEN}All dependencies are installed and compatible${NC}\n"
exit 0