Skip to content

Commit

Permalink
Fix uploadInfo and Loom game version detection (#18)
Browse files Browse the repository at this point in the history
- Fix game version detection with Loom
- Fix uploadInfo and related fields in TaskModrinthUpload by removing the afterEvaluate block
- Better CI setup
  • Loading branch information
triphora authored Mar 6, 2022
1 parent ef1535d commit c50ad2d
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4

[*.yml]
indent_size = 2
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on: [ push, pull_request ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: 17
distribution: adopt

- name: Build with Gradle
run: ./gradlew build
env:
GPG_KEYNAME: ${{ secrets.GPG_KEYNAME }}

- name: Upload artifacts
uses: actions/upload-artifact@v2
with:
name: Artifacts
path: build/libs
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml → .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
name: CI
name: Publish

on:
push:
branches:
- master
tags:
- 'v*.*.*'

jobs:
build:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: 8
java-version: 17
distribution: adopt

- name: Build with Gradle
Expand Down
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id 'maven-publish'
}

version = '2.0.1'
version = '2.0.2'
group = 'com.modrinth.minotaur'
archivesBaseName = 'Minotaur'
description = 'Modrinth plugin for publishing builds to the website!'
Expand All @@ -17,10 +17,19 @@ targetCompatibility = JavaVersion.VERSION_1_8

repositories {
mavenCentral()
maven {
name = "Fabric"
url = "https://maven.fabricmc.net"
content {
includeGroup "net.fabricmc"
}
}
}

dependencies {
compileOnly gradleApi()

compileOnly group: "net.fabricmc", name: "fabric-loom", version: "0.11.32"

implementation group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.13'
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13'
Expand Down Expand Up @@ -69,3 +78,9 @@ java {
withSourcesJar()
withJavadocJar()
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = "UTF-8"
//noinspection all
it.options.release = 8
}
36 changes: 8 additions & 28 deletions src/main/java/com/modrinth/minotaur/Minotaur.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,23 @@
/**
* The main class for Minotaur.
*/
@SuppressWarnings("unused")
public class Minotaur implements Plugin<Project> {
/**
* Does the following:
* <ol>
* <li>Creates the {@link ModrinthExtension} for the project</li>
* <li>Waits until the project is done evaluating</li>
* <li>Registers the {@code modrinth} task</li>
* <li>Detects the game versions for Fabric and Forge</li>
* <li>Sets default values that couldn't be done without an evaluated project (version number and name)</li>
* <li>Prints a debug line saying it's done!</li>
* </ol>
* Creates the {@link ModrinthExtension} for the project and registers the {@code modrinth} task
* @param project The Gradle project which Minotaur is applied to
*/
@Override
public void apply(final Project project) {
project.getExtensions().create("modrinth", ModrinthExtension.class, project);
project.getLogger().debug("Created the `modrinth` extension.");

project.afterEvaluate(evaluatedProject -> {
TaskContainer tasks = evaluatedProject.getTasks();
tasks.register("modrinth", TaskModrinthUpload.class, task -> {
task.setGroup("publishing");
task.setDescription("Upload project to Modrinth");
task.dependsOn(tasks.named("build"));
});

TaskModrinthUpload.detectGameVersionForge(evaluatedProject);
TaskModrinthUpload.detectGameVersionFabric(evaluatedProject);

final ModrinthExtension extension = evaluatedProject.getExtensions().getByType(ModrinthExtension.class);
if (extension.getVersionNumber().getOrNull() == null) {
extension.getVersionNumber().set(evaluatedProject.getVersion().toString());
}
if (extension.getVersionName().getOrNull() == null) {
extension.getVersionName().set(extension.getVersionNumber().get());
}
TaskContainer tasks = project.getTasks();
tasks.register("modrinth", TaskModrinthUpload.class, task -> {
task.setGroup("publishing");
task.setDescription("Upload project to Modrinth");
task.dependsOn(tasks.named("build"));
});
project.getLogger().debug("Registered the `modrinth` task.");

project.getLogger().debug("Successfully applied the Modrinth plugin!");
}
Expand Down
71 changes: 39 additions & 32 deletions src/main/java/com/modrinth/minotaur/TaskModrinthUpload.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.modrinth.minotaur;

import com.google.gson.Gson;
import com.modrinth.minotaur.compat.FabricLoomCompatibility;
import com.modrinth.minotaur.request.VersionData;
import com.modrinth.minotaur.responses.ResponseError;
import com.modrinth.minotaur.responses.ResponseUpload;
Expand All @@ -25,7 +26,6 @@
import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
Expand Down Expand Up @@ -72,10 +72,8 @@ public boolean wasUploadSuccessful() {
/**
* Defines what to do when the Modrinth upload task is invoked.
* <ol>
* <li>Attempts to automatically resolve the game version if one wasn't specified, throwing an exception if there
* are still none</li>
* <li>Attempts to automatically resolve the loader if one wasn't specified, throwing an exception if there still
* isn't one</li>
* <li>Attempts to automatically resolve various metadata items if not specified, throwing an exception if some
* things still don't have anything set</li>
* <li>Resolves each file or task to be uploaded, ensuring they're all valid</li>
* <li>Uploads these files to the Modrinth API under a new version</li>
* </ol>
Expand All @@ -84,11 +82,22 @@ public boolean wasUploadSuccessful() {
*/
@TaskAction
public void apply() {
this.getLogger().lifecycle("Minotaur: {}", this.getClass().getPackage().getImplementationVersion());
try {
// Add version number if it's null
if (extension.getVersionNumber().getOrNull() == null) {
extension.getVersionNumber().set(this.getProject().getVersion().toString());
}

// Add version name if it's null
if (extension.getVersionName().getOrNull() == null) {
extension.getVersionName().set(extension.getVersionNumber().get());
}

// Attempt to automatically resolve the game version if one wasn't specified.
if (extension.getGameVersions().get().isEmpty()) {
detectGameVersionForge(this.getProject());
detectGameVersionFabric(this.getProject());
this.detectGameVersionForge();
this.detectGameVersionFabric();
}

if (extension.getGameVersions().get().isEmpty()) {
Expand Down Expand Up @@ -271,9 +280,11 @@ else if (in instanceof TaskProvider<?>) {
/**
* Attempts to detect the game version by detecting ForgeGradle data in the build environment.
*/
static void detectGameVersionForge(Project project) {
private void detectGameVersionForge() {
// TODO confirm this actually works
Project project = this.getProject();
ModrinthExtension extension = project.getExtensions().getByType(ModrinthExtension.class);

try {
final ExtraPropertiesExtension extraProps = project.getExtensions().getExtraProperties();

Expand All @@ -285,7 +296,10 @@ static void detectGameVersionForge(Project project) {

if (forgeGameVersion != null && !forgeGameVersion.isEmpty()) {
project.getLogger().debug("Detected fallback game version {} from ForgeGradle.", forgeGameVersion);
extension.getGameVersions().add(forgeGameVersion);
if (extension.getGameVersions().get().isEmpty()) {
project.getLogger().debug("Adding game version {} because the game versions list is empty.", forgeGameVersion);
extension.getGameVersions().add(forgeGameVersion);
}
}
}
} catch (final Exception e) {
Expand All @@ -296,31 +310,24 @@ static void detectGameVersionForge(Project project) {
/**
* Attempts to detect the game version by detecting Loom data in the build environment.
*/
static void detectGameVersionFabric(Project project) {
// TODO this method does not work on Loom 0.6+
if (true) return;
ModrinthExtension extension = project.getExtensions().getByType(ModrinthExtension.class);
// Loom/Fabric Gradle detection.
try {
// Using reflection because loom isn't always available.
final Class<?> loomType = Class.forName("net.fabricmc.loom.LoomGradleExtension");
final Method getProvider = loomType.getMethod("getMinecraftProvider");
private void detectGameVersionFabric() {
Project project = this.getProject();
ModrinthExtension extension = this.getProject().getExtensions().getByType(ModrinthExtension.class);

final Class<?> minecraftProvider = Class.forName("net.fabricmc.loom.configuration.providers.MinecraftProviderImpl");
final Method getVersion = minecraftProvider.getMethod("minecraftVersion");

final Object loomExt = project.getExtensions().getByType(loomType);
final Object loomProvider = getProvider.invoke(loomExt);
final Object loomVersion = getVersion.invoke(loomProvider);

final String loomGameVersion = loomVersion.toString();

if (loomGameVersion != null && !loomGameVersion.isEmpty()) {
project.getLogger().debug("Detected fallback game version {} from Loom.", loomGameVersion);
extension.getGameVersions().add(loomGameVersion);
if (project.getPluginManager().findPlugin("fabric-loom") != null) {
try {
String loomGameVersion = FabricLoomCompatibility.detectGameVersion(project);
if (extension.getGameVersions().get().isEmpty()) {
project.getLogger().debug("Detected fallback game version {} from Loom.", loomGameVersion);
extension.getGameVersions().add(loomGameVersion);
} else {
project.getLogger().debug("Detected fallback game version {} from Loom, but did not apply because game versions list is not empty.", loomGameVersion);
}
} catch (final Exception e) {
project.getLogger().debug("Failed to detect Loom game version.", e);
}
} catch (final Exception e) {
project.getLogger().debug("Failed to detect Loom game version.", e);
} else {
project.getLogger().debug("Fabric Loom is not present; no game versions were added.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.modrinth.minotaur.compat;

import net.fabricmc.loom.configuration.DependencyInfo;
import net.fabricmc.loom.util.Constants;
import org.gradle.api.Project;

/**
* All the utility methods for compatibility with Fabric Loom.
*/
public class FabricLoomCompatibility {
/**
* Detects the game version being used by Fabric Loom.
* @param project The Gradle project that Minotaur is applied to
* @return The version of Minecraft that Loom is building against
*/
public static String detectGameVersion(final Project project) {
return DependencyInfo.create(project, Constants.Configurations.MINECRAFT).getDependency().getVersion();
}
}
6 changes: 3 additions & 3 deletions src/test/fg/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

plugins {
id "com.modrinth.minotaur" version "2.0.1-SNAPSHOT"
id "com.modrinth.minotaur" version "2.0.2-SNAPSHOT"
id "java"
id "idea"
}
Expand Down Expand Up @@ -42,15 +42,15 @@ idea {
import com.modrinth.minotaur.dependencies.DependencyType
import com.modrinth.minotaur.dependencies.ModDependency
import com.modrinth.minotaur.dependencies.VersionDependency

modrinth {
apiUrl = "https://staging-api.modrinth.com/v2"
projectId = "COR7NNIz"
uploadFile = jar
versionType = "beta"
gameVersions = ["1.RV-Pre1", "3D-Shareware-v1.34"]
//gameVersions = ["1.RV-Pre1", "3D-Shareware-v1.34"]
dependencies = [
new ModDependency("tsi4hUoN", "optional"),
new VersionDependency("igy4JZo5", DependencyType.INCOMPATIBLE)
]
debugMode = true
}
11 changes: 7 additions & 4 deletions src/test/loom/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
plugins {
id "fabric-loom" version "0.10-SNAPSHOT"
id "com.modrinth.minotaur" version "2.0.1"
id "fabric-loom" version "0.11-SNAPSHOT"
id "com.modrinth.minotaur" version "2.0.2-SNAPSHOT"
}

version = "1.0.387"
version = "1.0.368"
group = "com.modrinth.minotaur"

dependencies {
Expand Down Expand Up @@ -33,10 +33,13 @@ modrinth {
uploadFile = remapJar
additionalFiles = [sourcesJar]
versionType = "beta"
gameVersions = ["1.RV-Pre1", "3D-Shareware-v1.34"]
dependencies = [
new ModDependency("tsi4hUoN", "optional"),
new VersionDependency("igy4JZo5", DependencyType.INCOMPATIBLE)
]
debugMode = true
}

tasks.modrinth.doLast {
println tasks.modrinth.uploadInfo.id
}

0 comments on commit c50ad2d

Please sign in to comment.