Skip to content

Commit

Permalink
Switch to using full named configurations.
Browse files Browse the repository at this point in the history
I will await the first bug report where people fuck with these configs but so be it.

Closes #172
  • Loading branch information
marchermans committed May 16, 2024
1 parent e7230de commit e5b3814
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 108 deletions.
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
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,9 +60,17 @@ 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;
project.getLogger().lifecycle("Handling dependency " + moduleDependency.getGroup() + ":" + moduleDependency.getName() + " in configuration " + configuration.getName());
//Try replacing the dependency.
handleDependency(configuration, moduleDependency);
}
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 @@ -10,18 +10,20 @@
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);
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project, context);
configuration.fromDependencyCollector(this.getRuntime());
return configuration;
}
Expand Down
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("Run%s", StringCapitalizationUtils.capitalize(name))));

getConfigureAutomatically().convention(true);
getConfigureFromTypeWithName().convention(getConfigureAutomatically());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.neoforged.gradle.dsl.common.extensions.repository.Repository;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import net.neoforged.gradle.util.ModuleDependencyUtils;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ResolvedArtifact;
Expand All @@ -19,20 +20,23 @@ private ToolUtilities() {
public static File resolveTool(final Project project, final String tool) {
return resolveTool(project, () -> ConfigurationUtils.temporaryUnhandledConfiguration(
project.getConfigurations(),
"ToolLookupFor" + ModuleDependencyUtils.toConfigurationName(tool),
project.getDependencies().create(tool)
).getFiles().iterator().next());
}

public static ResolvedArtifact resolveToolArtifact(final Project project, final String tool) {
return resolveTool(project, () -> ConfigurationUtils.temporaryUnhandledConfiguration(
project.getConfigurations(),
"ToolLookupFor" + ModuleDependencyUtils.toConfigurationName(tool),
project.getDependencies().create(tool)
).getResolvedConfiguration().getResolvedArtifacts().iterator().next());
}

public static ResolvedArtifact resolveToolArtifact(final Project project, final Dependency tool) {
return resolveTool(project, () -> ConfigurationUtils.temporaryUnhandledConfiguration(
project.getConfigurations(),
"ToolLookupFor" + ModuleDependencyUtils.toConfigurationName(tool),
tool
).getResolvedConfiguration().getResolvedArtifacts().iterator().next());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,29 @@ import java.util.concurrent.atomic.AtomicInteger
@CompileStatic
class ConfigurationUtils {

private static AtomicInteger temporaryConfigurationCounter = new AtomicInteger(0)
private static Set<Configuration> UNHANDLED_CONFIGURATIONS = new HashSet<>()

private ConfigurationUtils() {
throw new IllegalStateException("Can not instantiate an instance of: ConfigurationUtils. This is a utility class")
}

/**
* Creates a detached configuration that can be resolved, but not consumed.
* Creates a configuration that can be resolved, but not consumed.
*
* @param project The project to create the configuration for
* @param context The context of the configuration
* @param dependencies The dependencies to add to the configuration
* @return The detached configuration
*/
static Configuration temporaryConfiguration(final Project project, final Dependency... dependencies) {
final Configuration configuration = project.getConfigurations().detachedConfiguration(dependencies);
static Configuration temporaryConfiguration(final Project project, final String context, final Dependency... dependencies) {
final Configuration configuration = project.getConfigurations().maybeCreate("neoGradleInternal${context.capitalize()}")

configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)
if (configuration.getDependencies().isEmpty()) {
configuration.getDependencies().addAll(dependencies)

configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)
}

final DependencyReplacement dependencyReplacement = project.getExtensions().getByType(DependencyReplacement.class)
dependencyReplacement.handleConfiguration(configuration)
Expand All @@ -45,27 +50,66 @@ class ConfigurationUtils {
}

/**
* Creates a detached configuration that can be resolved, but not consumed.
* Indicates if the given configuration is an unhandled configuration.
*
* @param configuration The configuration to check
* @return True if the configuration is unhandled, false otherwise
*/
public static boolean isUnhandledConfiguration(Configuration configuration) {
return UNHANDLED_CONFIGURATIONS.contains(configuration)
}

/**
* Creates a configuration that can be resolved, but not consumed, but on which no dependency replacement is applied.
*
* @param configurations The configuration handler.
* @param context The context of the configuration
* @param dependencies The dependencies to add to the configuration
* @return The detached configuration
*/
static Configuration temporaryUnhandledConfiguration(final ConfigurationContainer configurations, final Dependency... dependencies) {
final Configuration configuration = configurations.detachedConfiguration(dependencies)
configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)
static Configuration temporaryUnhandledConfiguration(final ConfigurationContainer configurations, final String context, final Dependency... dependencies) {
final Configuration configuration = configurations.maybeCreate("neoGradleInternalUnhandled${context.capitalize()}")
UNHANDLED_CONFIGURATIONS.add(configuration)

if (configuration.getDependencies().isEmpty()) {
configuration.getDependencies().addAll(dependencies)

configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)
}

return configuration
}

/**
* Creates a configuration that can be resolved, but not consumed, and does not report its dependencies as transitive.
*
* @param configurations The configuration handler.
* @param context The context of the configuration
* @param dependencies The dependencies to add to the configuration
* @return The detached configuration
*/
static Configuration temporaryUnhandledNotTransitiveConfiguration(final ConfigurationContainer configurations, final String context, final Dependency... dependencies) {
final Configuration configuration = configurations.maybeCreate("neoGradleInternalUnhandled${context.capitalize()}")
UNHANDLED_CONFIGURATIONS.add(configuration)

if (configuration.getDependencies().isEmpty()) {
configuration.getDependencies().addAll(dependencies)

configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)
configuration.setTransitive(false)
}

return configuration
}

/**
* Creates a provider that will resolve a temporary configuration containing the given dependency.
*/
static Provider<File> getArtifactProvider(Project project, Provider<? extends Object> dependencyNotationProvider) {
static Provider<File> getArtifactProvider(Project project, String context, Provider<? extends Object> dependencyNotationProvider) {
return dependencyNotationProvider.flatMap(dependencyNotation -> {
Configuration configuration = temporaryConfiguration(project, project.getDependencies().create(dependencyNotation));
configuration.transitive = false;
Configuration configuration = temporaryUnhandledNotTransitiveConfiguration(project.getConfigurations(), context, project.getDependencies().create(dependencyNotation));
return configuration.getElements().map(files -> files.iterator().next().getAsFile());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import net.minecraftforge.gdi.annotations.DSLProperty
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils
import net.neoforged.gradle.dsl.platform.util.CoordinateCollector
import net.neoforged.gradle.dsl.platform.util.LibraryCollector
import net.neoforged.gradle.util.ModuleDependencyUtils
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
Expand Down Expand Up @@ -171,6 +172,7 @@ abstract class InstallerProfile implements ConfigurableDSLElement<InstallerProfi
processor.getClasspath().set(processor.getJar().map(tool -> {
final Configuration detached = ConfigurationUtils.temporaryConfiguration(
project,
"InstallerProfileCoordinateLookup" + ModuleDependencyUtils.toConfigurationName(tool),
project.getDependencies().create(tool)
)

Expand All @@ -191,7 +193,10 @@ abstract class InstallerProfile implements ConfigurableDSLElement<InstallerProfi
dependencyCoordinates.add(tool)

final Dependency[] dependencies = dependencyCoordinates.stream().map { coord -> project.getDependencies().create(coord) }.toArray(Dependency[]::new)
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project, dependencies)
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(
project,
"InstallerProfileLibraryLookup",
dependencies)

final LibraryCollector collector = new LibraryCollector(project.getObjects(), project.getRepositories()
.withType(MavenArtifactRepository).stream().map { it.url }.collect(Collectors.toList()))
Expand Down
Loading

0 comments on commit e5b3814

Please sign in to comment.