mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	Core: Backup cleanup and fixes for Windows.
This commit is contained in:
		@ -8,6 +8,13 @@ class FoundationGradlePlugin : Plugin<Project> {
 | 
			
		||||
  override fun apply(project: Project) {
 | 
			
		||||
    project.extensions.create<FoundationExtension>("foundation")
 | 
			
		||||
    val setupPaperServer = project.tasks.create<SetupPaperServer>("setupPaperServer")
 | 
			
		||||
    project.afterEvaluate { ->
 | 
			
		||||
      setupPaperServer.dependsOn(*project.subprojects
 | 
			
		||||
        .filter { it.name.startsWith("foundation-") }
 | 
			
		||||
        .map { it.tasks.getByName("build") }
 | 
			
		||||
        .toTypedArray()
 | 
			
		||||
      )
 | 
			
		||||
    }
 | 
			
		||||
    val runPaperServer = project.tasks.create<RunPaperServer>("runPaperServer")
 | 
			
		||||
    runPaperServer.dependsOn(setupPaperServer)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -57,4 +57,9 @@ object Util {
 | 
			
		||||
 | 
			
		||||
    return outPath
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  fun isPlatformWindows(): Boolean {
 | 
			
		||||
    val os = System.getProperty("os.name")
 | 
			
		||||
    return os != null && os.lowercase().startsWith("windows")
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@ -42,25 +42,28 @@ class BackupCommand(
 | 
			
		||||
      return true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
    val server = sender.server
 | 
			
		||||
      server.scheduler.runTaskAsynchronously(plugin, Runnable {
 | 
			
		||||
        runBackup(server)
 | 
			
		||||
      })
 | 
			
		||||
    } catch (e: Exception) {
 | 
			
		||||
      sender.sendMessage(String.format("Failed to backup: %s", e.message))
 | 
			
		||||
    server.scheduler.runTaskAsynchronously(plugin) { ->
 | 
			
		||||
      runBackup(server, sender)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return true
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // TODO: Pull backup creation code into a separate service.
 | 
			
		||||
  private fun runBackup(server: Server) = try {
 | 
			
		||||
  private fun runBackup(server: Server, sender: CommandSender? = null) = try {
 | 
			
		||||
    RUNNING.set(true)
 | 
			
		||||
 | 
			
		||||
    server.scheduler.runTask(plugin) { ->
 | 
			
		||||
      server.sendMessage(Util.formatSystemMessage("Backup started."))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    val backupFileName = String.format("backup-%s.zip", Instant.now().toString())
 | 
			
		||||
    val backupTime = Instant.now()
 | 
			
		||||
    val backupIdentifier = if (Util.isPlatformWindows()) {
 | 
			
		||||
      backupTime.toEpochMilli().toString()
 | 
			
		||||
    } else {
 | 
			
		||||
      backupTime.toString()
 | 
			
		||||
    }
 | 
			
		||||
    val backupFileName = String.format("backup-%s.zip", backupIdentifier)
 | 
			
		||||
    val backupPath = backupsPath.resolve(backupFileName)
 | 
			
		||||
    val backupFile = backupPath.toFile()
 | 
			
		||||
 | 
			
		||||
@ -82,17 +85,26 @@ class BackupCommand(
 | 
			
		||||
      )
 | 
			
		||||
    }
 | 
			
		||||
    Unit
 | 
			
		||||
  } catch (e: Exception) {
 | 
			
		||||
    if (sender != null) {
 | 
			
		||||
      server.scheduler.runTask(plugin) { ->
 | 
			
		||||
        sender.sendMessage(String.format("Failed to backup: %s", e.message))
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    plugin.slF4JLogger.warn("Failed to backup.", e)
 | 
			
		||||
  } finally {
 | 
			
		||||
    RUNNING.set(false)
 | 
			
		||||
    server.scheduler.runTask(plugin) { ->
 | 
			
		||||
      server.sendMessage(Util.formatSystemMessage("Backup finished."))
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private fun backupPlugins(server: Server, zipStream: ZipOutputStream) {
 | 
			
		||||
    try {
 | 
			
		||||
      addDirectoryToZip(zipStream, server.pluginsFolder.toPath())
 | 
			
		||||
    } catch (e: IOException) {
 | 
			
		||||
      // TODO: Add error handling.
 | 
			
		||||
      e.printStackTrace()
 | 
			
		||||
      plugin.slF4JLogger.warn("Failed to backup plugins.", e)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user