Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tech-Debt] Switch to using full named configurations. #173

Merged
merged 9 commits into from
May 17, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public void apply(Project project) {

project.getExtensions().create(IdeManagementExtension.class, "ideManager", IdeManagementExtension.class, project);
project.getExtensions().create("allRuntimes", RuntimesExtension.class);
project.getExtensions().create(ArtifactDownloader.class, "artifactDownloader", ArtifactDownloaderExtension.class, project);
project.getExtensions().create(Repository.class, "ivyDummyRepository", IvyRepository.class, project);
project.getExtensions().create(MinecraftArtifactCache.class, "minecraftArtifactCache", MinecraftArtifactCacheExtension.class, project);
project.getExtensions().create(DependencyReplacement.class, "dependencyReplacements", ReplacementLogic.class, project);
Expand Down Expand Up @@ -323,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 @@ -6,6 +6,7 @@
import net.neoforged.gradle.dsl.common.extensions.MinecraftArtifactCache;
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.ConfigurationUtils;
import net.neoforged.gradle.dsl.common.util.DistributionType;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.Project;
Expand Down Expand Up @@ -76,6 +77,9 @@ private boolean hasMatchingArtifact(ExternalModuleDependency externalModuleDepen

private ReplacementResult generateReplacement(final Project project, final Dependency dependency) {
final String minecraftVersion = dependency.getVersion();
if (minecraftVersion == null)
throw new IllegalArgumentException("Dependency version is null");

return replacements.computeIfAbsent(minecraftVersion, (v) -> {
final MinecraftArtifactCache minecraftArtifactCacheExtension = project.getExtensions().getByType(MinecraftArtifactCache.class);

Expand All @@ -87,7 +91,10 @@ private ReplacementResult generateReplacement(final Project project, final Depen
return new ReplacementResult(
project,
extraJarTaskProvider,
project.getConfigurations().detachedConfiguration(),
ConfigurationUtils.temporaryUnhandledConfiguration(
project.getConfigurations(),
"EmptyExtraJarConfigurationFor" + minecraftVersion.replace(".", "_")
),
Collections.emptySet()
);
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public void handleConfiguration(Configuration configuration) {
//TODO: Figure out if there is any way to do this lazily.
//TODO: Configure each runs in an immutable context, so we can't add a listener to the dependencies.
configuration.getDependencies().whenObjectAdded(dependency -> {
//We need to check if our configuration is unhandled, we can only do this here and not in the register because of way we register unhandled configurations after their creation:
//TODO: Find a better way to handle this.
if (ConfigurationUtils.isUnhandledConfiguration(configuration)) {
//We don't handle this configuration.
return;
}

//We only support module based dependencies.
if (dependency instanceof ModuleDependency) {
final ModuleDependency moduleDependency = (ModuleDependency) dependency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraftforge.gdi.BaseDSLElement;
import net.neoforged.gradle.dsl.common.extensions.repository.Entry;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.util.ModuleDependencyUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -75,7 +76,10 @@ public Project getProject() {
public Entry.Builder from(Dependency dependency) {
return from(
dependency,
project.getConfigurations().detachedConfiguration()
ConfigurationUtils.temporaryUnhandledConfiguration(
project.getConfigurations(),
"EmptyDependenciesFor" + ModuleDependencyUtils.toConfigurationName(dependency)
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +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 org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolvedArtifact;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLOutputFactory;
Expand Down Expand Up @@ -103,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 Dependency extra : dependencies.getDependencies()) {
for (final ResolvedArtifact extra : dependencies.getResolvedConfiguration().getResolvedArtifacts()) {
this.writeDependency(extra);
}

this.writer.writeEndElement();
}

private void writeDependency(final Dependency dep) throws XMLStreamException {
final String classifier = ModuleDependencyUtils.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 @@ -120,19 +119,14 @@ private void writeDependency(final Dependency dep) throws XMLStreamException {
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.getGroup());
}
this.writer.writeAttribute("name", dep.getName());
this.writer.writeAttribute("rev", dep.getVersion());
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.getName());
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,29 +43,23 @@ 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;

private final ArtifactRepository gradleRepository;
private ArtifactRepository gradleRepository;

@Inject
public IvyRepository(Project project) {
this.project = project;
this.getRepositoryDirectory().convention(project.getLayout().getProjectDirectory().dir(".gradle/repositories"));
this.gradleRepository = this.createRepositories();
}

@Override
public Project getProject() {
return project;
}

@Override
public ArtifactRepository getRepository() {
return gradleRepository;
}

private ArtifactRepository createRepositories() {
return project.getRepositories().ivy(repositoryConfiguration(
"NeoGradle Artifacts",
Expand All @@ -76,6 +71,23 @@ private ArtifactRepository createRepositories() {
@NotNull
public abstract DirectoryProperty getRepositoryDirectory();

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

//Create the repo and register it.
this.gradleRepository = this.createRepositories();

//Now we write all data to the repo.
//Due to the way dep replacement works, and the fact that this set is a linked hashset,
//we do not need to sort these. And dependency that is dynamic and uses this repo,
//while also depending on another dynamic dependency, will have the other dynamic dependency
//written first.
this.entries.forEach(this::write);
}

@SuppressWarnings("SameParameterValue") // Potentially this needs extension in the future.
private Action<IvyArtifactRepository> repositoryConfiguration(
final String name,
Expand Down Expand Up @@ -139,6 +151,16 @@ public Set<Entry> getEntries() {
}

private void create(Entry entry) {
//We only register here.
//The repo is not active yet, if we were to write now, we could get into trouble
//when somebody tries to resolve a dependency with the same module identifier,
//while having a different classifier.
//So we write when the repo is enabled in an after evaluate, at that point all entries
//are registered, and we are good to go.
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 @@ -149,8 +171,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 @@ -6,23 +6,28 @@
import org.gradle.api.artifacts.Configuration;

import javax.inject.Inject;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class DependencyHandlerImpl implements DependencyHandler {

private final Project project;
private final String context;

@Inject
public DependencyHandlerImpl(Project project) {
public DependencyHandlerImpl(Project project, String context) {
this.project = project;
this.context = context;
}

public Project getProject() {
return project;
}

public Configuration getRuntimeConfiguration() {
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project);
configuration.fromDependencyCollector(this.getRuntime());
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project, context);
if (configuration.getDependencies().isEmpty()) {
configuration.fromDependencyCollector(this.getRuntime());
}
return configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import net.neoforged.gradle.common.util.constants.RunsConstants;
import net.neoforged.gradle.dsl.common.runs.run.Run;
import net.neoforged.gradle.dsl.common.runs.type.RunType;
import net.neoforged.gradle.util.StringCapitalizationUtils;
import org.apache.ivy.util.StringUtils;
import org.codehaus.groovy.util.StringUtil;
import org.gradle.api.GradleException;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
Expand Down Expand Up @@ -51,7 +54,7 @@ public RunImpl(final Project project, final String name) {
getIsDataGenerator().convention(false);
getIsGameTest().convention(false);
getShouldBuildAllProjects().convention(false);
getDependencies().convention(project.getObjects().newInstance(DependencyHandlerImpl.class, project));
getDependencies().convention(project.getObjects().newInstance(DependencyHandlerImpl.class, project, String.format("RunRuntimeDependencies%s", StringCapitalizationUtils.capitalize(name))));

getConfigureAutomatically().convention(true);
getConfigureFromTypeWithName().convention(getConfigureAutomatically());
Expand Down
Loading
Loading