Skip to content

Commit 58eb49a

Browse files
committed
Cleaned up EnhancedTask to fix some illegal access issues
1 parent 7107a7a commit 58eb49a

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.gradle.api.reflect.TypeOf;
2323
import org.jetbrains.annotations.ApiStatus;
2424
import org.jetbrains.annotations.Nullable;
25-
import org.jetbrains.annotations.VisibleForTesting;
2625

2726
import javax.inject.Inject;
2827
import java.io.IOException;
@@ -165,6 +164,19 @@ Implementing subclass of ToolExecBase should also implement (a subclass of) Enha
165164
.solution("Double check your task implementation."));
166165
}
167166

167+
/// Reports an implementation of [EnhancedTask] that does not implement [EnhancedTask#pluginType()]
168+
///
169+
/// @param task The affected task
170+
public final void reportToolExecNotEnhanced(Class<?> task) {
171+
this.getReporter().report(id("enhanced-task-no-plugin", "EnhancedTask doesn't implement #pluginType"), spec -> spec
172+
.details("""
173+
Implementation of EnhancedTask must implement #pluginType
174+
Affected task type: %s""".formatted(task))
175+
.severity(Severity.ERROR)
176+
.stackLocation()
177+
.solution("Double check your task implementation."));
178+
}
179+
168180
/// Reports an implementation of [ToolExecBase] that adds arguments without using [ToolExecBase#addArguments()]
169181
///
170182
/// @param task The affected task

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,44 @@
77
import org.gradle.api.Project;
88
import org.gradle.api.Task;
99
import org.gradle.api.file.Directory;
10+
import org.gradle.api.file.DirectoryProperty;
1011
import org.gradle.api.file.RegularFile;
1112
import org.gradle.api.provider.Provider;
1213
import org.gradle.api.tasks.Internal;
1314

15+
import java.io.File;
16+
1417
/// The enhanced task contains a handful of helper methods to make working with the enhanced plugin and caches easier.
15-
///
16-
/// @param <T> The type of enhanced plugin
17-
public interface EnhancedTask<T extends EnhancedPlugin<? super Project>> extends Task {
18+
public interface EnhancedTask extends Task {
1819
/// The enhanced plugin type for this task.
1920
///
2021
/// @return The plugin type
21-
@Internal Class<T> getPluginType();
22+
Class<? extends EnhancedPlugin<? super Project>> pluginType();
2223

23-
/// The enhanced plugin associated with this task.
24-
///
25-
/// @return The plugin
26-
default @Internal T getPlugin() {
27-
return this.getProject().getPlugins().getPlugin(this.getPluginType());
24+
private EnhancedPlugin<? super Project> getPlugin() {
25+
return this.getProject().getPlugins().getPlugin(this.pluginType());
26+
}
27+
28+
/// @see EnhancedPlugin#getTool(Tool)
29+
default Provider<File> getTool(Tool tool) {
30+
return this.getPlugin().getTool(tool);
31+
}
32+
33+
/// @see EnhancedPlugin#globalCaches()
34+
default DirectoryProperty globalCaches() {
35+
return this.getPlugin().globalCaches();
36+
}
37+
38+
/// @see EnhancedPlugin#localCaches()
39+
default DirectoryProperty localCaches() {
40+
return this.getPlugin().localCaches();
2841
}
2942

3043
/// The default output directory to use for this task if it outputs a directory.
3144
///
3245
/// @return A provider for the directory
3346
default @Internal Provider<Directory> getDefaultOutputDirectory() {
34-
return this.getPlugin().localCaches().dir(this.getName()).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
47+
return this.localCaches().dir(this.getName()).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
3548
}
3649

3750
/// The default output file to use for this task if it outputs a file. Uses the `.jar` extension.
@@ -46,6 +59,6 @@ public interface EnhancedTask<T extends EnhancedPlugin<? super Project>> extends
4659
/// @param ext The extension to use for the file
4760
/// @return A provider for the file
4861
default Provider<RegularFile> getDefaultOutputFile(String ext) {
49-
return this.getPlugin().localCaches().file("%s/output.%s".formatted(this.getName(), ext)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
62+
return this.localCaches().file("%s/output.%s".formatted(this.getName(), ext)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
5063
}
5164
}

src/main/groovy/net/minecraftforge/gradleutils/shared/ToolExecBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public abstract class ToolExecBase<P extends EnhancedProblems> extends JavaExec
4949
protected ToolExecBase(Class<P> problemsType, Tool tool) {
5050
this.problems = this.getObjectFactory().newInstance(problemsType);
5151

52-
if (this instanceof EnhancedTask<?> enhancedTask) {
52+
if (this instanceof EnhancedTask enhancedTask) {
5353
this.defaultToolDir = this.getObjectFactory().directoryProperty().value(
54-
enhancedTask.getPlugin().globalCaches().dir(tool.getName().toLowerCase(Locale.ENGLISH)).map(this.ensureFileLocationInternal())
54+
enhancedTask.globalCaches().dir(tool.getName().toLowerCase(Locale.ENGLISH)).map(this.ensureFileLocationInternal())
5555
);
56-
this.setClasspath(this.getObjectFactory().fileCollection().from(enhancedTask.getPlugin().getTool(tool)));
56+
this.setClasspath(this.getObjectFactory().fileCollection().from(enhancedTask.getTool(tool)));
5757
} else {
5858
this.getProject().afterEvaluate(project -> this.problems.reportToolExecNotEnhanced(this));
5959

0 commit comments

Comments
 (0)