Skip to content

Commit

Permalink
feat: show a success message once the update finishes on Linux and ma…
Browse files Browse the repository at this point in the history
…cOS (#26)

* feat: show a success message once the update finishes on Linux and macOS
  • Loading branch information
EchoEllet authored Jul 9, 2024
1 parent 089c06d commit d9eaa46
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
14 changes: 12 additions & 2 deletions common/src/main/kotlin/utils/CommandLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import kotlin.io.path.absolutePathString
* Run a command in the command line of the system
* @param args The arguments that is used to run the command, the reason this is not a String to
* prevent some issues and bugs with the paths or the arguments that have spacing
* @param reasonOfRunningTheCommand This is used to tell the user why we're running this command on the system
* should be something like `to check if the system in dark mode`
* @param reasonOfRunningTheCommand This is used to tell the user why we're running this command on the system.
* Should be something like `to check if the system in dark mode`
* @param isLoggingEnabled Should we print the result, output and errors to the log?
* */
fun commandLine(
Expand Down Expand Up @@ -57,6 +57,16 @@ fun commandLine(
}
}

/**
* Similar to [commandLine] without logging and waiting for the result.
* It doesn't handle the output or specific error to the output of the command.
* */
fun commandLineNonBlocking(vararg args: String): Result<Unit> =
runCatching {
ProcessBuilder(listOf(*args))
.start()
}

/**
* Has the similar functionality as [commandLine] specifically for using powershell commands in Windows
* */
Expand Down
6 changes: 6 additions & 0 deletions common/src/test/kotlin/utils/CommandLineTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ class CommandLineTest {
assertThrows<IOException> {
commandLine("java-incorrect", "--version", reasonOfRunningTheCommand = null).getOrThrow()
}
assertThrows<IOException> {
commandLineNonBlocking("java-incorrect", "--version").getOrThrow()
}
}

@Test
fun `test commandLine when using correct command`() {
assertDoesNotThrow {
commandLine("java", "--version", reasonOfRunningTheCommand = null).getOrThrow()
}
assertDoesNotThrow {
commandLineNonBlocking("java", "--version").getOrThrow()
}
}

@Test
Expand Down
50 changes: 48 additions & 2 deletions sync-script/src/main/kotlin/services/updater/JarAutoUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import services.HttpResponse
import utils.FileDownloader
import utils.SystemInfoProvider
import utils.buildHtml
import utils.commandLineNonBlocking
import utils.convertBytesToReadableMegabytesAsString
import utils.createFileWithParentDirectoriesOrTerminate
import utils.deleteExistingOrTerminate
import utils.executeBatchScriptInSeparateWindow
import utils.getRunningJarFilePath
import utils.moveToOrTerminate
import utils.os.LinuxDesktopEnvironment
import utils.os.OperatingSystem
import utils.terminateWithOrWithoutError
import utils.version.SemanticVersion
Expand Down Expand Up @@ -89,6 +91,7 @@ object JarAutoUpdater {
?.value
Result.success(projectVersion)
}

is HttpResponse.HttpFailure -> Result.failure(response.exception())
is HttpResponse.UnknownError -> Result.failure(response.exception)
}
Expand Down Expand Up @@ -185,6 +188,44 @@ object JarAutoUpdater {
overwrite = true,
fileEntityType = "JAR",
)

// The Application has been updated without automatically relaunching it, showing a message
val (windowTitle, message) = buildUpdateSuccessMessage()
val commandArgs: Array<String> =
when (OperatingSystem.current) {
OperatingSystem.Linux ->
when (LinuxDesktopEnvironment.current) {
LinuxDesktopEnvironment.KdePlasma ->
arrayOf(
"kdialog",
"--title",
windowTitle,
"--msgbox",
message,
)
else ->
arrayOf(
"zenity",
"--info",
"--title",
windowTitle,
"--text",
message,
)
}

OperatingSystem.MacOS ->
arrayOf(
"osascript",
"-e",
"display dialog \"$message\" with title \"$windowTitle\" buttons {\"OK\"} default button \"OK\"",
)

else -> error("The operating system is expected to be either Linux or macOS in the current check.")
}
commandLineNonBlocking(
*commandArgs,
).getOrThrow()
},
)
}
Expand All @@ -204,8 +245,7 @@ object JarAutoUpdater {
}
val secondsToWait = 1

val windowTitle = "Update Complete"
val message = "${ProjectInfoConstants.DISPLAY_NAME} has been updated. Relaunch to use the new version."
val (windowTitle, message) = buildUpdateSuccessMessage()

val messageVbsFilePath =
SyncScriptDotMinecraftFiles.SyncScriptData.Temp.path
Expand Down Expand Up @@ -239,4 +279,10 @@ object JarAutoUpdater {
// Will require the user to launch once again after the update.
terminateWithOrWithoutError()
}

private fun buildUpdateSuccessMessage(): Pair<String, String> =
Pair(
"Update Complete",
"${ProjectInfoConstants.DISPLAY_NAME} has been updated. Relaunch to use the new version.",
)
}

0 comments on commit d9eaa46

Please sign in to comment.