-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
416 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
src/main/java/net/fabricmc/loom/task/prod/AbstractProductionRunTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/net/fabricmc/loom/task/prod/ClientProductionRunTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/net/fabricmc/loom/task/prod/ServerProductionRunTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.