-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic version support #55
Open
shartte
wants to merge
1
commit into
main
Choose a base branch
from
dynversions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,10 +126,28 @@ public Artifact get(MavenCoordinate mavenCoordinate) throws IOException { | |
return externalArtifact; | ||
} | ||
|
||
// Yet another special case: dynamic versions! | ||
// Used in 1.12.1, for example. And yes, this will be very slow. | ||
if (mavenCoordinate.isDynamicVersion()) { | ||
var availableVersions = MavenMetadata.gatherVersions( | ||
downloadManager, | ||
repositoryBaseUrls, | ||
mavenCoordinate.groupId(), | ||
mavenCoordinate.artifactId() | ||
); | ||
for (var availableVersion : availableVersions) { | ||
if (mavenCoordinate.matchesVersion(availableVersion.version())) { | ||
var concreteMavenCoordinate = mavenCoordinate.withVersion(availableVersion.version()); | ||
return get(concreteMavenCoordinate, availableVersion.repositoryUrl()); | ||
} | ||
} | ||
} | ||
|
||
var finalLocation = artifactsCache.resolve(mavenCoordinate.toRelativeRepositoryPath()); | ||
|
||
// Special case: NeoForge reference libraries that are only available via the Mojang download server | ||
if (mavenCoordinate.groupId().equals("com.mojang") && mavenCoordinate.artifactId().equals("logging")) { | ||
if (mavenCoordinate.groupId().equals("com.mojang") && mavenCoordinate.artifactId().equals("logging") | ||
|| mavenCoordinate.groupId().equals("net.minecraft") && mavenCoordinate.artifactId().equals("launchwrapper")) { | ||
return get(mavenCoordinate, MINECRAFT_LIBRARIES_URI); | ||
} | ||
|
||
|
@@ -219,8 +237,8 @@ public Artifact downloadFromManifest(MinecraftVersionManifest versionManifest, S | |
var downloadSpec = versionManifest.downloads().get(type); | ||
if (downloadSpec == null) { | ||
throw new IllegalArgumentException("Minecraft version manifest " + versionManifest.id() | ||
+ " does not declare a download for " + type + ". Available: " | ||
+ versionManifest.downloads().keySet()); | ||
+ " does not declare a download for " + type + ". Available: " | ||
+ versionManifest.downloads().keySet()); | ||
} | ||
|
||
var extension = FilenameUtil.getExtension(downloadSpec.uri().getPath()); | ||
|
@@ -261,11 +279,22 @@ public interface DownloadAction { | |
private Artifact getFromExternalManifest(MavenCoordinate artifactCoordinate) { | ||
artifactCoordinate = normalizeExtension(artifactCoordinate); | ||
|
||
// Try direct match first | ||
var artifact = externallyProvided.get(artifactCoordinate); | ||
if (artifact != null) { | ||
return artifact; | ||
} | ||
|
||
// See if it's a dynamic version and match against all entries | ||
if (artifactCoordinate.isDynamicVersion()) { | ||
for (var entry : externallyProvided.entrySet()) { | ||
if (artifactCoordinate.matchesVersion(entry.getKey().version()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this check should be matches artifact first and version second |
||
&& entry.getKey().withVersion(artifactCoordinate.version()).equals(artifactCoordinate)) { | ||
return entry.getValue(); | ||
} | ||
} | ||
} | ||
|
||
// Fall back to looking up a wildcard version for dependency replacement in includeBuild scenarios | ||
if (!"*".equals(artifactCoordinate.version())) { | ||
artifact = externallyProvided.get(artifactCoordinate.withVersion("*")); | ||
|
92 changes: 92 additions & 0 deletions
92
src/main/java/net/neoforged/neoform/runtime/artifacts/MavenMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package net.neoforged.neoform.runtime.artifacts; | ||
|
||
import net.neoforged.neoform.runtime.downloads.DownloadManager; | ||
import net.neoforged.neoform.runtime.utils.Logger; | ||
import org.w3c.dom.Element; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Support class for querying maven metadata from a remote repository. | ||
* The format is documented here: https://maven.apache.org/repositories/metadata.html | ||
* We only deal with A-level metadata since we're interested in listing the versions of | ||
* a specific artifact. | ||
*/ | ||
final class MavenMetadata { | ||
private static final Logger LOG = Logger.create(); | ||
|
||
private MavenMetadata() { | ||
} | ||
|
||
static List<AvailableVersion> gatherVersions(DownloadManager downloadManager, | ||
List<URI> repositoryBaseUrls, | ||
String groupId, | ||
String artifactId) throws IOException { | ||
var versions = new ArrayList<AvailableVersion>(); | ||
for (var repositoryBaseUrl : repositoryBaseUrls) { | ||
versions.addAll(gatherVersions(downloadManager, repositoryBaseUrl, groupId, artifactId)); | ||
} | ||
return versions; | ||
} | ||
|
||
static List<AvailableVersion> gatherVersions(DownloadManager downloadManager, | ||
URI repositoryBaseUrl, | ||
String groupId, | ||
String artifactId) throws IOException { | ||
var metadataUri = repositoryBaseUrl.toString(); | ||
if (!metadataUri.endsWith("/")) { | ||
metadataUri += "/"; | ||
} | ||
metadataUri += groupId.replace('.', '/') + "/" + artifactId + "/maven-metadata.xml"; | ||
|
||
byte[] metadataContent; | ||
|
||
var tempFile = Files.createTempFile("maven-metadata", ".xml"); | ||
try { | ||
Files.deleteIfExists(tempFile); // The downloader should assume it does not exist yet | ||
downloadManager.download(URI.create(metadataUri), tempFile); | ||
metadataContent = Files.readAllBytes(tempFile); | ||
} catch (FileNotFoundException fnf) { | ||
return List.of(); // Repository doesn't have artifact | ||
} finally { | ||
Files.deleteIfExists(tempFile); | ||
} | ||
|
||
try (var in = new ByteArrayInputStream(metadataContent)) { | ||
var result = new ArrayList<AvailableVersion>(); | ||
var documentBuilder = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder(); | ||
var document = documentBuilder.parse(in).getDocumentElement(); | ||
var nodes = document.getChildNodes(); | ||
for (var i = 0; i < nodes.getLength(); i++) { | ||
if (nodes.item(i) instanceof Element versioningEl && "versioning".equals(versioningEl.getTagName())) { | ||
for (var versions = versioningEl.getFirstChild(); versions != null; versions = versions.getNextSibling()) { | ||
if (versions instanceof Element versionsEl && "versions".equals(versionsEl.getTagName())) { | ||
for (var child = versionsEl.getFirstChild(); child != null; child = child.getNextSibling()) { | ||
if (child instanceof Element childEl && "version".equals(childEl.getTagName())) { | ||
result.add(new AvailableVersion( | ||
repositoryBaseUrl, | ||
childEl.getTextContent().trim() | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} catch (Exception e) { | ||
LOG.println("Failed to parse Maven metadata from " + metadataUri + ": " + e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
record AvailableVersion(URI repositoryUrl, String version) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm being pedantic but I would surround the ands in their own parenthesis for clarity's sake