Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public abstract class DownloadPluginsSpec @Inject constructor(
modrinth.configure { add(id, version) }
}

/**
* Add a plugin download, downloading the latest available version.
*
* @param id plugin id on Modrinth
*/
public fun modrinth(id: String) {
modrinth.configure { add(id) }
}

// github extensions

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package xyz.jpenilla.runtask.pluginsapi

import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import xyz.jpenilla.runtask.util.HashingAlgorithm
import xyz.jpenilla.runtask.util.calculateHash
import xyz.jpenilla.runtask.util.toHexString
Expand Down Expand Up @@ -71,10 +72,11 @@ public abstract class ModrinthApiDownload : PluginApiDownload() {
public abstract val id: Property<String>

@get:Input
@get:Optional
public abstract val version: Property<String>

override fun toString(): String {
return "ModrinthApiDownload{url=${url.get()}, id=${id.get()}, version=${version.get()}}"
return "ModrinthApiDownload{url=${url.get()}, id=${id.get()}, version=${version.orNull ?: "latest"}}"
}

override fun equals(other: Any?): Boolean {
Expand All @@ -89,13 +91,13 @@ public abstract class ModrinthApiDownload : PluginApiDownload() {

return url.get() == other.url.get() &&
id.get() == other.id.get() &&
version.get() == other.version.get()
version.orNull == other.version.orNull
}

override fun hashCode(): Int {
var result = url.get().hashCode()
result = 31 * result + id.get().hashCode()
result = 31 * result + version.get().hashCode()
result = 31 * result + version.orNull.hashCode()
return result
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,37 @@ internal abstract class PluginDownloadServiceImpl : PluginDownloadService {
private fun resolveModrinthPlugin(progressLoggerFactory: ProgressLoggerFactory, download: ModrinthApiDownload): Path {
val cacheDir = parameters.cacheDirectory.get().asFile.toPath()

val apiVersion = download.version.get()
val apiPlugin = download.id.get()
val apiUrl = download.url.get()

val provider = manifest.modrinthProviders.computeIfAbsent(download.url.get()) { ModrinthProvider() }
val plugin = provider.computeIfAbsent(download.id.get()) { PluginVersions() }

val targetDir = cacheDir.resolve(Constants.MODRINTH_PLUGIN_DIR).resolve(apiPlugin)

// Resolve the version: if not specified, fetch the latest from the versions list endpoint
val apiVersion: String
if (!download.version.isPresent) {
val latestJsonName = "latest-json"
val latestJsonVersion = plugin[latestJsonName] ?: PluginVersion(fileName = "$apiPlugin-latest-versions.json", displayName = "modrinth:$apiPlugin:latest:metadata")
val latestJsonFile = targetDir.resolve(latestJsonVersion.fileName)
val latestRequestUrl = "$apiUrl/v2/project/$apiPlugin/version"
val latestJsonPath = download(
DownloadCtx(progressLoggerFactory, apiUrl, latestRequestUrl, targetDir, latestJsonFile, latestJsonVersion, setter = { plugin[latestJsonName] = it }, requireValidJar = false)
)

@OptIn(ExperimentalSerializationApi::class)
val versionList = latestJsonPath.inputStream().buffered().use {
mapper.decodeFromStream<List<ModrinthVersionListEntry>>(it)
}
apiVersion = versionList.firstOrNull()?.id ?: error("Could not find any versions for modrinth project $apiPlugin")
} else {
apiVersion = download.version.get()
}

val jsonVersionName = "$apiVersion-json"
val jsonVersion = plugin[jsonVersionName] ?: PluginVersion(fileName = "$apiPlugin-$apiVersion-info.json", displayName = "modrinth:$apiPlugin:$apiVersion:metadata")

val targetDir =
cacheDir.resolve(Constants.MODRINTH_PLUGIN_DIR).resolve(apiPlugin)
val jsonFile = targetDir.resolve(jsonVersion.fileName)

val versionRequestUrl = "$apiUrl/v2/project/$apiPlugin/version/$apiVersion"
Expand Down Expand Up @@ -365,6 +385,11 @@ private data class ModrinthVersionResponse(
)
}

@Serializable
private data class ModrinthVersionListEntry(
val id: String
)

// github types:
private typealias GitHubProvider = MutableMap<String, GitHubOwner>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public interface ModrinthApi : PluginApi<ModrinthApi, ModrinthApiDownload> {
* @param version plugin version id
*/
public fun add(id: String, version: String)

/**
* Add a plugin download, downloading the latest available version.
*
* @param id plugin id on Modrinth
*/
public fun add(id: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public abstract class ModrinthApiImpl @Inject constructor(private val name: Stri
jobs.add(job)
}

override fun add(id: String) {
val job = objects.newInstance(ModrinthApiDownload::class)
job.url.set(url.map { it.trimEnd('/') })
job.id.set(id)
jobs.add(job)
}

override fun copyConfiguration(api: ModrinthApi) {
url.set(api.url)
jobs.addAll(api.downloads)
Expand Down