diff --git a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java index fb08c8e13..e9216d00b 100644 --- a/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java +++ b/common/src/main/java/net/neoforged/gradle/common/CommonProjectPlugin.java @@ -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; @@ -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; @@ -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; @@ -47,6 +66,10 @@ public class CommonProjectPlugin implements Plugin { 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) { @@ -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); @@ -116,6 +139,8 @@ 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); @@ -123,7 +148,44 @@ public void apply(Project project) { 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 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(); diff --git a/common/src/main/java/net/neoforged/gradle/common/extensions/AccessTransformersExtension.java b/common/src/main/java/net/neoforged/gradle/common/extensions/AccessTransformersExtension.java index 53239b2b4..114e3b672 100644 --- a/common/src/main/java/net/neoforged/gradle/common/extensions/AccessTransformersExtension.java +++ b/common/src/main/java/net/neoforged/gradle/common/extensions/AccessTransformersExtension.java @@ -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 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 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); } } diff --git a/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/extensions/AccessTransformers.groovy b/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/extensions/AccessTransformers.groovy index 85aa47e62..67065a1bf 100644 --- a/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/extensions/AccessTransformers.groovy +++ b/dsl/common/src/main/groovy/net/neoforged/gradle/dsl/common/extensions/AccessTransformers.groovy @@ -2,10 +2,23 @@ package net.neoforged.gradle.dsl.common.extensions import groovy.transform.CompileStatic import net.minecraftforge.gdi.BaseDSLElementWithFilesAndEntries +import org.gradle.api.Action +import org.gradle.api.artifacts.ConfigurablePublishArtifact +import org.gradle.api.artifacts.Dependency +import org.gradle.api.artifacts.dsl.DependencyCollector /** * Defines a DSL extension which allows for the specification of access transformers. */ @CompileStatic interface AccessTransformers extends BaseDSLElementWithFilesAndEntries { + Dependency consume(Object notation) + + Dependency consumeApi(Object notation) + + void expose(Dependency dependency) + + void expose(Object path) + + void expose(Object path, Action action) } \ No newline at end of file