From dfbba054030b16e07af843fd7a265404cd370950 Mon Sep 17 00:00:00 2001 From: Logan Gorence Date: Thu, 23 Dec 2021 02:19:17 +0000 Subject: [PATCH] Update command now updates the plugins. --- .../foundation/core/command/UpdateCommand.kt | 16 +++++++++++- .../foundation/core/update/UpdateUtil.kt | 25 ++++++++++++++++--- foundation-core/src/main/resources/plugin.yml | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt index 13726db..d85e29d 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt @@ -4,6 +4,8 @@ import cloud.kubelet.foundation.core.update.UpdateUtil import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender +import kotlin.io.path.name +import kotlin.io.path.toPath class UpdateCommand : CommandExecutor { override fun onCommand( @@ -12,6 +14,13 @@ class UpdateCommand : CommandExecutor { label: String, args: Array ): Boolean { + val updateDir = sender.server.pluginsFolder.resolve("update") + if (!updateDir.exists()) { + sender.sendMessage("Error: Failed to create plugin update directory.") + return true + } + val updatePath = updateDir.toPath() + // TODO: Move to separate thread? val modules = UpdateUtil.fetchManifest() val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() } @@ -28,9 +37,14 @@ class UpdateCommand : CommandExecutor { if (plugin == null) { sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})") } else { - sender.sendMessage("${plugin.name}: ${manifest.version} (have ${plugin.description.version})") + val fileName = plugin.javaClass.protectionDomain.codeSource.location.toURI().toPath().name + val artifactPath = manifest.artifacts.getOrNull(0) ?: return@forEach + + sender.sendMessage("${plugin.name}: Updating ${plugin.description.version} to ${manifest.version}") + UpdateUtil.downloadArtifact(artifactPath, updatePath.resolve(fileName)) } } + sender.sendMessage("Restart to take effect") return true } diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/update/UpdateUtil.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/update/UpdateUtil.kt index 3d83031..118cf43 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/update/UpdateUtil.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/update/UpdateUtil.kt @@ -8,18 +8,23 @@ import java.net.URI import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse +import java.nio.file.Path object UpdateUtil { private val client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build() // TODO: Add environment variable override. Document it. - private const val manifestUrl = - "https://git.gorence.io/lgorence/foundation/-/jobs/artifacts/main/raw/build/manifests/update.json?job=build" + private const val basePath = + "https://git.gorence.io/lgorence/foundation/-/jobs/artifacts/main/raw" + private const val basePathQueryParams = "job=build" + private const val manifestPath = "build/manifests/update.json" fun fetchManifest() = fetchFile( - manifestUrl, MapSerializer(String.serializer(), ModuleManifest.serializer()), + getUrl(manifestPath), MapSerializer(String.serializer(), ModuleManifest.serializer()), ) + fun getUrl(path: String) = "$basePath/$path?$basePathQueryParams" + private inline fun fetchFile(url: String, strategy: DeserializationStrategy): T { val request = HttpRequest .newBuilder() @@ -37,4 +42,18 @@ object UpdateUtil { response.body() ) } + + fun downloadArtifact(path: String, outPath: Path) { + val request = HttpRequest + .newBuilder() + .GET() + .uri(URI.create(getUrl(path))) + .build() + + val response = client.send( + request, + HttpResponse.BodyHandlers.ofFile(outPath) + ) + response.body() + } } diff --git a/foundation-core/src/main/resources/plugin.yml b/foundation-core/src/main/resources/plugin.yml index ad20efc..d6657dc 100644 --- a/foundation-core/src/main/resources/plugin.yml +++ b/foundation-core/src/main/resources/plugin.yml @@ -12,7 +12,7 @@ commands: usage: /fbackup permission: foundation.command.backup fupdate: - description: List updates to the Foundation plugin + description: Update all Foundation plugins usage: /fupdate permission: foundation.command.update survival: