mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	Update command now updates the plugins.
This commit is contained in:
		@ -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<out String>
 | 
			
		||||
  ): 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
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -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 <reified T> fetchFile(url: String, strategy: DeserializationStrategy<T>): 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()
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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:
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user