-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Promote native dependencies to compile-time dependencies if they're i…
…n all platforms
- Loading branch information
Showing
3 changed files
with
177 additions
and
2 deletions.
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
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
122 changes: 122 additions & 0 deletions
122
buildSrc/src/main/java/net/neoforged/minecraftdependencies/MavenCoordinate.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,122 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Models the Maven coordinates for an artifact. | ||
*/ | ||
public record MavenCoordinate(String groupId, String artifactId, String extension, String classifier, String version) { | ||
public MavenCoordinate { | ||
Objects.requireNonNull(groupId); | ||
Objects.requireNonNull(artifactId); | ||
Objects.requireNonNull(version); | ||
if (extension == null) { | ||
extension = ""; | ||
} | ||
if (classifier == null) { | ||
classifier = ""; | ||
} | ||
} | ||
|
||
/** | ||
* Valid forms: | ||
* <ul> | ||
* <li>{@code groupId:artifactId:version}</li> | ||
* <li>{@code groupId:artifactId:version:classifier}</li> | ||
* <li>{@code groupId:artifactId:version:classifier@extension}</li> | ||
* <li>{@code groupId:artifactId:version@extension}</li> | ||
* </ul> | ||
*/ | ||
public static MavenCoordinate parse(String coordinate) { | ||
var coordinateAndExt = coordinate.split("@"); | ||
String extension = ""; | ||
if (coordinateAndExt.length > 2) { | ||
throw new IllegalArgumentException("Malformed Maven coordinate: " + coordinate); | ||
} else if (coordinateAndExt.length == 2) { | ||
extension = coordinateAndExt[1]; | ||
coordinate = coordinateAndExt[0]; | ||
} | ||
|
||
var parts = coordinate.split(":"); | ||
if (parts.length != 3 && parts.length != 4) { | ||
throw new IllegalArgumentException("Malformed Maven coordinate: " + coordinate); | ||
} | ||
|
||
var groupId = parts[0]; | ||
var artifactId = parts[1]; | ||
var version = parts[2]; | ||
var classifier = parts.length == 4 ? parts[3] : ""; | ||
return new MavenCoordinate(groupId, artifactId, extension, classifier, version); | ||
} | ||
|
||
/** | ||
* Constructs a path relative to the root of a Maven repository pointing to the artifact expressed through | ||
* these coordinates. | ||
*/ | ||
public Path toRelativeRepositoryPath() { | ||
final String fileName = artifactId + "-" + version + | ||
(!classifier.isEmpty() ? "-" + classifier : "") + | ||
(!extension.isEmpty() ? "." + extension : ".jar"); | ||
|
||
String[] groups = groupId.split("\\."); | ||
Path result = Paths.get(groups[0]); | ||
for (int i = 1; i < groups.length; i++) { | ||
result = result.resolve(groups[i]); | ||
} | ||
|
||
return result.resolve(artifactId).resolve(version).resolve(fileName); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var result = new StringBuilder(); | ||
result.append(groupId).append(":").append(artifactId).append(":").append(version); | ||
if (!classifier.isEmpty()) { | ||
result.append(":").append(classifier); | ||
} | ||
if (!extension.isEmpty()) { | ||
result.append("@").append(extension); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
public URI toRepositoryUri(URI baseUri) { | ||
var originalBaseUri = baseUri.toString(); | ||
var relativePath = toRelativeRepositoryPath().toString().replace('\\', '/'); | ||
if (originalBaseUri.endsWith("/")) { | ||
return URI.create(originalBaseUri + relativePath); | ||
} else { | ||
return URI.create(originalBaseUri + "/" + relativePath); | ||
} | ||
} | ||
|
||
public boolean equalsIgnoringVersion(MavenCoordinate other) { | ||
return groupId.equals(other.groupId) | ||
&& artifactId.equals(other.artifactId) | ||
&& extension.equals(other.extension) | ||
&& classifier.equals(other.classifier); | ||
} | ||
|
||
public MavenCoordinate withClassifier(String classifier) { | ||
return new MavenCoordinate( | ||
groupId, | ||
artifactId, | ||
extension, | ||
classifier, | ||
version | ||
); | ||
} | ||
|
||
public MavenCoordinate withVersion(String version) { | ||
return new MavenCoordinate( | ||
groupId, | ||
artifactId, | ||
extension, | ||
classifier, | ||
version | ||
); | ||
} | ||
} |