-
Notifications
You must be signed in to change notification settings - Fork 102
/
functions
executable file
·1600 lines (1529 loc) · 43.9 KB
/
functions
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
if [ -n "${DEVON_IDE_TRACE}" ]; then set -vx; fi
# Functions to be reused in devonfw-ide commands. Will actually be sourced,
# hash bang only for filetype detection and editor syntax support
if [ -z "${DEVON_IDE_HOME}" ]
then
cd "$(dirname "${BASH_SOURCE:-$0}")/.." || exit 1
DEVON_IDE_HOME="${PWD}"
cd - > /dev/null || exit 1
echo "DEVON_IDE_HOME variable set to ${DEVON_IDE_HOME}"
fi
# shellcheck source=scripts/environment-project
source "${DEVON_IDE_HOME}/scripts/environment-project" export
DEVON_DOWNLOAD_DIR="${DEVON_HOME_DIR}/Downloads/devonfw-ide"
# $1: message (may contain newlines with \n)
# $2: optional exit code
function doFail() {
doEchoAttention ""
doError "${1}\nWe are sorry for the inconvenience. Please check the above errors, resolve them and try again."
if [ -n "${2}" ]
then
doError "Exit code was ${2}"
if [ "${2}" = "0" ]
then
exit 1
fi
exit "${2}"
else
exit 255
fi
}
function doEchoAttention() {
echo
doWarning "******** ATTENTION ********"
if [ -n "${1}" ]
then
doWarning "${@}"
fi
}
# $1: message
# $2: exit code
function doResult() {
if [ "${2}" = 0 ]
then
doSuccess "Success: ${1}"
else
if [ -z "${2}" ]
then
doFail "Failed to ${1} (internal error missing exit code)"
else
doFail "Failed to ${1}" "${2}"
fi
fi
}
# $1: command
# $2: message
# $3: optional working directory
function doRunCommand() {
local cwd=${PWD}
if [ -n "${3}" ]
then
if [ -d "${3}" ]
then
cd "${3}" || exit 1
else
doFail "Working directory ${3} does not exist."
fi
fi
local message
if [ -z "${2}" ]
then
message="run command ${1/ */}"
else
message="${2} (${1/ */})"
fi
doDebug "Trying to ${message}"
eval "${1}"
result=${?}
if [ -n "${3}" ]
then
cd "${cwd}" || exit 1
fi
doResult "${message}" ${result}
}
function doIsForce() {
# shellcheck disable=SC2154
[ -n "${force}" ]
return
}
function doIsBatch() {
# shellcheck disable=SC2154
[ -n "${batch}" ]
return
}
function doRequireNotBatch() {
if doIsBatch
then
doFail "Cannot proceed in batch mode as interactive installation is required. Please rerun without batch option."
fi
}
function doIsQuiet() {
# shellcheck disable=SC2154
[ -n "${quiet}" ]
return
}
function doIsDebug() {
# shellcheck disable=SC2154
[ -n "${debug}" ] && [ -z "${quiet}" ]
return
}
# $1: basename of folder
function doIsIgnoredFolder() {
case "${1}" in
target)
return;;
eclipse-target)
return;;
node_modules)
return;;
.git)
return;;
.svn)
return;;
esac
return 255
}
# $@: messages to output
function doEcho() {
if doIsQuiet
then
return
fi
echo -e "${@}"
}
# $@: messages to output
function doInfo() {
echo -e "\033[34m${*}\033[39m"
}
# $@: messages to output
function doEchoStep() {
echo -e "\n\033[35m*** ${*} ***\033[39m"
}
# $@: messages to output
function doEchoOption() {
echo -e "\033[96m${*}\033[39m"
}
# $@: success message
function doEchoInteraction() {
echo -e "\033[96m${*}\033[39m"
}
function doDebug() {
if ! doIsDebug
then
return
fi
echo -e "\033[90m${*}\033[39m"
}
# $@: warning message
function doWarning() {
echo -e "\033[93m${*}\033[39m"
}
# $@: messages to output
function doError() {
echo -e "\033[91m${1}\033[39m"
}
# $@: success message
function doSuccess() {
echo -e "\033[92m${*}\033[39m"
}
# $@: success message
function doQuestion() {
doEchoInteraction "${@}"
}
# $@: warning message
function doConfirmWarning() {
doWarning "${@}"
echo
doAskToContinue ""
}
# $1: optional question
# $2: if 'return' will return with 255 otherwise exit if not continued
function doAskToContinue() {
local question="Do you want to continue? "
if [ -n "${1}" ]
then
question="${1}"
fi
if [ -n "${force}" ] || [ -n "${batch}" ]
then
doQuestion "${question}"
doEcho "Yes (batch/force)"
return
fi
local answer
while true
do
doQuestion "${question}"
read -r -p "(yes/no): " answer
if [ "${answer}" = "yes" ] || [ -z "${answer}" ]
then
return
elif [ "${answer}" = "no" ]
then
doEcho "No..."
if [ "${2}" = "return" ]
then
return 255
else
exit 255
fi
else
doQuestion "Please answer yes or no (or hit return for yes)."
fi
done
}
function doLogo() {
local logo=(
" .......... /// "
" .......... ///// dd fffff "
" .......... //////// ddd ffffff "
" .......... ////////// ddd ff "
" .......... ////////// dddddddd eeeeeee vvv vvv oooo nnnnnn fffffff ww ww"
".......... ////////// dddddddddd eeeeeeeeee vvv vvv oooooooooo nnnnnnnnnn fffffff ww w ww "
".......... ////////// dddd ddd eeee eee vvv vvv oooo oooo nnn nnn ff ww www ww "
".......... ////////// ddd ddd eeeeeeeeeee vvv vvv ooo ooo nnn nnn ff ww wwwww ww "
" .......... ////////// ddd ddd eeeeeeeeeee vvvvvvv ooo ooo nnn nnn ff ww wwwww ww "
" .......... ////////// ddd ddd eeee vvvvv ooo ooo nnn nnn ff wwww wwww "
" ........ ////////// dddddddd eeeeeeeeee vvv oooooooo nnn nnn ff ww ww "
" ..... ////////// ddddd eeeeee v oooo nnn nnn ff w w "
" ... ////////// "
)
local len
len="$(tput cols)"
if [ "$?" != 0 ]
then
len=80
fi
if [ "${len}" -gt 120 ]
then
len=120
fi
for ((i=0; i<${#logo[*]}; i=i+1))
do
echo "${logo[${i}]:0:$len}"
done
}
# $1: file or URL to open
function doOpen() {
if [ "${OSTYPE}" = "cygwin" ]
then
cygstart "${@}"
elif [ "${OSTYPE}" = "msys" ]
then
start "${@}"
else
open "${@}"
fi
}
function doLicenseAgreement() {
if ! [ -f "${DEVON_HOME_DIR}/.devon/.license.agreement" ]
then
echo
doLogo
echo
echo "Welcome to devonfw-ide!"
echo "This product and its 3rd party components is open-source software and can be used free (also commercially)."
echo "However, before using it you need to read the license agreement with all involved licenses agreements."
echo "With confirming you take notice and agree that there is no warranty for using this product and its 3rd party components."
echo "You are solely responsible for all risk implied by using this software."
echo "You will be able to find it in one of the following locations:"
echo "https://github.com/devonfw/ide/blob/master/documentation/LICENSE.asciidoc"
echo "Also it is included in ${DEVON_IDE_HOME}/devon-ide-doc.pdf"
echo
if ! doIsBatch
then
doOpen "https://github.com/devonfw/ide/blob/master/documentation/LICENSE.asciidoc"
fi
if doIsBatch
then
doFail "You need to accept these terms of use and all license agreements. Please rerun in interactive (non-batch) mode."
fi
if doAskToContinue "Do you accept these terms of use and all license agreements?" "return"
then
echo -e "On $(date +"%Y-%m-%d") at $(date +"%H:%M:%S") you accepted the devonfw-ide License.\nhttps://github.com/devonfw/ide/blob/master/documentation/LICENSE.asciidoc" > "${DEVON_HOME_DIR}/.devon/.license.agreement"
else
exit 255
fi
echo
fi
}
# ${1}: devon command name
# ${n+1}: additional args
function doDevonCommand() {
doDevonCommandAndReturn "${@}"
result=${?}
if [ ${result} != 0 ]
then
if ! doIsBatch
then
doAskToContinue "Failed to run devon ${*} - do you want to continue?"
else
exit ${result}
fi
fi
}
function doDevonCommandAllWorkspaces() {
cd "${DEVON_IDE_HOME}/workspaces" || exit 1
for file in *
do
if [ -d "${file}" ]
then
cd "${file}" || exit 1
doEcho "Repeating command ${*} in workspace ${PWD}"
"${DEVON_IDE_HOME}/scripts/devon" "${@}"
cd .. || exit 1
elif [ "${file}" != "readme.txt" ]
then
doWarning "The workspaces should only contain folder but ${file} is not"
fi
done
}
function doDevonCommandAndReturn() {
if [ -z "${1}" ]
then
doFail "Command is required and can not be omitted!"
fi
local command_name="${1}"
local command="${DEVON_IDE_HOME}/scripts/command/${1}"
if [ ! -e "${command}" ]
then
doFail "Undefined devon command: ${1}\nNot found at ${command}."
else
if [ ! -x "${command}" ]
then
doWarning "Command ${1} is not executable. Trying to repair..."
chmod a+x "${command}"
fi
shift
# shellcheck disable=SC2086
"${command}" ${force} ${quiet} ${debug} ${batch} "${@}"
result=${?}
if [ ${result} != 0 ]
then
doError "Command '${command_name} ${*}' failed with exit code ${result}"
return ${result}
fi
fi
}
# $1: the URL to download
# $2: the optional, target directory to save to
# $3: the software to download (e.g. 'java', 'mvn', 'node')
# $4: the version of the software to download
# $5: the optional edition (e.g. "enterprise" or "community")
# $6: the optional code (magic value to avoid tool specific quirks in general functions)
# $7: operating system name
# $8: the optional architecture (e.g. x64 or x86_64)
# $9: the optional extension (e.g. 'tar.gz' or 'zip')
# $10: the optional filename to download to
function doDownload() {
doLicenseAgreement
local url="${1}"
local target_dir="${2}"
local software="${3}"
local version="${4}"
local edition="${5}"
local code="${6}"
local os="${7}"
local arch="${8}"
local ext="${9}"
local filename="${10}"
if [ -z "${target_dir}" ]
then
target_dir="${DEVON_DOWNLOAD_DIR}"
fi
mkdir -p "${target_dir}"
local tmp_file
tmp_file=$(mktemp -ut "${software}-${version}.XXXXXX")
if [ "${url}" = "" ] || [ "${url}" = "-" ]
then
if [ -z "${arch}" ]
then
arch="$(uname -m)"
fi
if [ -z "${os}" ]
then
if doIsMacOs
then
os="mac"
elif doIsWindows
then
os="windows"
else
os="${OSTYPE}"
fi
fi
local mirrors_dir="${DEVON_IDE_HOME}/mirrors"
doDebug "Cloning or updating download mirrors"
doGitPullOrClone "${mirrors_dir}" "${DEVON_MIRRORS:-https://github.com/devonfw/ide-mirrors.git}"
local mirrors_software_dir="${mirrors_dir}/${software}"
if [ ! -d "${mirrors_software_dir}" ]
then
doFail "No mirror is configured at ${mirrors_software_dir}"
fi
local mirrors_override_dir="${mirrors_software_dir}"
local folder
local result
if [ -e "${mirrors_software_dir}/versions" ]
then
while IFS='<' read -r folder version_limit
do
if [ "${version_limit}" = '*' ]
then
result=2
else
doVersionCompare "${version}" "${version_limit}"
result=${?}
fi
# ${version} < ${version_limit} ?
if [ "${result}" = 2 ]
then
mirrors_override_dir="${mirrors_software_dir}/${folder}"
if [ ! -d "${mirrors_override_dir}" ]
then
doWarning "Configured version folder does not exist: ${mirrors_override_dir}"
fi
break;
fi
done < "${mirrors_software_dir}/versions"
fi
if [ "${mirrors_override_dir}" != "${mirrors_software_dir}" ]
then
doDebug "For software ${software} in version ${version} the mirror config is overridden by ${mirrors_override_dir}"
fi
# remap architecture
local mirrors_arch
local arch_new="${arch}"
mirrors_arch="$(doGetFirstExistingPath "${mirrors_override_dir}/arch-mappings" "${mirrors_software_dir}/arch-mappings" "${mirrors_dir}/arch-mappings")"
if [ -e "${mirrors_arch}" ]
then
while IFS="=" read -r key value
do
if [ "${key}" = "${arch_new}" ] || [ "${key}" = '*' ]
then
doDebug "Remapped architecture from ${arch_new} to ${value} for software ${software} in version ${version}"
arch_new="${value}"
break
fi
done < "${mirrors_arch}"
fi
# map OS to ext(ension)
local mirrors_ext
mirrors_ext="$(doGetFirstExistingPath "${mirrors_override_dir}/ext-mappings" "${mirrors_software_dir}/ext-mappings")"
if [ -e "${mirrors_ext}" ]
then
while IFS="=" read -r key value
do
if [ "${key}" = "${os}" ] || [ "${key}" = '*' ]
then
doDebug "Mapped extension to ${value} for OS ${os} and software ${software} in version ${version}"
ext="${value}"
break
fi
done < "${mirrors_ext}"
fi
# remap operating system
local mirrors_os
local os_new="${os}"
mirrors_os="$(doGetFirstExistingPathOrFail "${mirrors_override_dir}/os-mappings" "${mirrors_software_dir}/os-mappings" "${mirrors_dir}/os-mappings")"
local key
local value
while IFS="=" read -r key value
do
if [ "${key}" = "${os_new}" ] || [ "${key}" = '*' ]
then
doDebug "Remapped OS from ${os_new} to ${value} for software ${software} in version ${version}"
os_new="${value}"
break
fi
done < "${mirrors_os}"
# get mirror URLs
local mirrors_urls
mirrors_urls="$(doGetFirstExistingPathOrFail "${mirrors_override_dir}/urls" "${mirrors_software_dir}/urls")"
doDebug "Reading mirror URLs from ${mirrors_urls}"
local major="${version//[^0-9]*}"
local mirror
local mirrors
declare -a mirrors
while IFS= read -r mirror
do
mirrors[i]="${mirror}"
((++i))
done < "${mirrors_urls}"
# suffle mirrors to choose mirrors in random order and distribute load
local size="${#mirrors[*]}"
local rand
local tmp
local i
for ((i=size-1; i>0; i--))
do
rand=$((RANDOM%(size+1)))
tmp="${mirrors[i]}"
mirrors[i]="${mirrors[rand]}"
mirrors[rand]="${tmp}"
done
# download from mirrors, try next mirror on error
for mirror in "${mirrors[@]}"
do
local mirror_url="${mirror}"
mirror_url="${mirror_url//\$\{os\}/${os_new}}"
mirror_url="${mirror_url//\$\{version\}/${version}}"
mirror_url="${mirror_url//\$\{arch\}/${arch_new}}"
mirror_url="${mirror_url//\$\{ext\}/${ext}}"
mirror_url="${mirror_url//\$\{edition\}/${edition}}"
mirror_url="${mirror_url//\$\{major\}/${major}}"
mirror_url="${mirror_url//\$\{code\}/${code}}"
if [ -z "${filename}" ]
then
filename="${software}-${version}"
if [ -n "${edition}" ]
then
filename="${filename}-${edition}"
fi
if [ "${os}" != "-" ] && [ -n "${os}" ]
then
filename="${filename}-${os}"
fi
if [ -n "${ext}" ]
then
filename="${filename}.${ext}"
else
doWarning "No filename extension configured for ${software} in version ${version}"
filename="${filename}.$(doGetExtension "${mirror_url}")"
fi
fi
doDownloadInternal "${mirror_url}" "${tmp_file}" "${target_dir}" "${filename}"
local result=${?}
if [ "${result}" = 0 ] || [ "${result}" = 255 ]
then
return ${result}
fi
done
doFail "Download of ${software} in version ${version} failed after trying ${#mirrors[*]} mirror(s)."
else
doDownloadInternal "${url}" "${tmp_file}" "${target_dir}" "${filename}"
result=${?}
if [ "${result}" = 0 ] || [ "${result}" = 255 ]
then
return ${result}
fi
doFail "Failed to download ${filename} from ${1}" ${result}
fi
}
# $1: URL to download
# $2: path to tmp-file to download to
# $3: path to download folder
# $4: optional download filename
function doDownloadInternal() {
local url="${1}"
local tmp_file="${2}"
local target_folder="${3}"
DOWNLOAD_FILENAME="${4}"
if [ -z "${DOWNLOAD_FILENAME}" ]
then
DOWNLOAD_FILENAME="${url/*\//}"
fi
doEcho "Trying to download ${DOWNLOAD_FILENAME} from ${url}"
local target="${target_folder}/${DOWNLOAD_FILENAME}"
if [ -f "${target}" ] && [ -z "${force}" ]
then
doWarning "Artifact already exists at ${target}\nTo force update please delete the file and run again."
return 255
fi
local curl_opt=""
if [ -n "${quiet}" ]
then
curl_opt="--silent"
fi
curl ${curl_opt} -fL "${url}" -o "${tmp_file}"
local result=${?}
if [ "${result}" != 0 ]
then
doWarning "Failed to download ${url} with exit code ${result}"
return ${result}
else
mv "${tmp_file}" "${target}"
doSuccess "Download of ${DOWNLOAD_FILENAME} from ${url} succeeded."
fi
}
# $1: filename (potentiall including absolute or relative path)
# echos the file extension
function doGetExtension() {
local filename="${1/*\//}"
local ext="${filename/*\./}"
local filename_base="${filename:0:(${#filename}-${#ext}-1)}"
if [ "${filename_base/*\./.}" = ".tar" ]
then
ext="tar.${ext}"
fi
echo "${ext}"
}
#.$@ any number of file paths
# echos the first of the given paths that does exist
function doGetFirstExistingPathOrFail() {
local message="None of the followin path(s) exists:"
while [ -n "${1}" ]
do
message="${message}\n${1}"
if [ -e "${1}" ]
then
echo "${1}"
return
fi
shift
done
doFail "${message}"
}
#.$@ any number of file paths
# echos the first of the given paths that does exist
function doGetFirstExistingPath() {
while [ -n "${1}" ]
do
if [ -e "${1}" ]
then
echo "${1}"
return
fi
shift
done
return 1
}
# $1: the file to extract
# $2: optional folder to extract to
function doExtract() {
local target_dir=${DEVON_IDE_HOME}/updates/extracted
local filename=${1/*\//}
rm -rf "${target_dir}"
if [ -n "${2}" ]
then
target_dir="${target_dir}/${2}"
fi
mkdir -p "${target_dir}"
local ext="${filename/*\./.}"
local filename_base="${filename:0:(${#filename}-${#ext})}"
if [ "${filename_base/*\./.}" = ".tar" ] || [ "${ext}" = ".tar" ] || [ "${ext}" = ".tgz" ] || [ "${ext}" = ".tbz2" ]
then
doRunCommand "tar xf '${1}' -C '${target_dir}'"
elif [ "${ext}" = ".zip" ] || [ "${ext}" = ".jar" ]
then
doUnzip "${1}" "${target_dir}"
elif [ "${ext}" = ".dmg" ]
then
local mount_dir="${DEVON_IDE_HOME}/updates/volume"
mkdir -p "${mount_dir}"
doRunCommand "hdiutil attach -quiet -nobrowse -mountpoint '${mount_dir}' '${1}'"
doRunCommand "cp -a '${mount_dir}'/*.app '${target_dir}'"
doRunCommand "hdiutil detach -force '${mount_dir}'"
if [ -e "${target_dir}/Applications" ]
then
rm "${target_dir}/Applications"
fi
elif doIsInstallerExtension "${filename}"
then
doRunCommand "mv \"${1}\" \"${target_dir}\""
else
doFail "Unknown archive format: ${ext}. Can not extract ${1}"
fi
doSuccess "Successfully extracted/moved archive ${filename} to updates/extracted"
}
# $1: the file to unzip
# $2: target_dir
function doUnzip() {
if ! command -v unzip &> /dev/null
then
pckmngers=("apt-get" "yum" "pacman" "emerge" "zypp" "apk" "dnf" "brew")
for t in "${pckmngers[@]}"; do
command -v "${t}" > /dev/null
if [ "${?}" -eq "0" ]
then
pckmnger=${t}
fi
done
case ${pckmnger} in
apt-get) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
yum) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
pacman) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} -S unzip";;
emerge) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
zypp) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
apk) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} add unzip";;
dnf) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
brew) doEcho "installing unzip..."
doRunCommand "sudo ${pckmnger} install unzip";;
*) doEcho "please install unzip manually and start again"
exit;;
esac
fi
doEcho "unzip archive..."
doRunCommand "unzip -qn '${1}' -d '${target_dir}'"
}
function doInstallWithPackageManager() {
local first
for installString in "${@}"
do
first="${installString/ */}"
if command -v "${first}" > /dev/null
then
doEcho "Installing: ${installString}"
doRunCommand "sudo ${installString}"
return
fi
done
doError "No supported package manager found on your system.\nInstallation failed: ${1}"
}
# $1: filename (e.g. ${filename})
function doIsInstallerExtension()
{
local myext="${1/*\./.}"
if [ "${myext}" = ".msi" ] || [ "${myext}" = ".exe" ] || [ "${myext}" = ".pkg" ] || [ "${myext}" = ".bat" ]
then
return
fi
return 255
}
# $1: absolute path of file or folder to move
# $2: optional target (e.g. "${DEVON_IDE_HOME}/software/")
# $3: backup path
function doReplaceExtractedFile() {
if [ ! -e "${1}" ]
then
doFail "The file or folder to move does not exist: ${1}"
fi
local filename="${1/*\//}"
local target_dir="${DEVON_IDE_HOME}"
local target="${DEVON_IDE_HOME}/${filename}"
if [ -n "${2}" ]
then
target="${2}"
target_dir="$(dirname "${2}")"
fi
if [ "${target}" = "${DEVON_IDE_HOME}/scripts" ] && doIsWindows
then
(sleep 10;doBackup "${target}" "${3}";doRunCommand "mv '${1}' '${target}'") &
else
doBackup "${target}" "${3}"
if [ ! -d "${target_dir}" ]
then
mkdir -p "${target_dir}"
fi
doRunCommand "mv '${1}' '${target}'"
fi
}
# $1: source file or directory to backup
# $2: optional relative timestamp path
function doBackup() {
if [ -e "${1}" ]
then
local source="${1}"
local timestamp="${2}"
if [ -z "${timestamp}" ]
then
timestamp="$(date "+%y-%m-%d")"
fi
local backup_dir="${DEVON_IDE_HOME}/updates/backups/${timestamp}"
if [ -e "${backup_dir}/${source/*\//}" ]
then
backup_dir="${backup_dir}/$(date "+%H-%M-%S")"
fi
mkdir -p "${backup_dir}"
doEcho "Creating backup by moving existing ${source} to ${backup_dir}"
mv "${source}" "${backup_dir}/" || doFail "Failed to move ${source} to ${backup_dir}.\nMost probably this is a problem of windows file locks. In such case please terminate all processes locking the files and retry the operation."
fi
}
# $1: path of the extracted name to install (move to target)
# $2: optional target (e.g. "${DEVON_IDE_HOME}/software/")
# $3: backup path
# $4-$N: contents of $1 ($1/* so all files in the folder of $1 after globbing)
function doReplaceExtractedSkipSingleFolder() {
local source_dir="${1}"
local target_dir="${2}"
local backup_dir="${3}"
shift 3
if [ "${#*}" = 1 ] && [ -d "${1}" ] && [[ ! "${1}" =~ .*\.app ]]
then
source_dir="${1}"
fi
doReplaceExtractedFile "${source_dir}" "${target_dir}" "${backup_dir}"
}
# $1: path of the extracted name to install (move to target)
# $2: target (e.g. "${DEVON_IDE_HOME}/software/")
function doReplaceExtracted() {
local backup_dir
backup_dir="$(date +"%y-%m-%d")"
if [ "${2}" = "${DEVON_IDE_HOME}" ]
then
for extracted_file in "${1}"/*
do
if [ -e "${extracted_file}" ]
then
if [ "${extracted_file/*\//}" = "workspaces" ]
then
doEcho "as the target already exists, omitting ${extracted_file}"
else
doReplaceExtractedFile "${extracted_file}" "${2}/${extracted_file/*\//}" "${backup_dir}"
fi
fi
done
else
doReplaceExtractedSkipSingleFolder "${1}" "${2}" "${backup_dir}" "${1}"/*
fi
}
# $1: URL (to artifactId)
function doMavenGetLatestVersion() {
doEcho "Trying to determine the latest available version from ${1}/maven-metadata.xml"
LATEST_VERSION="$(curl -fs "${1}/maven-metadata.xml" | grep latest | sed "s/.*<latest>\([^<]*\)<\/latest>.*/\1/")"
result=${?}
if [ "${result}" != 0 ] || [ -z "${LATEST_VERSION}" ]
then
doFail "Failed to determine the latest version from ${1}/maven-metadata.xml.\nCheck that 'curl' is installed (curl -v) and you have internet connection available." ${result}
fi
}
# $1: version
function doIsDevVersion() {
[ "${1}" = "dev-SNAPSHOT" ] || [ "${1}" = "0-SNAPSHOT" ]
}
# defaults, may be overridden before calling doMavenArchetype
ARCHETYPE_GROUP_ID="com.devonfw.java.templates"
ARCHETYPE_ARTIFACT_ID="devon4j-template-server"
# ${1}-${n-1}: array
# ${n}: prefix to check if array contains element starting with
function doArrayContainsItemWithPrefix() {
local n
local element
n=${#}
element="${!n}"
for ((i=1; i < ${#}; i++)) {
if [[ "${!i}" == ${element}* ]]
then
return 0
fi
}
return 1
}
# $1: java package
# $2: optional groupId
# $3: optional artifactId
function doMavenArchetype() {
if [ -z "${1}" ]
then
doFail "Missing arguments for doMavenArchetype ${*}"
fi
local old_args
old_args="${*}"
local target_package
local target_group_id
local target_artifact_id
declare -a target_args
while [ -n "${1}" ]
do
if [ "${1:0:1}" = "-" ]
then
target_args+=("${1}")
else
if [ -z "${target_package}" ]
then
target_package="${1}"
elif [ -z "${target_group_id}" ]
then
target_group_id="${1}"
elif [ -z "${target_artifact_id}" ]
then
target_artifact_id="${1}"
else
doFail "Too many args: ${*}"
fi
fi
shift
done
if [ -n "${target_package}" ]
then
# package
if doArrayContainsItemWithPrefix "${target_args[@]}" "-Dpackage="
then
doFail "Duplicate package for archetype: ${old_args}"
else
target_args+=("-Dpackage=${target_package}")
fi
# artifactId
if doArrayContainsItemWithPrefix "${target_args[@]}" "-DartifactId="
then
if [ -n "${target_artifact_id}" ]
then
doFail "Duplicate artifactId for archetype: ${old_args}"
fi
else
if [ -z "${target_artifact_id}" ]
then
target_artifact_id="${target_package/*\./}"
fi
target_args+=("-DartifactId=${target_artifact_id}")
fi
# groupId
if doArrayContainsItemWithPrefix "${target_args[@]}" "-DgroupId="
then
if [ -n "${target_group_id}" ]
then
doFail "Duplicate groupId for archetype: ${old_args}"
fi
else
if [ -z "${target_group_id}" ]
then
local artifact_id="${target_package/*\./}"
if [ "$artifact_id" = "${target_package}" ]
then
target_group_id="${target_package}"
else
target_group_id="${target_package:0:(${#target_package}-${#artifact_id}-1)}"
fi
fi
target_args+=("-DgroupId=${target_group_id}")
fi
fi
if ! doArrayContainsItemWithPrefix "${target_args[@]}" "-Dversion="
then
target_args+=("-Dversion=1.0.0-SNAPSHOT")
fi
if ! doArrayContainsItemWithPrefix "${target_args[@]}" "-DarchetypeVersion="
then
doMavenGetLatestVersion "https://repo.maven.apache.org/maven2/${ARCHETYPE_GROUP_ID//.//}/${ARCHETYPE_ARTIFACT_ID}"
target_args+=("-DarchetypeVersion=${LATEST_VERSION}")
fi
if [ -z "${target_artifact_id}" ]
then
for ((i=1; i < ${#}; i++)) {
if [[ "${!i}" == -DartifactId=* ]]
then
target_artifact_id="${!i:13}" # "-DartifactId=" is 13 chars long
fi
}
fi
if [ -e "${target_artifact_id}" ]
then
doFail "Project ${target_artifact_id} already exists at ${PWD}"