/
ServiceFileTransformer.groovy
229 lines (201 loc) · 6.76 KB
/
ServiceFileTransformer.groovy
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.github.jengelman.gradle.plugins.shadow.transformers
import com.github.jengelman.gradle.plugins.shadow.relocation.RelocateClassContext
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction
import org.apache.tools.zip.ZipEntry
import org.apache.tools.zip.ZipOutputStream
import org.gradle.api.file.FileTreeElement
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.api.tasks.util.PatternSet
import org.codehaus.plexus.util.IOUtil
/**
* Modified from org.apache.maven.plugins.shade.resource.ServiceResourceTransformer.java
* <p>
* Resources transformer that appends entries in META-INF/services resources into
* a single resource. For example, if there are several META-INF/services/org.apache.maven.project.ProjectBuilder
* resources spread across many JARs the individual entries will all be concatenated into a single
* META-INF/services/org.apache.maven.project.ProjectBuilder resource packaged into the resultant JAR produced
* by the shading process.
*
* @author jvanzyl
* @author Charlie Knudsen
* @author John Engelman
*/
@CacheableTransformer
class ServiceFileTransformer implements Transformer, PatternFilterable {
private static final String SERVICES_PATTERN = "META-INF/services/**"
private static final String GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATTERN =
"META-INF/services/org.codehaus.groovy.runtime.ExtensionModule"
private Map<String, ServiceStream> serviceEntries = [:].withDefault { new ServiceStream() }
private final PatternSet patternSet =
new PatternSet().include(SERVICES_PATTERN).exclude(GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATTERN)
void setPath(String path) {
patternSet.setIncludes(["${path}/**"])
}
@Override
boolean canTransformResource(FileTreeElement element) {
FileTreeElement target = element instanceof ShadowCopyAction.ArchiveFileTreeElement ? element.asFileTreeElement() : element
return patternSet.asSpec.isSatisfiedBy(target)
}
@Override
void transform(TransformerContext context) {
def lines = context.is.readLines()
def targetPath = context.path
context.relocators.each {rel ->
if (rel.canRelocateClass(new File(targetPath).name)) {
targetPath = rel.relocateClass(RelocateClassContext.builder().className(targetPath).stats(context.stats).build())
}
lines.eachWithIndex { String line, int i ->
if (rel.canRelocateClass(line)) {
def lineContext = RelocateClassContext.builder().className(line).stats(context.stats).build()
lines[i] = rel.relocateClass(lineContext)
}
}
}
lines.each {line -> serviceEntries[targetPath].append(new ByteArrayInputStream(line.getBytes()))}
}
@Override
boolean hasTransformedResource() {
return serviceEntries.size() > 0
}
@Override
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
serviceEntries.each { String path, ServiceStream stream ->
ZipEntry entry = new ZipEntry(path)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
IOUtil.copy(stream.toInputStream(), os)
os.closeEntry()
}
}
static class ServiceStream extends ByteArrayOutputStream {
ServiceStream(){
super( 1024 )
}
void append(InputStream is ) throws IOException {
if ( super.count > 0 && super.buf[super.count - 1] != '\n' && super.buf[super.count - 1] != '\r' ) {
byte[] newline = '\n'.bytes
write(newline, 0, newline.length)
}
IOUtil.copy(is, this)
}
InputStream toInputStream() {
return new ByteArrayInputStream( super.buf, 0, super.count )
}
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer include(String... includes) {
patternSet.include(includes)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer include(Iterable<String> includes) {
patternSet.include(includes)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer include(Spec<FileTreeElement> includeSpec) {
patternSet.include(includeSpec)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer include(Closure includeSpec) {
patternSet.include(includeSpec)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer exclude(String... excludes) {
patternSet.exclude(excludes)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer exclude(Iterable<String> excludes) {
patternSet.exclude(excludes)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer exclude(Spec<FileTreeElement> excludeSpec) {
patternSet.exclude(excludeSpec)
return this
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer exclude(Closure excludeSpec) {
patternSet.exclude(excludeSpec)
return this
}
/**
* {@inheritDoc}
*/
@Override
@Input
Set<String> getIncludes() {
return patternSet.includes
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer setIncludes(Iterable<String> includes) {
patternSet.includes = includes
return this
}
/**
* {@inheritDoc}
*/
@Override
@Input
Set<String> getExcludes() {
return patternSet.excludes
}
/**
* {@inheritDoc}
*/
@Override
ServiceFileTransformer setExcludes(Iterable<String> excludes) {
patternSet.excludes = excludes
return this
}
}