Skip to content

Commit

Permalink
'DesktopUpdateInstaller接口下新增deleteOldUpdater方法用于删除可能存在在Ani/目录下的ani_up…
Browse files Browse the repository at this point in the history
…date更新程序;更改了install方法的windows实现,通过复制并启动ani_update.exe进行Ani的更新及重启'
  • Loading branch information
hacbit committed Aug 6, 2024
1 parent 6b60650 commit 72d8630
Showing 1 changed file with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import me.him188.ani.app.platform.FileOpener
import me.him188.ani.app.platform.Platform
import me.him188.ani.app.platform.currentPlatformDesktop
import me.him188.ani.utils.io.SystemPath
import me.him188.ani.utils.io.absolutePath
import me.him188.ani.utils.io.toFile
import me.him188.ani.utils.logging.info
import me.him188.ani.utils.logging.logger
Expand All @@ -17,6 +18,8 @@ interface DesktopUpdateInstaller : UpdateInstaller {
override fun openForManualInstallation(file: SystemPath, context: ContextMP) {
FileOpener.openInFileBrowser(file)
}

fun deleteOldUpdater()

companion object {
fun currentOS(): DesktopUpdateInstaller {
Expand All @@ -29,6 +32,10 @@ interface DesktopUpdateInstaller : UpdateInstaller {
}

object MacOSUpdateInstaller : DesktopUpdateInstaller {
override fun deleteOldUpdater() {
// no-op
}

override fun install(file: SystemPath, context: ContextMP): InstallationResult {
Desktop.getDesktop().open(file.toFile())
exitProcess(0)
Expand All @@ -47,16 +54,32 @@ object WindowsUpdateInstaller : DesktopUpdateInstaller {
return InstallationResult.Failed(InstallationFailureReason.UNSUPPORTED_FILE_STRUCTURE)
}

val installerScriptFile = appDir.resolve("install.cmd")
installerScriptFile.writeText(getInstallerScript(file.toFile()))
logger.info { "Installer script written to ${installerScriptFile.absolutePath}" }
val updateExecutable = appDir.resolve("app/resources/ani_update.exe")
if (!updateExecutable.exists()) {
logger.info { "'ani_update.exe' not found. Fallback to manual update" }
return InstallationResult.Failed(InstallationFailureReason.UNSUPPORTED_FILE_STRUCTURE)
}

// Copy ani_update.exe to current dir
val copiedUpdateExecutable = appDir.resolve("ani_update.exe")
updateExecutable.copyTo(copiedUpdateExecutable, true)

ProcessBuilder("cmd", "/c", "start", copiedUpdateExecutable.absolutePath, file.absolutePath, appDir.absolutePath)
.directory(appDir)
.start()

val processBuilder = ProcessBuilder("cmd", "/c", "start", "cmd", "/c", installerScriptFile.name)
processBuilder.directory(appDir).start()
logger.info { "Installer started" }
exitProcess(0)
}

override fun deleteOldUpdater() {
val appDir = File(System.getProperty("user.dir") ?: throw IllegalStateException("Cannot get app directory"))
val updateExecutable = appDir.resolve("ani_update.exe")
if (updateExecutable.exists()) {
updateExecutable.delete()
}
}

@Language("cmd")
private fun getInstallerScript(
zipFile: File,
Expand Down

0 comments on commit 72d8630

Please sign in to comment.