Break off update plugins code into separate object.

This commit is contained in:
Logan Gorence 2021-12-23 07:17:48 +00:00
parent 46ba0a4a44
commit 552ef608d9
No known key found for this signature in database
GPG Key ID: 9743CEF10935949A
2 changed files with 49 additions and 36 deletions

View File

@ -1,11 +1,9 @@
package cloud.kubelet.foundation.core.command
import cloud.kubelet.foundation.core.update.UpdateUtil
import cloud.kubelet.foundation.core.service.UpdateService
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(
@ -14,39 +12,7 @@ class UpdateCommand : CommandExecutor {
label: String,
args: Array<out String>
): Boolean {
val updateDir = sender.server.pluginsFolder.resolve("update")
updateDir.mkdir()
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() }
sender.sendMessage("Updates:")
modules.forEach { (name, manifest) ->
// Dumb naming problem. Don't want to fix it right now.
val plugin = if (name == "foundation-core") {
plugins["foundation"]
} else {
plugins[name.lowercase()]
}
if (plugin == null) {
sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})")
} else {
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")
UpdateService.updatePlugins(sender)
return true
}
}

View File

@ -0,0 +1,47 @@
package cloud.kubelet.foundation.core.service
import cloud.kubelet.foundation.core.update.UpdateUtil
import org.bukkit.command.CommandSender
import kotlin.io.path.name
import kotlin.io.path.toPath
// TODO: Switch to classes and use dependency injection with koin.
object UpdateService {
fun updatePlugins(sender: CommandSender, onFinish: (() -> Unit)? = null) {
val updateDir = sender.server.pluginsFolder.resolve("update")
updateDir.mkdir()
if (!updateDir.exists()) {
sender.sendMessage("Error: Failed to create plugin update directory.")
return
}
val updatePath = updateDir.toPath()
Thread {
val modules = UpdateUtil.fetchManifest()
val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() }
sender.sendMessage("Updates:")
modules.forEach { (name, manifest) ->
// Dumb naming problem. Don't want to fix it right now.
val plugin = if (name == "foundation-core") {
plugins["foundation"]
} else {
plugins[name.lowercase()]
}
if (plugin == null) {
sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})")
} else {
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")
if (onFinish != null) onFinish()
}.start()
}
}