Skip to content

Commit 5d79d04

Browse files
committed
Lower targets for gradleutils-shared
Need to target Gradle 8.13 because ForgeDev is currently on 8.14.1 right now, and FG6 will not run on Gradle 9. I need FG6 in the workspace with Forge for now since it acts as a reference point to help me move the patcher plugin application. Once ForgeDev is free of FG6, it will be updated to Gradle 9. Also, got a tip from the Gradle Slack that worker daemons (`WorkerExecutor`) can still run in Java 8 on Gradle 9, so making this shared code target Java 8 will be better long-term.
1 parent 848d716 commit 5d79d04

File tree

11 files changed

+188
-131
lines changed

11 files changed

+188
-131
lines changed

gradleutils-shared/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ version = gitversion.tagOffset
2323
println "Version: $version"
2424

2525
java {
26-
toolchain.languageVersion = JavaLanguageVersion.of(17)
26+
toolchain.languageVersion = JavaLanguageVersion.of(8)
2727
withSourcesJar()
2828
withJavadocJar()
2929
}
@@ -46,10 +46,10 @@ dependencies {
4646
compileOnly libs.nulls
4747

4848
// Gradle API
49-
compileOnly libs.gradle
49+
compileOnly sharedLibs.gradle
5050

5151
// Tools
52-
implementation libs.bundles.utils
52+
implementation sharedLibs.bundles.utils
5353
}
5454

5555
license {
@@ -84,6 +84,10 @@ tasks.withType(Javadoc).configureEach {
8484
}
8585

8686
publishing {
87+
repositories {
88+
maven gradleutils.publishingForgeMaven
89+
}
90+
8791
publications.register('mavenJava', MavenPublication) {
8892
from components.java
8993
artifactId = projectArtifactId
@@ -103,10 +107,6 @@ publishing {
103107
}
104108
}
105109
}
106-
107-
repositories {
108-
maven gradleutils.publishingForgeMaven
109-
}
110110
}
111111

112112
idea.module { downloadSources = downloadJavadoc = true }

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/Closures.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ public final class Closures {
6161

6262
@SuppressWarnings({"rawtypes", "unchecked"})
6363
private static <T> @UnknownNullability T invokeInternal(Closure closure, Object... object) {
64-
var original = Thread.currentThread().getContextClassLoader();
64+
ClassLoader original = Thread.currentThread().getContextClassLoader();
6565
Thread.currentThread().setContextClassLoader(closure.getClass().getClassLoader());
6666
try {
67-
var ret = closure.getMaximumNumberOfParameters() == 0 ? closure.call() : closure.call(object);
67+
Object ret = closure.getMaximumNumberOfParameters() == 0 ? closure.call() : closure.call(object);
6868
return ret != null ? (T) ret : null;
6969
} catch (InvokerInvocationException e) {
70-
throw e.getCause() instanceof RuntimeException rte ? rte : e;
70+
Throwable cause = e.getCause();
71+
throw cause instanceof RuntimeException ? (RuntimeException) cause : e;
7172
} finally {
7273
Thread.currentThread().setContextClassLoader(original);
7374
}
@@ -79,7 +80,7 @@ public final class Closures {
7980
/// @param <R> The return type of the function
8081
/// @return The closure
8182
/// @apiNote For static methods only.
82-
public static <R> Closure<R> unaryOperator(UnaryOperator<? extends R> function) {
83+
public static <R> Closure<R> unaryOperator(UnaryOperator<R> function) {
8384
return unaryOperator(ReflectionUtils.getCallingClass(), function);
8485
}
8586

@@ -90,7 +91,7 @@ public static <R> Closure<R> unaryOperator(UnaryOperator<? extends R> function)
9091
/// @param <R> The return type of the function
9192
/// @return The closure
9293
/// @apiNote For instance methods only.
93-
public static <R> Closure<R> unaryOperator(Object owner, UnaryOperator<? extends R> function) {
94+
public static <R> Closure<R> unaryOperator(Object owner, UnaryOperator<R> function) {
9495
return function(owner, function);
9596
}
9697

@@ -304,7 +305,7 @@ public Void doCall() {
304305
}
305306
}
306307

307-
private static sealed class Empty extends Closure<Void> permits Running {
308+
private static class Empty extends Closure<Void> {
308309
public Empty(Object owner) {
309310
super(owner, owner);
310311
}

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/EnhancedPlugin.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package net.minecraftforge.gradleutils.shared;
66

77
import org.codehaus.groovy.runtime.InvokerHelper;
8+
import org.gradle.StartParameter;
89
import org.gradle.api.Plugin;
910
import org.gradle.api.Project;
11+
import org.gradle.api.file.BuildLayout;
1012
import org.gradle.api.file.DirectoryProperty;
1113
import org.gradle.api.invocation.Gradle;
1214
import org.gradle.api.model.ObjectFactory;
@@ -36,6 +38,12 @@ public abstract class EnhancedPlugin<T> implements Plugin<T> {
3638
*/
3739
protected abstract @Inject ObjectFactory getObjects();
3840

41+
/**
42+
* @see <a href="https://docs.gradle.org/current/userguide/service_injection.html#buildlayout">BuildLayout
43+
* Service Injection</a>
44+
*/
45+
protected abstract @Inject BuildLayout getBuildLayout();
46+
3947
/**
4048
* @see <a href="https://docs.gradle.org/current/userguide/service_injection.html#providerfactory">ProviderFactory
4149
* Service Injection</a>
@@ -118,15 +126,15 @@ public final DirectoryProperty globalCaches() {
118126

119127
private DirectoryProperty makeGlobalCaches() {
120128
try {
121-
var startParameter = ((Gradle) InvokerHelper.getPropertySafe(this.target, "gradle")).getStartParameter();
122-
var gradleUserHomeDir = this.getObjects().directoryProperty().fileValue(startParameter.getGradleUserHomeDir());
129+
StartParameter startParameter = ((Gradle) InvokerHelper.getPropertySafe(this.target, "gradle")).getStartParameter();
130+
DirectoryProperty gradleUserHomeDir = this.getObjects().directoryProperty().fileValue(startParameter.getGradleUserHomeDir());
123131

124132
return this.getObjects().directoryProperty().convention(
125133
gradleUserHomeDir.dir("caches/minecraftforge/" + this.name).map(this.problemsInternal.ensureFileLocation())
126134
);
127135
} catch (Exception e) {
128136
throw this.problemsInternal.illegalPluginTarget(
129-
new IllegalArgumentException("Failed to get %s global caches directory for target: %s".formatted(this.displayName, this.target), e),
137+
new IllegalArgumentException(String.format("Failed to get %s global caches directory for target: %s", this.displayName, this.target), e),
130138
"types with access to Gradle (#getGradle()Lorg/gradle/api/invocation/Gradle), such as projects or settings."
131139
);
132140
}
@@ -149,19 +157,20 @@ public final DirectoryProperty localCaches() {
149157
private DirectoryProperty makeLocalCaches() {
150158
try {
151159
DirectoryProperty workingProjectBuildDir;
152-
if (this.target instanceof Project project) {
153-
workingProjectBuildDir = project.getLayout().getBuildDirectory();
160+
if (this.target instanceof Project) {
161+
workingProjectBuildDir = ((Project) this.target).getLayout().getBuildDirectory();
154162
} else {
155-
var startParameter = ((Gradle) InvokerHelper.getPropertySafe(this.target, "gradle")).getStartParameter();
156-
workingProjectBuildDir = this.getObjects().directoryProperty().fileValue(new File(Objects.requireNonNullElseGet(startParameter.getProjectDir(), startParameter::getCurrentDir), "build"));
163+
StartParameter startParameter = ((Gradle) InvokerHelper.getPropertySafe(this.target, "gradle")).getStartParameter();
164+
File projectDir = startParameter.getProjectDir();
165+
workingProjectBuildDir = this.getObjects().directoryProperty().fileValue(new File(projectDir != null ? projectDir : this.getBuildLayout().getRootDirectory().getAsFile(), "build"));
157166
}
158167

159168
return this.getObjects().directoryProperty().convention(
160169
workingProjectBuildDir.dir("minecraftforge/" + this.name).map(this.problemsInternal.ensureFileLocation())
161170
);
162171
} catch (Exception e) {
163172
throw this.problemsInternal.illegalPluginTarget(
164-
new IllegalArgumentException("Failed to get %s local caches directory for target: %s".formatted(this.displayName, this.getTarget()), e),
173+
new IllegalArgumentException(String.format("Failed to get %s local caches directory for target: %s", this.displayName, this.getTarget()), e),
165174
"projects or types with access to Gradle (#getGradle()Lorg/gradle/api/invocation/Gradle), such as settings."
166175
);
167176
}

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/EnhancedProblems.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.annotations.Nullable;
2727

2828
import javax.inject.Inject;
29+
import java.io.File;
2930
import java.io.IOException;
3031
import java.nio.file.Files;
3132
import java.util.Collection;
@@ -107,10 +108,10 @@ public final boolean test(String property) {
107108
/// @return The exception to throw
108109
public final RuntimeException illegalPluginTarget(Exception e, Class<?> firstAllowedTarget, Class<?>... allowedTargets) {
109110
return this.getReporter().throwing(e, id("invalid-plugin-target", "Invalid plugin target"), spec -> spec
110-
.details("""
111-
Attempted to apply the %s plugin to an invalid target.
112-
This plugin can only be applied on the following types:
113-
%s""".formatted(this.displayName, Stream.concat(Stream.of(firstAllowedTarget), Stream.of(allowedTargets)).map(Class::getName).collect(Collectors.joining(", ", "[", "]"))))
111+
.details(String.format(
112+
"Attempted to apply the %s plugin to an invalid target.\n" +
113+
"This plugin can only be applied on the following types:\n" +
114+
"%s", this.displayName, Stream.concat(Stream.of(firstAllowedTarget), Stream.of(allowedTargets)).map(Class::getName).collect(Collectors.joining(", ", "[", "]"))))
114115
.severity(Severity.ERROR)
115116
.stackLocation()
116117
.solution("Use a valid plugin target.")
@@ -124,9 +125,9 @@ public final RuntimeException illegalPluginTarget(Exception e, Class<?> firstAll
124125
/// @return The exception to throw
125126
public final RuntimeException illegalPluginTarget(Exception e, String allowedTargets) {
126127
return this.getReporter().throwing(e, id("invalid-plugin-target", "Invalid plugin target"), spec -> spec
127-
.details("""
128-
Attempted to apply the %s plugin to an invalid target.
129-
This plugin can only be applied on %s""".formatted(this.displayName, allowedTargets))
128+
.details(String.format(
129+
"Attempted to apply the %s plugin to an invalid target.\n" +
130+
"This plugin can only be applied on %s", this.displayName, allowedTargets))
130131
.severity(Severity.ERROR)
131132
.stackLocation()
132133
.solution("Use a valid plugin target.")
@@ -138,9 +139,9 @@ public final RuntimeException illegalPluginTarget(Exception e, String allowedTar
138139
/// @param e The exception that was caught, will be re-thrown (or wrapped with a [RuntimeException])
139140
/// @return The exception to throw
140141
public final RuntimeException pluginNotYetApplied(Exception e) {
141-
return this.getReporter().throwing(e, id("plugin-not-yet-applied", "%s is not applied".formatted(this.displayName)), spec -> spec
142-
.details("""
143-
Attempted to get details from the %s plugin, but it has not yet been applied to the target.""".formatted(this.displayName))
142+
return this.getReporter().throwing(e, id("plugin-not-yet-applied", String.format("%s is not applied", this.displayName)), spec -> spec
143+
.details(String.format(
144+
"Attempted to get details from the %s plugin, but it has not yet been applied to the target.", this.displayName))
144145
.severity(Severity.ERROR)
145146
.stackLocation()
146147
.solution("Apply the plugin before attempting to use it from the target's plugin manager.")
@@ -157,10 +158,10 @@ public final RuntimeException pluginNotYetApplied(Exception e) {
157158
/// @param task The affected task
158159
public final void reportToolExecNotEnhanced(Task task) {
159160
this.getReporter().report(id("tool-exec-not-enhanced", "ToolExec subclass doesn't implement EnhancedTask"), spec -> spec
160-
.details("""
161-
Implementing subclass of ToolExecBase should also implement (a subclass of) EnhancedTask.
162-
Not doing so will result in global caches being ignored. Please check your implementations.
163-
Affected task: %s (%s)""".formatted(task, task.getClass()))
161+
.details(String.format(
162+
"Implementing subclass of ToolExecBase should also implement (a subclass of) EnhancedTask.\n" +
163+
"Not doing so will result in global caches being ignored. Please check your implementations.\n" +
164+
"Affected task: %s (%s)", task, task.getClass()))
164165
.severity(Severity.WARNING)
165166
.stackLocation()
166167
.solution("Double check your task implementation."));
@@ -171,9 +172,9 @@ Implementing subclass of ToolExecBase should also implement (a subclass of) Enha
171172
/// @param task The affected task
172173
public final void reportToolExecNotEnhanced(Class<?> task) {
173174
this.getReporter().report(id("enhanced-task-no-plugin", "EnhancedTask doesn't implement #pluginType"), spec -> spec
174-
.details("""
175-
Implementation of EnhancedTask must implement #pluginType
176-
Affected task type: %s""".formatted(task))
175+
.details(String.format(
176+
"Implementation of EnhancedTask must implement #pluginType\n" +
177+
"Affected task type: %s", task))
177178
.severity(Severity.ERROR)
178179
.stackLocation()
179180
.solution("Double check your task implementation."));
@@ -184,10 +185,10 @@ public final void reportToolExecNotEnhanced(Class<?> task) {
184185
/// @param task The affected task
185186
public final void reportToolExecEagerArgs(Task task) {
186187
this.getReporter().report(id("tool-exec-eager-args", "ToolExecBase implementation adds arguments without using addArguments()"), spec -> spec
187-
.details("""
188-
A ToolExecBase task is eagerly adding arguments using JavaExec#args without using ToolExecBase#addArguments.
189-
This may cause implementations or superclasses to have their arguments ignored or missing.
190-
Affected task: %s (%s)""".formatted(task, task.getClass()))
188+
.details(String.format(
189+
"A ToolExecBase task is eagerly adding arguments using JavaExec#args without using ToolExecBase#addArguments.\n" +
190+
"This may cause implementations or superclasses to have their arguments ignored or missing.\n" +
191+
"Affected task: %s (%s)", task, task.getClass()))
191192
.severity(Severity.WARNING)
192193
.stackLocation()
193194
.solution("Use ToolExecBase#addArguments"));
@@ -203,15 +204,15 @@ public final void reportToolExecEagerArgs(Task task) {
203204
/// @return The transformer to apply onto a provider
204205
public final <T extends FileSystemLocation> Transformer<T, T> ensureFileLocation() {
205206
return file -> {
206-
var dir = file instanceof Directory ? file.getAsFile() : file.getAsFile().getParentFile();
207+
File dir = file instanceof Directory ? file.getAsFile() : file.getAsFile().getParentFile();
207208
try {
208209
Files.createDirectories(dir.toPath());
209210
} catch (IOException e) {
210211
throw this.getReporter().throwing(e, id("cannot-ensure-directory", "Failed to create directory"), spec -> spec
211-
.details("""
212-
Failed to create a directory required for %s to function.
213-
Directory: %s"""
214-
.formatted(this.displayName, dir.getAbsolutePath()))
212+
.details(String.format(
213+
"Failed to create a directory required for %s to function.\n" +
214+
"Directory: %s",
215+
this.displayName, dir.getAbsolutePath()))
215216
.severity(Severity.ERROR)
216217
.stackLocation()
217218
.solution("Ensure that the you have write access to the directory that needs to be created.")
@@ -264,8 +265,8 @@ default RuntimeException throwing(Throwable exception, Collection<? extends Prob
264265
return this.toRTE(exception);
265266
}
266267

267-
private RuntimeException toRTE(Throwable exception) {
268-
return exception instanceof RuntimeException rte ? rte : new RuntimeException(exception);
268+
default RuntimeException toRTE(Throwable exception) {
269+
return exception instanceof RuntimeException ? (RuntimeException) exception : new RuntimeException(exception);
269270
}
270271
}
271272

@@ -310,7 +311,7 @@ private Problems unwrapProblems() {
310311

311312
private Predicate<String> unwrapProperties() {
312313
try {
313-
var providers = Objects.requireNonNull(this.getProviders());
314+
ProviderFactory providers = Objects.requireNonNull(this.getProviders());
314315
return property -> isTrue(providers, property);
315316
} catch (Exception e) {
316317
return Boolean::getBoolean;

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/EnhancedTask.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.gradle.api.file.RegularFile;
1212
import org.gradle.api.provider.Provider;
1313
import org.gradle.api.tasks.Internal;
14+
import org.jetbrains.annotations.VisibleForTesting;
1415

1516
import java.io.File;
1617

@@ -21,7 +22,8 @@ public interface EnhancedTask extends Task {
2122
/// @return The plugin type
2223
Class<? extends EnhancedPlugin<? super Project>> pluginType();
2324

24-
private EnhancedPlugin<? super Project> getPlugin() {
25+
@VisibleForTesting
26+
default @Internal EnhancedPlugin<? super Project> getPlugin() {
2527
return this.getProject().getPlugins().getPlugin(this.pluginType());
2628
}
2729

@@ -59,6 +61,6 @@ default DirectoryProperty localCaches() {
5961
/// @param ext The extension to use for the file
6062
/// @return A provider for the file
6163
default Provider<RegularFile> getDefaultOutputFile(String ext) {
62-
return this.localCaches().file("%s/output.%s".formatted(this.getName(), ext)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
64+
return this.localCaches().file(String.format("%s/output.%s", this.getName(), ext)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
6365
}
6466
}

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/Lazy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/// [callables][Callable], as the closure API allows chaining via [Closure#compose(Closure)] and
1919
/// [Closure#andThen(Closure)]. They can still be created using callables.
2020
/// @see Actionable
21-
public sealed class Lazy<T> implements Supplier<T>, Callable<T> permits Lazy.Actionable {
21+
public class Lazy<T> implements Supplier<T>, Callable<T> {
2222
/// Creates a simple lazy of the given callable.
2323
///
2424
/// @param <T> The return type of the callable
@@ -144,7 +144,7 @@ public boolean isPresent() {
144144
///
145145
/// @return The a new actionable lazy copied from this one
146146
public Actionable<T> copy() {
147-
var ret = new Actionable<>(this.closure);
147+
Actionable<T> ret = new Actionable<>(this.closure);
148148
ret.value = this.value;
149149
ret.present = this.present;
150150
ret.modifications = this.modifications;

0 commit comments

Comments
 (0)