@@ -15,62 +15,83 @@ class WitnessPluginExtension {
15
15
class WitnessPlugin implements Plugin<Project > {
16
16
17
17
static String calculateSha256 (file ) {
18
- MessageDigest md = MessageDigest . getInstance(" SHA-256" );
19
- file. eachByte 4096 , {bytes , size ->
20
- md. update(bytes, 0 , size);
18
+ MessageDigest md = MessageDigest . getInstance(' SHA-256' )
19
+ file. eachByte 4096 , { bytes , size ->
20
+ md. update(bytes, 0 , size)
21
21
}
22
- return md. digest(). collect {String . format " %02x" , it}. join();
22
+ return md. digest(). collect { String . format ' %02x' , it }. join()
23
23
}
24
24
25
25
void apply (Project project ) {
26
- project. extensions. create(" dependencyVerification" , WitnessPluginExtension )
26
+ project. extensions. create(' dependencyVerification' , WitnessPluginExtension )
27
+
27
28
project. afterEvaluate {
28
- project. dependencyVerification. verify. each {
29
- assertion ->
30
- List parts = assertion[0 ]. tokenize(' :' )
31
- String group = parts. get(0 )
32
- String name = parts. get(1 )
33
- String hash = assertion[1 ]
34
-
35
- def artifacts = allArtifacts(project). findAll {
36
- return it. name. equals(name) && it. moduleVersion. id. group. equals(group)
29
+ project. tasks
30
+ .findAll { it. name =~ / compile/ }
31
+ .each {
32
+ it. dependsOn(' verifyChecksums' )
37
33
}
34
+ }
35
+
36
+ project. task(' verifyChecksums' ) {
37
+ group = ' Gradle Witness'
38
+ description = ' Verify the contents of dependencyVerification block in witness-verifications.gradle file(s) match the checksums of dependencies.'
38
39
39
- artifacts . forEach { dependency ->
40
- println " Verifying " + group + " : " + name
40
+ doLast {
41
+ def allArtifacts = allArtifacts(project)
41
42
42
- if (dependency == null ) {
43
- throw new InvalidUserDataException (" No dependency for integrity assertion found: " + group + " :" + name)
43
+ project. dependencyVerification. verify. each {
44
+ assertion ->
45
+ List parts = assertion[0 ]. tokenize(' :' )
46
+ String group = parts. get(0 )
47
+ String name = parts. get(1 )
48
+ String hash = assertion[1 ]
49
+
50
+ def artifacts = allArtifacts. findAll {
51
+ it. moduleVersion. id. group == group && it. name == name
44
52
}
45
53
46
- if (! hash. equals(calculateSha256(dependency. file))) {
47
- throw new InvalidUserDataException (" Checksum failed for " + assertion)
54
+ artifacts. forEach { dependency ->
55
+ println " Verifying $group :$name "
56
+
57
+ if (dependency == null ) {
58
+ throw new InvalidUserDataException (" No dependency for integrity assertion found: $group :$name " )
59
+ }
60
+
61
+ if (hash != calculateSha256(dependency. file)) {
62
+ throw new InvalidUserDataException (" Checksum failed for $assertion " )
63
+ }
48
64
}
49
- }
65
+ }
50
66
}
51
67
}
52
68
53
- project. task(' calculateChecksums' ). doLast {
54
- def stringBuilder = new StringBuilder ()
69
+ project. task(' calculateChecksums' ) {
70
+ group = ' Gradle Witness'
71
+ description = ' Recalculate checksums of dependencies and update the witness-verifications.gradle file(s).'
55
72
56
- stringBuilder . append ' // Auto-generated, use ./gradlew calculateChecksums to regenerate \n\n '
57
- stringBuilder. append ' dependencyVerification { \n '
73
+ doLast {
74
+ def stringBuilder = new StringBuilder ()
58
75
59
- stringBuilder. append ' verify = [\n '
76
+ stringBuilder. append ' // Auto-generated, use ./gradlew calculateChecksums to regenerate\n\n '
77
+ stringBuilder. append ' dependencyVerification {\n '
60
78
61
- allArtifacts(project)
62
- .findAll { dep -> ! dep. id. componentIdentifier. displayName. startsWith(' project :' ) }
63
- .collect { dep -> " ['$dep . moduleVersion . id . group :$dep . name :$dep . moduleVersion . id . version ',\n '${ calculateSha256(dep.file)} ']" }
64
- .sort()
65
- .unique()
66
- .each {
67
- dep -> stringBuilder. append " \n $dep ,\n "
68
- }
79
+ stringBuilder. append ' verify = [\n '
69
80
70
- stringBuilder. append " ]\n "
71
- stringBuilder. append " }\n "
81
+ allArtifacts(project)
82
+ .findAll { dep -> ! dep. id. componentIdentifier. displayName. startsWith(' project :' ) }
83
+ .collect { dep -> " ['$dep . moduleVersion . id . group :$dep . name :$dep . moduleVersion . id . version ',\n '${ calculateSha256(dep.file)} ']" }
84
+ .sort()
85
+ .unique()
86
+ .each {
87
+ dep -> stringBuilder. append " \n $dep ,\n "
88
+ }
89
+
90
+ stringBuilder. append ' ]\n '
91
+ stringBuilder. append ' }\n '
72
92
73
- project. file(" witness-verifications.gradle" ). write(stringBuilder. toString())
93
+ project. file(' witness-verifications.gradle' ). write(stringBuilder. toString())
94
+ }
74
95
}
75
96
}
76
97
0 commit comments