-
-
Notifications
You must be signed in to change notification settings - Fork 776
/
ByteBuddyCopyOutputTask.java
129 lines (118 loc) · 4.33 KB
/
ByteBuddyCopyOutputTask.java
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
/*
* Copyright 2014 - Present Rafael Winterhalter
*
* Licensed 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 net.bytebuddy.build.gradle.android;
import com.android.build.api.artifact.MultipleArtifact;
import com.android.build.api.variant.Variant;
import net.bytebuddy.utility.FileSystem;
import org.gradle.api.Action;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileType;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.FileChange;
import org.gradle.work.Incremental;
import org.gradle.work.InputChanges;
import java.io.File;
import java.io.IOException;
/**
* Collects locally compiled classes (from both the Java and Kotlin compilers) and places them in a
* single output folder that will be later used as input for Byte Buddy.
*/
public abstract class ByteBuddyCopyOutputTask extends DefaultTask {
/**
* Returns the local class path.
*
* @return The local class path.
*/
@Incremental
@Classpath
public abstract ConfigurableFileCollection getLocalClasspath();
/**
* Returns the output directory.
*
* @return The output directory.
*/
@OutputDirectory
public abstract DirectoryProperty getOutputDir();
/**
* The action to execute.
*
* @param inputChanges The incremental changes of the task.
*/
@TaskAction
public void execute(InputChanges inputChanges) {
for (FileChange fileChange : inputChanges.getFileChanges(getLocalClasspath())) {
if (fileChange.getFileType() == FileType.DIRECTORY) {
return;
}
File target = getOutputDir().file(fileChange.getNormalizedPath()).get().getAsFile();
switch (fileChange.getChangeType()) {
case REMOVED:
if (target.delete()) {
getLogger().debug("Deleted file {}", target);
}
break;
case ADDED:
case MODIFIED:
try {
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
FileSystem.getInstance().copy(fileChange.getFile(), target);
} catch (IOException e) {
throw new IllegalStateException("Failed to copy " + fileChange.getFile() + " to " + target, e);
}
break;
default:
throw new IllegalStateException();
}
}
}
/**
* A configuration action for a {@link ByteBuddyCopyOutputTask}.
*/
protected static class ConfigurationAction implements Action<ByteBuddyCopyOutputTask> {
/**
* The current Gradle project.
*/
private final Project project;
/**
* The current variant.
*/
private final Variant variant;
/**
* Creates a new configuration action for a {@link ByteBuddyCopyOutputTask}.
*
* @param project The current Gradle project.
* @param variant The current variant.
*/
protected ConfigurationAction(Project project, Variant variant) {
this.project = project;
this.variant = variant;
}
/**
* {@inheritDoc}
*/
public void execute(ByteBuddyCopyOutputTask task) {
task.getLocalClasspath().from(variant.getArtifacts().getAll(MultipleArtifact.ALL_CLASSES_DIRS.INSTANCE));
task.getOutputDir().set(project.getLayout().getBuildDirectory().dir("intermediates/incremental/" + task.getName()));
}
}
}