-
Notifications
You must be signed in to change notification settings - Fork 16
/
melting-pot.sh
executable file
·965 lines (888 loc) · 32.9 KB
/
melting-pot.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
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
#!/bin/sh
# ============================================================================
# melting-pot.sh
# ============================================================================
# Tests all components of a project affected by changes in its dependencies.
#
# In particular, this script detects problems caused by diamond dependency
# structures:
#
# https://jlbp.dev/what-is-a-diamond-dependency-conflict
#
# This "melting pot" build rebuilds every dependency of a project, but at
# unified dependency versions matching those of the toplevel project.
#
# For example, net.imagej:imagej:2.5.0 depends on many components including
# org.scijava:scijava-common:2.88.1 and net.imagej:imagej-ops:0.46.1,
# both of which depend on org.scijava:parsington. But:
#
# - org.scijava:scijava-common:2.88.1 depends on org.scijava:parsington:3.0.0
# - net.imagej:imagej-ops:0.46.1 depends on org.scijava:parsington:2.0.0
#
# ImageJ2 can only depend on one of these versions at runtime. The newer one,
# ideally. SciJava projects use the pom-scijava parent POM as a Bill of
# Materials (BOM) to declare these winning versions, which works great...
# EXCEPT for when newer versions break backwards compatibility, as happened
# here: it's a SemVer-versioned project at different major version numbers.
#
# Enter this melting-pot script. It rebuilds each project dependency from
# source and runs the unit tests, but with all dependency versions pinned to
# match those of the toplevel project.
#
# So in the example above, this script:
#
# 1. gathers the dependencies of net.imagej:imagej:2.5.0;
# 2. clones each dependency from SCM at the correct release tag;
# 3. rebuilds each dependency, but with dependency versions overridden to
# those of net.imagej:imagej:2.5.0 rather than those originally used for
# that dependency at that release.
#
# So e.g. in the above scenario, net.imagej:imagej-ops:0.46.1 will be rebuilt
# against org.scijava:parsington:3.0.0, and we will discover whether any of
# parsington's breaking API changes from 2.0.0 to 3.0.0 actually impact the
# compilation or (tested) runtime behavior of imagej-ops.
#
# IMPORTANT IMPLEMENTATION DETAIL! The override works by setting a version
# property for each component of the form "artifactId.version"; it is assumed
# that all components declare their dependencies using version properties of
# this form. E.g.:
#
# <dependency>
# <groupId>com.google.guava</groupId>
# <artifactId>guava</artifactId>
# <version>${guava.version}</version>
# </dependency>
#
# Using dependencyManagement is fine too, as long as it then uses this pattern
# to declare the versions as properties, which can be overridden.
#
# Any dependency which does not declare a version property matching this
# assumption will not be properly overridden in the melting pot!
#
# Author: Curtis Rueden
# ============================================================================
# -- Constants --
meltingPotCache="$HOME/.cache/scijava/melting-pot"
# -- Functions --
stderr() { >&2 printf "$@\n"; }
debug() { test "$debug" && stderr "+ $@"; }
info() { test "$verbose" && stderr "\e[0;37m[INFO] $@\e[0m"; }
warn() { stderr "\e[0;33m[WARNING] $@\e[0m"; }
error() { stderr "\e[0;31m[ERROR] $@\e[0m"; }
die() { error $1; exit $2; }
unknownArg() { error "Unknown option: $@"; usage=1; }
checkPrereqs() {
while [ $# -gt 0 ]
do
which $1 > /dev/null 2> /dev/null
test $? -ne 0 && die "Missing prerequisite: $1" 255
shift
done
}
verifyPrereqs() {
checkPrereqs git mvn xmllint
git --version | grep -q 'git version 2' ||
die "Please use git v2.x; older versions (<=1.7.9.5 at least) mishandle 'git clone <tag> --depth 1'" 254
}
parseArguments() {
while [ $# -gt 0 ]
do
case "$1" in
-b|--branch)
branch="$2"
shift
;;
-c|--changes)
test "$changes" && changes="$changes,$2" || changes="$2"
shift
;;
-i|--includes)
test "$includes" && includes="$includes,$2" || includes="$2"
shift
;;
-e|--excludes)
test "$excludes" && excludes="$excludes,$2" || excludes="$2"
shift
;;
-r|--remoteRepos)
test "$remoteRepos" && remoteRepos="$remoteRepos,$2" || remoteRepos="$2"
shift
;;
-l|--localRepo)
repoBase="$2"
shift
;;
-o|--outputDir)
outputDir="$2"
shift
;;
-p|--prune)
prune=1
;;
-v|--verbose)
verbose=1
;;
-d|--debug)
debug=1
;;
-f|--force)
force=1
;;
-s|--skipBuild)
skipBuild=1
;;
-h|--help)
usage=1
;;
-*)
unknownArg "$1"
;;
*)
test -z "$project" && project="$1" ||
unknownArg "$1"
;;
esac
shift
done
test -z "$project" -a -z "$usage" &&
error "No project specified!" && usage=1
if [ "$usage" ]
then
echo "Usage: $(basename "$0") <project> [-b <branch>] [-c <GAVs>] \\
[-i <GAs>] [-e <GAs>] [-r <URLs>] [-l <dir>] [-o <dir>] [-pvfsh]
<project>
The project to build, including dependencies, with consistent versions.
Can be either G:A:V form, or a local directory pointing at a project.
-b, --branch
Override the branch/tag of the project to build. By default,
the branch used will be the tag named \"artifactId-version\".
-c, --changes
Comma-separated list of GAVs to inject into the project, replacing
normal versions. E.g.: \"com.mycompany:myartifact:1.2.3-SNAPSHOT\"
-i, --includes
Comma-separated list of GAs (no version; wildcards OK for G or A) to
include in the build. All by default. E.g.: \"mystuff:*,myotherstuff:*\"
-e, --excludes
Comma-separated list of GAs (no version; wildcards OK for G or A) to
exclude from the build. E.g.: \"mystuff:extraneous,mystuff:irrelevant\"
-r, --remoteRepos
Comma-separated list of additional remote Maven repositories to check
for artifacts, in the format id::[layout]::url or just url.
-l, --localRepos
Overrides the directory of the Maven local repository cache.
-o, --outputDir
Overrides the output directory. The default is \"melting-pot\".
-p, --prune
Build only the components which themselves depend on a changed
artifact. This will make the build much faster, at the expense of
not fully testing runtime compatibility across all components.
-v, --verbose
Enable verbose/debugging output.
-f, --force
Wipe out the output directory if it already exists.
-s, --skipBuild
Skips the final build step. Useful for automated testing.
-h, --help
Display this usage information.
--== Example ==--
sh melting-pot.sh net.imagej:imagej-common:0.15.1 \\
-r https://maven.scijava.org/content/groups/public \\
-c org.scijava:scijava-common:2.44.2 \\
-i 'org.scijava:*,net.imagej:*,net.imglib2:*,io.scif:*' \\
-e net.imglib2:imglib2-roi \\
-v -f -s
This command tests net.imagej:imagej-common:0.15.1 along with all of its
dependencies, pulled from its usual SciJava Maven repository location.
The -c flag is used to override the org.scijava:scijava-common
dependency to use version 2.44.2 instead of its declared version 2.42.0.
Note that such overrides do not need to be release versions; you can
also test SNAPSHOTs the same way.
The -i option is used to include all imagej-common dependencies with
groupIds org.scijava, net.imagej, net.imglib2 and io.scif in the pot.
The -e flag is used to exclude net.imglib2:imglib2-roi from the pot.
"
exit 1
fi
# If project is a local directory path, get its absolute path.
test -d "$project" && project=$(cd "$project" && pwd)
# Assign default parameter values.
test "$outputDir" || outputDir="melting-pot"
test "$repoBase" || repoBase="$HOME/.m2/repository"
}
createDir() {
test -z "$force" -a -e "$1" &&
die "Directory already exists: $1" 2
rm -rf "$1"
mkdir -p "$1"
cd "$1"
}
groupId() {
echo "${1%%:*}"
}
artifactId() {
local result="${1#*:}" # strip groupId
echo "${result%%:*}"
}
version() {
local result="${1#*:}" # strip groupId
case "$result" in
*:*)
result="${result#*:}" # strip artifactId
case "$result" in
*:*:*:*)
# G:A:P:C:V:S
result="${result#*:}" # strip packaging
result="${result#*:}" # strip classifier
;;
*:*:*)
# G:A:P:V:S
result="${result#*:}" # strip packaging
;;
*)
# G:A:V or G:A:V:?
;;
esac
echo "${result%%:*}"
;;
esac
}
classifier() {
local result="${1#*:}" # strip groupId
case "$result" in
*:*)
result="${result#*:}" # strip artifactId
case "$result" in
*:*:*:*)
# G:A:P:C:V:S
result="${result#*:}" # strip packaging
;;
*:*:*)
# G:A:P:V:S
result=""
;;
*:*)
# G:A:V:C
result="${result#*:}" # strip version
;;
*)
# G:A:V
result=""
;;
esac
echo "${result%%:*}"
;;
esac
}
# Converts the given GAV into a path in the local repository cache.
repoPath() {
local gPath="$(echo "$(groupId "$1")" | tr :. /)"
local aPath="$(artifactId "$1")"
local vPath="$(version "$1")"
echo "$repoBase/$gPath/$aPath/$vPath"
}
# Gets the path to the given GAV's POM file in the local repository cache.
pomPath() {
local pomFile="$(artifactId "$1")-$(version "$1").pom"
echo "$(repoPath "$1")/$pomFile"
}
# Fetches the POM for the given GAV into the local repository cache.
downloadPOM() {
local g="$(groupId "$1")"
local a="$(artifactId "$1")"
local v="$(version "$1")"
debug "mvn dependency:get \\
-DremoteRepositories=\"$remoteRepos\" \\
-DgroupId=\"$g\" \\
-DartifactId=\"$a\" \\
-Dversion=\"$v\" \\
-Dpackaging=pom"
mvn dependency:get \
-DremoteRepositories="$remoteRepos" \
-DgroupId="$g" \
-DartifactId="$a" \
-Dversion="$v" \
-Dpackaging=pom > /dev/null ||
die "Problem fetching $g:$a:$v from $remoteRepos" 4
}
# Gets the POM path for the given GAV, ensuring it exists locally.
pom() {
local pomPath="$(pomPath "$1")"
test -f "$pomPath" || downloadPOM "$1"
test -f "$pomPath" || die "Cannot access POM: $pomPath" 9
echo "$pomPath"
}
# For the given XML file on disk ($1), gets the value of the
# specified XPath expression of the form "//$2/$3/$4/...".
xpath() {
local xmlFile="$1"
shift
local expression="$@"
local xpath="/"
while [ $# -gt 0 ]
do
# NB: Ignore namespace issues; see: http://stackoverflow.com/a/8266075
xpath="$xpath/*[local-name()='$1']"
shift
done
local value=$(xmllint --xpath "$xpath" "$xmlFile" 2> /dev/null |
sed -E 's/^[^>]*>(.*)<[^<]*$/\1/')
debug "xpath $xmlFile $expression -> $value"
echo "$value"
}
# For the given GAV ($1), recursively gets the value of the
# specified XPath expression of the form "//$2/$3/$4/...".
pomValue() {
local pomPath="$(pom "$1")"
test "$pomPath" || die "Cannot discern POM path for $1" 6
shift
local value="$(xpath "$pomPath" $@)"
if [ "$value" ]
then
echo "$value"
else
# Path not found in POM; look in the parent POM.
local pg="$(xpath "$pomPath" project parent groupId)"
if [ "$pg" ]
then
# There is a parent POM declaration in this POM.
local pa="$(xpath "$pomPath" project parent artifactId)"
local pv="$(xpath "$pomPath" project parent version)"
pomValue "$pg:$pa:$pv" $@
fi
fi
}
# Gets the SCM URL for the given GAV.
scmURL() {
pomValue "$1" project scm connection | sed 's/^scm:git://' |
sed 's_git:\(//github.com/\)_https:\1_'
}
# Gets the SCM tag for the given GAV.
scmTag() {
local tag=$(pomValue "$1" project scm tag)
if [ -z "$tag" -o "$tag" = "HEAD" ]
then
# The <scm><tag> value was not set properly,
# so we try to guess the tag naming scheme. :-/
warn "$1: improper scm tag value; scanning remote tags..."
local a=$(artifactId "$1")
local v=$(version "$1")
local scmURL="$(scmURL "$1")"
# TODO: Avoid network use. We can scan the locally cached repo.
# But this gets complicated when the locally cached repo is
# out of date, and the needed tag is not there yet...
debug "git ls-remote --tags \"$scmURL\" | sed 's/.*refs\/tags\///'"
local allTags="$(git ls-remote --tags "$scmURL" | sed 's/.*refs\/tags\///' ||
error "$1: Invalid scm url: $scmURL")"
for tag in "$a-$v" "$v" "v$v"
do
echo "$allTags" | grep -q "^$tag$" && {
info "$1: inferred tag: $tag"
echo "$tag"
return
}
done
error "$1: inscrutable tag scheme -- using default branch"
else
echo "$tag"
fi
}
# Ensures the source code for the given GAV exists in the melting-pot
# structure, and is up-to-date with the remote. Returns the directory.
resolveSource() {
local g=$(groupId "$1")
local a=$(artifactId "$1")
local cachedRepoDir="$meltingPotCache/$g/$a"
if [ ! -d "$cachedRepoDir" ]
then
# Source does not exist locally. Clone it into the melting pot cache.
local scmURL="$(scmURL "$1")"
test "$scmURL" || die "$1: cannot glean SCM URL" 10
info "$1: cached repository not found; cloning from remote: $scmURL"
debug "git clone --bare \"$scmURL\" \"$cachedRepoDir\""
git clone --bare "$scmURL" "$cachedRepoDir" 2> /dev/null ||
die "$1: could not clone project source from $scmURL" 3
fi
# Check whether the needed branch/tag exists.
local scmBranch
test "$2" && scmBranch="$2" || scmBranch="$(scmTag "$1")"
if [ "$scmBranch" ]
then
# Successfully gleaned SCM branch/tag.
debug "git ls-remote \"file://$cachedRepoDir\" | grep -q \"\brefs/tags/$scmBranch$\""
git ls-remote "file://$cachedRepoDir" | grep -q "\brefs/tags/$scmBranch$" || {
# Couldn't find the scmBranch as a tag in the cached repo. Either the
# tag is new, or it's not a tag ref at all (e.g. it's a branch).
# So let's update from the original remote repository.
info "$1: local tag not found for ref '$scmBranch'"
info "$1: updating cached repository: $cachedRepoDir"
cd "$cachedRepoDir"
debug "git fetch --tags"
if [ "$debug" ]
then
git fetch --tags
else
git fetch --tags > /dev/null
fi
cd - > /dev/null
}
else
# No SCM branch/tag; fall back to the default branch.
info "$1: updating cached repository: $cachedRepoDir"
cd "$cachedRepoDir"
debug "git fetch"
if [ "$debug" ]
then
git fetch
else
git fetch > /dev/null
fi
head=$(cat HEAD)
scmBranch=${head##*/}
info "$1: detected default branch as $scmBranch"
cd - > /dev/null
fi
# Shallow clone the source at the given version into melting-pot structure.
local destDir="$g/$a"
debug "git clone \"file://$cachedRepoDir\" --branch \"$scmBranch\" --depth 1 \"$destDir\""
git clone "file://$cachedRepoDir" --branch "$scmBranch" --depth 1 "$destDir" 2> /dev/null ||
die "$1: could not clone branch '$scmBranch' from local cache" 15
# Save the GAV string to a file, for convenience.
echo "$1" > "$destDir/gav"
# Now verify that the cloned pom.xml contains the expected version!
local expectedVersion=$(version "$1")
local actualVersion=$(xpath "$destDir/pom.xml" project version)
test "$expectedVersion" = "$actualVersion" ||
warn "$1: POM contains wrong version: $actualVersion"
echo "$destDir"
}
# Gets the list of dependencies for the project in the CWD.
deps() {
cd "$1" || die "No such directory: $1" 16
debug "mvn -B -DincludeScope=runtime dependency:list"
local depList="$(mvn -B -DincludeScope=runtime dependency:list)" ||
die "Problem fetching dependencies!" 5
echo "$depList" | grep '^\[INFO\] \w' |
sed 's/\[INFO\] //' | sed 's/ .*//' | sort
cd - > /dev/null
}
# Checks whether the given GA(V) matches the specified filter pattern.
gaMatch() {
local ga="$1"
local filter="$2"
local g="$(groupId "$ga")"
local a="$(artifactId "$ga")"
local fg="$(groupId "$filter")"
local fa="$(artifactId "$filter")"
test "$fg" = "$g" -o "$fg" = "*" || return
test "$fa" = "$a" -o "$fa" = "*" || return
echo 1
}
# Determines whether the given GA(V) version is being overridden.
isChanged() {
local IFS=","
local change
for change in $changes
do
test "$(gaMatch "$1" "$change")" && echo 1 && return
done
}
# Determines whether the given GA(V) meets the inclusion criteria.
isIncluded() {
# do not include the changed artifacts we are testing against
test "$(isChanged "$1")" && return
local IFS=","
# ensure GA is not excluded
local exclude
for exclude in $excludes
do
test "$(gaMatch "$1" "$exclude")" && return
done
# ensure GA is included
test -z "$includes" && echo 1 && return
local include
for include in $includes
do
test "$(gaMatch "$1" "$include")" && echo 1 && return
done
}
# Deletes components which do not depend on a changed GAV.
pruneReactor() {
local dir
for dir in */*
do
info "Checking relevance of component $dir"
local deps="$(deps "$dir")"
test "$deps" || die "Cannot glean dependencies for '$dir'" 8
# Determine whether the component depends on a changed GAV.
local keep
unset keep
local dep
for dep in $deps
do
test "$(isChanged "$dep")" && keep=1 && break
done
# If the component is irrelevant, prune it.
if [ -z "$keep" ]
then
info "Pruning irrelevant component: $dir"
rm -rf "$dir"
fi
done
}
# Tests if the given directory contains the appropriate source code.
isProject() {
local a="$(xpath "$1/pom.xml" project artifactId)"
test "$1" = "LOCAL/PROJECT" -o "$a" = "$(basename "$1")" && echo 1
}
# Generates melt.sh, covering all projects in the current directory.
generateMeltScript() {
echo '#!/bin/sh' > melt.sh
echo 'trap "exit" INT' >> melt.sh
echo 'echo "Melting the pot..."' >> melt.sh
echo 'dir=$(cd "$(dirname "$0")" && pwd)' >> melt.sh
echo 'failCount=0' >> melt.sh
echo 'for f in \' >> melt.sh
local dir
for dir in */*
do
if [ "$(isProject "$dir")" ]
then
echo " $dir \\" >> melt.sh
else
# Check for a child component of a multi-module project.
local childDir="$dir/$(basename "$dir")"
test "$(isProject "$childDir")" &&
echo " $childDir \\" >> melt.sh
fi
done
echo >> melt.sh
echo 'do' >> melt.sh
echo ' if [ "$("$dir/prior-success.sh" "$f")" ]' >> melt.sh
echo ' then' >> melt.sh
echo ' printf "\e[0;36m[SKIPPED] $f (prior success)\e[0m\n"' >> melt.sh
echo ' continue' >> melt.sh
echo ' fi' >> melt.sh
echo ' cd "$f"' >> melt.sh
echo ' "$dir/build.sh" >build.log 2>&1 && {' >> melt.sh
echo ' printf "\e[0;32m[SUCCESS] $f\e[0m\n"' >> melt.sh
echo ' "$dir/record-success.sh" "$f"' >> melt.sh
echo ' } || {' >> melt.sh
echo ' printf "\e[0;31m[FAILURE] $f\e[0m\n"' >> melt.sh
echo ' failCount=$((failCount+1))' >> melt.sh
echo ' }' >> melt.sh
echo ' cd - >/dev/null' >> melt.sh
echo 'done' >> melt.sh
echo 'test "$failCount" -gt 255 && failCount=255' >> melt.sh
echo 'exit "$failCount"' >> melt.sh
chmod +x melt.sh
}
# Generates helper scripts, including prior-success.sh and record-success.sh.
generateHelperScripts() {
cat <<\PRIOR > prior-success.sh
#!/bin/sh
test "$1" || {
printf "\e[0;31m[ERROR] Please specify project to check.\e[0m\n"
exit 1
}
stderr() { >&2 printf "$@\n"; }
debug() { test "$DEBUG" && stderr "\e[0;37m[DEBUG] $@\e[0m"; }
info() { stderr "\e[0;37m[INFO] $@\e[0m"; }
warn() { stderr "\e[0;33m[WARNING] $@\e[0m"; }
dir=$(cd "$(dirname "$0")" && pwd)
# Check build.log for BUILD SUCCESS.
buildLog="$dir/$1/build.log"
test -f "$buildLog" && tail -n6 "$buildLog" | grep -qF 'BUILD SUCCESS' && {
echo "build.log"
exit 0
}
# Check success.log for matching dependency configuration.
successLog="$HOME/.cache/scijava/melting-pot/$1.success.log"
test -f "$successLog" || exit 0
row=1
mismatch1=
success=
for deps in $(cat "$successLog")
do
debug "$1: Checking dep config: $deps"
mismatch=
for dep in $(echo "$deps" | tr ',' '\n')
do
# g:a:p:v:s -> <g.a.version>v</g.a.version>
s=${dep##*:}
case "$s" in
test) continue ;; # skip test dependencies
none) continue ;; # empty dependency config
esac
gapv=${dep%:*}
g=${gapv%%:*}
apv=${gapv#*:}
a=${apv%%:*}
v=${apv##*:}
bomV=$(grep -o " <$g\.$a\.version>[^>]*" "$dir/version-pins.xml" | sed 's;[^>]*>\([^>]*\)<.*;\1;')
if [ "$bomV" != "${bomV#*-SNAPSHOT*}" ]
then
warn "$1: Snapshot dependency pin detected: $g:$a:$bomV -- forcing a rebuild"
exit 0
elif [ "$bomV" != "$v" ]
then
# G:A property is not set to this V.
# Now check if the property is even declared.
if [ "$bomV" ]
then
# G:A version is mismatched.
mismatch="$mismatch\n* $g:$a:$v -> $bomV"
else
# G:A version is not pinned.
test "$row" -ne 1 ||
warn "$1: Unpinned dependency: $dep"
fi
fi
done
if [ "$mismatch" ]
then
test "$row" -eq 1 && mismatch1=$mismatch ||
debug "$1: Dependency changes since success #$row:$mismatch"
else
success=$deps
break
fi
row=$((row+1))
done
test "$success" && echo "$success" || {
test "$mismatch1" &&
info "$1: Dependency changes since last success:$mismatch1" ||
info "$1: No prior successes"
}
PRIOR
chmod +x prior-success.sh
cat <<\RECORD > record-success.sh
#!/bin/sh
test "$1" || {
printf "\e[0;31m[ERROR] Please specify project to update.\e[0m\n"
exit 1
}
containsLine() {
pattern=$1
file=$2
test -f "$file" || return
# HACK: The obvious way to do this is:
#
# grep -qxF "$pattern" "$file"
#
# Unfortunately, BSD grep dies with "out of memory" when the pattern is 5111
# characters or longer. So let's do something needlessly complex instead!
cat "$file" | while read line
do
test "$pattern" = "$line" && echo 1 && break
done
}
dir=$(cd "$(dirname "$0")" && pwd)
buildLog="$dir/$1/build.log"
test -f "$buildLog" || exit 1
successLog="$HOME/.cache/scijava/melting-pot/$1.success.log"
mkdir -p "$(dirname "$successLog")"
# Record dependency configuration of successful build.
deps=$(grep '^\[[^ ]*INFO[^ ]*\] \w' "$buildLog" |
sed -e 's/^[^ ]* *//' -e 's/ -- .*//' -e 's/ (\([^)]*\))/-\1/' |
sort | tr '\n' ',')
if [ "$deps" = "${deps%*-SNAPSHOT*}" -a -z "$(containsLine "$deps" "$successLog")" ]
then
# NB: *Prepend*, rather than append, the new successful configuration.
# We do this because it is more likely this new configuration will be
# encountered again in the future, as dependency versions are highly
# likely to repeatedly increment, rather than moving backwards.
echo "$deps" > "$successLog".new
test -f "$successLog" && cat "$successLog" >> "$successLog".new
mv -f "$successLog".new "$successLog"
fi
RECORD
chmod +x record-success.sh
}
# Creates and tests an appropriate multi-module reactor for the given project.
# All relevant dependencies which match the inclusion criteria are linked into
# the multi-module build, with each changed GAV overridding the originally
# specified version for the corresponding GA.
meltDown() {
# Fetch the project source code.
if [ -d "$1" ]
then
# Use local directory for the specified project.
test -d "$1" || die "No such directory: $1" 11
test -f "$1/pom.xml" || die "Not a Maven project: $1" 12
case "$(uname)" in
MINGW*)
warn "Skipping inclusion of local project due to lack of symlink support."
local projectDir="$1"
;;
*)
info "Local Maven project: $1"
mkdir -p "LOCAL"
local projectDir="LOCAL/PROJECT"
ln -s "$1" "$projectDir"
;;
esac
else
# Treat specified project as a GAV.
info "Fetching project source"
local projectDir=$(resolveSource "$1" "$branch")
test $? -eq 0 || exit $?
fi
# Get the project dependencies.
info "Determining project dependencies"
local deps="$(deps "$projectDir")"
test "$deps" || die "Cannot glean project dependencies" 7
# Generate helper scripts. We need prior-success.sh
# to decide whether to include each component.
generateHelperScripts
# Process the dependencies.
info "Processing project dependencies"
local versionProps=""
local dep
for dep in $deps
do
local g="$(groupId "$dep")"
local a="$(artifactId "$dep")"
local aa
echo "$a" | grep -q '^[0-9]' && aa="_$a" || aa="$a"
local v="$(version "$dep")"
local c="$(classifier "$dep")"
test -z "$c" || continue # skip secondary artifacts
local gav="$g:$a:$v"
test -z "$(isChanged "$gav")" &&
versionProps="$versionProps
<$g.$a.version>$v</$g.$a.version> <$aa.version>$v</$aa.version>"
done
# Override versions of changed GAVs.
info "Processing changed components"
local TLS=,
local gav
for gav in $changes
do
local a="$(artifactId "$gav")"
local v="$(version "$gav")"
versionProps="$versionProps
<$a.version>$v</$a.version>"
done
unset TLS
# Generate version-pins.xml.
info "Generating version-pins.xml configuration"
echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' > version-pins.xml
echo ' xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 https://maven.apache.org/xsd/settings-1.1.0.xsd">' >> version-pins.xml
echo ' <profiles>' >> version-pins.xml
echo ' <profile>' >> version-pins.xml
echo ' <id>version-pins</id>' >> version-pins.xml
echo ' <activation>' >> version-pins.xml
echo ' <activeByDefault>true</activeByDefault>' >> version-pins.xml
echo ' </activation>' >> version-pins.xml
echo ' <properties>' >> version-pins.xml
echo "$versionProps" >> version-pins.xml
echo ' </properties>' >> version-pins.xml
echo ' </profile>' >> version-pins.xml
echo ' </profiles>' >> version-pins.xml
echo '</settings>' >> version-pins.xml
# Generate build script.
info "Generating build.sh script"
echo '#!/bin/sh' > build.sh
echo >> build.sh
echo 'dir=$(cd "$(dirname "$0")" && pwd)' >> build.sh
echo >> build.sh
echo 'mvnPin() {' >> build.sh
# NB: We do *not* include -B here, because we want build.sh to preserve
# colored output if the version of Maven is new enough. We will take care
# elsewhere when parsing it to be flexible about whether colors are present.
echo ' mvn -s "$dir/version-pins.xml" -Denforcer.skip $@' >> build.sh
echo '}' >> build.sh
echo >> build.sh
echo 'unpackArtifact() {' >> build.sh
echo ' # Download and unpack the given artifact' >> build.sh
echo ' # (G:A:V) to the specified location.' >> build.sh
echo ' gav=$1' >> build.sh
echo ' out=$2' >> build.sh
echo >> build.sh
echo ' repoPrefix=$HOME/.m2/repository # TODO: generalize this' >> build.sh
echo ' g=${gav%%:*}; r=${gav#*:}; a=${r%%:*}; v=${r##*:}' >> build.sh
echo ' gavPath="$(echo "$g" | tr "." "/")/$a/$v/$a-$v"' >> build.sh
echo ' jarPath="$repoPrefix/$gavPath.jar"' >> build.sh
echo >> build.sh
echo ' # HACK: The best goal to use would be dependency:unpack,' >> build.sh
echo ' # or failing that, dependency:copy followed by jar xf.' >> build.sh
echo ' # But those goals do not support remoteRepositories;' >> build.sh
echo ' # see https://issues.apache.org/jira/browse/MDEP-390.' >> build.sh
echo ' # So we use dependency:get and then extract it by hand.' >> build.sh
echo ' mvnPin dependency:get \' >> build.sh
echo " -DremoteRepositories=\"$remoteRepos\" \\" >> build.sh
echo ' -Dartifact="$gav" &&' >> build.sh
echo >> build.sh
echo ' test -f "$jarPath" &&' >> build.sh
echo ' mkdir -p "$out" &&' >> build.sh
echo ' cd "$out" &&' >> build.sh
echo ' jar xf "$jarPath" &&' >> build.sh
echo ' cd - >/dev/null' >> build.sh
echo '}' >> build.sh
echo >> build.sh
echo 'mvnPin dependency:list dependency:tree &&' >> build.sh
echo >> build.sh
echo 'if [ -f gav ]' >> build.sh
echo 'then' >> build.sh
echo ' echo' >> build.sh
echo ' echo "================================================"' >> build.sh
echo ' echo "========= Testing with deployed binary ========="' >> build.sh
echo ' echo "================================================"' >> build.sh
echo ' unpackArtifact "$(cat gav)" target/classes &&' >> build.sh
echo ' mvnPin \' >> build.sh
echo ' -Dmaven.main.skip=true \' >> build.sh
echo ' -Dmaven.resources.skip=true \' >> build.sh
echo ' test $@' >> build.sh
echo 'fi &&' >> build.sh
echo >> build.sh
echo 'echo &&' >> build.sh
echo 'echo "================================================" &&' >> build.sh
echo 'echo "============ Rebuilding from source ============" &&' >> build.sh
echo 'echo "================================================" &&' >> build.sh
echo 'mvnPin clean test $@' >> build.sh
chmod +x build.sh
# Clone source code.
info "Cloning source code"
for dep in $deps
do
local g="$(groupId "$dep")"
local a="$(artifactId "$dep")"
local v="$(version "$dep")"
local c="$(classifier "$dep")"
test -z "$c" || continue # skip secondary artifacts
local gav="$g:$a:$v"
if [ "$(isIncluded "$gav")" ]
then
if [ "$v" != "${v%-SNAPSHOT}" ]
then
info "$g:$a: forcing inclusion due to SNAPSHOT version"
elif [ "$(./prior-success.sh "$g/$a")" ]
then
info "$g:$a: skipping version $v due to prior successful build"
continue
fi
info "$g:$a: resolving source for version $v"
resolveSource "$gav" >/dev/null
fi
done
# Prune the build, if applicable.
test "$prune" && pruneReactor
# Generate melt script.
info "Generating melt.sh script"
generateMeltScript
# Build everything.
if [ "$skipBuild" ]
then
info "Skipping the build; run melt.sh to do it."
else
info "Building the project!"
# NB: All code is fresh; no need to clean.
sh melt.sh || die "Melt failed" 13
fi
info "Melt complete: $1"
}
# -- Main --
verifyPrereqs
parseArguments $@
createDir "$outputDir"
meltDown "$project"