From 0c1160eb62dab92831a9d15845ea17979ed9bec1 Mon Sep 17 00:00:00 2001 From: Diogo Trindade Date: Sun, 26 Mar 2023 22:06:01 +0100 Subject: [PATCH] Linux - Added update script I need this on my linux machine :) --- .../ApplicationStateManager.cs | 34 ++++++++++ src/Artemis.UI.Linux/Artemis.UI.Linux.csproj | 1 + src/Artemis.UI.Linux/Scripts/update.sh | 68 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/Artemis.UI.Linux/Scripts/update.sh diff --git a/src/Artemis.UI.Linux/ApplicationStateManager.cs b/src/Artemis.UI.Linux/ApplicationStateManager.cs index 997c19ef3..2a57426a3 100644 --- a/src/Artemis.UI.Linux/ApplicationStateManager.cs +++ b/src/Artemis.UI.Linux/ApplicationStateManager.cs @@ -29,6 +29,7 @@ public class ApplicationStateManager Core.Utilities.ShutdownRequested += UtilitiesOnShutdownRequested; Core.Utilities.RestartRequested += UtilitiesOnRestartRequested; + Core.Utilities.UpdateRequested += UtilitiesOnUpdateRequested; // On OS shutdown dispose the IOC container just so device providers get a chance to clean up if (Application.Current?.ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) @@ -138,4 +139,37 @@ public class ApplicationStateManager //todo } + + private void UtilitiesOnUpdateRequested(object? sender, UpdateEventArgs e) + { + List argsList = new(StartupArguments); + if (e.Silent && !argsList.Contains("--minimized")) + argsList.Add("--minimized"); + + // Retain startup arguments after update by providing them to the script + string script = Path.Combine(Constants.UpdatingFolder, "installing", "Scripts", "update.sh"); + string source = $"\"{Path.Combine(Constants.UpdatingFolder, "installing")}\""; + string destination = $"\"{Constants.ApplicationFolder}\""; + string args = argsList.Any() ? $"\"'{string.Join(' ', argsList)}'\"" : ""; + + RunScriptWithOutputFile(script, $"{source} {destination} {args}", Path.Combine(Constants.DataFolder, "update-log.txt")); + + // Lets try a graceful shutdown, PowerShell will kill if needed + if (Application.Current?.ApplicationLifetime is IControlledApplicationLifetime controlledApplicationLifetime) + Dispatcher.UIThread.Post(() => controlledApplicationLifetime.Shutdown()); + } + + private void RunScriptWithOutputFile(string script, string arguments, string outputFile) + { + // Use > for files that are bigger than 200kb to start fresh, otherwise use >> to append + string redirectSymbol = File.Exists(outputFile) && new FileInfo(outputFile).Length > 200000 ? ">" : ">>"; + ProcessStartInfo info = new() + { + Arguments = $"\"{script}\" {arguments} {redirectSymbol} \"{outputFile}\"", + FileName = "/bin/bash", + WindowStyle = ProcessWindowStyle.Hidden, + CreateNoWindow = true, + }; + Process.Start(info); + } } \ No newline at end of file diff --git a/src/Artemis.UI.Linux/Artemis.UI.Linux.csproj b/src/Artemis.UI.Linux/Artemis.UI.Linux.csproj index e93cdd4f2..3d48d5686 100644 --- a/src/Artemis.UI.Linux/Artemis.UI.Linux.csproj +++ b/src/Artemis.UI.Linux/Artemis.UI.Linux.csproj @@ -29,5 +29,6 @@ + \ No newline at end of file diff --git a/src/Artemis.UI.Linux/Scripts/update.sh b/src/Artemis.UI.Linux/Scripts/update.sh new file mode 100644 index 000000000..db79c50fb --- /dev/null +++ b/src/Artemis.UI.Linux/Scripts/update.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# This script is used to update Artemis on Linux. +# Arguments: +# $1 = sourceDirectory, the directory where the new files are located +# $2 = destinationDirectory, the directory where the old files are located +# $3 = artemisArgs, the arguments to pass to the Artemis executable + +sourceDirectory=$1 +destinationDirectory=$2 +artemisArgs=$3 + +echo "sourceDirectory $sourceDirectory" +echo "destinationDirectory $destinationDirectory" +echo "artemisArgs $artemisArgs" +exit 0 + +# Wait for up to 10 seconds for the Artemis process to exit +i=0 +while [ $i -le 10 ] +do + if ! pgrep -x "Artemis.UI.Linux" > /dev/null + then + break + fi + sleep 1 + i=$((i+1)) +done + +# If the Artemis process is still running, kill it +if pgrep -x "Artemis.UI.Linux" > /dev/null +then + pkill -x "Artemis.UI.Linux" +fi + +# Check if the destination directory exists +if [ ! -d "$destinationDirectory" ] +then + echo "Destination directory does not exist" + exit 1 +fi + +# Clear the destination directory but don't remove it +echo "Cleaning up old version where needed" +rm -rf "{$destinationDirectory:?}/"* + +# Move the contents of the source directory to the destination directory +echo "Installing new files" +mv "$sourceDirectory"/* "$destinationDirectory" + +# Remove the now empty source directory +rmdir "$sourceDirectory" + +echo "Finished! Restarting Artemis" +sleep 1 + +# When finished, start Artemis again + +# If the user has specified arguments, pass them to the executable +if [ -z "$artemisArgs" ] +then + "$1/Artemis.UI.Linux" & +else + "$1/Artemis.UI.Linux" "$artemisArgs" & +fi + + +