Skip to content

Commit e1fdfb6

Browse files
committed
Make compatible with Gradle 4.3 (#2)
This was done by using Groovy's `InvokerHelper` to wrap the check for `task.archiveFile`. In Gradle 5.0 and older, `AbstractArchiveTask` exposed the output with `task.archivePath`. If the former can't be found, it will try to find the latter. If neither can be found, both exceptions will be thrown with the latter being included as a suppressed exception to the former. This plugin still has a lot of problems with configuration cache, so a 2.0 will be created in due time that will target 9.0.0 (or 7.3) to address those problems. There are a lot of remedies for configuration cache that only exist in newer versions of Gradle.
1 parent f3c6d9e commit e1fdfb6

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencyResolutionManagement {
2525
// Gradle API
2626
// Original: https://github.com/remal-gradle-api/packages/packages/760197?version=7.0.2
2727
// Mirror: https://repos.moddinglegacy.com/#/modding-legacy/name/remal/gradle-api/gradle-api/7.0.2
28-
version 'gradle', '5.1'
28+
version 'gradle', '4.3'
2929
library 'gradle', 'name.remal.gradle-api', 'gradle-api' versionRef 'gradle'
3030
}
3131
}

src/main/java/net/minecraftforge/gradlejarsigner/SignTask.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
import java.util.Collections;
1818
import java.util.HashMap;
1919
import java.util.Map;
20+
import java.util.Objects;
2021
import java.util.Set;
2122
import java.util.Map.Entry;
2223
import java.util.jar.JarOutputStream;
2324
import java.util.zip.ZipEntry;
2425
import java.util.zip.ZipFile;
2526

27+
import org.codehaus.groovy.runtime.InvokerHelper;
2628
import org.gradle.api.Task;
29+
import org.gradle.api.file.RegularFile;
30+
import org.gradle.api.provider.Provider;
2731
import org.gradle.api.tasks.TaskInputs;
2832
import org.gradle.api.file.FileTreeElement;
2933
import org.gradle.api.file.FileVisitDetails;
3034
import org.gradle.api.file.FileVisitor;
31-
import org.gradle.api.file.RegularFileProperty;
3235
import org.gradle.api.model.ObjectFactory;
3336
import org.gradle.api.provider.Property;
3437
import org.gradle.api.specs.Spec;
@@ -48,7 +51,7 @@ public class SignTask implements PatternFilterable {
4851
private final Property<String> storePass;
4952
private final Property<String> keyPass;
5053
private final Property<String> keyStoreData;
51-
private final RegularFileProperty keyStoreFile;
54+
private final Property<File> keyStoreFile;
5255
private final PatternSet patternSet = new PatternSet();
5356

5457
@SuppressWarnings("serial")
@@ -65,7 +68,7 @@ public class SignTask implements PatternFilterable {
6568
this.storePass = objs.property(String.class);
6669
this.keyPass = objs.property(String.class);
6770
this.keyStoreData = objs.property(String.class);
68-
this.keyStoreFile = objs.fileProperty();
71+
this.keyStoreFile = objs.property(File.class);
6972

7073
this.parent.configure(new Closure<Object>(parent) {
7174
@SuppressWarnings("unused")
@@ -112,11 +115,32 @@ private boolean hasEnoughInfo() {
112115
(this.keyStoreData.isPresent() || this.keyStoreFile.isPresent());
113116
}
114117

118+
@SuppressWarnings("unchecked")
119+
private static File getTaskArchiveFile(Zip task) {
120+
try {
121+
// Try getting the new thing first, to avoid potential deprecation methods
122+
Provider<RegularFile> archiveFile = (Provider<RegularFile>) Objects.requireNonNull(
123+
InvokerHelper.getProperty(task, "archiveFile")
124+
);
125+
return archiveFile.get().getAsFile();
126+
} catch (Exception e) {
127+
// In older gradle? hope the old thing works.
128+
try {
129+
return task.getArchivePath();
130+
} catch (Exception suppressed) {
131+
// Well, we tried. Throw the first exception since it's for the latest Gradle version.
132+
// Add the first exception so it's not lost in the stacktrace, just in case.
133+
e.addSuppressed(suppressed);
134+
throw new IllegalStateException("Could not access the parent task's output file", e);
135+
}
136+
}
137+
}
138+
115139
private <T extends Task> void sign(T task) throws IOException {
116140
final Map<String, Entry<byte[], Long>> ignoredStuff = new HashMap<>();
117141

118142
File tmp = this.parent.getTemporaryDir();
119-
File output = this.parent.getArchiveFile().get().getAsFile();
143+
File output = getTaskArchiveFile(this.parent);
120144
File original = new File(tmp, output.getName() + ".original");
121145
Files.move(output.toPath(), original.toPath(), StandardCopyOption.REPLACE_EXISTING);
122146

@@ -132,7 +156,7 @@ private <T extends Task> void sign(T task) throws IOException {
132156
if (this.keyStoreFile.isPresent()) {
133157
if (this.keyStoreData.isPresent())
134158
throw new IllegalStateException("Both KeyStoreFile and KeyStoreData can not be set at the same time");
135-
keyStore = this.keyStoreFile.get().getAsFile();
159+
keyStore = this.keyStoreFile.get();
136160
} else if (this.keyStoreData.isPresent()) {
137161
byte[] data = Base64.getDecoder().decode(this.keyStoreData.get().getBytes(StandardCharsets.UTF_8));
138162
keyStore = new File(tmp, "keystore");
@@ -158,7 +182,7 @@ private <T extends Task> void sign(T task) throws IOException {
158182
}
159183

160184
if (!ignoredStuff.isEmpty())
161-
writeOutputJar(output, this.parent.getArchiveFile().get().getAsFile(), ignoredStuff);
185+
writeOutputJar(output, getTaskArchiveFile(this.parent), ignoredStuff);
162186
}
163187

164188
private void processInputJar(File input, File output, final Map<String, Entry<byte[], Long>> unsigned) throws IOException {

0 commit comments

Comments
 (0)