Skip to content

Commit

Permalink
replace DownloadTargetMetaFileTask with TargetVersionSource
Browse files Browse the repository at this point in the history
  • Loading branch information
supersaiyansubtlety committed Nov 28, 2024
1 parent 7ae0d89 commit a347ea9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 77 deletions.
27 changes: 9 additions & 18 deletions buildSrc/src/main/java/quilt/internal/plugin/TargetDiffPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
import quilt.internal.task.diff.DecompileTargetVineflowerTask;
import quilt.internal.task.diff.DiffDirectoriesTask;
import quilt.internal.task.diff.DownloadTargetMappingJarTask;
import quilt.internal.task.diff.DownloadTargetMetaFileTask;
import quilt.internal.task.diff.ExtractTargetMappingJarTask;
import quilt.internal.task.diff.RemapTargetMinecraftJarTask;
import quilt.internal.task.diff.RemapTargetUnpickDefinitionsTask;
import quilt.internal.task.diff.TargetVersionConsumingTask;
import quilt.internal.task.diff.UnpickTargetJarTask;
import quilt.internal.task.diff.UnpickVersionsMatchConsumingTask;
import quilt.internal.task.setup.DownloadMinecraftLibrariesTask;
import quilt.internal.util.TargetVersionSource;

import java.io.FileReader;
import java.io.IOException;
Expand All @@ -57,8 +57,7 @@
* {@link TargetVersionConsumingTask}s with the following defaults:
* <ul>
* <li> {@link TargetVersionConsumingTask#getTargetVersion() targetVersion}:
* {@value DownloadTargetMetaFileTask#DOWNLOAD_TARGET_META_FILE_TASK_NAME}'s
* {@linkplain DownloadTargetMetaFileTask#provideTargetVersion() provided target version}
* a version obtained from a {@link TargetVersionSource}
* <li> run {@link Task#onlyIf(Spec) onlyIf} their
* {@link TargetVersionConsumingTask#getTargetVersion() targetVersion}
* {@link Provider#isPresent() isPresent}
Expand Down Expand Up @@ -100,22 +99,14 @@ public void apply(@NotNull Project project) {
final TaskContainer tasks = project.getTasks();

{
final var downloadTargetMetaFile = tasks.register(
DownloadTargetMetaFileTask.DOWNLOAD_TARGET_META_FILE_TASK_NAME,
DownloadTargetMetaFileTask.class,
task -> {
task.getMinecraftVersion().convention(quiltExt.getMinecraftVersion());

task.getDest().convention(this.provideMinecraftBuildFile(
task.getMinecraftVersion().map(createQuiltFileNameBuilder("." + Extensions.JSON))
));
}
);

// put mapped provider in a property so all tasks use the same cached value
final Property<String> targetVersion = this.getObjects().property(String.class);
targetVersion.set(
downloadTargetMetaFile.flatMap(DownloadTargetMetaFileTask::provideTargetVersion)
this.getProviders().of(
TargetVersionSource.class,
spec -> spec.parameters(params ->
params.getMinecraftVersion().set(quiltExt.getMinecraftVersion())
)
)
);

tasks.withType(TargetVersionConsumingTask.class).configureEach(task -> {
Expand Down Expand Up @@ -297,7 +288,7 @@ public static Provider<Boolean> provideUnpickVersionsMatch(

return parsed.getAsJsonObject().get("unpickVersion").getAsString();
})
.map(targetVersion -> targetVersion.equals(unpickVersion))
.map(targetUnpickVersion -> targetUnpickVersion.equals(unpickVersion))
.orElse(false);
}

Expand Down

This file was deleted.

24 changes: 24 additions & 0 deletions buildSrc/src/main/java/quilt/internal/util/DownloadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_OK;

public final class DownloadUtil {
private DownloadUtil() { }
Expand Down Expand Up @@ -94,4 +99,23 @@ public static URL urlOf(String url) {
public static URL urlOfUnhandled(String url) throws MalformedURLException, URISyntaxException {
return new URI(url).toURL();
}

public static Optional<HttpURLConnection> openAvailableConnection(String httpUrl) {
return openAvailableConnection(urlOf(httpUrl));
}

public static Optional<HttpURLConnection> openAvailableConnection(URL httpUrl) {
try {
if (httpUrl.openConnection() instanceof HttpURLConnection connection) {
final int code = connection.getResponseCode();
if (code >= HTTP_OK && code < HTTP_BAD_REQUEST) {
return Optional.of(connection);
}
}
} catch (IOException e) {
throw new GradleException("Failed to connect to url: " + httpUrl, e);
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package quilt.internal.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.gradle.api.GradleException;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.ValueSource;
import org.gradle.api.provider.ValueSourceParameters;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.stream.StreamSupport;

import static quilt.internal.util.DownloadUtil.openAvailableConnection;

public abstract class TargetVersionSource implements ValueSource<String, TargetVersionSource.Params> {
private static final Logger LOGGER = LoggerFactory.getLogger(TargetVersionSource.class);

@Override
@Nullable
public String obtain() {
final String url = "https://meta.quiltmc.org/v3/versions/quilt-mappings/" +
this.getParameters().getMinecraftVersion().get();

return openAvailableConnection(url)
.flatMap(connection -> {
final JsonElement meta;
try (var in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
meta = JsonParser.parseReader(in);
} catch (IOException e) {
throw new GradleException("Failed to read meta file from url: " + url, e);
}

return StreamSupport.stream(meta.getAsJsonArray().spliterator(), false)
.map(JsonElement::getAsJsonObject)
.max(Comparator.comparing(
object -> object.get("build").getAsInt(),
Integer::compare
))
.map(object -> object.get("version"))
.map(JsonElement::getAsString);
})
.orElseGet(() -> {
LOGGER.warn(":target version meta unavailable");

return null;
});
}

public interface Params extends ValueSourceParameters {
Property<String> getMinecraftVersion();
}
}

1 comment on commit a347ea9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No difference between head and target.

Please sign in to comment.