Skip to content

Commit

Permalink
Production run tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Jan 1, 2025
1 parent 454e32e commit 5661d39
Show file tree
Hide file tree
Showing 6 changed files with 416 additions and 1 deletion.
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,178 @@
/*
* 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;

@ApiStatus.Experimental
public abstract class AbstractProductionRunTask extends AbstractLoomTask {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractProductionRunTask.class);

@Classpath
public abstract ConfigurableFileCollection getMods();

@Input
public abstract ListProperty<String> getJvmArgs();

@Input
public abstract ListProperty<String> getProgramArgs();

@OutputDirectory
public abstract DirectoryProperty getRunDir();

@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,90 @@
/*
* 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;

@ApiStatus.Experimental
public abstract 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()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.util.stream.Stream;

import javax.inject.Inject;

import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputFile;
import org.jetbrains.annotations.ApiStatus;

import net.fabricmc.loom.util.ZipUtils;

@ApiStatus.Experimental
public abstract class ServerProductionRunTask extends AbstractProductionRunTask {
@Input
public abstract Property<String> getLoaderVersion();

@Input
public abstract Property<String> getMinecraftVersion();

@Input
public abstract Property<String> getInstallerVersion();

// Internal options

@ApiStatus.Internal
@OutputFile
public abstract RegularFileProperty getInstallPropertiesJar();

@Inject
public ServerProductionRunTask() {
getLoaderVersion().convention(getProjectLoaderVersion());
getMinecraftVersion().convention(getExtension().getMinecraftVersion());
getInstallPropertiesJar().convention(getProject().getLayout().getBuildDirectory().file("server_properties.jar"));

getMainClass().convention("net.fabricmc.installer.ServerLauncher");
getClasspath().from(detachedConfigurationProvider("net.fabricmc:fabric-installer:%s:server", getInstallerVersion()));

getProgramArgs().add("nogui");
}

@Override
public void run() throws IOException {
ZipUtils.add(
getInstallPropertiesJar().get().getAsFile().toPath(),
"install.properties",
"fabric-loader-version=%s\ngame-version=%s".formatted(getLoaderVersion().get(), getMinecraftVersion().get())
);

super.run();
}

@Override
protected Stream<File> streamClasspath() {
return Stream.concat(
super.streamClasspath(),
Stream.of(getInstallPropertiesJar().get().getAsFile())
);
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/fabricmc/loom/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public static final class Configurations {
*/
public static final String LOCAL_RUNTIME = "localRuntime";
public static final String NAMED_ELEMENTS = "namedElements";
/**
* The configuration that contains the Minecraft client and loader runtime libraries, as used by the production run tasks.
*/
public static final String MINECRAFT_TEST_CLIENT_RUNTIME_LIBRARIES = "minecraftTestClientRuntimeLibraries";

private Configurations() {
}
Expand Down
Loading

0 comments on commit 5661d39

Please sign in to comment.