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 @@ -35,7 +35,7 @@ public non-sealed abstract class EnhancedPlugin<T> implements Plugin<T>, Enhance
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 @@ -20,6 +20,8 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.FileCollectionDependency;
import org.gradle.api.artifacts.ModuleIdentifier;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.logging.LogLevel;
import org.gradle.api.logging.Logger;
Expand Down Expand Up @@ -250,6 +252,129 @@ public List<String> getArgs() {

//region Dependency Information

public record SimpleModuleIdentifier(String getGroup, String getName) implements ModuleIdentifier {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My be worth stealing this its tried and tested.

static SimpleModuleIdentifier of(String group, String name) {
return new SimpleModuleIdentifier(group, name);
}

static SimpleModuleIdentifier of(String module) {
var substring = module.split(":");
if (substring.length != 2)
throw new IllegalArgumentException("Invalid non-versioned module identifier: " + module);

return of(substring[0], substring[1]);
}
}

public record SimpleModuleVersionIdentifier(ModuleIdentifier getModule, String getVersion,
@Nullable String classifier,
String extension) implements ModuleVersionIdentifier {
@Override
public String getGroup() {
return getModule.getGroup();
}

@Override
public String getName() {
return getModule.getName();
}

public String getDownloadUrl(String prefix) {
var builder = new StringBuilder();

// Use HTTPS by default if protocol not defined
if (!prefix.contains("://"))
builder.append("https://");

builder.append(prefix);

// Account for trailing slash
if (!prefix.endsWith("/"))
builder.append('/');

builder.append(getGroup().replace('.', '/'))
.append('/').append(getName())
.append('/').append(getVersion())
.append('/').append(getName()).append('-').append(getVersion());

if (classifier != null)
builder.append('-').append(classifier);

return builder.append('.').append(extension).toString();
}

static SimpleModuleVersionIdentifier of(ModuleIdentifier module, String version) {
return of(module, version, null, "jar");
}

static SimpleModuleVersionIdentifier of(ModuleIdentifier module, String version, @Nullable String classifier, String extension) {
return new SimpleModuleVersionIdentifier(module, version, classifier, extension);
}

static SimpleModuleVersionIdentifier of(String module, String version) {
return of(SimpleModuleIdentifier.of(module), version);
}

static SimpleModuleVersionIdentifier of(String group, String name, String version) {
return of(SimpleModuleIdentifier.of(group, name), version);
}

static SimpleModuleVersionIdentifier of(String group, String name, String version, @Nullable String classifier, String extension) {
return of(SimpleModuleIdentifier.of(group, name), version, classifier, extension);
}

static SimpleModuleVersionIdentifier of(String artifact) {
var split = artifact.split(":", 4);
var group = split[0];
var name = split[1];

String version;
@Nullable String classifier = null;
String extension = "jar";

// Check if version has @ before :
if (split[2].indexOf('@') > 0) {
if (split.length > 3)
throw new IllegalArgumentException("Invalid module version identifier (found @ character before another : character): " + artifact);

var s = split[2].split("@");
version = s[0];
extension = s[1];
} else {
version = split[2];
}

// Check if classifier has an @
if (split.length > 3) {
if (split[3].indexOf('@') > 0) {
var s = split[2].split("@");
classifier = s[0];
extension = s[1];
} else {
classifier = split[3];
}
}

return of(group, name, version, classifier, extension);
}
}

public static SimpleModuleVersionIdentifier moduleOf(String artifact) {
return SimpleModuleVersionIdentifier.of(artifact);
}

public static SimpleModuleIdentifier moduleOf(String group, String name) {
return SimpleModuleIdentifier.of(group, name);
}

public static SimpleModuleVersionIdentifier moduleOf(String group, String name, String version) {
return SimpleModuleVersionIdentifier.of(group, name, version);
}

public static SimpleModuleVersionIdentifier moduleOf(ModuleIdentifier module, String version) {
return SimpleModuleVersionIdentifier.of(module, version);
}

public static String dependencyToArtifactString(Dependency dependency) {
var builder = new StringBuilder();

Expand Down Expand Up @@ -437,7 +562,7 @@ public static Comparator<String> versionComparator() {
public static Class<? extends Comparator<String>> versionComparatorClass() {
return StaticVersionComparator.class;
}
//endergion
//endregion

//region Domain Object Handling

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

import org.gradle.api.Action;
import org.gradle.api.Named;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Property;
Expand All @@ -23,38 +24,61 @@
public sealed interface Tool extends Named, Serializable permits ToolInternal, Tool.Resolved {
/// Creates a new tool with the given information.
///
/// @param name The name for this tool (will be used in the file name)
/// @param version The version for this tool (will be used in the file name)
/// @param downloadUrl The download URL for this tool
/// @param name The name for this tool, used to reference it in configuration and for the file name
/// @param artifact The artifact for this tool, used to get the download URL
/// @param mavenUrl The maven URL this tool is hosted on (if protocol is omitted, prepends `https://`, and
/// appends adds trailing slash if missing) (default: `https://maven.minecraftforge.net/`)
/// @param javaVersion The Java version this tool was built with, or should run on
/// @param mainClass The main class to use when executing this tool
/// @param mainClass The main class to use when executing this tool (optional)
/// @return The tool
static Tool of(String name, String version, String downloadUrl, int javaVersion, String mainClass) {
return new ToolImpl(name, version, downloadUrl, javaVersion, mainClass);
static Tool of(String name, String artifact, String mavenUrl, int javaVersion, String mainClass) {
return new ToolImpl(name, artifact, mavenUrl, javaVersion, mainClass);
}

/// Creates a new tool with the given information.
///
/// @param name The name for this tool (will be used in the file name)
/// @param version The version for this tool (will be used in the file name)
/// @param downloadUrl The download URL for this tool
/// @param name The name for this tool, used to reference it in configuration and for the file name
/// @param artifact The artifact for this tool, used to get the download URL
/// @param javaVersion The Java version this tool was built with, or should run on
/// @param mainClass The main class to use when executing this tool (optional)
/// @return The tool
static Tool of(String name, String version, String downloadUrl, int javaVersion) {
return new ToolImpl(name, version, downloadUrl, javaVersion, null);
static Tool of(String name, String artifact, int javaVersion, String mainClass) {
return new ToolImpl(name, artifact, null, javaVersion, mainClass);
}

/// Creates a new tool with the given information.
///
/// @param name The name for this tool, used to reference it in configuration and for the file name
/// @param artifact The artifact for this tool, used to get the download URL
/// @param mavenUrl The maven URL this tool is hosted on (if protocol is omitted, prepends `https://`, and
/// appends adds trailing slash if missing) (default: `https://maven.minecraftforge.net/`)
/// @param javaVersion The Java version this tool was built with, or should run on
/// @return The tool
static Tool of(String name, String artifact, String mavenUrl, int javaVersion) {
return new ToolImpl(name, artifact, mavenUrl, javaVersion, null);
}

/// Creates a new tool with the given information.
///
/// @param name The name for this tool, used to reference it in configuration and for the file name
/// @param artifact The artifact for this tool, used to get the download URL
/// @param javaVersion The Java version this tool was built with, or should run on
/// @return The tool
static Tool of(String name, String artifact, int javaVersion) {
return new ToolImpl(name, artifact, null, javaVersion, null);
}

/// The module for this tool.
///
/// @return The module for this tool
ModuleVersionIdentifier getModule();

/// The name for this tool. Primarily used by [ToolExecBase] to create a default tool directory.
///
/// @return The name of this tool
@Override
String getName();

/// The version of this tool.
///
/// @return The version of this tool
String getVersion();

/// The Java version this tool was built with. Primarily used by [ToolExecBase] to determine the
/// [org.gradle.jvm.toolchain.JavaLauncher].
///
Expand Down
Loading