Skip to content

Commit

Permalink
Add the ability to specify version dependencies by number (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
triphora authored Jan 3, 2023
1 parent 4def590 commit 8b0e988
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 60 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ modrinth {
// The scope can be `required`, `optional`, `incompatible`, or `embedded`
// The type can either be `project` or `version`
required.project "fabric-api" // Creates a new required dependency on Fabric API
optional.version "sodium", "mc1.19.3-0.4.8" // Creates a new optional dependency on this specific version of Sodium
}
}
```
Expand Down Expand Up @@ -93,6 +94,7 @@ modrinth {
// The scope can be `required`, `optional`, `incompatible`, or `embedded`
// The type can either be `project` or `version`
required.project("fabric-api") // Creates a new required dependency on Fabric API
optional.version("sodium", "mc1.19.3-0.4.8") // Creates a new optional dependency on this specific version of Sodium
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'com.gradle.plugin-publish' version '1.1.0'
}

version = '2.5.0'
version = '2.6.0'
group = 'com.modrinth.minotaur'
archivesBaseName = 'Minotaur'
description = 'Modrinth plugin for publishing builds to the website!'
Expand Down
24 changes: 8 additions & 16 deletions fg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,27 @@ dependencies {
minecraft "net.minecraftforge:forge:1.18.1-39.0.9"
}

idea {
module {
downloadJavadoc = false
downloadSources = false
}
}

java {
withSourcesJar()
}
java.withSourcesJar()

import com.modrinth.minotaur.dependencies.DependencyType
import com.modrinth.minotaur.dependencies.ModDependency
import com.modrinth.minotaur.dependencies.VersionDependency

modrinth {
apiUrl = STAGING_API_URL
projectId = "freeeedom"
projectId = "fred-3"
uploadFile = jar
additionalFiles = [project.file("build/libs/fg-$version-sources.jar")]
versionType = "beta"
dependencies = [
new ModDependency("big-mod", "optional"),
new VersionDependency("IgXqCrMH", DependencyType.INCOMPATIBLE)
new ModDependency("test-project", "optional"),
new VersionDependency("CE5sWFeF", DependencyType.INCOMPATIBLE)
]
dependencies {
required.project "modmod"
incompatible.version "M8VeghJn"
embedded.project "updatefixerupper"
required.project "corrupted"
optional.version "UakHulqw"
incompatible.version "kdIr8lsr", "b1.2+mod"
embedded.project "sbbGxWge"
}
debugMode = true
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
24 changes: 8 additions & 16 deletions loom/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,27 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:0.12.12"
}

idea {
module {
downloadJavadoc = false
downloadSources = false
}
}

java {
withSourcesJar()
}
java.withSourcesJar()

import com.modrinth.minotaur.dependencies.DependencyType
import com.modrinth.minotaur.dependencies.ModDependency
import com.modrinth.minotaur.dependencies.VersionDependency

modrinth {
apiUrl = STAGING_API_URL
projectId = "freeeedom"
projectId = "fred-3"
uploadFile = remapJar
additionalFiles = [project.file("build/libs/loom-$version-sources.jar")]
versionType = "beta"
dependencies = [
new ModDependency("big-mod", "optional"),
new VersionDependency("IgXqCrMH", DependencyType.INCOMPATIBLE)
new ModDependency("test-project", "optional"),
new VersionDependency("CE5sWFeF", DependencyType.INCOMPATIBLE)
]
dependencies {
required.project "modmod"
incompatible.version "M8VeghJn"
embedded.project "updatefixerupper"
required.project "corrupted"
optional.version "UakHulqw"
incompatible.version "kdIr8lsr", "b1.2+mod"
embedded.project "sbbGxWge"
}
debugMode = true
}
12 changes: 7 additions & 5 deletions src/main/java/com/modrinth/minotaur/TaskModrinthUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.modrinth.minotaur.dependencies.Dependency;
import com.modrinth.minotaur.dependencies.ModDependency;
import com.modrinth.minotaur.dependencies.VersionDependency;
import com.modrinth.minotaur.request.VersionData;
import com.modrinth.minotaur.responses.ResponseError;
import com.modrinth.minotaur.responses.ResponseUpload;
Expand All @@ -28,10 +29,7 @@
import java.util.List;
import java.util.Locale;

import static com.modrinth.minotaur.Util.createHttpClient;
import static com.modrinth.minotaur.Util.getExtension;
import static com.modrinth.minotaur.Util.getUploadEndpoint;
import static com.modrinth.minotaur.Util.resolveId;
import static com.modrinth.minotaur.Util.*;

/**
* A task used to communicate with Modrinth for the purpose of uploading build artifacts.
Expand Down Expand Up @@ -134,8 +132,12 @@ public void apply() {
if (dependency instanceof ModDependency) {
String id = resolveId(this.getProject(), ((ModDependency) dependency).getProjectId(), log);
this.dependencies.add(new ModDependency(id, dependency.getDependencyType()));
} else if (dependency instanceof VersionDependency) {
VersionDependency dep = (VersionDependency) dependency;
String id = resolveVersionId(this.getProject(), dep.getProjectId(), dep.getVersionId(), log);
this.dependencies.add(new VersionDependency(id, dependency.getDependencyType()));
} else {
this.dependencies.add(dependency);
throw new GradleException("Dependency was not an instance of ModDependency or VersionDependency!");
}
}

Expand Down
51 changes: 47 additions & 4 deletions src/main/java/com/modrinth/minotaur/Util.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.modrinth.minotaur;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.*;
import com.modrinth.minotaur.responses.ResponseError;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -101,6 +98,52 @@ static String resolveId(Project project, String projectId, Logger log) throws IO
return element.getAsJsonObject().get("id").getAsString();
}

/**
* Returns a project ID from a project ID or slug
*
* @param versionId ID or version number of the project to resolve
* @param log Logger to use
* @return ID of the resolved project
*/
static String resolveVersionId(Project project, String projectId, String versionId, Logger log) throws IOException {
HttpClient client = createHttpClient();
HttpGet getVersion = new HttpGet(String.format("%sversion/%s", getUploadEndpoint(project), versionId));

if (client.execute(getVersion).getStatusLine().getStatusCode() == 200) {
return versionId;
}

String id = resolveId(project, projectId, log);
HttpGet getVersions = new HttpGet(String.format("%s/project/%s/version", getUploadEndpoint(project), id));
HttpResponse response = client.execute(getVersions);

int code = response.getStatusLine().getStatusCode();
if (code != 200) {
String error = String.format("Failed to resolve versions of project ID \"%s\"! Received status code %s", id, code);
log.error(error);
throw new GradleException(error, new IllegalStateException(error));
}

String returned = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
JsonElement versions = JsonParser.parseString(returned);
if (!versions.isJsonArray()) {
String error = "Invalid API response during version ID resolution! Expected JSON array but got: " + returned;
log.error(error);
throw new GradleException(error, new IllegalStateException(error));
}

for (JsonElement element : versions.getAsJsonArray()) {
JsonObject version = element.getAsJsonObject();
if (version.get("version_number").getAsString().equals(versionId)) {
return version.get("id").getAsString();
}
}

String error = String.format("Failed to resolve version number \"%s\"!", versionId);
log.error(error);
throw new GradleException(error);
}

/**
* Attempts to resolve a file using an arbitrary object provided by a user defined gradle
* task.
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/modrinth/minotaur/dependencies/Dependency.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,32 @@ public class Dependency {
*/
@Expose
@SerializedName("dependency_type")
private final String dependencyType;
private final DependencyType dependencyType;

/**
* Creates a new dependency relationship.
*
* @param id The ID of the project or version to create a dependency with.
* @param type The type of dependency being created.
*/
@ApiStatus.Internal
Dependency(String id, DependencyType type) {
this.dependencyType = type.toString().toLowerCase(Locale.ROOT);
Dependency(DependencyType type) {
this.dependencyType = type;
}

/**
* Creates a new dependency relationship.
*
* @param id The ID of the project or version to create a dependency with.
* @param type The type of dependency being created.
*/
@ApiStatus.Internal
Dependency(String id, String type) {
this.dependencyType = type;
Dependency(String type) {
this.dependencyType = DependencyType.fromString(type);
}

/**
* @return {@link #dependencyType}
*/
public String getDependencyType() {
return this.dependencyType;
return this.dependencyType.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import com.google.gson.annotations.SerializedName;

import java.util.Locale;

/**
* The enum representing the three types of dependencies supported by Modrinth.
*/
@SuppressWarnings("unused")
public enum DependencyType {
/**
* The version requires an instance of the project/version to work.
Expand All @@ -24,9 +25,35 @@ public enum DependencyType {
*/
@SerializedName("incompatible")
INCOMPATIBLE,

/**
* The version contains this other project/version within it.
*/
@SerializedName("embedded")
EMBEDDED
EMBEDDED;

public String toString() {
return this.name().toLowerCase(Locale.ROOT);
}

/**
* @param in string input
* @return a {@link DependencyType} from a String
* @throws IllegalStateException when the input is not one of the recognized types
*/
public static DependencyType fromString(String in) {
// Java 8 :evaporate:
if (in.equalsIgnoreCase("required")) {
return REQUIRED;
} else if (in.equalsIgnoreCase("optional")) {
return OPTIONAL;
} else if (in.equalsIgnoreCase("incompatible")) {
return INCOMPATIBLE;
} else if (in.equalsIgnoreCase("embedded")) {
return EMBEDDED;
}

throw new IllegalStateException("Invalid dependency type specified!" +
"Must be one of `required`, `optional`, `incompatible`, or `embedded`");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.Locale;

/**
* Class for making a dependency on a mod.
*/
Expand All @@ -23,7 +25,7 @@ public class ModDependency extends Dependency {
* @param type The type of dependency being created.
*/
public ModDependency(String id, DependencyType type) {
super(id, type);
super(type.toString().toLowerCase(Locale.ROOT));
this.projectId = id;
}

Expand All @@ -34,7 +36,7 @@ public ModDependency(String id, DependencyType type) {
* @param type The type of dependency being created.
*/
public ModDependency(String id, String type) {
super(id, type);
super(type);
this.projectId = id;
}

Expand Down
Loading

0 comments on commit 8b0e988

Please sign in to comment.