Skip to content

Commit

Permalink
add ArtifactFileProducingTask and implement it on CompressTinyTask
Browse files Browse the repository at this point in the history
  • Loading branch information
supersaiyansubtlety committed Oct 26, 2024
1 parent bbfee5e commit 6f3bcc0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ publishing {
version Constants.MAPPINGS_VERSION

// quilt-mappings-<version>-tiny.gz
artifact(compressTiny.compressedTiny) {
artifact(compressTiny.artifactFile) {
classifier CompressTinyTask.TINY_CLASSIFIER
builtBy compressTiny
// builtBy compressTiny
}
// quilt-mappings-<version>.jar
artifact(tinyJar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected Tasks applyImpl(@NotNull Project project) {
}
);

final var tinyJar = tasks.register(
tasks.register(
TinyJarTask.TINY_JAR_TASK_NAME,
TinyJarTask.class,
task -> {
Expand All @@ -215,11 +215,15 @@ protected Tasks applyImpl(@NotNull Project project) {
task -> {
task.getMappings().convention(mergeTiny.flatMap(MergeTinyTask::getOutputMappings));

task.getCompressedTiny().convention(
tinyJar.flatMap(TinyJarTask::getDestinationDirectory).map(dir ->
dir.file(ARCHIVE_FILE_NAME_PREFIX + "-" + CompressTinyTask.TINY_CLASSIFIER + ".gz")
)
);
task.getArtifactBaseName().convention(Constants.MAPPINGS_NAME);

task.getArtifactVersion().convention(Constants.MAPPINGS_VERSION);

task.getArtifactClassifier().convention(CompressTinyTask.TINY_CLASSIFIER);

task.getArtifactExtension().convention("gz");

task.getDestinationDirectory().convention(this.getLibsDir());
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import quilt.internal.Constants;
import quilt.internal.plugin.abstraction.DefaultTaskedMappingsProjectPlugin;
import quilt.internal.tasks.VersionDownloadInfoConsumingTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package quilt.internal.tasks;

import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFile;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.publish.maven.MavenPublication;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;

import javax.inject.Inject;

// TODO would it be screwy to implement PublishArtifact on this?
// It would allow passing the task itself to MavenPublication#artifact, instead of artifactFile.

/**
* A task that produces an {@link #getArtifactFile() artifactFile}.
* <p>
* The path to the {@link #getArtifactFile() artifactFile} is built from the task's name and destination properties,
* and {@link MavenPublication#artifact(Object)} can interpolate artifact metadata from the name's format.
*/
public interface ArtifactFileProducingTask extends MappingsTask {
@Inject
ObjectFactory getObjects();

@Input
Property<String> getArtifactBaseName();

@Optional
@Input
Property<String> getArtifactAppendix();

@Optional
@Input
Property<String> getArtifactVersion();

@Optional
@Input
Property<String> getArtifactClassifier();

@Input
Property<String> getArtifactExtension();

/**
* Required
*/
@Internal("Represented as part of artifactFile")
DirectoryProperty getDestinationDirectory();

/**
* The artifact produced by this task.
* <p>
* The path to the file takes the form:<br>
* {@code [destination]/[baseName]-[appendix]-[version]-[classifier].[extension]}
* <p>
* This standard format allows {@link MavenPublication#artifact(Object)} to interpolate the
* {@linkplain #getArtifactClassifier() classifier} and the {@linkplain #getArtifactExtension() extension}.
*/
@OutputFile
default Provider<RegularFile> getArtifactFile() {
// zzzzzip
return this.getArtifactBaseName()
.zip(this.getArtifactAppendix().orElse(""), ArtifactFileProducingTask::dashJoin)
.zip(this.getArtifactVersion().orElse(""), ArtifactFileProducingTask::dashJoin)
.zip(this.getArtifactClassifier().orElse(""), ArtifactFileProducingTask::dashJoin)
.zip(this.getArtifactExtension(), (name, ext) -> name + "." + ext)
.zip(this.getDestinationDirectory(), (name, dest) -> dest.file(name));
}

private static String dashJoin(String left, String right) {
return right.isEmpty()
? left
: left + "-" + right;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskContainer;
import quilt.internal.Constants.Groups;
import quilt.internal.plugin.MapMinecraftJarsPlugin;
import quilt.internal.tasks.ArtifactFileProducingTask;
import quilt.internal.tasks.DefaultMappingsTask;

public abstract class CompressTinyTask extends DefaultMappingsTask {
public abstract class CompressTinyTask extends DefaultMappingsTask implements ArtifactFileProducingTask {
/**
* {@linkplain TaskContainer#register Registered} by {@link MapMinecraftJarsPlugin}.
*/
Expand All @@ -25,9 +25,6 @@ public abstract class CompressTinyTask extends DefaultMappingsTask {
@InputFile
public abstract RegularFileProperty getMappings();

@OutputFile
public abstract RegularFileProperty getCompressedTiny();

public CompressTinyTask() {
super(Groups.BUILD_MAPPINGS);
}
Expand All @@ -38,13 +35,13 @@ public void compressTiny() throws IOException {

try (
final var outputStream =
new GZIPOutputStream(new FileOutputStream(this.getCompressedTiny().get().getAsFile()));
final var fileInputStream = new FileInputStream(this.getMappings().get().getAsFile())
new GZIPOutputStream(new FileOutputStream(this.getArtifactFile().get().getAsFile()));
final var inputStream = new FileInputStream(this.getMappings().get().getAsFile())
) {
final byte[] buffer = new byte[1024];

int length;
while ((length = fileInputStream.read(buffer)) > 0) {
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import quilt.internal.tasks.EnigmaProfileConsumingTask;
import quilt.internal.tasks.MappingsDirConsumingTask;

// TODO use getArgumentProviders instead of overriding exec
@UntrackedTask(because =
"""
These input and output to the same directory, which doesn't work with Gradle's task graph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.quiltmc.launchermeta.version_manifest.VersionEntry;
import org.quiltmc.launchermeta.version_manifest.VersionManifest;
import quilt.internal.Constants;
import quilt.internal.tasks.setup.DownloadWantedVersionManifestTask;

import java.io.File;
import java.io.IOException;
Expand Down

0 comments on commit 6f3bcc0

Please sign in to comment.