Skip to content

Commit

Permalink
Don't feed TransformedJars everywhere
Browse files Browse the repository at this point in the history
Let's be a little more explicit here. This should make it easier to work
with NF, where we don't have a separate client jar.
  • Loading branch information
SquidDev committed Jun 11, 2024
1 parent eec0a8b commit a698898
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cc.tweaked.vanillaextract.api.MappingsConfiguration;
import cc.tweaked.vanillaextract.api.VanillaMinecraftExtension;
import cc.tweaked.vanillaextract.configurations.MinecraftJar;
import cc.tweaked.vanillaextract.configurations.MinecraftConfiguration;
import cc.tweaked.vanillaextract.core.mappings.MappingProvider;
import cc.tweaked.vanillaextract.core.mappings.MojangMappings;
import cc.tweaked.vanillaextract.core.mappings.ParchmentMappings;
Expand Down Expand Up @@ -136,7 +136,7 @@ public void unpickImpl(Provider<? extends ModuleDependency> dependency) {
// Now add a dependency on the :constants jar. This includes some classes defining additional constants. We
// need on the classpath for Unpick to work, so let's add them in.
var constantsDependency = Dependencies.withClassifier(dependency, "constants");
project.getDependencies().addProvider(MinecraftJar.COMMON.getCompileConfigurationName(), constantsDependency);
project.getDependencies().addProvider(MinecraftJar.CLIENT_ONLY.getCompileConfigurationName(), constantsDependency);
project.getDependencies().addProvider(MinecraftConfiguration.COMMON.getCompileConfigurationName(), constantsDependency);
project.getDependencies().addProvider(MinecraftConfiguration.CLIENT_ONLY.getCompileConfigurationName(), constantsDependency);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.tweaked.vanillaextract;

import cc.tweaked.vanillaextract.api.VanillaMinecraftExtension;
import cc.tweaked.vanillaextract.configurations.MinecraftConfiguration;
import cc.tweaked.vanillaextract.configurations.MinecraftSetup;
import cc.tweaked.vanillaextract.core.mappings.MappingProvider;
import cc.tweaked.vanillaextract.core.minecraft.TransformedMinecraftProvider;
Expand All @@ -10,6 +11,7 @@
import org.gradle.api.Project;
import org.gradle.api.file.FileSystemLocation;

import java.util.List;
import java.util.Set;

public abstract class VanillaPlugin extends CommonPlugin {
Expand Down Expand Up @@ -45,8 +47,17 @@ public void apply(Project project) {
VanillaPlugin::configureMinecraft
));

new MinecraftSetup(project).setup(minecraft);
Decompile.setup(project, extension, minecraft);
// Set up the Minecraft configurations, and add our generated jars to their appropriate config.
var setup = new MinecraftSetup(project);
setup.setup();
setup.addDependency(MinecraftConfiguration.COMMON, minecraft.map(x -> x.common().release().coordinate()), true, true);
setup.addDependency(MinecraftConfiguration.CLIENT_ONLY, minecraft.map(x -> x.clientOnly().release().coordinate()), true, true);

// Set up the decompile task, with our two jars.
Decompile.setup(project, extension, List.of(
new Decompile.Target(MinecraftConfiguration.COMMON, minecraft.map(x -> x.common().path().toFile())),
new Decompile.Target(MinecraftConfiguration.CLIENT_ONLY, minecraft.map(x -> x.clientOnly().path().toFile()))
));
}

private static TransformedMinecraftProvider.TransformedJars configureMinecraft(GlobalMinecraftProvider service, String version, MappingProvider mappings, Set<FileSystemLocation> accessWideners) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package cc.tweaked.vanillaextract.configurations;

import cc.tweaked.vanillaextract.core.minecraft.TransformedMinecraftProvider;
import org.gradle.api.artifacts.Configuration;

/**
* A {@link Configuration} that Minecraft dependencies are added to.
*
* @see MinecraftSetup
*/
public enum MinecraftJar {
public enum MinecraftConfiguration {
COMMON("minecraftCommon"),
CLIENT_ONLY("minecraftClientOnly");

private final String compile;
private final String runtime;

MinecraftJar(String name) {
MinecraftConfiguration(String name) {
this.compile = name + "Compile";
this.runtime = name + "Runtime";
}

public TransformedMinecraftProvider.TransformedJar getJar(TransformedMinecraftProvider.TransformedJars jars) {
return switch (this) {
case COMMON -> jars.common();
case CLIENT_ONLY -> jars.clientOnly();
};
}

public String getCompileConfigurationName() {
return compile;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cc.tweaked.vanillaextract.configurations;

import cc.tweaked.vanillaextract.core.minecraft.TransformedMinecraftProvider;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -37,7 +36,7 @@
* <p>
* We also provide several convenience functions which may be used for custom build logic:
* <ul>
* <li>{@link #addMinecraftDependency(SourceSet, MinecraftJar)}: adds the Minecraft jar to other source sets.</li>
* <li>{@link #addMinecraftDependency(SourceSet, MinecraftConfiguration)}: adds the Minecraft jar to other source sets.</li>
* <li>
* {@link #extendClasspath(SourceSet, SourceSet)}: Perform some magic tricks to inherit dependencies between
* source sets.
Expand All @@ -59,12 +58,12 @@ public MinecraftSetup(Project project) {
this.sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
}

public void setup(Provider<TransformedMinecraftProvider.TransformedJars> jars) {
public void setup() {
createClientSources();
setupOutgoingConfigurations();

// Create a new runtime and compile-time configuration for the common/server and client jars.
for (var config : MinecraftJar.values()) {
for (var config : MinecraftConfiguration.values()) {
configurations.create(config.getCompileConfigurationName(), c -> {
c.setVisible(false);
c.setCanBeResolved(true); // Bit nasty, but needed for decompilation.
Expand All @@ -76,11 +75,6 @@ public void setup(Provider<TransformedMinecraftProvider.TransformedJars> jars) {
c.setCanBeResolved(false);
c.setCanBeConsumed(false);
});

// And add our Minecraft jar as a dependency for these configurations.
var dependency = jars.map(x -> config.getJar(x).release().coordinate());
project.getDependencies().addProvider(config.getCompileConfigurationName(), dependency);
project.getDependencies().addProvider(config.getRuntimeConfigurationName(), dependency);
}

// TODO: It would be nice to handle Minecraft's native dependencies in a dynamic way using Gradle's variants.
Expand All @@ -89,9 +83,22 @@ public void setup(Provider<TransformedMinecraftProvider.TransformedJars> jars) {
// which we might be able to expand.

// Then add these dependencies to the main, client and test source set.
addMinecraftDependency(sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME), MinecraftJar.COMMON);
addMinecraftDependency(sourceSets.getByName(CLIENT_SOURCE_SET_NAME), MinecraftJar.CLIENT_ONLY);
addMinecraftDependency(sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME), MinecraftJar.CLIENT_ONLY);
addMinecraftDependency(sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME), MinecraftConfiguration.COMMON);
addMinecraftDependency(sourceSets.getByName(CLIENT_SOURCE_SET_NAME), MinecraftConfiguration.CLIENT_ONLY);
addMinecraftDependency(sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME), MinecraftConfiguration.CLIENT_ONLY);
}

/**
* Add a dependency to one of our configurations.
*
* @param config The configuration to add this dependency to.
* @param dependency The dependency to add. This should be a provider with any dependency notation.
* @param compileTime Whether to add this as a compile-time dependency.
* @param runtime Whether to add this as a runtime dependency.
*/
public void addDependency(MinecraftConfiguration config, Provider<?> dependency, boolean compileTime, boolean runtime) {
if (compileTime) project.getDependencies().addProvider(config.getCompileConfigurationName(), dependency);
if (runtime) project.getDependencies().addProvider(config.getRuntimeConfigurationName(), dependency);
}

/**
Expand Down Expand Up @@ -282,7 +289,7 @@ private void setupOutgoingConfiguration(
* @param sourceSet The source set to add Minecraft to.
* @param configuration The Minecraft configuration to depend on.
*/
public void addMinecraftDependency(SourceSet sourceSet, MinecraftJar configuration) {
public void addMinecraftDependency(SourceSet sourceSet, MinecraftConfiguration configuration) {
extendsFrom(sourceSet.getCompileClasspathConfigurationName(), configuration.getCompileConfigurationName());
extendsFrom(sourceSet.getRuntimeClasspathConfigurationName(), configuration.getRuntimeConfigurationName());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cc.tweaked.vanillaextract.decompile;

import cc.tweaked.vanillaextract.MinecraftExtensionImpl;
import cc.tweaked.vanillaextract.configurations.MinecraftJar;
import cc.tweaked.vanillaextract.core.minecraft.TransformedMinecraftProvider;
import cc.tweaked.vanillaextract.configurations.MinecraftConfiguration;
import cc.tweaked.vanillaextract.core.unpick.UnpickMetadata;
import cc.tweaked.vanillaextract.utils.Dependencies;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.provider.Provider;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
Expand All @@ -24,9 +24,9 @@ public final class Decompile {
/**
* @param project The current project.
* @param extension The Minecraft extension, used to (re-)provision Minecraft and get Loom version.
* @param jars Our provisioned Minecraft jars.
* @param jars The classes to decompile.
*/
public static void setup(Project project, MinecraftExtensionImpl extension, Provider<TransformedMinecraftProvider.TransformedJars> jars) {
public static void setup(Project project, MinecraftExtensionImpl extension, List<Target> jars) {
// Create a new configuration and add Vineflower to it. This allows users to override Vineflower if needed.
var decompiler = project.getConfigurations().create(VINEFLOWER_CONFIGURATION_NAME, c -> {
c.setDescription("The Vineflower decompiler");
Expand Down Expand Up @@ -77,20 +77,26 @@ public static void setup(Project project, MinecraftExtensionImpl extension, Prov
task.getDecompilerClasspath().setFrom(decompiler);
task.getDecompilerClasspath().disallowChanges();

// Set each Minecraft jar as a target for the task.
var projectLayout = project.getLayout();
for (var configuration : MinecraftJar.values()) {
// Map the jar to a RegularFile, and get the configuration it belongs to. Then add them to the
// decompiler task.
var file = projectLayout.file(jars.map(x -> configuration.getJar(x).path().toFile()));
var classpath = project.getConfigurations().getByName(configuration.getCompileConfigurationName());
for (var jar : jars) {
var file = jar.file();
var classpath = task.getProject().getConfigurations().getByName(jar.configuration().getCompileConfigurationName());

task.addTarget(t -> {
t.getInputFile().set(file);
t.getClasspath().from(classpath.minus(projectLayout.files(file)));
t.getInputFile().fileProvider(file);
t.getClasspath().from(classpath.minus(task.getProject().getLayout().files(file)));
});
}
task.getTargets().disallowChanges();
});
}

/**
* A target jar that we should decompile.
*
* @param configuration The Minecraft configuration this jar exists in.
* @param file The file to decompile.
* @see DecompileTask.Target
*/
public record Target(MinecraftConfiguration configuration, Provider<File> file) {
}
}

0 comments on commit a698898

Please sign in to comment.