Skip to content

Commit 33cea37

Browse files
authored
Merge pull request #1506 from NeuralFault/linux-update-fix
Fix: Linux AppImage update handling for in-app updating.
2 parents 3d76f0e + 84f5085 commit 33cea37

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

StabilityMatrix.Avalonia/ViewModels/Dialogs/UpdateViewModel.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,68 @@ await updateHelper.DownloadUpdate(
162162
);
163163
}
164164

165+
// Handle Linux AppImage update
166+
if (Compat.IsLinux && Environment.GetEnvironmentVariable("APPIMAGE") is { } appImage)
167+
{
168+
try
169+
{
170+
var updateScriptPath = UpdateHelper.UpdateFolder.JoinFile("update_script.sh").FullPath;
171+
var newAppImage = UpdateHelper.ExecutablePath.FullPath;
172+
173+
var scriptContent = """
174+
#!/bin/bash
175+
set -e
176+
177+
# Wait for the process to exit
178+
while kill -0 "$PID" 2>/dev/null; do
179+
sleep 0.5
180+
done
181+
182+
# Move the new AppImage over the old one
183+
mv -f "$NEW_APPIMAGE" "$OLD_APPIMAGE"
184+
chmod +x "$OLD_APPIMAGE"
185+
186+
# Launch the new AppImage detached
187+
"$OLD_APPIMAGE" > /dev/null 2>&1 &
188+
disown
189+
""";
190+
await File.WriteAllTextAsync(updateScriptPath, scriptContent);
191+
192+
File.SetUnixFileMode(
193+
updateScriptPath,
194+
UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute
195+
);
196+
197+
var startInfo = new System.Diagnostics.ProcessStartInfo
198+
{
199+
FileName = "/usr/bin/env",
200+
Arguments = $"bash \"{updateScriptPath}\"",
201+
UseShellExecute = false,
202+
CreateNoWindow = true,
203+
};
204+
205+
startInfo.EnvironmentVariables["PID"] = Environment.ProcessId.ToString();
206+
startInfo.EnvironmentVariables["NEW_APPIMAGE"] = newAppImage;
207+
startInfo.EnvironmentVariables["OLD_APPIMAGE"] = appImage;
208+
209+
System.Diagnostics.Process.Start(startInfo);
210+
211+
App.Shutdown();
212+
return;
213+
}
214+
catch (Exception ex)
215+
{
216+
logger.LogError(ex, "Failed to execute AppImage update script");
217+
218+
var dialog = DialogHelper.CreateMarkdownDialog(
219+
"AppImage update script failed. \nCould not replace old AppImage with new version. Please check directory permissions. \nFalling back to standard update process. User intervention required: \nAfter program closes, \nplease move the new AppImage extracted in the '.StabilityMatrixUpdate' hidden directory to the old AppImage overwriting it. \n\nClose this dialog to continue with standard update process.",
220+
Resources.Label_UnexpectedErrorOccurred
221+
);
222+
223+
await dialog.ShowAsync();
224+
}
225+
}
226+
165227
// Set current version for update messages
166228
settingsManager.Transaction(
167229
s => s.UpdatingFromVersion = Compat.AppVersion,

0 commit comments

Comments
 (0)