forked from ktorio/ktor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifier.gradle
203 lines (161 loc) · 6.12 KB
/
verifier.gradle
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
import groovy.io.FileType
import groovy.json.JsonSlurper
import org.xml.sax.ErrorHandler
import org.xml.sax.SAXException
import org.xml.sax.SAXParseException
import java.util.regex.Matcher
task cleanPublications(type: Delete) {
delete new File(rootProject.buildDir, 'm2')
}
def isBadTextNode(node) {
return node == null || node.size() != 1 || !node.text().trim()
}
def isEmptyTagList(node) {
return node == null || node.size() == 0
}
def verifyArtifact(File versionDir, List<File> files, String ext, String kind) {
if (files.count { it.name.toLowerCase().endsWith(ext) } == 0) {
throw new GradleException("No $ext artifact found at $versionDir (kind $kind)")
}
}
def validatePom(File file) {
def parser = new XmlSlurper(false, true, false)
def errors = []
parser.errorHandler = new ErrorHandler() {
@Override
void warning(SAXParseException exception) throws SAXException {
}
@Override
void error(SAXParseException exception) throws SAXException {
errors += exception
}
@Override
void fatalError(SAXParseException exception) throws SAXException {
errors += exception
}
}
def xml = parser.parse(file)
if (!errors.isEmpty()) {
throw errors[0]
}
if (isBadTextNode(xml.name)) {
errors += ['<name> tag is missing']
}
if (isBadTextNode(xml.description)) {
errors += ['<description> tag is missing']
}
if (isBadTextNode(xml.url)) {
errors += ['<url> tag is missing']
}
def licenses = xml.licenses.license
if (isEmptyTagList(licenses)) {
errors += 'No licenses specified'
} else if (licenses.any { isBadTextNode(it.name) || isBadTextNode(it.url) || isBadTextNode(it.distribution) }) {
errors += 'License section is incomplete'
}
def developers = xml.developers.developer
if (isEmptyTagList(developers)) {
errors += 'No developers specified'
} else if (developers.any { isBadTextNode(it.id) || isBadTextNode(it.name) || isBadTextNode(it.organization) || isBadTextNode(it.organizationUrl)}) {
errors += 'Developer section is incomplete'
}
def scm = xml.scm.url
if (isEmptyTagList(scm)) {
errors += 'No scm specified'
}
if (!errors.isEmpty()) {
throw new GradleException("Pom verification failed for $file.name: ${errors.join(', ')}")
}
}
String packaging(File moduleDir) {
def pomFile = moduleDir.listFiles()?.find { it.name.endsWith('.pom') }
if (pomFile == null || !pomFile.exists()) {
throw new GradleException("POM file is missing at $moduleDir")
}
def packaing = new XmlSlurper(false, true, false).parse(pomFile).packaging.text()
if (!packaing?.trim()) return 'jar'
return packaing
}
def lookupTasks = {
def targetNames = kotlin.targets.collect { it.name }
def tasks = []
allprojects {
tasks += targetNames.collect { project.tasks.findByName("publish${it.capitalize()}PublicationToTestLocalRepository") }
.findAll()
}
return [cleanPublications] + tasks
}
// returns jvm, js, native or some native target name
String guessModuleKind(File dir, File pomFile) {
def artifactId_ = new XmlSlurper(false, true).parse(pomFile).artifactId.text()
def kind = null
allprojects {
project.publishing.publications.findAll { !it.name.contains('-test') && it.artifactId == artifactId_ }.each {
kind = it.name
}
}
if (!kind) {
allprojects { project ->
println "project $project.name"
project.publishing.publications.each {
println "publication $it.name with ${it.artifactId} (looking for $artifactId_, found = ${it.artifactId == artifactId_})"
}
}
throw new GradleException("Not found")
}
return kind
}
task verifyPublications(dependsOn: lookupTasks) {
doLast {
def m2 = new File(rootProject.buildDir, 'm2')
def pomFiles = []
def moduleFiles = []
m2.eachFileRecurse(FileType.FILES) { child ->
if (child.name.toLowerCase() != 'maven-metadata.xml') {
def ext = child.name.split(/\./).last().toLowerCase()
switch (ext) {
case 'pom':
pomFiles += child
break
case 'module':
moduleFiles += child
break
}
}
}
pomFiles.each { File file ->
validatePom(file)
}
def groupDir = new File(m2, project.group.replaceAll(/\./, Matcher.quoteReplacement(File.separator)))
def verified = 0
groupDir.eachDir { artifactDir ->
def versionDir = new File(artifactDir, project.version)
if (versionDir.exists()) {
def files = versionDir.listFiles()?.findAll { it.isFile() } ?: []
def moduleKind = guessModuleKind(versionDir, files.find { it.name.endsWith(".pom") })
def packaging = '.' + packaging(versionDir)
verifyArtifact(versionDir, files, packaging, moduleKind)
verifyArtifact(versionDir, files, '-javadoc.jar', moduleKind)
verifyArtifact(versionDir, files, '-sources.jar', moduleKind)
if (moduleKind != 'js' && moduleKind != 'jvm' && moduleKind != 'jvmWithJava' && moduleKind != 'metadata') {
verifyArtifact(versionDir, files, '.module', moduleKind)
}
verified ++
}
}
if (verified == 0) {
throw new GradleException("No installed modules were found at $groupDir")
}
moduleFiles.each { File file ->
def moduleFile = new JsonSlurper().parse(file)
if (moduleFile.variants.name.isEmpty()) {
throw new GradleException("No variants found in module file $file")
}
}
}
}
tasks.whenTaskAdded { task ->
if (task.name.startsWith('publish') && task.name.endsWith('TestLocalRepository')) {
tasks.getByName('verifyPublications').dependsOn(task)
}
}