Skip to content

Commit

Permalink
Initial working and booting version
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Aug 30, 2024
1 parent e830353 commit 8b21978
Show file tree
Hide file tree
Showing 11 changed files with 957 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.neoforged.gradle.dsl.neoform.runtime.specification

import groovy.transform.CompileStatic
import net.neoforged.gradle.dsl.common.runtime.spec.Specification
import net.neoforged.gradle.dsl.common.util.DistributionType
import org.gradle.api.file.FileCollection
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -62,5 +63,15 @@ interface NeoFormSpecification extends Specification {
*/
@NotNull
B withAdditionalDependencies(@NotNull final FileCollection files);

/**
* Removes all common elements from the output of this runtime.
* This means that code shared between the client and server distribution types will be removed.
*
* @return The builder.
* @implNote If this is activated yet {@link DistributionType#CLIENT} is not used as the distribution type, then an exception will be thrown on spec construction.
*/
@NotNull
B removeCommonElements()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.neoforged.gradle.dsl.platform.dynamic

import groovy.transform.CompileStatic
import net.minecraftforge.gdi.ConfigurableDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.provider.Property

/**
* Represents the configuration for a NeoForm dynamic project.
*/
@CompileStatic
abstract class NeoFormProjectConfiguration implements ConfigurableDSLElement<NeoFormProjectConfiguration> {

/**
* @return True if the source sets should be split based on distribution, false otherwise.
*/
@DSLProperty
abstract Property<Boolean> getSplitSourceSets();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.neoforged.gradle.dsl.platform.dynamic

import groovy.transform.CompileStatic
import net.minecraftforge.gdi.BaseDSLElement
import net.minecraftforge.gdi.ConfigurableDSLElement
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Optional

import javax.inject.Inject

@CompileStatic
abstract class RuntimeProjectConfiguration implements BaseDSLElement<RuntimeProjectConfiguration> {

private final Project project

@Inject
RuntimeProjectConfiguration(Project project) {
this.project = project
}

Project getProject() {
return project
}

@DSLProperty
abstract Property<String> getNeoFormVersion()

/**
* @return The directory containing the patches to apply to the project.
*/
@DSLProperty
abstract DirectoryProperty getPatches()

/**
* @return The directory containing the rejects from applying patches to the project.
*/
@DSLProperty
abstract DirectoryProperty getRejects()

/**
* @return The directory containing the legacy patches to migrate when split source sets are enabled.
*/
@Optional
@DSLProperty
abstract DirectoryProperty getLegacyPatches()

/**
* @return True if the source sets should be split based on distribution, false otherwise.
*/
@DSLProperty
abstract Property<Boolean> getSplitSourceSets();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.neoforged.gradle.util.TransformerUtils;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection;
Expand Down Expand Up @@ -191,18 +192,12 @@ private static TaskProvider<? extends Runtime> createDecompile(NeoFormRuntimeSpe
}

private static String getDecompilerLogLevelArg(DecompilerLogLevel logLevel, String version) {
switch (logLevel) {
case TRACE:
return "trace";
case INFO:
return "info";
case WARN:
return "warn";
case ERROR:
return "error";
default:
throw new GradleException("LogLevel " + logLevel + " not supported by " + version);
}
return switch (logLevel) {
case TRACE -> "trace";
case INFO -> "info";
case WARN -> "warn";
case ERROR -> "error";
};
}

private TaskProvider<? extends Runtime> createExecute(final NeoFormRuntimeSpecification spec, final NeoFormConfigConfigurationSpecV1.Step step, final NeoFormConfigConfigurationSpecV1.Function function) {
Expand Down Expand Up @@ -283,7 +278,6 @@ protected NeoFormRuntimeDefinition doCreate(final NeoFormRuntimeSpecification sp
spec.getProject().getDependencies().create(library)
));


final Map<String, String> symbolicDataSources = buildDataFilesMap(neoFormConfig, spec.getDistribution());

final TaskProvider<? extends ArtifactProvider> sourceJarTask = spec.getProject().getTasks().register("supplySourcesFor" + spec.getIdentifier(), ArtifactProvider.class, task -> {
Expand Down Expand Up @@ -441,11 +435,24 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {
context.getLibrariesTask().flatMap(WithOutput::getOutput)
);

if (spec.noCommonElements()) {
if (!spec.getDistribution().isClient()) {
throw new InvalidUserDataException("Common elements can only be stripped from the client or joined distribution");
}

TaskProvider<? extends WithOutput> distInjectionInput = recompileInput;
recompileInput = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "injectDistMarkers"), DistOnlyInjector.class, task -> {
task.getInputFile().set(distInjectionInput.flatMap(WithOutput::getOutput));
configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, task);
});
}

final FileCollection recompileDependencies = spec.getAdditionalRecompileDependencies().plus(spec.getProject().files(definition.getMinecraftDependenciesConfiguration()));

TaskProvider<? extends WithOutput> unpackSourcesInput = recompileInput;
final TaskProvider<UnpackZip> unpackSources = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "unzipSources"), UnpackZip.class, task -> {
task.getInput().from(
recompileInput.flatMap(WithOutput::getOutput)
unpackSourcesInput.flatMap(WithOutput::getOutput)
.map(sourceJar -> task.getArchiveOperations().zipTree(sourceJar).matching(sp -> sp.include("**/*.java")).getAsFileTree())
);
});
Expand Down Expand Up @@ -476,21 +483,58 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) {

recompileTask.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask));

TaskProvider<? extends WithOutput> packTaskInput = recompileInput;
final TaskProvider<PackJar> packTask = spec.getProject()
.getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "packRecomp"), PackJar.class, task -> {
task.getInputFiles().from(recompileInput.flatMap(WithOutput::getOutput).map(task.getArchiveOperations()::zipTree).map(zipTree -> zipTree.matching(sp -> sp.exclude("**/*.java"))));
task.getInputFiles().from(packTaskInput.flatMap(WithOutput::getOutput).map(task.getArchiveOperations()::zipTree).map(zipTree -> zipTree.matching(sp -> sp.exclude("**/*.java"))));
task.getInputFiles().from(recompileTask.flatMap(AbstractCompile::getDestinationDirectory));
});
packTask.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask));

taskOutputs.put(recompileTask.getName(), packTask);

//When we are running in split sourceset mode then we need to strip the common elements from the source and raw jars
//This is indicated by the noCommonElements flag in the spec, however we can only do this for the client, any other
//Configuration is invalid.
final TaskProvider<? extends WithOutput> rawSource;
final TaskProvider<? extends WithOutput> sourceSource;
if (spec.noCommonElements()) {
if (!spec.getDistribution().isClient()) {
throw new InvalidUserDataException("Common elements can only be stripped from the client or joined distribution");
}

//Reuse existing split tasks, in blacklist mode.
rawSource = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "removeCommonRaw"), StripJar.class, task -> {
task.getInput().set(packTask.flatMap(WithOutput::getOutput));
task.getIsWhitelistMode().set(false);
task.getMappingsFiles().setFrom(definition.getGameArtifactProvidingTasks().get(GameArtifact.SERVER_MAPPINGS));
task.getFileExtension().set("class");
task.convertNamesToPaths();

configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, task);
});

TaskProvider<? extends WithOutput> sourceSourceInput = recompileInput;
sourceSource = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "removeCommonSource"), StripJar.class, task -> {
task.getInput().set(sourceSourceInput.flatMap(WithOutput::getOutput));
task.getIsWhitelistMode().set(false);
task.getMappingsFiles().setFrom(definition.getGameArtifactProvidingTasks().get(GameArtifact.SERVER_MAPPINGS));
task.getFileExtension().set("java");
task.convertNamesToPaths();

configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, task);
});
} else {
rawSource = packTask;
sourceSource = recompileInput;
}

definition.getSourceJarTask().configure(task -> {
task.getInputFiles().from(recompileInput);
task.getInputFiles().from(sourceSource.flatMap(WithOutput::getOutput));
task.dependsOn(remapTask);
});
definition.getRawJarTask().configure(task -> {
task.getInputFiles().from(packTask.flatMap(WithOutput::getOutput));
task.getInputFiles().from(rawSource.flatMap(WithOutput::getOutput));
task.dependsOn(packTask);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class NeoFormRuntimeSpecification extends CommonRuntimeSpecification impl
private final Provider<File> neoFormArchive;
private final NeoFormConfigConfigurationSpecV2 config;
private final FileCollection additionalRecompileDependencies;
private final boolean noCommonElements;

private NeoFormRuntimeSpecification(Project project,
String version,
Expand All @@ -47,11 +48,12 @@ private NeoFormRuntimeSpecification(Project project,
Multimap<String, TaskTreeAdapter> preTaskTypeAdapters,
Multimap<String, TaskTreeAdapter> postTypeAdapters,
Multimap<String, TaskCustomizer<? extends Task>> taskCustomizers,
FileCollection additionalRecompileDependencies) {
FileCollection additionalRecompileDependencies, boolean noCommonElements) {
super(project, "neoForm", version, side, preTaskTypeAdapters, postTypeAdapters, taskCustomizers, NeoFormRuntimeExtension.class);
this.neoFormArchive = neoFormArchive;
this.config = config;
this.additionalRecompileDependencies = additionalRecompileDependencies;
this.noCommonElements = noCommonElements;
}

public NeoFormConfigConfigurationSpecV2 getConfig() {
Expand Down Expand Up @@ -80,6 +82,10 @@ public Provider<File> getNeoFormArchive() {
return additionalRecompileDependencies;
}

public boolean noCommonElements() {
return noCommonElements;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -104,6 +110,7 @@ public static final class Builder extends CommonRuntimeSpecification.Builder<Neo

private Dependency neoFormDependency;
private FileCollection additionalDependencies;
private boolean noCommonElements = false;

private Builder(Project project) {
super(project);
Expand Down Expand Up @@ -140,6 +147,13 @@ public Builder withAdditionalDependencies(final FileCollection files) {
return getThis();
}

@NotNull
@Override
public Builder removeCommonElements() {
this.noCommonElements = true;
return getThis();
}

public @NotNull NeoFormRuntimeSpecification build() {
ResolvedArtifact artifact = ToolUtilities.resolveToolArtifact(project, neoFormDependency);
File archive = artifact.getFile();
Expand All @@ -153,6 +167,10 @@ public Builder withAdditionalDependencies(final FileCollection files) {
throw new GradleException("Failed to read NeoForm config file from version " + effectiveVersion);
}

if (noCommonElements && !distributionType.get().isClient()) {
throw new GradleException("Cannot remove common elements from a none client distribution");
}

return new NeoFormRuntimeSpecification(
project,
effectiveVersion,
Expand All @@ -167,8 +185,8 @@ public Builder withAdditionalDependencies(final FileCollection files) {
preTaskAdapters,
postTaskAdapters,
taskCustomizers,
additionalDependencies
);
additionalDependencies,
noCommonElements);
}
}

Expand Down
Loading

0 comments on commit 8b21978

Please sign in to comment.