Skip to content

Commit

Permalink
Make tool resolution work without removing and re-adding.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed May 16, 2024
1 parent cfa0668 commit 660e8cc
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ private void setupAccessTransformerConfigurations(Project project, AccessTransfo

@SuppressWarnings("unchecked")
private void applyAfterEvaluate(final Project project) {
//Enable the dyn repo, all tools should be resolved now.
final Repository repository = project.getExtensions().getByType(Repository.class);
repository.enable();

//We now eagerly get all runs and configure them.
final NamedDomainObjectContainer<Run> runs = (NamedDomainObjectContainer<Run>) project.getExtensions().getByName(RunsConstants.Extensions.RUNS);
runs.forEach(run -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
*/
package net.neoforged.gradle.common.extensions.repository;

import net.neoforged.gradle.dsl.common.extensions.repository.RepositoryEntryLegacy;
import net.neoforged.gradle.util.IndentingXmlStreamWriter;
import net.neoforged.gradle.util.ModuleDependencyUtils;
import net.neoforged.gradle.util.ResolvedDependencyUtils;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.ResolvedArtifact;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLOutputFactory;
Expand Down Expand Up @@ -105,15 +102,15 @@ private void writeInfo(final Dependency entry) throws XMLStreamException {
private void writeDependencies(final Configuration dependencies) throws XMLStreamException {
this.writer.writeStartElement("dependencies");

for (final ResolvedDependency extra : dependencies.getResolvedConfiguration().getFirstLevelModuleDependencies()) {
for (final ResolvedArtifact extra : dependencies.getResolvedConfiguration().getResolvedArtifacts()) {
this.writeDependency(extra);
}

this.writer.writeEndElement();
}

private void writeDependency(final ResolvedDependency dep) throws XMLStreamException {
final String classifier = ResolvedDependencyUtils.getClassifierOrEmpty(dep);
private void writeDependency(final ResolvedArtifact dep) throws XMLStreamException {
final String classifier = dep.getClassifier() == null ? "" : dep.getClassifier();
final boolean hasClassifier = !classifier.isEmpty();

if (hasClassifier) {
Expand All @@ -122,19 +119,14 @@ private void writeDependency(final ResolvedDependency dep) throws XMLStreamExcep
this.writer.writeEmptyElement("dependency");
}

if (dep instanceof RepositoryEntryLegacy) {
final RepositoryEntryLegacy<?,?> entry = (RepositoryEntryLegacy<?,?>) dep;
this.writer.writeAttribute("org", entry.getFullGroup());
} else {
this.writer.writeAttribute("org", dep.getModuleGroup());
}
this.writer.writeAttribute("name", dep.getModuleName());
this.writer.writeAttribute("rev", dep.getModuleVersion());
this.writer.writeAttribute("org", dep.getModuleVersion().getId().getGroup());
this.writer.writeAttribute("name", dep.getModuleVersion().getId().getName());
this.writer.writeAttribute("rev", dep.getModuleVersion().getId().getVersion());
this.writer.writeAttribute("transitive", "false");

if (hasClassifier) {
this.writer.writeEmptyElement("artifact");
this.writer.writeAttribute("name", dep.getModuleName());
this.writer.writeAttribute("name", dep.getModuleVersion().getId().getName());
this.writer.writeAttribute("classifier", classifier);
this.writer.writeAttribute("ext", "jar");
this.writer.writeEndElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -42,7 +43,7 @@ public abstract class IvyRepository implements ConfigurableDSLElement<Repository
*/
public static final String IVY_METADATA_PATTERN = "[organisation]/[module]/[revision]/ivy-[revision]-ng" + METADATA_VERSION + ".xml";

private final Set<Entry> entries = Sets.newConcurrentHashSet();
private final Set<Entry> entries = Collections.synchronizedSet(new LinkedHashSet<>());

private final Project project;

Expand All @@ -52,7 +53,6 @@ public abstract class IvyRepository implements ConfigurableDSLElement<Repository
public IvyRepository(Project project) {
this.project = project;
this.getRepositoryDirectory().convention(project.getLayout().getProjectDirectory().dir(".gradle/repositories"));
this.enable();
}

@Override
Expand All @@ -73,12 +73,12 @@ private ArtifactRepository createRepositories() {

@Override
public void enable() {
this.gradleRepository = this.createRepositories();
}
if (this.gradleRepository != null) {
throw new IllegalStateException("Repository already enabled");
}

@Override
public void disable() {
project.getRepositories().remove(gradleRepository);
this.gradleRepository = this.createRepositories();
this.entries.forEach(this::write);
}

@SuppressWarnings("SameParameterValue") // Potentially this needs extension in the future.
Expand Down Expand Up @@ -144,6 +144,10 @@ public Set<Entry> getEntries() {
}

private void create(Entry entry) {
this.entries.add(entry);
}

private void write(Entry entry) {
final Dependency dependency = entry.getDependency();
final Configuration dependencies = entry.getDependencies();
final boolean hasSources = entry.hasSources();
Expand All @@ -154,8 +158,6 @@ private void create(Entry entry) {
} catch (IOException | XMLStreamException e) {
throw new RuntimeException("Failed to write dummy data", e);
}

this.entries.add(entry);
}

private void writeDummyDataIfNeeded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ private static <T> T resolveTool(final Project project, final Supplier<T> search
//Grab the dynamic repository
final Repository repository = project.getExtensions().getByType(Repository.class);

//Disable the repository
repository.disable();

//Resolve the tool
final T resolvedArtifact = searcher.get();

//Re-add the repository
repository.enable();

//Return the resolved artifact
return resolvedArtifact;
return searcher.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ interface Repository extends BaseDSLElement<Repository> {
*/
Set<Entry> getEntries()

/**
* Disables the repository.
*/
void disable()

/**
* Enables the repository.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,52 @@ class FunctionalTests extends BuilderBasedTestSpecification {
when:
def run = project.run {
it.tasks('clean', 'build')
it.stacktrace()
}

then:
run.task(':clean').outcome == TaskOutcome.SUCCESS
run.task(':build').outcome == TaskOutcome.SUCCESS
}

def "a mod with userdev as dependency and official mappings has the client-extra jar as a dependency"() {
given:
def project = create("gradle_userdev_references_client", {
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')
}

then:
run.task(':dependencies').outcome == TaskOutcome.SUCCESS
run.output.contains("net.minecraft:client")
}

def "the userdev runtime by default supports the build cache"() {
given:
def project = create("userdev_supports_loading_from_buildcache", {
Expand Down Expand Up @@ -145,7 +184,6 @@ class FunctionalTests extends BuilderBasedTestSpecification {
when:
def initialRun = project.run {
it.tasks('build')
it.stacktrace()
}

then:
Expand Down

0 comments on commit 660e8cc

Please sign in to comment.