Skip to content

Commit

Permalink
Merge branch 'NG_7.0' into fix/multi-project-leaking
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans authored May 13, 2024
2 parents 37ce7bb + 8faf299 commit 222d308
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

import net.neoforged.gradle.common.caching.CentralCacheService;
import net.neoforged.gradle.common.dependency.ExtraJarDependencyManager;
import net.neoforged.gradle.common.extensions.*;
import net.neoforged.gradle.common.extensions.AccessTransformersExtension;
import net.neoforged.gradle.common.extensions.ArtifactDownloaderExtension;
import net.neoforged.gradle.common.extensions.ConfigurationDataExtension;
import net.neoforged.gradle.common.extensions.ExtensionManager;
import net.neoforged.gradle.common.extensions.IdeManagementExtension;
import net.neoforged.gradle.common.extensions.MappingsExtension;
import net.neoforged.gradle.common.extensions.MinecraftArtifactCacheExtension;
import net.neoforged.gradle.common.extensions.MinecraftExtension;
import net.neoforged.gradle.common.extensions.ProjectEvaluationExtension;
import net.neoforged.gradle.common.extensions.ProjectHolderExtension;
import net.neoforged.gradle.common.extensions.dependency.creation.ProjectBasedDependencyCreator;
import net.neoforged.gradle.common.extensions.dependency.replacement.DependencyReplacementsExtension;
import net.neoforged.gradle.common.extensions.repository.IvyDummyRepositoryExtension;
Expand All @@ -13,12 +22,18 @@
import net.neoforged.gradle.common.runtime.extensions.RuntimesExtension;
import net.neoforged.gradle.common.runtime.naming.OfficialNamingChannelConfigurator;
import net.neoforged.gradle.common.tasks.DisplayMappingsLicenseTask;
import net.neoforged.gradle.common.util.FileCacheUtils;
import net.neoforged.gradle.common.util.TaskDependencyUtils;
import net.neoforged.gradle.common.util.constants.RunsConstants;
import net.neoforged.gradle.common.util.exceptions.MultipleDefinitionsFoundException;
import net.neoforged.gradle.common.util.run.RunsUtil;
import net.neoforged.gradle.dsl.common.extensions.*;
import net.neoforged.gradle.dsl.common.extensions.AccessTransformers;
import net.neoforged.gradle.dsl.common.extensions.ArtifactDownloader;
import net.neoforged.gradle.dsl.common.extensions.ConfigurationData;
import net.neoforged.gradle.dsl.common.extensions.Mappings;
import net.neoforged.gradle.dsl.common.extensions.Minecraft;
import net.neoforged.gradle.dsl.common.extensions.MinecraftArtifactCache;
import net.neoforged.gradle.dsl.common.extensions.ProjectHolder;
import net.neoforged.gradle.dsl.common.extensions.RunnableSourceSet;
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement;
import net.neoforged.gradle.dsl.common.extensions.repository.Repository;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
Expand All @@ -30,7 +45,11 @@
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.attributes.Category;
import org.gradle.api.component.AdhocComponentWithVariants;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Delete;
Expand All @@ -47,6 +66,10 @@ public class CommonProjectPlugin implements Plugin<Project> {

public static final String ASSETS_SERVICE = "ng_assets";
public static final String LIBRARIES_SERVICE = "ng_libraries";
public static final String ACCESS_TRANSFORMER_ELEMENTS_CONFIGURATION = "accessTransformerElements";
public static final String ACCESS_TRANSFORMER_API_CONFIGURATION = "accessTransformerApi";
public static final String ACCESS_TRANSFORMER_CONFIGURATION = "accessTransformer";
static final String ACCESS_TRANSFORMER_CATEGORY = "accesstransformer";

@Override
public void apply(Project project) {
Expand All @@ -71,7 +94,7 @@ public void apply(Project project) {
project.getExtensions().create(Repository.class, "ivyDummyRepository", IvyDummyRepositoryExtension.class, project);
project.getExtensions().create(MinecraftArtifactCache.class, "minecraftArtifactCache", MinecraftArtifactCacheExtension.class, project);
project.getExtensions().create(DependencyReplacement.class, "dependencyReplacements", DependencyReplacementsExtension.class, project, project.getObjects().newInstance(ProjectBasedDependencyCreator.class, project));
project.getExtensions().create(AccessTransformers.class, "accessTransformers", AccessTransformersExtension.class, project);
AccessTransformers accessTransformers = project.getExtensions().create(AccessTransformers.class, "accessTransformers", AccessTransformersExtension.class, project);
project.getExtensions().create("extensionManager", ExtensionManager.class, project);
project.getExtensions().create("clientExtraJarDependencyManager", ExtraJarDependencyManager.class, project);
final ConfigurationData configurationData = project.getExtensions().create( ConfigurationData.class, "configurationData", ConfigurationDataExtension.class, project);
Expand Down Expand Up @@ -116,14 +139,53 @@ public void apply(Project project) {
RunsConstants.Extensions.RUNS,
project.getObjects().domainObjectContainer(Run.class, name -> RunsUtil.create(project, name))
);

setupAccessTransformerConfigurations(project, accessTransformers);

IdeRunIntegrationManager.getInstance().setup(project);

project.getTasks().named("clean", Delete.class, delete -> {
delete.delete(configurationData.getLocation());
});
}


@SuppressWarnings("UnstableApiUsage")
private void setupAccessTransformerConfigurations(Project project, AccessTransformers accessTransformersExtension) {
Configuration accessTransformerElements = project.getConfigurations().maybeCreate(ACCESS_TRANSFORMER_ELEMENTS_CONFIGURATION);
Configuration accessTransformerApi = project.getConfigurations().maybeCreate(ACCESS_TRANSFORMER_API_CONFIGURATION);
Configuration accessTransformer = project.getConfigurations().maybeCreate(ACCESS_TRANSFORMER_CONFIGURATION);

accessTransformerApi.setCanBeConsumed(false);
accessTransformerApi.setCanBeResolved(false);

accessTransformer.setCanBeConsumed(false);
accessTransformer.setCanBeResolved(true);

accessTransformerElements.setCanBeConsumed(true);
accessTransformerElements.setCanBeResolved(false);
accessTransformerElements.setCanBeDeclared(false);

Action<AttributeContainer> action = attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, ACCESS_TRANSFORMER_CATEGORY));
};

accessTransformerElements.attributes(action);
accessTransformer.attributes(action);

accessTransformer.extendsFrom(accessTransformerApi);
accessTransformerElements.extendsFrom(accessTransformerApi);

// Now we set up the component, conditionally
AdhocComponentWithVariants java = (AdhocComponentWithVariants) project.getComponents().getByName("java");
Runnable enable = () -> java.addVariantsFromConfiguration(accessTransformerElements, variant -> {});

accessTransformerElements.getAllDependencies().configureEach(dep -> enable.run());
accessTransformerElements.getArtifacts().configureEach(artifact -> enable.run());

// And add resolved ATs to the extension
accessTransformersExtension.getFiles().from(accessTransformer);
}

private void applyAfterEvaluate(final Project project) {
RuntimesExtension runtimesExtension = project.getExtensions().getByType(RuntimesExtension.class);
runtimesExtension.bakeDefinitions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.component.ComponentIdentifier;
import org.gradle.api.artifacts.component.ComponentSelector;
import org.gradle.api.artifacts.component.ModuleComponentIdentifier;
import org.gradle.api.artifacts.component.ModuleComponentSelector;
import org.gradle.api.artifacts.result.*;
import org.gradle.api.artifacts.result.DependencyResult;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.artifacts.result.ResolvedComponentResult;
import org.gradle.api.artifacts.result.ResolvedDependencyResult;
import org.gradle.api.artifacts.result.ResolvedVariantResult;
import org.gradle.api.artifacts.type.ArtifactTypeDefinition;
import org.gradle.api.capabilities.Capability;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.SetProperty;
Expand All @@ -23,7 +25,14 @@
import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public abstract class JarJarArtifacts {
Expand Down Expand Up @@ -68,6 +77,7 @@ public JarJarArtifacts() {

includedArtifacts.finalizeValueOnRead();
includedRootComponents.finalizeValueOnRead();
getResolvedArtifacts().finalizeValueOnRead();

final DependencyFilter filter = getDependencyFilter();
final DependencyVersionInformationHandler versionHandler = getDependencyVersionInformationHandler();
Expand All @@ -83,6 +93,14 @@ public void configuration(Configuration jarJarConfiguration) {
getIncludedRootComponents().add(jarJarConfiguration.getIncoming().getResolutionResult().getRootComponent());
}

public void setConfigurations(Collection<? extends Configuration> configurations) {
includedRootComponents.empty();
includedArtifacts.empty();
for (Configuration configuration : configurations) {
configuration(configuration);
}
}

private static List<ResolvedJarJarArtifact> getIncludedJars(DependencyFilter filter, DependencyVersionInformationHandler versionHandler, Set<ResolvedComponentResult> rootComponents, Set<ResolvedArtifactResult> artifacts) {
Map<ContainedJarIdentifier, String> versions = new HashMap<>();
Map<ContainedJarIdentifier, String> versionRanges = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,52 @@
package net.neoforged.gradle.common.extensions;

import net.neoforged.gradle.common.CommonProjectPlugin;
import net.neoforged.gradle.common.extensions.base.BaseFilesWithEntriesExtension;
import net.neoforged.gradle.dsl.common.extensions.AccessTransformers;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ConfigurablePublishArtifact;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.dsl.ArtifactHandler;
import org.gradle.api.artifacts.dsl.DependencyHandler;

import javax.inject.Inject;

public abstract class AccessTransformersExtension extends BaseFilesWithEntriesExtension<AccessTransformers> implements AccessTransformers {
private transient final DependencyHandler projectDependencies;
private transient final ArtifactHandler projectArtifacts;

@Inject
public AccessTransformersExtension(Project project) {
super(project);

this.projectDependencies = project.getDependencies();
this.projectArtifacts = project.getArtifacts();
}

@Override
public Dependency consume(Object notation) {
return this.projectDependencies.add(CommonProjectPlugin.ACCESS_TRANSFORMER_CONFIGURATION, notation);
}

@Override
public Dependency consumeApi(Object notation) {
return this.projectDependencies.add(CommonProjectPlugin.ACCESS_TRANSFORMER_API_CONFIGURATION, notation);
}

@Override
public void expose(Object path, Action<ConfigurablePublishArtifact> action) {
file(path);
projectArtifacts.add(CommonProjectPlugin.ACCESS_TRANSFORMER_ELEMENTS_CONFIGURATION, path, action);
}

@Override
public void expose(Object path) {
expose(path, artifacts -> {});
}

@Override
public void expose(Dependency dependency) {
projectDependencies.add(CommonProjectPlugin.ACCESS_TRANSFORMER_API_CONFIGURATION, dependency);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.neoforged.gradle.common.runs.run;

import net.neoforged.gradle.dsl.common.runs.run.DependencyHandler;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
Expand All @@ -14,80 +15,18 @@ public abstract class DependencyHandlerImpl implements DependencyHandler {

private final Project project;

private final Configuration configuration;

@Inject
public DependencyHandlerImpl(Project project) {
this.project = project;
this.configuration = project.getConfigurations().detachedConfiguration();
this.configuration.setCanBeResolved(true);
this.configuration.setCanBeConsumed(false);
this.configuration.setTransitive(false);
}

public Project getProject() {
return project;
}

public Configuration getConfiguration() {
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project);
configuration.fromDependencyCollector(this.getRuntime());
return configuration;
}

@Override
public Dependency runtime(Object dependencyNotation) {
if (dependencyNotation instanceof Configuration) {
this.configuration.extendsFrom((Configuration) dependencyNotation);
return null;
}
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency runtime(Object dependencyNotation, Action<Dependency> configureClosure) {
if (dependencyNotation instanceof Configuration) {
if (configureClosure != null) {
throw new GradleException("Cannot add a Configuration with a configuration closure.");
}
this.configuration.extendsFrom((Configuration) dependencyNotation);
return null;
}
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configureClosure.execute(dependency);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency create(Object dependencyNotation) {
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency create(Object dependencyNotation, Action<Dependency> configureClosure) {
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configureClosure.execute(dependency);
return dependency;
}

@Override
public Dependency module(Object notation) {
return project.getDependencies().module(notation);
}

@Override
public Dependency module(Object notation, Action<Dependency> configureClosure) {
final Dependency dependency = project.getDependencies().module(notation);
configureClosure.execute(dependency);
return dependency;
}

@Override
public Dependency project(Map<String, ?> notation) {
final Dependency dependency = project.getDependencies().project(notation);
return dependency;
}
}
13 changes: 11 additions & 2 deletions common/src/main/java/net/neoforged/gradle/common/tasks/JarJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DuplicatesStrategy;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.bundling.Jar;
Expand All @@ -21,6 +22,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -44,12 +46,14 @@ public JarJar() {
setManifest(new DefaultInheritManifest(getServices().get(FileResolver.class)));
}

@Nested
// Already included in the JarJarArtifacts
@Internal
public DependencyFilter getDependencyFilter() {
return getJarJarArtifacts().getDependencyFilter();
}

@Nested
// Already included in the JarJarArtifacts
@Internal
public DependencyVersionInformationHandler getDependencyVersionInformationHandler() {
return getJarJarArtifacts().getDependencyVersionInformationHandler();
}
Expand Down Expand Up @@ -100,6 +104,11 @@ public void configuration(Configuration jarJarConfiguration) {
dependsOn(jarJarConfiguration);
}

public void setConfigurations(Collection<? extends Configuration> configurations) {
getJarJarArtifacts().setConfigurations(configurations);
configurations.forEach(this::dependsOn);
}

private Path getJarJarMetadataPath() {
return getTemporaryDir().toPath().resolve("metadata.json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.internal.jvm.Jvm;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec;

import java.io.File;
import java.util.Objects;

public abstract class JavaRuntimeTask extends DownloadingTask implements WithJavaVersion {

public JavaRuntimeTask() {
getJavaVersion().convention(getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain().getLanguageVersion());
getJavaLauncher().convention(getJavaToolChain().flatMap(toolChain -> {
if (!getJavaVersion().isPresent()) {
return toolChain.launcherFor(new CurrentJvmToolchainSpec(getObjectFactory()));
return toolChain.launcherFor(javaToolchainSpec -> javaToolchainSpec.getLanguageVersion().set(JavaLanguageVersion.of(Objects.requireNonNull(Jvm.current().getJavaVersion()).getMajorVersion())));
}

return toolChain.launcherFor(spec -> {
Expand Down
Loading

0 comments on commit 222d308

Please sign in to comment.