Skip to content

Commit

Permalink
Update to NFRT 1.0 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte authored Jul 21, 2024
1 parent 48c3ef4 commit ad1468a
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* the Minecraft artifacts for compiling and mods.
*/
public abstract class NeoFormRuntime {
private static final String DEFAULT_NFRT_VERSION = "0.1.70";
private static final String DEFAULT_NFRT_VERSION = "1.0.0";

@Inject
public NeoFormRuntime(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -20,9 +21,14 @@
* using the NFRT CLI.
*/
@DisableCachingByDefault(because = "Implements its own caching")
abstract class CreateMinecraftArtifactsTask extends NeoFormRuntimeEngineTask {
abstract class CreateMinecraftArtifactsTask extends NeoFormRuntimeTask {
@Inject
public CreateMinecraftArtifactsTask() {
// When cache is disabled, the task is NEVER up-to-date to aid with debugging problems
getOutputs().upToDateWhen(task -> ((CreateMinecraftArtifactsTask) task).getEnableCache().get());
getEnableCache().convention(false);
getUseEclipseCompiler().convention(false);
getAnalyzeCacheMisses().convention(false);
}

@InputFiles
Expand Down Expand Up @@ -59,6 +65,34 @@ public CreateMinecraftArtifactsTask() {
@OutputFile
abstract RegularFileProperty getResourcesArtifact();

/**
* Gradle dependency notation for the NeoForge userdev artifact.
* Either this or {@link #getNeoFormArtifact()} must be specified.
*/
@Input
@Optional
abstract Property<String> getNeoForgeArtifact();

/**
* Gradle dependency notation for the NeoForm data artifact.
* Either this or {@link #getNeoForgeArtifact()} must be specified.
*/
@Input
@Optional
abstract Property<String> getNeoFormArtifact();

/**
* Enables use of the cache.
*/
@Internal
abstract Property<Boolean> getEnableCache();

@Internal
abstract Property<Boolean> getAnalyzeCacheMisses();

@Input
abstract Property<Boolean> getUseEclipseCompiler();

@TaskAction
public void createArtifacts() {
var args = new ArrayList<String>();
Expand Down Expand Up @@ -94,6 +128,29 @@ public void createArtifacts() {
}
}

if (!getEnableCache().get()) {
args.add("--disable-cache");
}

if (getAnalyzeCacheMisses().get()) {
args.add("--analyze-cache-misses");
}

if (getUseEclipseCompiler().get()) {
args.add("--use-eclipse-compiler");
}

// Note that it is possible to specify both
if (getNeoForgeArtifact().isPresent()) {
Collections.addAll(args, "--neoforge", getNeoForgeArtifact().get());
}
if (getNeoFormArtifact().isPresent()) {
Collections.addAll(args, "--neoform", getNeoFormArtifact().get());
}
if (!getNeoFormArtifact().isPresent() && !getNeoForgeArtifact().isPresent()) {
throw new GradleException("You need to specify at least 'version' or 'neoFormVersion' in the 'neoForge' block of your build script.");
}

Collections.addAll(
args,
"--dist", "joined",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,95 @@
package net.neoforged.moddevgradle.internal;

import org.gradle.api.GradleException;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import javax.inject.Inject;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

/**
* Use the NFRT CLI to download the asset index and assets for the Minecraft version used by the
* underlying NeoForge/NeoForm configuration.
*/
abstract class DownloadAssetsTask extends NeoFormRuntimeEngineTask {
abstract class DownloadAssetsTask extends NeoFormRuntimeTask {
@Inject
public DownloadAssetsTask() {
}

/**
* Gradle dependency notation for the NeoForm data artifact, from which a Minecraft version will be derived.
* <p>
* To determine the minecraft version, the following properties will be checked in-order and the first one will be used:
* <ol>
* <li>{@link #getMinecraftVersion()}</li>
* <li>{@link #getNeoFormArtifact()}</li>
* <li>this property</li>
* </ol>
*/
@Input
@Optional
abstract Property<String> getNeoForgeArtifact();

/**
* Gradle dependency notation for the NeoForm data artifact, from which a Minecraft version will be derived.
* <p>
* To determine the minecraft version, the following properties will be checked in-order and the first one will be used:
* <ol>
* <li>{@link #getMinecraftVersion()}</li>
* <li>this property</li>
* <li>{@link #getNeoForgeArtifact()}</li>
* </ol>
*/
@Input
@Optional
abstract Property<String> getNeoFormArtifact();

/**
* The Minecraft version to download the assets for.
* <p>
* To determine the minecraft version, the following properties will be checked in-order and the first one will be used:
* <ol>
* <li>this property</li>
* <li>{@link #getNeoFormArtifact()}</li>
* <li>{@link #getNeoForgeArtifact()}</li>
* </ol>
*/
@Input
@Optional
abstract Property<String> getMinecraftVersion();

/**
* A properties file will be written to this location which can be read by the runtime tasks
* to determine where the asset index and asset root are located.
*/
@OutputFile
abstract RegularFileProperty getAssetPropertiesFile();

@TaskAction
public void createArtifacts() {
run(List.of(

var args = new ArrayList<String>();
Collections.addAll(args,
"download-assets",
"--output-properties-to", getAssetPropertiesFile().get().getAsFile().getAbsolutePath()
));
"--write-properties", getAssetPropertiesFile().get().getAsFile().getAbsolutePath()
);

// Only one should be specified, we try to use the best one.
if (getMinecraftVersion().isPresent()) {
Collections.addAll(args, "--minecraft-version", getMinecraftVersion().get());
} else if (getNeoFormArtifact().isPresent()) {
Collections.addAll(args, "--neoform", getNeoFormArtifact().get());
} else if (getNeoForgeArtifact().isPresent()) {
Collections.addAll(args, "--neoforge", getNeoForgeArtifact().get());
} else {
throw new GradleException("One of minecraftVersion, neoFormArtifact or neoForgeArtifact must be specified to download assets.");
}

run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,10 @@ public void apply(Project project) {
});

// Configure common properties of NeoFormRuntimeEngineTask
Consumer<NeoFormRuntimeEngineTask> configureEngineTask = task -> {
Consumer<NeoFormRuntimeTask> configureNfrtTask = task -> {
var nfrtSettings = extension.getNeoFormRuntime();
task.getVerbose().set(nfrtSettings.getVerbose());
task.getEnableCache().set(nfrtSettings.getEnableCache());
task.getAnalyzeCacheMisses().set(nfrtSettings.getAnalyzeCacheMisses());
task.getUseEclipseCompiler().set(nfrtSettings.getUseEclipseCompiler());
task.getArtifactManifestFile().set(createManifest.get().getManifestFile());
task.getNeoForgeArtifact().set(extension.getVersion().map(version -> "net.neoforged:neoforge:" + version));
task.getNeoFormArtifact().set(extension.getNeoFormVersion().map(version -> "net.neoforged:neoform:" + version + "@zip"));
task.getNeoFormRuntime().from(neoFormRuntimeConfig);
};

Expand Down Expand Up @@ -235,15 +230,24 @@ public void apply(Project project) {
task.getSourcesArtifact().set(jarPathFactory.apply("-sources"));
task.getResourcesArtifact().set(jarPathFactory.apply("-resources-aka-client-extra"));

configureEngineTask.accept(task);
var nfrtSettings = extension.getNeoFormRuntime();
task.getEnableCache().set(nfrtSettings.getEnableCache());
task.getAnalyzeCacheMisses().set(nfrtSettings.getAnalyzeCacheMisses());
task.getUseEclipseCompiler().set(nfrtSettings.getUseEclipseCompiler());
task.getNeoForgeArtifact().set(getNeoForgeUserDevDependencyNotation(extension));
task.getNeoFormArtifact().set(getNeoFormDataDependencyNotation(extension));

configureNfrtTask.accept(task);
});

var downloadAssets = tasks.register("downloadAssets", DownloadAssetsTask.class, task -> {
// Not in the internal group in case someone wants to "preload" the asset before they go offline
task.setGroup(TASK_GROUP);
task.setDescription("Downloads the Minecraft assets and asset index needed to run a Minecraft client or generate client-side resources.");
task.getAssetPropertiesFile().set(modDevBuildDir.map(dir -> dir.file("minecraft_assets.properties")));
configureEngineTask.accept(task);
task.getNeoForgeArtifact().set(getNeoForgeUserDevDependencyNotation(extension));
task.getNeoFormArtifact().set(getNeoFormDataDependencyNotation(extension));
configureNfrtTask.accept(task);
});

// For IntelliJ, we attach a combined sources+classes artifact which enables an "Attach Sources..." link for IJ users
Expand Down Expand Up @@ -443,6 +447,14 @@ public void apply(Project project) {
configureEclipseModel(project, ideSyncTask, createArtifacts, extension, prepareRunTasks);
}

private static Provider<String> getNeoFormDataDependencyNotation(NeoForgeExtension extension) {
return extension.getNeoFormVersion().map(version -> "net.neoforged:neoform:" + version + "@zip");
}

private static Provider<String> getNeoForgeUserDevDependencyNotation(NeoForgeExtension extension) {
return extension.getVersion().map(version -> "net.neoforged:neoforge:" + version + ":userdev");
}

/**
* Collects all dependencies needed by the NeoFormRuntime
*/
Expand Down

This file was deleted.

Loading

0 comments on commit ad1468a

Please sign in to comment.