Skip to content

Commit

Permalink
Add system for publishing and consuming ATs (#146)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Hermans <[email protected]>
  • Loading branch information
lukebemish and marchermans authored May 13, 2024
1 parent a54164e commit 8faf299
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 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
@@ -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
Expand Up @@ -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<AccessTransformers> {
Dependency consume(Object notation)

Dependency consumeApi(Object notation)

void expose(Dependency dependency)

void expose(Object path)

void expose(Object path, Action<ConfigurablePublishArtifact> action)
}

0 comments on commit 8faf299

Please sign in to comment.