Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ gradlePlugin {

plugins.register('gradleutils') {
id = 'net.minecraftforge.gradleutils'
implementationClass = 'net.minecraftforge.gradleutils.GradleUtilsPlugin'
implementationClass = 'net.minecraftforge.gradleutils.internal.GradleUtilsPlugin'
displayName = gradleutils.displayName
description = project.description
tags = ['minecraftforge']
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-rc-1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-rc-2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
/// needing to duplicate code across projects.
///
/// @param <T> The type of target
public non-sealed abstract class EnhancedPlugin<T> implements Plugin<T>, EnhancedPluginAdditions {
public abstract non-sealed class EnhancedPlugin<T> implements Plugin<T>, EnhancedPluginAdditions {
private final String name;
private final String displayName;
private final @Nullable String toolsExtName;

private @UnknownNullability T target;
private ToolsExtensionImpl tools = this.getObjects().newInstance(ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
private @Nullable ToolsExtensionImpl tools;
private final EnhancedProblems problemsInternal;

/// The object factory provided by Gradle services.
Expand Down Expand Up @@ -66,11 +66,6 @@ public non-sealed abstract class EnhancedPlugin<T> implements Plugin<T>, Enhance
/// Service Injection</a>
protected abstract @Inject ProviderFactory getProviders();

/// The Java toolchain service provided by Gradle services.
///
/// @return The Java toolchain service
protected abstract @Inject JavaToolchainService getJavaToolchains();

/// This constructor must be called by all subclasses using a public constructor annotated with [Inject]. The name
/// and display name passed in are used in a minimal instance of [EnhancedProblems], which is used to set up the
/// plugin's [global][#globalCaches()] and [local][#localCaches()] caches. Additionally, the name is used to
Expand Down Expand Up @@ -103,12 +98,20 @@ protected EnhancedPlugin(String name, String displayName, @Nullable String tools
/// @param target The target for this plugin
@Override
public final void apply(T target) {
this.setup(this.target = target);
if (this.toolsExtName != null && target instanceof ExtensionAware extensionAware) {
this.tools = (ToolsExtensionImpl) extensionAware.getExtensions().create(ToolsExtension.class, this.toolsExtName, ToolsExtensionImpl.class);

try {
var gradle = (Gradle) InvokerHelper.getProperty(this.target, "gradle");
var tools = (ToolsExtensionImpl) gradle.getExtensions().findByName(this.toolsExtName);
if (tools != null)
this.tools.definitions.addAll(tools.definitions);
} catch (Exception ignored) { }
} else {
this.tools = this.getObjects().newInstance(ToolsExtensionImpl.class);
}

if (this.toolsExtName != null && target instanceof ExtensionAware extensionAware)
this.tools = extensionAware.getExtensions().create(this.toolsExtName, ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
// else
// this.tools = this.getObjects().newInstance(ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
this.setup(this.target = target);
}

/// Called when this plugin is applied to do setup work.
Expand Down Expand Up @@ -138,6 +141,9 @@ final EnhancedProblems getProblemsInternal() {

@Override
public Tool.Resolved getTool(Tool tool) {
if (this.tools == null)
throw new IllegalStateException("Plugin has not yet been applied");

ProviderFactory providers;
try {
providers = this.target instanceof Project ? this.getProviders() : ((Gradle) InvokerHelper.getProperty(this.target, "gradle")).getRootProject().getProviders();
Expand All @@ -148,11 +154,6 @@ public Tool.Resolved getTool(Tool tool) {
return ((ToolInternal) tool).get(this.globalCaches(), providers, this.tools);
}

// NOTE: Use this in Tool implementations. Enhanced plugins do not enforce application on projects.
JavaToolchainService toolchainsForTools() {
return this.target instanceof Project ? this.getJavaToolchains() : ((Gradle) InvokerHelper.getProperty(this.target, "gradle")).getRootProject().getExtensions().getByType(JavaToolchainService.class);
}


/* CACHES */

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/// This interface defines the additional methods added by [EnhancedPlugin]. They are additionally accessible in tasks
/// that implement [EnhancedTask].
public sealed interface EnhancedPluginAdditions permits EnhancedPlugin, EnhancedTask {
public sealed interface EnhancedPluginAdditions permits EnhancedPlugin, EnhancedTask, EnhancedTaskAdditions {
/// Gets a provider to the file for a [Tool] to be used. The tool's state is managed by Gradle through the
/// [org.gradle.api.provider.ValueSource] API and will not cause caching issues.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// The enhanced task contains a handful of helper methods to make working with the enhanced plugin and caches easier.
///
/// @param <P> The type of enhanced problems
public non-sealed interface EnhancedTask<P extends EnhancedProblems> extends Task, EnhancedPluginAdditions {
public non-sealed interface EnhancedTask<P extends EnhancedProblems> extends Task, EnhancedPluginAdditions, EnhancedTaskAdditions {
/// The enhanced plugin type for this task.
///
/// @return The plugin type
Expand All @@ -27,70 +27,14 @@ public non-sealed interface EnhancedTask<P extends EnhancedProblems> extends Tas
/// @return The problems type
Class<P> problemsType();

private EnhancedPlugin<? super Project> getPlugin() {
return this.getProject().getPlugins().getPlugin(this.pluginType());
}

@Override
default Tool.Resolved getTool(Tool tool) {
return this.getPlugin().getTool(tool);
}

@Override
default DirectoryProperty globalCaches() {
return this.getPlugin().globalCaches();
}

@Override
default DirectoryProperty localCaches() {
return this.getPlugin().localCaches();
}

@Override
default DirectoryProperty rootProjectDirectory() {
return this.getPlugin().rootProjectDirectory();
default EnhancedPlugin<? super Project> plugin() {
return this.getProject().getPlugins().getPlugin(this.pluginType());
}

@Override
default DirectoryProperty workingProjectDirectory() {
return this.getPlugin().workingProjectDirectory();
}

/// The default output directory to use for this task if it outputs a directory.
///
/// @return A provider for the directory
default @Internal Provider<Directory> getDefaultOutputDirectory() {
return this.localCaches().dir(this.getName()).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
}

/// The default output file to use for this task if it outputs a file. Uses the `.jar` extension.
///
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultOutputFile() {
return this.getDefaultOutputFile("jar");
}

/// The default output file to use for this task if it outputs a file.
///
/// @param ext The extension to use for the file
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultOutputFile(String ext) {
return this.getOutputFile("output." + ext);
}

/// The default output log file to use for this task.
///
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultLogFile() {
return this.getOutputFile("log.txt");
}

/// A file with the specified name in the default output directory.
///
/// @param fileName The name of the output file
/// @return A provider for the file
default @Internal Provider<RegularFile> getOutputFile(String fileName) {
return this.localCaches().file(String.format("%s/%s", this.getName(), fileName)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
default String baseName() {
return this.getName();
}

default void afterEvaluate(Action<? super Project> action) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.minecraftforge.gradleutils.shared;

import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Internal;

sealed interface EnhancedTaskAdditions extends EnhancedPluginAdditions permits EnhancedTask, ToolExecSpec {
EnhancedPlugin<?> plugin();

String baseName();

default Tool.Resolved getTool(Tool tool) {
return this.plugin().getTool(tool);
}

default DirectoryProperty globalCaches() {
return this.plugin().globalCaches();
}

default DirectoryProperty localCaches() {
return this.plugin().localCaches();
}

default DirectoryProperty rootProjectDirectory() {
return this.plugin().rootProjectDirectory();
}

default DirectoryProperty workingProjectDirectory() {
return this.plugin().workingProjectDirectory();
}

/// The default output directory to use for this task if it outputs a directory.
///
/// @return A provider for the directory
default @Internal Provider<Directory> getDefaultOutputDirectory() {
return this.localCaches().dir(this.baseName()).map(this.plugin().getProblemsInternal().ensureFileLocation());
}

/// The default output file to use for this task if it outputs a file. Uses the `.jar` extension.
///
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultOutputFile() {
return this.getDefaultOutputFile("jar");
}

/// The default output file to use for this task if it outputs a file.
///
/// @param ext The extension to use for the file
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultOutputFile(String ext) {
return this.getOutputFile("output." + ext);
}

/// The default output log file to use for this task.
///
/// @return A provider for the file
default @Internal Provider<RegularFile> getDefaultLogFile() {
return this.getOutputFile("log.txt");
}

/// A file with the specified name in the default output directory.
///
/// @param fileName The name of the output file
/// @return A provider for the file
default @Internal Provider<RegularFile> getOutputFile(String fileName) {
return this.localCaches().file(String.format("%s/%s", this.baseName(), fileName)).map(this.plugin().getProblemsInternal().ensureFileLocation());
}
}
Loading