Skip to content

Commit bf3801c

Browse files
authored
ByteBuddyDirTask to transform a directory of jar files (#1712)
1 parent f5f8073 commit bf3801c

File tree

2 files changed

+283
-0
lines changed

2 files changed

+283
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* Copyright 2014 - Present Rafael Winterhalter
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.bytebuddy.build.gradle;
17+
18+
import net.bytebuddy.build.Plugin;
19+
import net.bytebuddy.utility.nullability.MaybeNull;
20+
import org.gradle.api.tasks.*;
21+
import org.gradle.api.tasks.PathSensitive;
22+
import org.gradle.api.tasks.PathSensitivity;
23+
24+
import javax.inject.Inject;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.nio.file.Path;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
32+
/**
33+
* A Byte Buddy task implementation that does not use modern Gradle APIs.
34+
*/
35+
public class ByteBuddyDirTask extends AbstractByteBuddyTask {
36+
37+
/**
38+
* The source dir.
39+
*/
40+
private File source;
41+
42+
/**
43+
* The target dir.
44+
*/
45+
private File target;
46+
47+
/**
48+
* The class path to supply to the plugin engine.
49+
*/
50+
private Iterable<File> classPath;
51+
52+
/**
53+
* A set of classes that is used for discovery of plugins.
54+
*/
55+
@MaybeNull
56+
private Iterable<File> discoverySet;
57+
58+
/**
59+
* Creates a new simple Byte Buddy task.
60+
*/
61+
@Inject
62+
@SuppressWarnings("this-escape")
63+
public ByteBuddyDirTask() {
64+
new ByteBuddyDirTaskExtension(null).configure(this);
65+
}
66+
67+
/**
68+
* Returns the task's source dir.
69+
*
70+
* @return The task's source dir.
71+
*/
72+
@InputDirectory
73+
@PathSensitive(PathSensitivity.RELATIVE)
74+
public File getSource() {
75+
return source;
76+
}
77+
78+
/**
79+
* Sets the task's source dir.
80+
*
81+
* @param source The task's source dir.
82+
*/
83+
public void setSource(File source) {
84+
this.source = source;
85+
}
86+
87+
/**
88+
* Returns the task's target dir.
89+
*
90+
* @return The task's target dir.
91+
*/
92+
@OutputDirectory
93+
public File getTarget() {
94+
return target;
95+
}
96+
97+
/**
98+
* Sets the task's target dir.
99+
*
100+
* @param target The task's target dir.
101+
*/
102+
public void setTarget(File target) {
103+
this.target = target;
104+
}
105+
106+
/**
107+
* Returns the class path to supply to the plugin engine.
108+
*
109+
* @return The class path to supply to the plugin engine.
110+
*/
111+
@InputFiles
112+
@CompileClasspath
113+
public Iterable<File> getClassPath() {
114+
return classPath;
115+
}
116+
117+
/**
118+
* Sets the class path to supply to the plugin engine.
119+
*
120+
* @param classPath The class path to supply to the plugin engine.
121+
*/
122+
public void setClassPath(Iterable<File> classPath) {
123+
this.classPath = classPath;
124+
}
125+
126+
/**
127+
* Returns the source set to resolve plugin names from or {@code null} if no such source set is used.
128+
*
129+
* @return The source set to resolve plugin names from or {@code null} if no such source set is used.
130+
*/
131+
@MaybeNull
132+
@InputFiles
133+
@Optional
134+
public Iterable<File> getDiscoverySet() {
135+
return discoverySet;
136+
}
137+
138+
/**
139+
* Defines the source set to resolve plugin names from or {@code null} if no such source set is used.
140+
*
141+
* @param discoverySet The source set to resolve plugin names from or {@code null} if no such source set is used.
142+
*/
143+
public void setDiscoverySet(@MaybeNull Iterable<File> discoverySet) {
144+
this.discoverySet = discoverySet;
145+
}
146+
147+
@Override
148+
protected File source() {
149+
return getSource();
150+
}
151+
152+
@Override
153+
protected File target() {
154+
return getTarget();
155+
}
156+
157+
@Override
158+
protected Iterable<File> classPath() {
159+
return getClassPath();
160+
}
161+
162+
@Override
163+
@MaybeNull
164+
protected Iterable<File> discoverySet() {
165+
return discoverySet;
166+
}
167+
168+
/**
169+
* Applies this task.
170+
*
171+
* @throws IOException If an I/O exception is thrown.
172+
*/
173+
@TaskAction
174+
public void apply() throws IOException {
175+
if (!getSource().equals(getTarget()) && deleteRecursively(getTarget())) {
176+
getLogger().debug("Deleted target dir {}", getTarget());
177+
}
178+
ArrayList<File> files = new ArrayList<File>(Collections.singleton(getSource()));
179+
File candidate;
180+
do {
181+
candidate = files.remove(0);
182+
File[] file = candidate.listFiles();
183+
if (file != null) {
184+
files.addAll(Arrays.asList(file));
185+
} else {
186+
Path relative = getSource().toPath().relativize(candidate.toPath());
187+
File targetCandidate = getTarget().toPath().resolve(relative).toFile();
188+
getLogger().debug("Created dir " + targetCandidate.getParent() + ": " + targetCandidate.getParentFile().mkdirs());
189+
getLogger().debug("Created file " + targetCandidate + ": " + targetCandidate.createNewFile());
190+
doApply(new Plugin.Engine.Source.ForJarFile(candidate), new Plugin.Engine.Target.ForJarFile(targetCandidate));
191+
}
192+
} while (!files.isEmpty());
193+
}
194+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2014 - Present Rafael Winterhalter
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.bytebuddy.build.gradle;
17+
18+
import net.bytebuddy.utility.nullability.MaybeNull;
19+
import net.bytebuddy.utility.nullability.UnknownNull;
20+
import org.gradle.api.Project;
21+
import org.gradle.api.file.FileCollection;
22+
import org.gradle.api.tasks.InputFiles;
23+
import org.gradle.api.tasks.Optional;
24+
25+
import javax.inject.Inject;
26+
import java.io.File;
27+
28+
/**
29+
* A Byte Buddy dir task extension.
30+
*/
31+
public class ByteBuddyDirTaskExtension extends AbstractByteBuddyTaskExtension<ByteBuddyDirTask> {
32+
33+
/**
34+
* A set of classes that is used for discovery of plugins.
35+
*/
36+
@MaybeNull
37+
private Iterable<File> discoverySet;
38+
39+
/**
40+
* Creates a new Byte Buddy dir extension.
41+
*
42+
* @param project The current Gradle project.
43+
*/
44+
@Inject
45+
public ByteBuddyDirTaskExtension(@UnknownNull Project project) {
46+
super(project);
47+
}
48+
49+
/**
50+
* Returns the source set to resolve plugin names from or {@code null} if no such source set is used.
51+
*
52+
* @return The source set to resolve plugin names from or {@code null} if no such source set is used.
53+
*/
54+
@MaybeNull
55+
@InputFiles
56+
@Optional
57+
public Iterable<File> getDiscoverySet() {
58+
return discoverySet;
59+
}
60+
61+
/**
62+
* Defines the source set to resolve plugin names from or {@code null} if no such source set is used.
63+
*
64+
* @param discoverySet The source set to resolve plugin names from or {@code null} if no such source set is used.
65+
*/
66+
public void setDiscoverySet(@MaybeNull Iterable<File> discoverySet) {
67+
this.discoverySet = discoverySet;
68+
}
69+
70+
@Override
71+
protected boolean isEmptyDiscovery() {
72+
return discoverySet == null || !discoverySet.iterator().hasNext();
73+
}
74+
75+
@Override
76+
protected void doConfigure(ByteBuddyDirTask task) {
77+
task.setDiscoverySet(discoverySet);
78+
}
79+
80+
@Override
81+
protected Class<ByteBuddyDirTask> toType() {
82+
return ByteBuddyDirTask.class;
83+
}
84+
85+
@Override
86+
protected void discoverySet(FileCollection fileCollection) {
87+
discoverySet = fileCollection;
88+
}
89+
}

0 commit comments

Comments
 (0)