Skip to content

[FG7] Add a way to configure Tools (Mavenizer/Slimelauncher) #971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: FG_7.0
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions src/main/groovy/net/minecraftforge/gradle/ForgeGradlePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.ArchiveOperations
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.ProjectLayout
import org.gradle.api.flow.FlowProviders
Expand Down Expand Up @@ -82,6 +83,14 @@ class ForgeGradlePlugin implements Plugin<ExtensionAware> {
this.&getFileSystemOperations,
this.&getArchiveOperations
)

if (target instanceof Project) {
Project project = (Project) target;

for (final def tool in Tools.values()) {
project.getConfigurations().register(tool.getConfiguration())
}
}
}

@PackageScope DirectoryProperty getGlobalCaches() {
Expand All @@ -93,10 +102,16 @@ class ForgeGradlePlugin implements Plugin<ExtensionAware> {
}

@SuppressWarnings('GrDeprecatedAPIUsage') // Intentional deprecation, please use this method
@PackageScope Provider<File> getTool(Tools tool) {
tool.get(this.globalCaches, this.providers)
@PackageScope FileCollection getTool(Tools tool) {
objects.fileCollection().from(tool.get(this.globalCaches, this.providers))
}

@SuppressWarnings('GrDeprecatedAPIUsage') // Intentional deprecation, please use this method
@PackageScope FileCollection getTool(Tools tool, Project project) {
tool.get(project, this.globalCaches, this.providers)
}


@CompileDynamic
private File getGradleUserHomeDir(ExtensionAware target) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/
package net.minecraftforge.gradle;

import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Internal;

Expand All @@ -17,10 +19,15 @@ private ForgeGradlePlugin getPlugin() {
}

@Internal
default Provider<File> getTool(Tools tool) {
default FileCollection getTool(Tools tool) {
return this.getPlugin().getTool(tool);
}

@Internal
default FileCollection getTool(Tools tool, Project project) {
return this.getPlugin().getTool(tool, project);
}

@Internal
default DirectoryProperty getGlobalCaches() {
return this.getPlugin().getGlobalCaches();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import java.nio.file.Files
task.inherit(configs, options.name)
options.apply(task)

task.classpath task.getTool(Tools.SLIME_LAUNCHER)
task.classpath task.getTool(Tools.SLIME_LAUNCHER, project)

if (task.buildAllProjects)
task.dependsOn project.allprojects.collect { it.tasks.named(LifecycleBasePlugin.ASSEMBLE_TASK_NAME) }.toArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SyncMinecraftMaven(Problems problems, ObjectFactory objects, ProviderFact
this.setDescription("Syncs the Minecraft dependencies using Minecraft Mavenizer.");

// JavaExec
this.getExecutable().convention(this.getTool(Tools.MINECRAFT_MAVEN));
this.getExecutable().convention(this.getTool(Tools.MINECRAFT_MAVEN, getProject()));
this.getJavaLauncher().convention(Util.launcherForStrictly(this.getProject().getExtensions().getByType(JavaToolchainService.class), Constants.MCMAVEN_JAVA_VERSION).map(j -> j.getExecutablePath().toString()));
this.getMainClass().convention(Constants.MCMAVEN_MAIN);

Expand Down
38 changes: 35 additions & 3 deletions src/main/groovy/net/minecraftforge/gradle/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import net.minecraftforge.util.download.DownloadUtils;
import net.minecraftforge.util.hash.HashStore;
import org.gradle.api.Project;
import org.gradle.api.artifacts.UnknownConfigurationException;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
Expand All @@ -25,15 +28,17 @@
import static net.minecraftforge.gradle.ForgeGradlePlugin.LOGGER;

enum Tools {
MINECRAFT_MAVEN("minecraft-maven-" + Constants.MCMAVEN_VERSION + ".jar", Constants.MCMAVEN_DL_URL),
SLIME_LAUNCHER("slime-launcher-" + Constants.SL_VERSION + ".jar", Constants.SL_DL_URL);
MINECRAFT_MAVEN("minecraft-maven-" + Constants.MCMAVEN_VERSION + ".jar", Constants.MCMAVEN_DL_URL, "mavenizer"),
SLIME_LAUNCHER("slime-launcher-" + Constants.SL_VERSION + ".jar", Constants.SL_DL_URL, "slimelauncher");

private final String fileName;
private final String downloadUrl;
private final String configuration;

Tools(String fileName, String downloadUrl) {
Tools(String fileName, String downloadUrl, String configuration) {
this.fileName = fileName;
this.downloadUrl = downloadUrl;
this.configuration = configuration;
}

/// Gets a provider for this tool using the given caches directory and provider factory.
Expand All @@ -51,6 +56,33 @@ Provider<File> get(DirectoryProperty cachesDir, ProviderFactory providers) {
}));
}

/// Gets a provider for this tool using the configuration, otherwise default to {@link Tools#get(Project, DirectoryProperty, ProviderFactory)}
///
/// @param project The Project
/// @param cachesDir The caches directory to store the tool
/// @param providers The provider factory to use
/// @return A provider for the tool as a [file][File]
/// @deprecated Use [ForgeGradlePlugin#getTool(Tools, Project)] <- [org.gradle.api.plugins.PluginContainer#getPlugin(Class)]
/// <- [org.gradle.api.plugins.PluginAware#getPlugins()]
@Deprecated
FileCollection get(Project project, DirectoryProperty cachesDir, ProviderFactory providers) {
try {
final var cfg = project.getConfigurations().getByName(getConfiguration());
final var files = cfg.getFiles();
if (!files.isEmpty()) {
return project.getObjects().fileCollection().from(cfg.getFiles());
}
} catch (UnknownConfigurationException exception) {
System.out.println("[FG7] Unknown Configuration %s, using defaults for tool %s".formatted(getConfiguration(), toString()));
}

return project.getObjects().fileCollection().from(get(cachesDir, providers));
}

String getConfiguration() {
return configuration;
}

static abstract class Source implements ValueSource<File, Source.Parameters> {
interface Parameters extends ValueSourceParameters {
@InputFile @PathSensitive(PathSensitivity.ABSOLUTE) RegularFileProperty getInputFile();
Expand Down