Skip to content

Commit

Permalink
Support setting CurseForge additional file display name.
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
modmuss50 committed Nov 14, 2024
1 parent 0bf970b commit 55bc756
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/pages/platforms/curseforge.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ publishMods {
// Set a changelog using text, markdown, or html (defaults to markdown)
changelog = "# Markdown changelog content"
changelogType = "markdown"
// Set the display name of an additional file
additionalFile(jar) {
name = "Fabric"
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import me.modmuss50.mpp.Validators
import me.modmuss50.mpp.path
import org.gradle.api.Action
import org.gradle.api.JavaVersion
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.logging.Logger
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import org.jetbrains.annotations.ApiStatus
import javax.inject.Inject
import kotlin.random.Random
import kotlin.reflect.KClass
Expand Down Expand Up @@ -57,6 +61,10 @@ interface CurseforgeOptions : PlatformOptions, PlatformOptionsInternal<Curseforg
@get:Input
val changelogType: Property<String>

@get:Nested
@get:ApiStatus.Internal
val additionalFilesExt: MapProperty<ConfigurableFileCollection, AdditionalFileOptions>

fun from(other: CurseforgeOptions) {
super.from(other)
fromDependencies(other)
Expand All @@ -68,6 +76,7 @@ interface CurseforgeOptions : PlatformOptions, PlatformOptionsInternal<Curseforg
javaVersions.convention(other.javaVersions)
apiEndpoint.convention(other.apiEndpoint)
changelogType.convention(other.changelogType)
additionalFilesExt.convention(other.additionalFilesExt)
}

fun from(other: Provider<CurseforgeOptions>) {
Expand All @@ -93,6 +102,17 @@ interface CurseforgeOptions : PlatformOptions, PlatformOptionsInternal<Curseforg
)
}

fun additionalFile(file: Any, action: Action<AdditionalFileOptions>) {
val options = objectFactory.newInstance(AdditionalFileOptions::class.java)
action.execute(options)

val fileCollection = objectFactory.fileCollection()
fileCollection.from(file)

additionalFiles.from(fileCollection)
additionalFilesExt.put(fileCollection, options)
}

override fun setInternalDefaults() {
apiEndpoint.convention("https://minecraft.curseforge.com")
changelogType.convention("markdown")
Expand All @@ -119,6 +139,17 @@ interface CurseforgeVersionRangeOptions {
val end: Property<String>
}

/**
* Options for additional files to upload alongside the main file
*/
interface AdditionalFileOptions {
/**
* The display name of the additional file
*/
@get:Input
val name: Property<String>
}

/**
* Provides shorthand methods for adding dependencies to curseforge
*/
Expand Down Expand Up @@ -242,8 +273,13 @@ abstract class Curseforge @Inject constructor(name: String) : Platform(name), Cu
api.uploadFile(projectId.get(), file.path, metadata)
}

val additionalFileOptions = additionalFilesExt.get().map { (key, value) ->
key.singleFile.toPath() to value
}.toMap()

for (additionalFile in additionalFiles.files) {
val additionalMetadata = metadata.copy(parentFileID = response.id, gameVersions = null, displayName = null)
val fileOptions = additionalFileOptions[additionalFile.toPath()]
val additionalMetadata = metadata.copy(parentFileID = response.id, gameVersions = null, displayName = fileOptions?.name?.orNull)

HttpUtils.retry(maxRetries.get(), "Failed to upload additional file") {
api.uploadFile(projectId.get(), additionalFile.toPath(), additionalMetadata)
Expand Down
49 changes: 49 additions & 0 deletions src/test/kotlin/me/modmuss50/mpp/test/curseforge/CurseforgeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,53 @@ class CurseforgeTest : IntegrationTest {
assertEquals(TaskOutcome.FAILED, result.task(":publishCurseforge")!!.outcome)
assertContains(result.output, "minecraftVersions contains duplicate values: [1.20.1]")
}

@Test
fun additionalFiles() {
val server = MockWebServer(MockCurseforgeApi())

val result = gradleTest()
.buildScript(
"""
val fabricJar = tasks.register("fabricJar", Jar::class.java) {
archiveClassifier = "fabric"
}
val forgeJar = tasks.register("forgeJar", Jar::class.java) {
archiveClassifier = "forge"
}
publishMods {
file = tasks.jar.flatMap { it.archiveFile }
changelog = "<p>Hello!</p>"
version = "1.0.0"
type = BETA
modLoaders.add("fabric")
curseforge {
accessToken = "123"
projectId = "123456"
minecraftVersions.add("1.20.1")
additionalFile(fabricJar.flatMap { it.archiveFile }) {
name = "Fabric"
}
additionalFile(forgeJar.flatMap { it.archiveFile }) {
name = "Forge"
}
apiEndpoint = "${server.endpoint}"
}
}
""".trimIndent(),
)
.run("publishCurseforge")
server.close()

val metadata = server.api.allMetadata

assertEquals(TaskOutcome.SUCCESS, result.task(":publishCurseforge")!!.outcome)
assertEquals(3, metadata.size)
assertEquals("Fabric", metadata[1].displayName)
assertEquals("Forge", metadata[2].displayName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MockCurseforgeApi : MockWebServer.MockApi {
@OptIn(ExperimentalSerializationApi::class)
val json = Json { ignoreUnknownKeys = true; explicitNulls = false }
var lastMetadata: CurseforgeApi.UploadFileMetadata? = null
var allMetadata: ArrayList<CurseforgeApi.UploadFileMetadata> = ArrayList()
val files: ArrayList<String> = ArrayList()

override fun routes(): EndpointGroup {
Expand Down Expand Up @@ -77,6 +78,7 @@ class MockCurseforgeApi : MockWebServer.MockApi {
}

lastMetadata = json.decodeFromString(metadata)
allMetadata.add(lastMetadata!!)
files.add(file.filename())

context.result("""{"id": "20402"}""")
Expand Down

0 comments on commit 55bc756

Please sign in to comment.