Skip to content

Commit

Permalink
Allow accessing uploadFile as a RegularFileProperty (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 15, 2023
1 parent f1be52e commit c9dc1f2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
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.7.1'
version = '2.7.2'
group = 'com.modrinth.minotaur'
archivesBaseName = 'Minotaur'
description = 'Modrinth plugin for publishing builds to the website!'
Expand Down
35 changes: 15 additions & 20 deletions src/main/java/com/modrinth/minotaur/Minotaur.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -12,8 +11,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

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

Expand Down Expand Up @@ -77,27 +74,25 @@ public void apply(final Project project) {
return;
}

Task task = evaluatedProject.getTasks().getByName("modrinth");
List<Object> candidateAATs = new ArrayList<>();
evaluatedProject.getTasks().named("modrinth", TaskModrinthUpload.class).configure(task -> {
task.getWiredInputFiles().from(ext.getFile());

candidateAATs.add(ext.getUploadFile().getOrNull());
candidateAATs.addAll(ext.getAdditionalFiles().get());
ext.getAdditionalFiles().get().forEach(file -> {
if (file == null) {
return;
}

candidateAATs.forEach(file -> {
if (file == null) {
return;
}
// Try to get an AbstractArchiveTask from the input file by whatever means possible.
if (file instanceof AbstractArchiveTask) {
task.dependsOn(file);
} else if (file instanceof TaskProvider<?> &&
((TaskProvider<?>) file).get() instanceof AbstractArchiveTask) {
task.dependsOn(((TaskProvider<?>) file).get());
}
});

// Try to get an AbstractArchiveTask from the input file by whatever means possible.
if (file instanceof AbstractArchiveTask) {
task.dependsOn(file);
} else if (file instanceof TaskProvider<?> &&
((TaskProvider<?>) file).get() instanceof AbstractArchiveTask) {
task.dependsOn(((TaskProvider<?>) file).get());
}
evaluatedProject.getLogger().debug("Made the `modrinth` task depend on the upload file and additional files.");
});

evaluatedProject.getLogger().debug("Made the `modrinth` task depend on the upload file and additional files.");
});

project.getLogger().debug("Successfully applied the Modrinth plugin!");
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/modrinth/minotaur/ModrinthExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.modrinth.minotaur.dependencies.container.DependencyDSL;
import masecla.modrinth4j.model.version.ProjectVersion.VersionType;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;

Expand All @@ -13,7 +14,8 @@
*/
public class ModrinthExtension extends DependencyDSL {
private final Property<String> apiUrl, token, projectId, versionNumber, versionName, changelog, versionType, syncBodyFrom;
private final Property<Object> uploadFile;
private final Property<Object> legacyUploadFile;
private final RegularFileProperty file;
private final ListProperty<Object> additionalFiles;
private final ListProperty<String> gameVersions, loaders;
private final ListProperty<Dependency> dependencies;
Expand Down Expand Up @@ -51,7 +53,8 @@ public ModrinthExtension(Project project) {
versionNumber = project.getObjects().property(String.class);
versionName = project.getObjects().property(String.class);
changelog = project.getObjects().property(String.class).convention(DEFAULT_CHANGELOG);
uploadFile = project.getObjects().property(Object.class);
legacyUploadFile = project.getObjects().property(Object.class);
file = project.getObjects().fileProperty().convention(legacyUploadFile.flatMap(o -> Util.resolveFileProperty(project, o)));
additionalFiles = project.getObjects().listProperty(Object.class).empty();
versionType = project.getObjects().property(String.class).convention(DEFAULT_VERSION_TYPE);
gameVersions = project.getObjects().listProperty(String.class).empty();
Expand Down Expand Up @@ -116,7 +119,16 @@ public Property<String> getChangelog() {
* {@link Util#resolveFile(Project, Object)}.
*/
public Property<Object> getUploadFile() {
return this.uploadFile;
return this.legacyUploadFile;
}

/**
* The upload artifact file.
*
* @return file property
*/
public RegularFileProperty getFile() {
return this.file;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/modrinth/minotaur/TaskModrinthUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import masecla.modrinth4j.model.version.ProjectVersion.VersionType;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.plugins.PluginManager;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.jetbrains.annotations.ApiStatus;

Expand All @@ -24,7 +27,7 @@
/**
* A task used to communicate with Modrinth for the purpose of uploading build artifacts.
*/
public class TaskModrinthUpload extends DefaultTask {
public abstract class TaskModrinthUpload extends DefaultTask {
/**
* The response from the API when the file was uploaded successfully.
*/
Expand Down Expand Up @@ -55,6 +58,16 @@ public boolean wasUploadSuccessful() {
return newVersion != null;
}

/**
* Input property used to add automatic task dependencies.
*
* @return property
*/
@InputFiles
@Optional
@ApiStatus.Internal
public abstract ConfigurableFileCollection getWiredInputFiles();

/**
* Defines what to do when the Modrinth upload task is invoked.
* <ol>
Expand Down Expand Up @@ -145,7 +158,7 @@ public void apply() {

// Get each of the files, starting with the primary file
List<File> files = new ArrayList<>();
files.add(resolveFile(getProject(), ext.getUploadFile().get()));
files.add(ext.getFile().get().getAsFile());

// Convert each of the Object files from the extension to a proper File
ext.getAdditionalFiles().get().forEach(file -> {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/modrinth/minotaur/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import masecla.modrinth4j.main.ModrinthAPI;
import masecla.modrinth4j.model.user.ModrinthUser;
import org.gradle.api.Project;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -106,4 +108,32 @@ static File resolveFile(Project project, Object in) {
// None of the previous checks worked. Fall back to Gradle's built-in file resolution mechanics.
return project.file(in);
}

static Provider<RegularFile> resolveFileProperty(Project project, Object in) {
if (in == null) {
// If input is null we can't really do anything...
return project.getObjects().fileProperty();
} else if (in instanceof File) {
// If the file is a Java file handle no additional handling is needed.
return project.getLayout().file(project.provider(() -> (File) in));
} else if (in instanceof AbstractArchiveTask) {
// Grabs the file from an archive task. Allows build scripts to do things like the jar task directly.
return ((AbstractArchiveTask) in).getArchiveFile();
} else if (in instanceof TaskProvider<?>) {
// Grabs the file from an archive task wrapped in a provider. Allows Kotlin DSL buildscripts to also specify
// the jar task directly, rather than having to call #get() before running.
Object provided = ((TaskProvider<?>) in).get();

return ((TaskProvider<?>) in).flatMap(task -> {
// Check to see if the task provided is actually an AbstractArchiveTask.
if (provided instanceof AbstractArchiveTask) {
return ((AbstractArchiveTask) provided).getArchiveFile();
}
return project.getLayout().file(project.provider(() -> project.file(in)));
});
}

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

0 comments on commit c9dc1f2

Please sign in to comment.