Skip to content

Commit

Permalink
Remove Minotaur.project (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellemes authored Nov 6, 2022
1 parent 62a7681 commit acabe2c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
11 changes: 1 addition & 10 deletions src/main/java/com/modrinth/minotaur/Minotaur.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.jetbrains.annotations.ApiStatus;

import static com.modrinth.minotaur.Util.getExtension;

/**
* The main class for Minotaur.
*/
public class Minotaur implements Plugin<Project> {
/**
* Internal utility for grabbing the project which Minotaur is applied to
*/
@ApiStatus.Internal
public static Project project;

/**
* Creates the {@link ModrinthExtension} for the project and registers the {@code modrinth} and
* {@code modrinthSyncBody} tasks.
* @param project The Gradle project which Minotaur is applied to
*/
@Override
public void apply(final Project project) {
Minotaur.project = project;

project.getExtensions().create("modrinth", ModrinthExtension.class, project);
project.getLogger().debug("Created the `modrinth` extension.");

Expand All @@ -48,7 +39,7 @@ public void apply(final Project project) {
project.getLogger().debug("Registered the `modrinthSyncBody` task.");

project.afterEvaluate(evaluatedProject -> {
ModrinthExtension extension = getExtension();
ModrinthExtension extension = getExtension(evaluatedProject);
Task task = evaluatedProject.getTasks().getByName("modrinth");
if (extension.getUploadFile().getOrNull() != null) {
Object uploadFile = extension.getUploadFile().get();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/modrinth/minotaur/ModrinthExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Property<String> getChangelog() {

/**
* @return The upload artifact file. This can be any object type that is resolvable by
* {@link Util#resolveFile(Object)}.
* {@link Util#resolveFile(Project, Object)}.
*/
public Property<Object> getUploadFile() {
return this.uploadFile;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/modrinth/minotaur/TaskModrinthSyncBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TaskModrinthSyncBody extends DefaultTask {
/**
* The extension used for getting the data supplied in the buildscript.
*/
private final ModrinthExtension extension = getExtension();
private final ModrinthExtension extension = getExtension(this.getProject());

/**
* The response from the API when the body failed to upload.
Expand All @@ -53,7 +53,7 @@ public void apply() {
String excludeRegex = "(?m)<!-- modrinth_exclude\\.start -->(.|\n|\r\n)*?<!-- modrinth_exclude\\.end -->";

final HttpClient client = Util.createHttpClient();
final HttpPatch patch = new HttpPatch(getUploadEndpoint() + "project/" + resolveId(extension.getProjectId().get()));
final HttpPatch patch = new HttpPatch(getUploadEndpoint(this.getProject()) + "project/" + resolveId(this.getProject(), extension.getProjectId().get()));

patch.addHeader("Authorization", extension.getToken().get());

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/modrinth/minotaur/TaskModrinthUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class TaskModrinthUpload extends DefaultTask {
/**
* The extension used for getting the data supplied in the buildscript.
*/
private final ModrinthExtension extension = getExtension();
private final ModrinthExtension extension = getExtension(this.getProject());

/**
* Easy way to access Gradle's logging.
Expand Down Expand Up @@ -132,7 +132,7 @@ public void apply() {
dependencies.addAll(extension.getDependencies().get());
for (Dependency dependency : dependencies) {
if (dependency instanceof ModDependency) {
String id = resolveId(((ModDependency) dependency).getProjectId(), log);
String id = resolveId(this.getProject(), ((ModDependency) dependency).getProjectId(), log);
this.dependencies.add(new ModDependency(id, dependency.getDependencyType()));
} else {
this.dependencies.add(dependency);
Expand All @@ -141,11 +141,11 @@ public void apply() {

List<Object> fileObjects = new ArrayList<>();
List<File> filesToUpload = new ArrayList<>();
fileObjects.add(Util.resolveFile(extension.getUploadFile().get()));
fileObjects.add(Util.resolveFile(this.getProject(), extension.getUploadFile().get()));
fileObjects.addAll(extension.getAdditionalFiles().get());

for (Object fileObject : fileObjects) {
final File resolvedFile = Util.resolveFile(fileObject);
final File resolvedFile = Util.resolveFile(this.getProject(), fileObject);

// Ensure the file actually exists before trying to upload it.
if (resolvedFile == null || !resolvedFile.exists()) {
Expand Down Expand Up @@ -175,7 +175,7 @@ public void apply() {
*/
public void upload(List<File> files) throws IOException {
final HttpClient client = createHttpClient();
final HttpPost post = new HttpPost(getUploadEndpoint() + "version");
final HttpPost post = new HttpPost(getUploadEndpoint(this.getProject()) + "version");

post.addHeader("Authorization", extension.getToken().get());

Expand All @@ -188,7 +188,7 @@ public void upload(List<File> files) throws IOException {
}

final VersionData data = new VersionData();
data.setProjectId(resolveId(extension.getProjectId().get()));
data.setProjectId(resolveId(this.getProject(), extension.getProjectId().get()));
data.setVersionNumber(extension.getVersionNumber().get());
data.setVersionTitle(extension.getVersionName().get());
data.setChangelog(extension.getChangelog().get().replaceAll("\r\n", "\n"));
Expand All @@ -208,7 +208,7 @@ public void upload(List<File> files) throws IOException {
form.addTextBody("data", GSON.toJson(data), ContentType.APPLICATION_JSON);

for (int i = 0; i < files.size(); i++) {
log.debug("Uploading {} to {}.", files.get(i).getPath(), getUploadEndpoint() + "version");
log.debug("Uploading {} to {}.", files.get(i).getPath(), getUploadEndpoint(this.getProject()) + "version");
form.addBinaryBody(String.valueOf(i), files.get(i));
}

Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/modrinth/minotaur/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,8 +30,8 @@ class Util {
/**
* @return The {@link ModrinthExtension} for the project
*/
static ModrinthExtension getExtension() {
return Minotaur.project.getExtensions().getByType(ModrinthExtension.class);
static ModrinthExtension getExtension(Project project) {
return project.getExtensions().getByType(ModrinthExtension.class);
}

/**
Expand All @@ -54,8 +55,8 @@ static Gson createGsonInstance() {
*
* @return The upload API endpoint.
*/
static String getUploadEndpoint() {
String apiUrl = getExtension().getApiUrl().get();
static String getUploadEndpoint(Project project) {
String apiUrl = getExtension(project).getApiUrl().get();
return apiUrl + (apiUrl.endsWith("/") ? "" : "/");
}

Expand All @@ -65,8 +66,8 @@ static String getUploadEndpoint() {
* @param projectId ID or slug of the project to resolve
* @return ID of the resolved project
*/
static String resolveId(String projectId) throws IOException {
return resolveId(projectId, Minotaur.project.getLogger());
static String resolveId(Project project, String projectId) throws IOException {
return resolveId(project, projectId, project.getLogger());
}

/**
Expand All @@ -76,10 +77,10 @@ static String resolveId(String projectId) throws IOException {
* @param log Logger to use
* @return ID of the resolved project
*/
static String resolveId(String projectId, Logger log) throws IOException {
static String resolveId(Project project, String projectId, Logger log) throws IOException {
HttpClient client = createHttpClient();
HttpGet get = new HttpGet(String.format("%sproject/%s/check", getUploadEndpoint(), projectId));
get.addHeader("Authorization", getExtension().getToken().get());
HttpGet get = new HttpGet(String.format("%sproject/%s/check", getUploadEndpoint(project), projectId));
get.addHeader("Authorization", getExtension(project).getToken().get());
HttpResponse response = client.execute(get);

int code = response.getStatusLine().getStatusCode();
Expand Down Expand Up @@ -111,7 +112,7 @@ static String resolveId(String projectId, Logger log) throws IOException {
* @return A file handle for the resolved input. If the input can not be resolved this will be null or the fallback.
*/
@Nullable
static File resolveFile(Object in) {
static File resolveFile(Project project, Object in) {
if (in == null) {
// If input is null we can't really do anything...
return null;
Expand All @@ -133,6 +134,6 @@ static File resolveFile(Object in) {
}

// None of the previous checks worked. Fall back to Gradle's built-in file resolution mechanics.
return Minotaur.project.file(in);
return project.file(in);
}
}

0 comments on commit acabe2c

Please sign in to comment.