Skip to content
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

Production run tasks #1241

Merged
merged 3 commits into from
Jan 2, 2025
Merged
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
4 changes: 3 additions & 1 deletion gradle/runtime.libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dev-launch-injector = "0.2.1+build.8"
terminal-console-appender = "1.3.0"
jetbrains-annotations = "25.0.0"
native-support = "1.0.1"
fabric-installer = "1.0.1"

[libraries]
# Decompilers
Expand All @@ -22,4 +23,5 @@ mixin-compile-extensions = { module = "net.fabricmc:fabric-mixin-compile-extensi
dev-launch-injector = { module = "net.fabricmc:dev-launch-injector", version.ref = "dev-launch-injector" }
terminal-console-appender = { module = "net.minecrell:terminalconsoleappender", version.ref = "terminal-console-appender" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
native-support = { module = "net.fabricmc:fabric-loom-native-support", version.ref = "native-support" }
native-support = { module = "net.fabricmc:fabric-loom-native-support", version.ref = "native-support" }
fabric-installer = { module = "net.fabricmc:fabric-installer", version.ref = "fabric-installer" }
4 changes: 1 addition & 3 deletions gradle/test.libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mixin = "0.15.3+mixin.0.8.7"

gradle-nightly = "8.13-20241222002427+0000"
fabric-loader = "0.16.9"
fabric-installer = "1.0.1"

[libraries]
spock = { module = "org.spockframework:spock-core", version.ref = "spock" }
Expand All @@ -19,5 +18,4 @@ mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" }
java-debug = { module = "com.microsoft.java:com.microsoft.java.debug.core", version.ref = "java-debug" }
mixin = { module = "net.fabricmc:sponge-mixin", version.ref = "mixin" }
gradle-nightly = { module = "org.gradle:dummy", version.ref = "gradle-nightly" }
fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-loader" }
fabric-installer = { module = "net.fabricmc:fabric-installer", version.ref = "fabric-installer" }
fabric-loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric-loader" }
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public void run() {
getDependencies().add(Constants.Configurations.LOOM_DEVELOPMENT_DEPENDENCIES, LoomVersions.TERMINAL_CONSOLE_APPENDER.mavenNotation());
getDependencies().add(JavaPlugin.COMPILE_ONLY_CONFIGURATION_NAME, LoomVersions.JETBRAINS_ANNOTATIONS.mavenNotation());
getDependencies().add(JavaPlugin.TEST_COMPILE_ONLY_CONFIGURATION_NAME, LoomVersions.JETBRAINS_ANNOTATIONS.mavenNotation());

register(Constants.Configurations.MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES, Role.RESOLVABLE);
extendsFrom(Constants.Configurations.MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES, Constants.Configurations.MINECRAFT_NATIVES);
extendsFrom(Constants.Configurations.MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES, Constants.Configurations.MINECRAFT_CLIENT_RUNTIME_LIBRARIES);
extendsFrom(Constants.Configurations.MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES, Constants.Configurations.LOADER_DEPENDENCIES);
}

private NamedDomainObjectProvider<Configuration> register(String name, Role role) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.task.prod;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.inject.Inject;

import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.JavaToolchainSpec;
import org.gradle.process.ExecOperations;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.jetbrains.annotations.ApiStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.fabricmc.loom.configuration.InstallerData;
import net.fabricmc.loom.task.AbstractLoomTask;
import net.fabricmc.loom.task.RemapTaskConfiguration;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.gradle.GradleUtils;

/**
* This is the base task for running the game in a "production" like environment. Using intermediary names, and not enabling development only features.
*
* <p>Do not use this task directly, use {@link ClientProductionRunTask} or {@link ServerProductionRunTask} instead.
*/
@ApiStatus.Experimental
public abstract sealed class AbstractProductionRunTask extends AbstractLoomTask permits ClientProductionRunTask, ServerProductionRunTask {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractProductionRunTask.class);

/**
* A collection of mods that will be used when running the game. The mods must be remapped to run with intermediary names.
*
* <p>By default this includes the remapped jar.
*/
@Classpath
public abstract ConfigurableFileCollection getMods();

/**
* A list of additional JVM arguments to pass to the game.
*/
@Input
public abstract ListProperty<String> getJvmArgs();

/**
* A list of additional program arguments to pass to the game.
*/
@Input
public abstract ListProperty<String> getProgramArgs();

/**
* The directory to run the game in.
*/
@OutputDirectory
public abstract DirectoryProperty getRunDir();

/**
* The {@link JavaLauncher} to use when running the game, this can be used to specify a specific Java version to use.
*
* <p>See: <a href="https://docs.gradle.org/current/userguide/toolchains.html#sec:plugins_toolchains">Java Toolchains</a>
* @return
*/
@Nested
public abstract Property<JavaLauncher> getJavaLauncher();

// Internal options
@ApiStatus.Internal
@Classpath
protected abstract ConfigurableFileCollection getClasspath();

@ApiStatus.Internal
@Input
protected abstract Property<String> getMainClass();

@Inject
protected abstract ExecOperations getExecOperations();

@Inject
protected abstract JavaToolchainService getJavaToolchainService();

@Inject
public AbstractProductionRunTask() {
JavaToolchainSpec defaultToolchain = getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain();
getJavaLauncher().convention(getJavaToolchainService().launcherFor(defaultToolchain));
getRunDir().convention(getProject().getLayout().getProjectDirectory().dir("run"));

if (!GradleUtils.getBooleanProperty(getProject(), Constants.Properties.DONT_REMAP)) {
getMods().from(getProject().getTasks().named(RemapTaskConfiguration.REMAP_JAR_TASK_NAME));
}
}

@TaskAction
public void run() throws IOException {
Files.createDirectories(getRunDir().get().getAsFile().toPath());

ExecResult result = getExecOperations().exec(exec -> {
configureCommand(exec);
configureJvmArgs(exec);
configureClasspath(exec);
configureMainClass(exec);
configureProgramArgs(exec);

exec.setWorkingDir(getRunDir());

LOGGER.debug("Running command: {}", exec.getCommandLine());
});
result.assertNormalExitValue();
}

protected void configureCommand(ExecSpec exec) {
exec.commandLine(getJavaLauncher().get().getExecutablePath());
}

protected void configureJvmArgs(ExecSpec exec) {
exec.args(getJvmArgs().get());
exec.args("-Dfabric.addMods=" + joinFiles(getMods().getFiles().stream()));
}

protected Stream<File> streamClasspath() {
return getClasspath().getFiles().stream();
}

protected void configureClasspath(ExecSpec exec) {
exec.args("-cp");
exec.args(joinFiles(streamClasspath()));
}

protected void configureMainClass(ExecSpec exec) {
exec.args(getMainClass().get());
}

protected void configureProgramArgs(ExecSpec exec) {
exec.args(getProgramArgs().get());
}

@Internal
protected Provider<String> getProjectLoaderVersion() {
return getProject().provider(() -> {
InstallerData installerData = getExtension().getInstallerData();

if (installerData == null) {
return null;
}

return installerData.version();
});
}

protected Provider<Configuration> detachedConfigurationProvider(String mavenNotation, Provider<String> versionProvider) {
return versionProvider.map(version -> {
Dependency serverLauncher = getProject().getDependencies().create(mavenNotation.formatted(version));
return getProject().getConfigurations().detachedConfiguration(serverLauncher);
});
}

private static String joinFiles(Stream<File> stream) {
return stream.map(File::getAbsolutePath)
.collect(Collectors.joining(File.pathSeparator));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2025 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.task.prod;

import java.io.File;

import javax.inject.Inject;

import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.process.ExecSpec;
import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.Platform;

/**
* A task that runs the Minecraft client in a similar way to a production launcher. You must manually register a task of this type to use it.
*/
@ApiStatus.Experimental
public abstract non-sealed class ClientProductionRunTask extends AbstractProductionRunTask {
// Internal options
@Input
protected abstract Property<String> getAssetsIndex();

@InputFiles
protected abstract DirectoryProperty getAssetsDir();

@Inject
public ClientProductionRunTask() {
getAssetsIndex().set(getExtension().getMinecraftVersion()
.map(minecraftVersion -> getExtension()
.getMinecraftProvider()
.getVersionInfo()
.assetIndex()
.fabricId(minecraftVersion)
)
);
getAssetsDir().set(new File(getExtension().getFiles().getUserCache(), "assets"));
getMainClass().convention("net.fabricmc.loader.impl.launch.knot.KnotClient");

getClasspath().from(getExtension().getMinecraftProvider().getMinecraftClientJar());
getClasspath().from(detachedConfigurationProvider("net.fabricmc:fabric-loader:%s", getProjectLoaderVersion()));
getClasspath().from(detachedConfigurationProvider("net.fabricmc:intermediary:%s", getExtension().getMinecraftVersion()));
getClasspath().from(getProject().getConfigurations().named(Constants.Configurations.MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES));

dependsOn("downloadAssets");
}

@Override
protected void configureJvmArgs(ExecSpec exec) {
super.configureJvmArgs(exec);

if (Platform.CURRENT.getOperatingSystem().isMacOS()) {
exec.args("-XstartOnFirstThread");
}
}

@Override
protected void configureProgramArgs(ExecSpec exec) {
super.configureProgramArgs(exec);

exec.args(
"--assetIndex", getAssetsIndex().get(),
"--assetsDir", getAssetsDir().get().getAsFile().getAbsolutePath(),
"--gameDir", getRunDir().get().getAsFile().getAbsolutePath()
);
}
}
Loading