mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	Break off update plugins code into separate object.
This commit is contained in:
		@ -1,11 +1,9 @@
 | 
				
			|||||||
package cloud.kubelet.foundation.core.command
 | 
					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.Command
 | 
				
			||||||
import org.bukkit.command.CommandExecutor
 | 
					import org.bukkit.command.CommandExecutor
 | 
				
			||||||
import org.bukkit.command.CommandSender
 | 
					import org.bukkit.command.CommandSender
 | 
				
			||||||
import kotlin.io.path.name
 | 
					 | 
				
			||||||
import kotlin.io.path.toPath
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UpdateCommand : CommandExecutor {
 | 
					class UpdateCommand : CommandExecutor {
 | 
				
			||||||
  override fun onCommand(
 | 
					  override fun onCommand(
 | 
				
			||||||
@ -14,39 +12,7 @@ class UpdateCommand : CommandExecutor {
 | 
				
			|||||||
    label: String,
 | 
					    label: String,
 | 
				
			||||||
    args: Array<out String>
 | 
					    args: Array<out String>
 | 
				
			||||||
  ): Boolean {
 | 
					  ): Boolean {
 | 
				
			||||||
    val updateDir = sender.server.pluginsFolder.resolve("update")
 | 
					    UpdateService.updatePlugins(sender)
 | 
				
			||||||
    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")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return true
 | 
					    return true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -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()
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user