-
Notifications
You must be signed in to change notification settings - Fork 4
/
zanata-functions
executable file
·904 lines (836 loc) · 26.1 KB
/
zanata-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
#!/bin/bash
### NAME
### zanata-functions
###
### SYNOPSIS
### zanata-functions help
### zanata-functions help-list-sections
### zanata-functions help-section <section>
### zanata-functions run <function> [functionArguments] ...
###
### DESCRIPTION
### This program can be sourced by other program and serve as library,
### or run the individual function.
###
### You can just source this program to use all the functions.
### Or run as standalone by using:
###
### zanata-functions run <function> [functionArguments]
###
### If you don't want to clone the whole zanata-scripts, you can invoke this
### by:
###
### curl -q https://raw.githubusercontent.com/zanata/zanata-scripts/master/zanata-functions | bash -s - [opt1...]
###
###
set -eu
### FUNCTIONS
function zanata_functions_get_path(){
shopt -s extdebug
declare -F zanata_functions_get_path| sed -e 's|^[^ ]* [0-9]* ||'
shopt -u extdebug
}
###
### zanata_help_raw <script>
### Print raw help (the lines start with '###') of a script.
zanata_help_raw(){
local script="$1"
[ -z "$script" ] && script=$0
sed -r -n -e '/^### ?/ {s|^### ?||p}' $script
}
zanata_help_parse_raw(){
declare -gA ZanataHelpSections
local currentSection=''
local regex='^[A-Z][A-Z_]*'
while read ;do
if [[ $REPLY =~ $regex ]];then
currentSection=$REPLY
elif [[ -n $currentSection ]];then
[[ -n ${ZanataHelpSections[$currentSection]:-} ]] && ZanataHelpSections[$currentSection]+='\n'
ZanataHelpSections[$currentSection]+="$REPLY"
fi
done
}
###
### zanata_help_init <script>
### Read help from script and zanata-env.sh when ZanataHelpSections is not defined
zanata_help_init(){
local script="$1"
if [[ -z ${ZanataHelpSections:-} ]]; then
zanata_help_parse_raw < <(zanata_help_raw $script)
[[ -n ${ZanataEnvSh:-} && -r $ZanataEnvSh ]] && zanata_help_parse_raw < <(zanata_help_raw "$ZanataEnvSh")
fi
}
###
### zanata_help_list_sections <script>
### List the help sections of a script.
function zanata_help_list_sections(){
local script="$1"
zanata_help_init "$script"
for section in "${!ZanataHelpSections[@]}"; do
echo $section
done
}
###
### zanata_help_section <script> <section>
### Print the specified help section of a script.
function zanata_help_section(){
local script="$1"
local section="$2"
zanata_help_init "$script"
echo -e "${ZanataHelpSections[$section]}"
}
###
### zanata_script_help_short <script>
### Print the short help of a script.
function zanata_script_help_short(){
local script=$1
zanata_help_init "$script"
MustHaveSectionArray=( SYNOPSIS ARGUMENTS OPTIONS ENVIRONMENT )
for section in "${MustHaveSectionArray[@]}"; do
if [[ -n ${ZanataHelpSections[$section]:-} ]]; then
echo $section
echo -e "${ZanataHelpSections[$section]}\n"
fi
done
}
###
### zanata_script_help <script> <section>
### Print the detail help of a script.
function zanata_script_help(){
local script=$1
zanata_help_init "$script"
HeaderSectionArray=( NAME SYNOPSIS DESCRIPTION ARGUMENTS OPTIONS )
FooterSectionArray=( EXIT_STATUS ENVIRONMENT FILES NOTES BUGS EXAMPLE SEE_ALSO )
declare -a MidSectionArray
for section in "${HeaderSectionArray[@]}"; do
if [[ -n ${ZanataHelpSections[$section]:-} ]]; then
echo $section
echo -e "${ZanataHelpSections[$section]}\n"
fi
done
## Other sections should be in middle
for section in "${!ZanataHelpSections[@]}"; do
for sec in "${HeaderSectionArray[@]}"; do
[[ $sec == $section ]] && break
done
[[ $sec == $section ]] && continue
for sec in "${FooterSectionArray[@]}"; do
[[ $sec == $section ]] && break
done
[[ $sec == $section ]] && continue
echo $section
echo -e "${ZanataHelpSections[$section]}\n"
done
for section in "${FooterSectionArray[@]}"; do
if [[ -n ${ZanataHelpSections[$section]:-} ]]; then
echo $section
echo -e "${ZanataHelpSections[$section]}\n"
fi
done
}
###
### echo_stderr [args] ...
### Echo to stderr. Args are same with echo.
### Note that ZANATA_QUIET_MODE has no effect on this function
echo_stderr(){
echo "$@" > /dev/stderr
}
###
### branch_checkout <repoName> <branch> [parentDir]
### checkout <branch> for <repoName>.
### Note that:
### 1. this won't cd to the repoName directory.
### 2. If local branch will be override by remote branch
branch_checkout(){
local repoName=$1
local branch=$2
local parentDir=${3-}
[[ -z $parentDir ]] && parentDir=${PWD}
cd ${parentDir}/${repoName}
git remote update origin > /dev/stderr
git checkout ${branch} > /dev/stderr
branch_forced_pull
cd - > /dev/stderr
}
###
### branch_does_exist <branch>
### Tell whether the <branch> exists.
### This function assumes you are already in the git checkout
### directory.
### Prints:
### yes: branch exists.
### remote: branch exists at remote, not local.
### no: branch does not exist in remote and local.
### Returns:
### EXIT_OK: The branch exists
### EXIT_RETURN_FALSE: The branch does not exists.
branch_does_exist(){
local branch=$1
if git rev-parse --verify ${branch} &>/dev/null;then
echo "yes"
return $EXIT_OK
elif git ls-remote --heads origin $branch | grep $branch >& /dev/null ;then
echo "remote"
return $EXIT_OK
fi
echo "no"
return $EXIT_RETURN_FALSE
}
###
### branch_forced_pull [remote]
### Withdraw local changes and pull the remote,
### which, by default, is 'origin'
### Note that function does nothing to a detached HEAD
branch_forced_pull(){
local branch=$(branch_get_current)
local remote=${1:-origin}
if [[ $branch = 'HEAD' ]] ;then
return
fi
git fetch $remote $branch > /dev/stderr
git reset --hard "$remote/$branch" > /dev/stderr
}
###
### branch_get_current
### Return current branch name, or HEAD when detach.
### Note that this does not work with git detach mode.
branch_get_current(){
git rev-parse --abbrev-ref HEAD
}
###
### branch_get_releasing
### Determine RELEASING_BRANCH, the branch that we recommend downstream to use,
### and release process should mainly happen here.
### This function assumes you are in correct checkout directory.
### It is:
### 'release' if that branch exists; otherwise "master"
branch_get_releasing(){
local ret=$(branch_does_exist release)
if [[ $ret = yes || $ret = remote ]] ;then
echo "release"
return 0
fi
echo "master"
return 0
}
###
### branch_prepare <branch> [command line args]
### Prepare and checkout correct branch
### <branch> can be actual branch name, or RELEASING for the releasing branch.
###
### This assumes the module can be determine by either
### 1. part of $0, e.g. zanata-maven-release-platform
### 2. First non-optional argument ($1)
### 3. JenkinMode=1 (which branch is ready already)
###
### Assigned Variables:
### RepoName: Repository name
### Module: (Deprecated) Same as RepoName.
### ShiftOffset: how many argument need to be shift
### Prints: shift offset (either 0 or 1)
### WorkingBranch: Branch that will be working on.
function branch_prepare(){
local branch=$1
shift
ShiftOffset=0
## Get module name
RepoName=$(get_module $0)
if [[ $RepoName = '-' ]];then
## See whether RepoName is provided in first argument
RepoName=$(get_module ${1-})
if [[ $RepoName = '-' ]];then
if [[ ${JustUseCurrentDirectoryMode-} -ne 1 ]];then
failed ${EXIT_FATAL_UNKNOWN_MODULE} "RepoName is not given and not in JustUseCurrentDirectoryMode"
else
## Default module in JustUseCurrentDirectoryMode is the directory name
RepoName=$(basename $PWD)
fi
else
## RepoName provided in first argument
ShiftOffset=1
fi
fi
if [[ ${JustUseCurrentDirectoryMode-} -eq 0 ]];then
print_status " ensure $RepoName exists"
ensure_repo $RepoName
cd ${WORK_ROOT}/${RepoName}
if [[ $branch = "RELEASING" ]];then
WorkingBranch=$(branch_get_releasing)
else
WorkingBranch=$branch
fi
print_status " checking out $WorkingBranch of $RepoName"
branch_checkout $RepoName $WorkingBranch $WORK_ROOT
else
WorkingBranch=$(branch_get_current)
fi
## Make sure the work tree is clean
git clean -fdX
Module=$RepoName
}
### detect_remote_repo_latest_version <URL> <tagGlob>
### This does not need to clone the repository to get the latest version
detect_remote_repo_latest_version(){
local url=$1
local tagGlob=$2
git ls-remote --tags "$url" "refs/tags/$tagGlob[^^{}]" | awk -F '-' '{ print $2, ($3 == "" )?"zfinal":$3, $4}' | sort -V | tr ' ' '-' | sed -e 's/-zfinal-//g' | tail -n 1
}
###
### detect_module_tag [-q] <-j|module>
### Print latest tag of module
function detect_module_tag(){
local module=
local justUseCurrentDirectoryMode=0
local quietMode=0
if [ "$1" = "-q" ];then
quietMode=1
shift
fi
if [ "$1" = "-j" ];then
justUseCurrentDirectoryMode=1;
else
module=$1
fi
if [ $justUseCurrentDirectoryMode -eq 0 ];then
ensure_repo_generic ${WORK_ROOT} ${module}
cd ${WORK_ROOT}/${module}
fi
git fetch --tags 1>/dev/stderr
## This tell you the latest tag of all branch.
local latestTag=$(git describe --tags $( git rev-list --tags --max-count 1))
if [ $justUseCurrentDirectoryMode -eq 0 ];then
cd - > /dev/stderr
fi
if [ $quietMode -eq 0 ];then
print_status " detect_module_tag: ${module:+${module}:} Latest tag: $latestTag"
fi
echo "${latestTag}"
}
###
### detect_module_version [-q] <-j|module>
### Print latest tagged module version.
### This chops the alphabet part of detect_module_tag and
### return strings that start with number.
### e.g. platform-4.0.0-m-7 -> 4.0.0-m-7
function detect_module_version(){
local tag=$(detect_module_tag $@)
sed -n -e 's/[a-zA-Z-]*-\([0-9][0-9.-]*\)/\1/p' <<< $tag
}
###
### ensure_repo_generic <parentDir> <module> [git_repo_url]
### Ensure repo is at <parentDir>/<module>
function ensure_repo_generic(){
local parentDir=$1
local module=$2
local git_repo_url=${3-}
print_status " Ensure the repo $module is at ${parentDir}/${module}"
if [ -z "$git_repo_url" ];then
git_repo_url=${ZANATA_GIT_URL_PREFIX}/${module}.git
fi
mkdir -p ${parentDir}
cd ${parentDir}
if [ ! -d ${module} ];then
git clone "${git_repo_url}"
if [ $? -ne 0 ];then
failed ${EXIT_FATAL_MISSING_DEPENDENCY} "[ERROR] Failed to clone ${git_repo_url}"
fi
fi
cd - >/dev/null
}
###
### ensure_repo <module> [git_repo_url]
### Ensure repo is at $WORK_ROOT/<module>
function ensure_repo(){
ensure_repo_generic "$WORK_ROOT" "$1" "${2-}"
}
###
### exit_if_failed <var> <exitStatus> <exitMsg>
### Exit when <var> is not 0.
function exit_if_failed(){
if [ "$1" -ne 0 ];then
failed $2 "$3"
fi
}
###
### exit_print_error <exitStatus>
### Print error message just before exit.
### This function does not meant to call directly.
### Environment:
### EXIT_MSG: Error message to print.
EXIT_MSG=
function exit_print_error(){
local exitStatus=$?
if [[ ${ZANATA_QUIET_MODE-} -ne 1 ]];then
if [ -n "${exitStatus-}" ];then
case $exitStatus in
${EXIT_OK} )
echo_stderr "### Done ${EXIT_MSG}"
;;
${EXIT_FATAL_INVALID_OPTIONS} )
zanata_script_help_short $0
echo_stderr "### [FATAL] Invalid Options: ${EXIT_MSG}"
;;
${EXIT_FATAL_MISSING_DEPENDENCY} )
echo_stderr "### [FATAL] Missing dependencies: ${EXIT_MSG}"
;;
${EXIT_FATAL_UNKNOWN_MODULE} )
zanata_script_help_short $0
echo_stderr "### [FATAL] Unknown or Invalid module: ${EXIT_MSG}"
;;
${EXIT_FATAL_FAIL} )
echo_stderr "### [FATAL] Fail: ${EXIT_MSG}"
;;
${EXIT_FATAL_INVALID_ARGUMENTS} )
echo_stderr "### [FATAL] Invalid Arugments: ${EXIT_MSG}"
;;
${EXIT_RETURN_FALSE} )
echo_stderr "### [RETURN] FALSE: ${EXIT_MSG}"
;;
* )
echo_stderr "### Unexpected Exit Code ${exitStatus}: ${EXIT_MSG}"
;;
esac
fi
fi
exit ${exitStatus}
}
###
### failed <exitStatus> <exitMsg>
### Exit with the error code <exitStatus> and error message <exitMsg>.
function failed(){
local exitStatus=$1
EXIT_MSG="$2"
exit $exitStatus
}
###
### get_module <argument>
### Determine module.
### Print: <module or - for unknown>
function get_module(){
case ${1-} in
*assets)
echo "zanata-assets"
;;
*jgettext)
echo "jgettext"
;;
*platform)
echo "zanata-platform"
;;
*openprops)
echo "openprops"
;;
*)
echo "-"
;;
esac
}
###
### get_artifact_id <argument>
### Obtain SCM Tag like zanata-parent, api
### Print: <SCM Tag>
function get_artifact_id(){
case $1 in
*api)
echo "api"
;;
*assets)
echo "zanata-assets"
;;
*jgettext)
echo "jgettext"
;;
*platform)
echo "platform"
;;
*openprops)
echo "openprops"
;;
*)
failed ${EXIT_FATAL_UNKNOWN_MODULE} "get_artifact_id: module $1 not recognized "
;;
esac
}
###
### get_artifact_var_value <artifact> <var> [defaultValue]
### Get value of environment variable of an artifact.
### For example, get_artifact_var_value platform staging_repository
### It looks the environment variable PLATFORM_STAGING_REPOSITORY.
### If the environment exists, it returns value;
### if not, it returns the defaultValue if it is given;
### otherwise returns empty string.
### Print: <Value>
function get_artifact_var_value(){
local artifactId=$1
local var=$2
local defaultValue=${3:-}
local envVar=$(tr a-z A-Z <<<"${artifactId}_${var}")
if [[ -v $envVar ]]; then
echo $(eval echo \$$envVar)
else
echo "$defaultValue"
fi
}
###
### is_maven_packaging [str]
function is_maven_packaging(){
case $1 in
pom | jar | maven-plugin | ejb | war | ear | rar | par | zip )
return 0
;;
esac
return 1
}
###
### is_maven_scope [str]
function is_maven_scope(){
case $1 in
compile | provided | runtime | test | system )
return 0
;;
esac
return 1
}
###
### parse_gav <GAV>
### Parse GAV.
### GAV can either in following formats
### commons-cli:commons-cli:1.0
### commons-cli:commons-cli:jar:1.0
### commons-cli:commons-cli:jar:1.0:test
### commons-cli:commons-cli:jar:source:1.0:test
function parse_gav(){
local gav=$1
GroupId=$(awk -F ':' '{print $1}'<<<$gav)
ArtifactId=$(awk -F ':' '{print $2}'<<<$gav)
Packaging=
Classifier=
Version=
Scope=
local c3=$(awk -F ':' '{print $3}'<<<$gav)
local c4=$(awk -F ':' '{print $4}'<<<$gav)
local c5=$(awk -F ':' '{print $5}'<<<$gav)
local c6=$(awk -F ':' '{print $6}'<<<$gav)
if [[ -n "$c6" ]];then
if ! is_maven_scope $c6 ;then
failed $EXIT_ERROR_FAIL "Invalid scope $c6 from $gav"
fi
Scope=$c6
fi
if [[ -n "$c5" ]];then
## c5 can either be scope or version
if [[ -n "$c6" ]];then
Version=$c5
elif is_maven_scope $c5 ;then
Scope=$c5
else
Version=$c5
fi
fi
if [[ -n "$c4" ]];then
## c4 can either be classifier, version or scope
if [[ -n "$Version" ]];then
Classifier=$c4
elif [[ -n "$Scope" ]];then
Version=$c4
elif is_maven_scope $c4 ;then
Scope=$c4
else
Version=$c4
fi
fi
if [[ -z "$c3" ]];then
failed $EXIT_FATAL_INVALID_OPTIONS "Invalid gav $gav"
fi
## c3 can either be packaging, or version
if is_maven_packaging $c3 ;then
Packaging=$c3
elif [[ -n "$Version" ]];then
failed $EXIT_FATAL_INVALID_OPTIONS "Invalid maven packaging $c3 from gav $gav"
else
Version=$c3
fi
test -n "$GroupId" -a -n "$ArtifactId" -a -n "$Version"
exit_if_failed $? ${EXIT_FATAL_INVALID_OPTIONS} "GAV $gav is invalid, should be in groupId:artifactId:version"
Gav="$GroupId:$ArtifactId:$Version"
}
###
### ga_to_rpm_name <GroupId> <artifactId>
### GroupId-ArtifactId to RPM name.
function ga_to_rpm_name(){
echo "$1-$2"
}
###
### gav_to_nv <GroupId> <artifactId> <Version>
### GroupId-ArtifactId-Version to RPM name-version
function gav_to_nv(){
local version=$(sed -e 's/-/_/g' <<<$3)
echo "$(ga_to_rpm_name $1 $2)-$version"
}
###
### gav_to_nvr <GroupId> <artifactId> <Version> [SpecRelease]
### GroupId-ArtifactId-Version to RPM name-version-release
### release default is 1
### Note the release does not include dist tag (.el7)
function gav_to_nvr(){
local groupId=$1
local artifactId=$2
local version=$(sed -e 's/-/_/g'<<<$3)
local release=${4-}
if [[ -z $release ]];then
release=1
fi
echo "$(gav_to_nv $groupId $artifactId $version)-$release"
}
function gav_to_maven_artifact_dir(){
local groupId=$1
local artifactId=$2
local version=$3
local retStr=$(sed -e 's|\.|/|g' <<< ${groupId})
retStr+="/${artifactId}/${version}"
echo "$retStr"
}
###
### gav_to_maven_artifact_file <GroupId> <artifactId> <Version> [Packaging] [Classifier]
### Return the artifact filename when given Gav.
function gav_to_maven_artifact_file(){
local groupId=$1
local artifactId=$2
local version=$3
local packaging=${4-}
local classifier=${5-}
if [ -z "$packaging" ];then
packaging=jar
fi
echo "${artifactId}-${version}${classifier:+-${classifier}}.${packaging}"
}
###
### git_detached_merge_branch <branch>
### Merge a detached commit to branch.
### If the commit is not detached, then no-op.
git_detached_merge_branch(){
local branch=$1
local commitSha=$(git rev-parse HEAD)
local tmpBranch="br-$commitSha"
if [[ $(branch_get_current) = HEAD ]]; then
git branch $tmpBranch $commitSha
git checkout $branch
branch_forced_pull
git merge $tmpBranch
git branch -D $tmpBranch
fi
}
###
### maven_project_version
### Return the project_version in maven
### Note that this function assume you are already in directory that contains pom.xml
maven_project_version(){
$MvnCmd -q -Dexec.executable="echo" -Dexec.args='${project.version}' --non-recursive org.codehaus.mojo:exec-maven-plugin:exec
}
###
### print_status [-n] [-q] [-s] [-t <Stage>] [message]
### Print the status message.
### Options:
### -n: Do not print end-of-line in the end.
### -q: Quiet, do not output anything
### -s: prepend separation line before message
### -t <Stage>: Specify Stage
### If -s is specified, it will pad separation line before message, like
### "(Caller)[Stage]============================== Start"
### It will use following Environment variables
### Stage: As stage inside []
ZnatNoEOL=0
function print_status(){
local outputStr=
local optionArray=()
local optionN=0
local optionS=0
if [[ ${ZANATA_QUIET_MODE-} -eq 1 ]];then
return
fi
while [[ $1 == -* ]]; do
case $1 in
-n )
optionArray+=( -n )
shift
optionN=1
;;
-q )
return
;;
-s )
optionS=1
shift
;;
-t )
shift
Stage=$1
shift
;;
esac
done
if [ $ZnatNoEOL -eq 0 ];then
## Previous line already ended
local caller=$(basename ${BASH_SOURCE[1]})
outputStr+="### "
if [[ $caller != $ProgramName ]];then
outputStr+="($caller)"
fi
outputStr+="[${Stage-}]"
fi
if [ $optionS -eq 1 ];then
outputStr+="=============================="
fi
local message="$1"
outputStr+="$message"
if [ -n "${optionArray-}" ];then
echo_stderr ${optionArray[@]} "$outputStr"
else
echo_stderr "$outputStr"
fi
ZnatNoEOL=$optionN
}
###
### repo_name_get
### In current directory, get the GitHub RepoName.
### e.g. 'zanata-platform', 'openprops'
### This function also set environment
### Print: repo_name
### Returns:
### EXIT_OK: Successfully gets RepoName
### EXIT_FATAL_GIT_FAILED: Failed in git, usually indicated current directory
repo_name_get(){
## url variable definition need to be separated with assignment,
## otherwise exitCode will be 0 even when failed.
local url
set +e
url=$(git config --get remote.origin.url)
local exitCode=$?
set -e
[[ $exitCode -eq 0 ]] || failed $EXIT_FATAL_GIT_FAILED "[repo_name_get] Failed in git, wrong directory perhaps"
sed -nr -e 's|^.*/([A-Za-z0-9-]*)(.git)?$|\1|p' <<<"$url"
}
##
## run_command <command> [args ... ]
## On Dry-run mode, just show the command,
## otherwise run the command.
run_command(){
local xtrace=0
if [[ ${DryRunMode:-} -eq 1 ]];then
print_status " [Dry-Run] $*"
ExitCode=$EXIT_OK
else
[[ $- = *x* ]] && xtrace=1
[[ $xtrace -ne 0 ]] || set -x
"$@"
ExitCode=$?
[[ $xtrace -ne 0 ]] || set +x
fi
}
###
### time_stamp [DATE_FORMAT]
### Use stamp like "2015-06-29-06:56:34-953717856", base on UTC
### Default: '%F-%R-%N'
function time_stamp(){
local DATE_FORMAT=${1-}
if [ -z "$DATE_FORMAT" ];then
DATE_FORMAT='%F-%R-%N'
fi
date -u +"$DATE_FORMAT"
}
###
### version_next <version> [index]
### Returns next version of version.
### If index is not given, this function adding one to last number.
### Index, starting from 0, indicate the part of version you want to change.
###
### For example:
### "version_next 4.5.0" returns 4.5.1
### "version_next 4.5.0 1" returns 4.6.0
###
### Version delimiter are: . _ -
version_next(){
local thisVersion=$1
local index=${2-}
local versionArray=( ${thisVersion//[._-]/ } )
local delimiterArray=( ${thisVersion//[0-9a-zA-Z]/ } )
local lastIndex=${#versionArray[@]}-1
local result=
if [[ -z $index ]] ;then
index=lastIndex
fi
let versionArray[$index]=${versionArray[$index]}+1
for((i=0;i<lastIndex;i++)); do
result+=${versionArray[$i]}${delimiterArray[$i]}
done
result+=${versionArray[$lastIndex]}
echo "$result"
}
## Find maven command
if [[ -x ./mvnw ]]; then
export MvnCmd=$PWD/mvnw
else
set +e
MvnCmd=$(which mvn 2>/dev/null)
set -e
fi
## Find zanata-env.sh
case $(basename $0) in
bash )
## Remote Mode assumed
export ZANATA_REMOTE_PROGRAM=${ZANATA_REMOTE_PROGRAM:=zanata-functions}
: ${ZANATA_SCRIPTS_BRANCH:=master}
;;
* )
;;
esac
if [[ -n ${ZANATA_REMOTE_PROGRAM:-} ]]; then
## We keep a copy in local so help works
mkdir -p "${TMP_ROOT:=/tmp/zanata}"
ZanataScriptsDir=$TMP_ROOT
## Download zanata-env and zanata-functions so help works
ZanataEnvSh="$ZanataScriptsDir/zanata-env.sh"
curl -o "$ZanataEnvSh" -q https://raw.githubusercontent.com/zanata/zanata-scripts/$ZANATA_SCRIPTS_BRANCH/zanata-env.sh
source "$ZanataEnvSh"
ProgramName="$ZANATA_REMOTE_PROGRAM"
ProgramFullFilename=$ZanataScriptsDir/$ProgramName
if [[ $ProgramName = 'zanata-functions' ]]; then
curl -o $ProgramFullFilename -q https://raw.githubusercontent.com/zanata/zanata-scripts/$ZANATA_SCRIPTS_BRANCH/zanata-functions
fi
else
ZanataScriptsDir=$(dirname $(realpath ${BASH_SOURCE[0]}))
ZanataEnvSh="$ZanataScriptsDir/zanata-env.sh"
source "$ZanataEnvSh"
ProgramFullFilename=${BASH_SOURCE[$((${#BASH_SOURCE[@]}-1))]}
ProgramName=$(basename $ProgramFullFilename)
fi
case $ProgramName in
*-functions )
trap exit_print_error EXIT
case ${1-} in
help )
zanata_script_help $ProgramFullFilename
exit $EXIT_OK
;;
help-section )
zanata_help_section $ProgramFullFilename $2
exit $EXIT_OK
;;
help-list-sections )
zanata_help_list_sections $ProgramFullFilename
exit $EXIT_OK
;;
run )
shift
$@
exit $?
;;
* )
failed $EXIT_FATAL_INVALID_OPTIONS "Invalid sub-command $1"
;;
esac
zanata_script_help_short $ProgramFullFilename
;;
esac
# vim: set expandtab ts=4 sw=4 si: