Skip to content

Commit

Permalink
Clean up DSL slightly and handle AW/II sharing across fabric subprojects
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Oct 19, 2024
1 parent 429896e commit 4087c8e
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 44 deletions.
19 changes: 13 additions & 6 deletions src/main/java/dev/lukebemish/crochet/model/CrochetExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.gradle.api.Action;
import org.gradle.api.ExtensiblePolymorphicDomainObjectContainer;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.NamedDomainObjectProvider;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;

Expand Down Expand Up @@ -61,17 +61,24 @@ public ExtensiblePolymorphicDomainObjectContainer<MinecraftInstallation> getInst
return installations;
}

public void fabric(String name, Action<FabricInstallation> action) {
installations.register(name, FabricInstallation.class, action);
public void installations(Action<ExtensiblePolymorphicDomainObjectContainer<MinecraftInstallation>> action) {
action.execute(installations);
}

public void vanilla(String name, Action<VanillaInstallation> action) {
installations.register(name, VanillaInstallation.class, action);
public NamedDomainObjectProvider<FabricInstallation> fabric(String name, Action<FabricInstallation> action) {
return installations.register(name, FabricInstallation.class, action);
}

public NamedDomainObjectProvider<VanillaInstallation> vanilla(String name, Action<VanillaInstallation> action) {
return installations.register(name, VanillaInstallation.class, action);
}

@Nested
public abstract NamedDomainObjectContainer<Run> getRuns();

public void runs(Action<NamedDomainObjectContainer<Run>> action) {
action.execute(getRuns());
}

private final Map<SourceSet, String> sourceSets = new HashMap<>();

void forSourceSet(String installation, SourceSet sourceSet) {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/dev/lukebemish/crochet/model/FabricInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import static dev.lukebemish.crochet.internal.ConfigurationUtils.copyAttributes;

public abstract class FabricInstallation extends AbstractVanillaInstallation {
static final String ACCESS_WIDENER_CATEGORY = "accesswidener";

final Configuration loaderConfiguration;
final Configuration intermediaryMinecraft;
final Configuration mappingsClasspath;
Expand All @@ -70,6 +72,7 @@ public abstract class FabricInstallation extends AbstractVanillaInstallation {
private final TaskProvider<MappingsWriter> intermediaryToNamed;
private final TaskProvider<MappingsWriter> namedToIntermediary;
final Configuration accessWideners;
final Configuration accessWidenersElements;
final TaskProvider<ExtractFabricDependencies> extractFabricForDependencies;

@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -97,6 +100,7 @@ public FabricInstallation(String name, CrochetExtension extension) {
});
fabricConfigMaker.getAccessWideners().from(project.fileTree(extractFabricForDependencies.flatMap(ExtractFabricDependencies::getOutputDirectory)).builtBy(extractFabricForDependencies).filter(it -> it.getName().endsWith(".accesswidener")));
fabricConfigMaker.getInterfaceInjection().from(project.fileTree(extractFabricForDependencies.flatMap(ExtractFabricDependencies::getOutputDirectory)).builtBy(extractFabricForDependencies).filter(it -> it.getName().equals("interface_injections.json")));
project.getDependencies().add(this.injectedInterfaces.get().getName(), project.fileTree(extractFabricForDependencies.flatMap(ExtractFabricDependencies::getOutputDirectory)).builtBy(extractFabricForDependencies).filter(it -> it.getName().equals("neo_interface_injections.json")));

this.binaryArtifactsTask.configure(task -> {
task.getTargets().add(TaskGraphExecution.GraphOutput.of("downloadClientMappings.output", mappings, project.getObjects()));
Expand Down Expand Up @@ -125,6 +129,12 @@ public FabricInstallation(String name, CrochetExtension extension) {
config.fromDependencyCollector(getDependencies().getAccessWideners());
config.setCanBeConsumed(false);
}).get();
this.accessWidenersElements = project.getConfigurations().register(name+"AccessWidenersElements", config -> {
config.setCanBeResolved(false);
config.setCanBeDeclared(false);
config.setCanBeConsumed(false);
config.extendsFrom(this.accessWideners);
}).get();
fabricConfigMaker.getAccessWideners().from(accessWideners);

var intermediaryJar = workingDirectory.map(it -> it.file("intermediary.jar"));
Expand Down Expand Up @@ -450,6 +460,12 @@ private void forFeatureShared(SourceSet sourceSet, Action<FabricSourceSetDepende
nonModApiElements.setCanBeResolved(false);

FeatureUtils.forSourceSetFeature(project, sourceSet.getName(), context -> {
context.withCapabilities(accessWidenersElements);
accessWidenersElements.setCanBeConsumed(true);
accessWidenersElements.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, ACCESS_WIDENER_CATEGORY));
});

context.withCapabilities(modRuntimeElements);
context.withCapabilities(modApiElements);
context.withCapabilities(nonModRuntimeElements);
Expand Down Expand Up @@ -710,6 +726,41 @@ private void forFeatureShared(SourceSet sourceSet, Action<FabricSourceSetDepende
apiElements.extendsFrom(modApi);
modApiElements.extendsFrom(modApi);

var interfaceInjectionCompile = modCompileClasspath.getIncoming().artifactView(config -> {
config.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, MinecraftInstallation.INTERFACE_INJECTION_CATEGORY));
});
config.withVariantReselection();
config.setLenient(true);
});
var interfaceInjectionRuntime = modRuntimeClasspath.getIncoming().artifactView(config -> {
config.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, MinecraftInstallation.INTERFACE_INJECTION_CATEGORY));
});
config.withVariantReselection();
config.setLenient(true);
});
var accessWidenersCompile = modCompileClasspath.getIncoming().artifactView(config -> {
config.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, ACCESS_WIDENER_CATEGORY));
});
config.withVariantReselection();
config.setLenient(true);
});
var accessWidenersRuntime = modRuntimeClasspath.getIncoming().artifactView(config -> {
config.attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.getObjects().named(Category.class, ACCESS_WIDENER_CATEGORY));
});
config.withVariantReselection();
config.setLenient(true);
});
this.extractFabricForDependencies.configure(task -> {
task.getFloatingCompileNeoInterfaceInjections().from(interfaceInjectionCompile.getFiles());
task.getFloatingRuntimeNeoInterfaceInjections().from(interfaceInjectionRuntime.getFiles());
task.getFloatingCompileAccessWideners().from(accessWidenersCompile.getFiles());
task.getFloatingRuntimeAccessWideners().from(accessWidenersRuntime.getFiles());
});

var remappedCompileClasspath = project.getConfigurations().maybeCreate(sourceSet.getTaskName("crochetRemapped", "compileClasspath"));
project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName()).extendsFrom(remappedCompileClasspath);

Expand Down Expand Up @@ -809,6 +860,11 @@ record Pair<T, U>(T first, U second) implements Serializable {}
}
}

@Override
protected boolean canPublishInjectedInterfaces() {
return false;
}

@Override
protected FabricInstallationDependencies makeDependencies(Project project) {
return project.getObjects().newInstance(FabricInstallationDependencies.class, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class MinecraftInstallation implements Named {
private static final String ACCESS_TRANSFORMER_CATEGORY = "accesstransformer";
private static final String INTERFACE_INJECTION_CATEGORY = "interfaceinjection";
static final String ACCESS_TRANSFORMER_CATEGORY = "accesstransformer";
static final String INTERFACE_INJECTION_CATEGORY = "interfaceinjection";

private final String name;
private final Set<SourceSet> sourceSets = new LinkedHashSet<>();
Expand Down Expand Up @@ -171,6 +171,7 @@ public void forFeature(SourceSet sourceSet) {
forFeatureShared(context);

AtomicBoolean atsAdded = new AtomicBoolean(false);
context.withCapabilities(accessTransformersElements.get());
accessTransformersElements.get().setCanBeConsumed(true);
accessTransformersElements.get().attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, crochetExtension.project.getObjects().named(Category.class, ACCESS_TRANSFORMER_CATEGORY));
Expand All @@ -187,23 +188,28 @@ public void forFeature(SourceSet sourceSet) {
});

AtomicBoolean iisAdded = new AtomicBoolean(false);
context.withCapabilities(injectedInterfacesElements.get());
injectedInterfacesElements.get().setCanBeConsumed(true);
injectedInterfacesElements.get().attributes(attributes -> {
attributes.attribute(Category.CATEGORY_ATTRIBUTE, crochetExtension.project.getObjects().named(Category.class, INTERFACE_INJECTION_CATEGORY));
});
injectedInterfacesElements.get().getDependencies().configureEach(dep -> {
if (!iisAdded.compareAndSet(false, true)) {
if (canPublishInjectedInterfaces() && !iisAdded.compareAndSet(false, true)) {
context.publishWithVariants(injectedInterfacesElements.get());
}
});
injectedInterfacesElements.get().getOutgoing().getArtifacts().configureEach(artifact -> {
if (!iisAdded.compareAndSet(false, true)) {
if (canPublishInjectedInterfaces() && !iisAdded.compareAndSet(false, true)) {
context.publishWithVariants(injectedInterfacesElements.get());
}
});
});
}

protected boolean canPublishInjectedInterfaces() {
return true;
}

public void forLocalFeature(SourceSet sourceSet) {
if (sourceSets.add(sourceSet)) {
this.crochetExtension.forSourceSet(this.getName(), sourceSet);
Expand Down
Loading

0 comments on commit 4087c8e

Please sign in to comment.