Skip to content

Commit

Permalink
[Tech-Debt] Resolve the issue where Mixin Extras does not appear on t…
Browse files Browse the repository at this point in the history
…he CP (#176)
  • Loading branch information
marchermans authored May 17, 2024
1 parent 0fa3c01 commit 8d2e3e3
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,14 @@ Entry createDummyDependency(final Dependency dependency, final ReplacementResult
}

// Create a new repository entry for the dependency, using the replacement result.
final ExternalModuleDependency externalModuleDependency = (ExternalModuleDependency) dependency;
ExternalModuleDependency externalModuleDependency = (ExternalModuleDependency) dependency;
//Check if the result is replacement aware.
if (result instanceof ReplacementAware) {
final ReplacementAware replacementAware = (ReplacementAware) result;
//Let it alter the dependency, this allows support for version ranges, and strict versioning.
externalModuleDependency = replacementAware.getReplacementDependency(externalModuleDependency);
}

final Repository extension = project.getExtensions().getByType(Repository.class);
return extension.withEntry(
project.getObjects().newInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ public Project getProject() {
}

private ArtifactRepository createRepositories() {
return project.getRepositories().ivy(repositoryConfiguration(
final ArtifactRepository repository = project.getRepositories().ivy(repositoryConfiguration(
"NeoGradle Artifacts",
getRepositoryDirectory()
));

project.getRepositories().remove(repository);
project.getRepositories().addFirst(repository);

return repository;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.neoforged.gradle.dsl.common.extensions.dependency.replacement

import net.neoforged.gradle.dsl.common.tasks.WithOutput
import org.gradle.api.artifacts.ExternalModuleDependency
import org.gradle.api.tasks.TaskProvider

/**
Expand All @@ -18,4 +19,12 @@ interface ReplacementAware {
TaskProvider<? extends WithOutput> copiesRawJar,
TaskProvider<? extends WithOutput> copiesMappedJar
);

/**
* Gets the replacement dependency for the given dependency.
*
* @param externalModuleDependency The dependency to get the replacement for.
* @return The replacement dependency.
*/
ExternalModuleDependency getReplacementDependency(ExternalModuleDependency externalModuleDependency)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import net.neoforged.gradle.common.util.constants.RunsConstants;
import net.neoforged.gradle.dsl.common.runs.ide.extensions.IdeaRunExtension;
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.userdev.extension.UserDev;
import net.neoforged.gradle.neoform.NeoFormPlugin;
import net.neoforged.gradle.platform.extensions.DynamicProjectExtension;
import net.neoforged.gradle.platform.runtime.runtime.definition.RuntimeDevRuntimeDefinition;
import net.neoforged.gradle.userdev.extension.UserDevExtension;
import net.neoforged.gradle.userdev.runtime.extension.UserDevRuntimeExtension;
import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
Expand Down Expand Up @@ -45,8 +40,4 @@ private void configureRun(final Project project, final Run run) {

project.getExtensions().getByType(IdeManagementExtension.class).onIdea((project1, idea, ideaExtension) -> run.getExtensions().getByType(IdeaRunExtension.class).getPrimarySourceSet().convention(mainSourceSet));
}

private Provider<String> buildModClasses(final Project project, final Run run) {
return project.provider(() -> Stream.concat(run.getModSources().get().stream().map(source -> source.getOutput().getResourcesDir()), run.getModSources().get().stream().map(source -> source.getOutput().getClassesDirs().getFiles()).flatMap(Collection::stream)).map(File::getAbsolutePath).map(path -> String.format("minecraft%%%%%s", path)).collect(Collectors.joining(File.pathSeparator)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,125 @@ class FunctionalTests extends BuilderBasedTestSpecification {
run.task(':neoFormRecompile').outcome == TaskOutcome.SUCCESS
}

def "userdev supports version range resolution"() {
given:
def project = create("userdev_supports_version_ranges", {
it.build("""
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
implementation 'net.neoforged:neoforge:[20,)'
}
""")
it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """
package net.neoforged.gradle.userdev;
import net.minecraft.client.Minecraft;
public class FunctionalTests {
public static void main(String[] args) {
System.out.println(Minecraft.getInstance().getClass().toString());
}
}
""")
it.withToolchains()
})

when:
def run = project.run {
it.tasks('dependencies', "--configuration", "compileClasspath")
}

then:
run.task(':dependencies').outcome == TaskOutcome.SUCCESS
run.output.contains("\\--- net.neoforged:neoforge:")
}

def "userdev supports complex version resolution"() {
given:
def project = create("userdev_supports_complex_versions", {
it.build("""
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
implementation ('net.neoforged:neoforge') {
version {
strictly '[20.4.167, 20.5)'
require '20.4.188'
}
}
}
""")
it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """
package net.neoforged.gradle.userdev;
import net.minecraft.client.Minecraft;
public class FunctionalTests {
public static void main(String[] args) {
System.out.println(Minecraft.getInstance().getClass().toString());
}
}
""")
it.withToolchains()
})

when:
def run = project.run {
it.tasks('dependencies', "--configuration", "compileClasspath")
}

then:
run.task(':dependencies').outcome == TaskOutcome.SUCCESS
run.output.contains("\\--- net.neoforged:neoforge:20.4.188")
}

def "a mod with userdev as dependency has a mixin-extra dependency on the compile classpath"() {
given:
def project = create("userdev_adds_mixin_extra_on_compile_classpath", {
it.build("""
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
implementation 'net.neoforged:neoforge:+'
}
""")
it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """
package net.neoforged.gradle.userdev;
import net.minecraft.client.Minecraft;
public class FunctionalTests {
public static void main(String[] args) {
System.out.println(Minecraft.getInstance().getClass().toString());
}
}
""")
it.withToolchains()
})

when:
def run = project.run {
it.tasks('dependencies', "--configuration", "compileClasspath")
}

then:
run.task(':dependencies').outcome == TaskOutcome.SUCCESS
run.output.contains("+--- io.github.llamalad7:mixinextras-neoforge")
}

def "a mod with userdev as dependency and official mappings can compile through gradle"() {
given:
def project = create("compile_with_gradle_and_official_mappings", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import net.neoforged.gradle.common.extensions.DefaultJarJarFeature;
import net.neoforged.gradle.common.extensions.JarJarExtension;
import net.neoforged.gradle.dsl.common.extensions.JarJar;
import net.neoforged.gradle.dsl.userdev.extension.UserDev;
import net.neoforged.gradle.neoform.NeoFormPlugin;
import net.neoforged.gradle.userdev.dependency.UserDevDependencyManager;
import net.neoforged.gradle.userdev.extension.UserDevExtension;
import net.neoforged.gradle.userdev.runtime.extension.UserDevRuntimeExtension;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand All @@ -22,7 +20,6 @@ public class UserDevProjectPlugin implements Plugin<Project> {
public void apply(Project project) {
project.getPlugins().apply(NeoFormPlugin.class);

project.getExtensions().create(UserDev.class, "userDev", UserDevExtension.class, project);
project.getExtensions().create("userDevRuntime", UserDevRuntimeExtension.class, project);

UserDevDependencyManager.getInstance().apply(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package net.neoforged.gradle.userdev.dependency;

import com.google.common.collect.Sets;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement;
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.ReplacementResult;
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.dsl.common.util.DistributionType;
import net.neoforged.gradle.dsl.userdev.extension.UserDev;
import net.neoforged.gradle.userdev.runtime.definition.UserDevRuntimeDefinition;
import net.neoforged.gradle.userdev.runtime.extension.UserDevRuntimeExtension;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencyArtifact;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.provider.Provider;
import org.gradle.api.artifacts.*;

import java.util.Collections;
import java.util.Objects;
Expand Down Expand Up @@ -97,16 +89,21 @@ private static boolean hasSourcesArtifact(ExternalModuleDependency externalModul

private static UserDevRuntimeDefinition buildForgeUserDevRuntimeFrom(Project project, ExternalModuleDependency dependency) {
final UserDevRuntimeExtension forgeRuntimeExtension = project.getExtensions().getByType(UserDevRuntimeExtension.class);
final UserDev userDevExtension = project.getExtensions().getByType(UserDev.class);


return forgeRuntimeExtension.maybeCreateFor(dependency, builder -> {
final Provider<String> version = project.provider(dependency::getVersion).orElse(userDevExtension.getDefaultForgeVersion());
final Provider<String> group = project.provider(dependency::getGroup).orElse(userDevExtension.getDefaultForgeGroup());
final Provider<String> name = project.provider(dependency::getName).orElse(userDevExtension.getDefaultForgeName());

builder.withForgeVersion(version);
builder.withForgeGroup(group);
builder.withForgeName(name);

final ExternalModuleDependency clone = dependency.copy();
clone.artifact(artifact -> {
artifact.setExtension("jar");
artifact.setClassifier("userdev");
});

final Configuration userdevLookup = ConfigurationUtils.temporaryUnhandledConfiguration(project.getConfigurations(), "ResolveRequestedNeoForgeVersion", clone);
final ResolvedArtifact resolvedArtifact = userdevLookup.getResolvedConfiguration().getFirstLevelModuleDependencies().iterator().next().getModuleArtifacts().iterator().next();

builder.withForgeVersion(resolvedArtifact.getModuleVersion().getId().getVersion());
builder.withForgeGroup(resolvedArtifact.getModuleVersion().getId().getGroup());
builder.withForgeName(resolvedArtifact.getModuleVersion().getId().getName());
builder.withDistributionType(DistributionType.JOINED);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ExternalModuleDependency;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -36,4 +38,17 @@ public void onTasksCreated(TaskProvider<? extends WithOutput> copiesRawJar, Task
//Register the classpath element producer
definition.setUserdevClasspathElementProducer(copiesRawJar);
}

@Override
public ExternalModuleDependency getReplacementDependency(ExternalModuleDependency externalModuleDependency) {
final Dependency resolvedExactVersionDependency = getProject().getDependencies()
.create(
definition.getSpecification().getForgeGroup() + ":" + definition.getSpecification().getForgeName() + ":" + definition.getSpecification().getForgeVersion()
);

if (!(resolvedExactVersionDependency instanceof ExternalModuleDependency))
throw new IllegalStateException("Resolved dependency is not an ExternalModuleDependency");

return (ExternalModuleDependency) resolvedExactVersionDependency;
}
}

This file was deleted.

Loading

0 comments on commit 8d2e3e3

Please sign in to comment.