Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit 6211936

Browse files
committed
Move Gradle plugin to use Property
1 parent 59d1e8b commit 6211936

2 files changed

Lines changed: 54 additions & 113 deletions

File tree

gradle-plugin/src/main/java/net/raphimc/javadowngrader/gradle/task/DowngradeJarTask.java

Lines changed: 35 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
import net.raphimc.javadowngrader.impl.classtransform.util.ClassNameUtil;
2626
import net.raphimc.javadowngrader.runtime.RuntimeRoot;
2727
import org.gradle.api.DefaultTask;
28-
import org.gradle.api.file.FileCollection;
29-
import org.gradle.api.tasks.Internal;
28+
import org.gradle.api.file.ConfigurableFileCollection;
29+
import org.gradle.api.file.RegularFileProperty;
30+
import org.gradle.api.provider.Property;
31+
import org.gradle.api.tasks.Input;
32+
import org.gradle.api.tasks.InputFile;
33+
import org.gradle.api.tasks.InputFiles;
3034
import org.gradle.api.tasks.TaskAction;
3135
import org.objectweb.asm.Opcodes;
3236

@@ -36,59 +40,57 @@
3640
import java.io.UncheckedIOException;
3741
import java.net.URI;
3842
import java.net.URISyntaxException;
39-
import java.nio.file.FileSystem;
40-
import java.nio.file.FileSystems;
41-
import java.nio.file.Files;
42-
import java.nio.file.Path;
43+
import java.nio.file.*;
4344
import java.util.Collection;
4445
import java.util.Collections;
4546
import java.util.HashSet;
46-
import java.util.Objects;
4747
import java.util.stream.Stream;
4848

49-
public class DowngradeJarTask extends DefaultTask {
49+
public abstract class DowngradeJarTask extends DefaultTask {
5050

51-
@Internal
52-
private File input;
51+
@InputFile
52+
public abstract RegularFileProperty getInput();
5353

54-
@Internal
55-
private String outputSuffix = "-downgraded";
54+
@Input
55+
public abstract Property<String> getOutputSuffix();
5656

57-
@Internal
58-
private FileCollection compileClassPath;
57+
@InputFiles
58+
public abstract ConfigurableFileCollection getCompileClassPath();
5959

60-
@Internal
61-
private int targetVersion = Opcodes.V1_8;
60+
@Input
61+
public abstract Property<Integer> getTargetVersion();
6262

63-
@Internal
64-
private boolean copyRuntimeClasses = true;
63+
@Input
64+
public abstract Property<Boolean> getCopyRuntimeClasses();
65+
66+
public DowngradeJarTask() {
67+
getOutputSuffix().convention("-downgraded");
68+
getTargetVersion().convention(Opcodes.V1_8);
69+
getCopyRuntimeClasses().convention(true);
70+
}
6571

6672
@TaskAction
6773
public void run() throws IOException, URISyntaxException {
68-
Objects.requireNonNull(this.input, "input must be set");
69-
Objects.requireNonNull(this.outputSuffix, "outputSuffix must be set");
70-
Objects.requireNonNull(this.compileClassPath, "compileClassPath must be set");
71-
if (!this.input.exists()) throw new IllegalArgumentException("input does not exist");
72-
if (!this.input.isFile() || !this.input.getName().endsWith(".jar")) throw new IllegalArgumentException("input is not a jar file");
73-
74-
System.out.println("Downgrading jar: " + this.input.getName());
75-
try (FileSystem inFs = FileSystems.newFileSystem(this.input.toPath(), null)) {
74+
final File inputFile = getInput().getAsFile().get();
75+
System.out.println("Downgrading jar: " + inputFile);
76+
77+
try (FileSystem inFs = FileSystems.newFileSystem(inputFile.toPath(), null)) {
7678
final Path inRoot = inFs.getRootDirectories().iterator().next();
7779

7880
final Collection<String> runtimeDeps = new HashSet<>();
7981
final TransformerManager transformerManager = new TransformerManager(
80-
new PathClassProvider(inRoot, new LazyFileClassProvider(this.compileClassPath.getFiles(), new BasicClassProvider()))
82+
new PathClassProvider(inRoot, new LazyFileClassProvider(getCompileClassPath().getFiles(), new BasicClassProvider()))
8183
);
8284
transformerManager.addBytecodeTransformer(
8385
JavaDowngraderTransformer.builder(transformerManager)
84-
.targetVersion(targetVersion)
86+
.targetVersion(getTargetVersion().get())
8587
.classFilter(c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c))))
8688
.depCollector(runtimeDeps::add)
8789
.build()
8890
);
8991

90-
final String outputName = this.input.getName().substring(0, this.input.getName().length() - 4) + this.outputSuffix;
91-
final File outputFile = new File(this.input.getParentFile(), outputName + ".jar");
92+
final String outputName = inputFile.getName().substring(0, inputFile.getName().length() - 4) + getOutputSuffix().get();
93+
final File outputFile = new File(inputFile.getParentFile(), outputName + ".jar");
9294

9395
try (FileSystem outFs = FileSystems.newFileSystem(new URI("jar:" + outputFile.toURI()), Collections.singletonMap("create", "true"))) {
9496
final Path outRoot = outFs.getRootDirectories().iterator().next();
@@ -108,7 +110,7 @@ public void run() throws IOException, URISyntaxException {
108110
Files.createDirectories(parent);
109111
}
110112
if (!relative.endsWith(".class") || relative.contains("META-INF/versions/")) {
111-
Files.copy(path, dest);
113+
Files.copy(path, dest, StandardCopyOption.REPLACE_EXISTING);
112114
return;
113115
}
114116
final String className = ClassNameUtil.toClassName(relative);
@@ -127,7 +129,7 @@ public void run() throws IOException, URISyntaxException {
127129
}
128130

129131
// Copy runtime classes
130-
if (this.copyRuntimeClasses) {
132+
if (getCopyRuntimeClasses().get()) {
131133
for (final String runtimeDep : runtimeDeps) {
132134
final String classPath = runtimeDep.concat(".class");
133135
try (InputStream is = RuntimeRoot.class.getResourceAsStream("/" + classPath)) {
@@ -137,52 +139,12 @@ public void run() throws IOException, URISyntaxException {
137139
if (parent != null) {
138140
Files.createDirectories(parent);
139141
}
140-
Files.copy(is, dest);
142+
Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING);
141143
}
142144
}
143145
}
144146
}
145147
}
146148
}
147149

148-
public File getInput() {
149-
return this.input;
150-
}
151-
152-
public String getOutputSuffix() {
153-
return this.outputSuffix;
154-
}
155-
156-
public FileCollection getCompileClassPath() {
157-
return this.compileClassPath;
158-
}
159-
160-
public int getTargetVersion() {
161-
return this.targetVersion;
162-
}
163-
164-
public boolean getCopyRuntimeClasses() {
165-
return this.copyRuntimeClasses;
166-
}
167-
168-
public void setInput(final File input) {
169-
this.input = input;
170-
}
171-
172-
public void setOutputSuffix(final String outputSuffix) {
173-
this.outputSuffix = outputSuffix;
174-
}
175-
176-
public void setCompileClassPath(final FileCollection compileClassPath) {
177-
this.compileClassPath = compileClassPath;
178-
}
179-
180-
public void setTargetVersion(final int targetVersion) {
181-
this.targetVersion = targetVersion;
182-
}
183-
184-
public void setCopyRuntimeClasses(final boolean copyRuntimeClasses) {
185-
this.copyRuntimeClasses = copyRuntimeClasses;
186-
}
187-
188150
}

gradle-plugin/src/main/java/net/raphimc/javadowngrader/gradle/task/DowngradeSourceSetTask.java

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import net.raphimc.javadowngrader.impl.classtransform.util.ClassNameUtil;
2626
import net.raphimc.javadowngrader.runtime.RuntimeRoot;
2727
import org.gradle.api.DefaultTask;
28-
import org.gradle.api.tasks.Internal;
28+
import org.gradle.api.provider.Property;
29+
import org.gradle.api.tasks.Input;
2930
import org.gradle.api.tasks.SourceSet;
3031
import org.gradle.api.tasks.TaskAction;
3132
import org.objectweb.asm.Opcodes;
@@ -36,37 +37,39 @@
3637
import java.io.UncheckedIOException;
3738
import java.nio.file.Files;
3839
import java.nio.file.Path;
40+
import java.nio.file.StandardCopyOption;
3941
import java.util.Collection;
4042
import java.util.HashSet;
41-
import java.util.Objects;
4243
import java.util.stream.Stream;
4344

44-
public class DowngradeSourceSetTask extends DefaultTask {
45+
public abstract class DowngradeSourceSetTask extends DefaultTask {
46+
@Input
47+
public abstract Property<SourceSet> getSourceSet();
4548

46-
@Internal
47-
private SourceSet sourceSet;
49+
@Input
50+
public abstract Property<Integer> getTargetVersion();
4851

49-
@Internal
50-
private int targetVersion = Opcodes.V1_8;
52+
@Input
53+
public abstract Property<Boolean> getCopyRuntimeClasses();
5154

52-
@Internal
53-
private boolean copyRuntimeClasses = true;
55+
public DowngradeSourceSetTask() {
56+
getTargetVersion().convention(Opcodes.V1_8);
57+
getCopyRuntimeClasses().convention(true);
58+
}
5459

5560
@TaskAction
5661
public void run() throws IOException {
57-
Objects.requireNonNull(this.sourceSet, "sourceSet must be set");
58-
59-
for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) {
62+
for (File classesDir : getSourceSet().get().getOutput().getClassesDirs()) {
6063
System.out.println("Downgrading source set: " + this.getProject().getProjectDir().toPath().relativize(classesDir.toPath()));
6164
final Path inRoot = classesDir.toPath();
6265

6366
final Collection<String> runtimeDeps = new HashSet<>();
6467
final TransformerManager transformerManager = new TransformerManager(
65-
new PathClassProvider(inRoot, new LazyFileClassProvider(this.sourceSet.getCompileClasspath().getFiles(), new BasicClassProvider()))
68+
new PathClassProvider(inRoot, new LazyFileClassProvider(getSourceSet().get().getCompileClasspath().getFiles(), new BasicClassProvider()))
6669
);
6770
transformerManager.addBytecodeTransformer(
6871
JavaDowngraderTransformer.builder(transformerManager)
69-
.targetVersion(targetVersion)
72+
.targetVersion(getTargetVersion().get())
7073
.classFilter(c -> Files.isRegularFile(inRoot.resolve(ClassNameUtil.toClassFilename(c))))
7174
.depCollector(runtimeDeps::add)
7275
.build()
@@ -96,7 +99,7 @@ public void run() throws IOException {
9699
}
97100

98101
// Copy runtime classes
99-
if (this.copyRuntimeClasses) {
102+
if (getCopyRuntimeClasses().get()) {
100103
for (final String runtimeDep : runtimeDeps) {
101104
final String classPath = runtimeDep.concat(".class");
102105
try (InputStream is = RuntimeRoot.class.getResourceAsStream("/" + classPath)) {
@@ -106,35 +109,11 @@ public void run() throws IOException {
106109
if (parent != null) {
107110
Files.createDirectories(parent);
108111
}
109-
Files.copy(is, dest);
112+
Files.copy(is, dest, StandardCopyOption.REPLACE_EXISTING);
110113
}
111114
}
112115
}
113116
}
114117
}
115118

116-
public SourceSet getSourceSet() {
117-
return this.sourceSet;
118-
}
119-
120-
public int getTargetVersion() {
121-
return this.targetVersion;
122-
}
123-
124-
public boolean getCopyRuntimeClasses() {
125-
return this.copyRuntimeClasses;
126-
}
127-
128-
public void setSourceSet(final SourceSet sourceSet) {
129-
this.sourceSet = sourceSet;
130-
}
131-
132-
public void setTargetVersion(final int targetVersion) {
133-
this.targetVersion = targetVersion;
134-
}
135-
136-
public void setCopyRuntimeClasses(final boolean copyRuntimeClasses) {
137-
this.copyRuntimeClasses = copyRuntimeClasses;
138-
}
139-
140119
}

0 commit comments

Comments
 (0)