Browse Source

Fixed update script; it also now restarts after updating.

Kenric Nugteren 2 năm trước cách đây
mục cha
commit
8ed5ce45ec
2 tập tin đã thay đổi với 50 bổ sung22 xóa
  1. 8 4
      prs.server/Forms/Configuration.xaml.cs
  2. 42 18
      prs.shared/Update.cs

+ 8 - 4
prs.server/Forms/Configuration.xaml.cs

@@ -80,12 +80,16 @@ namespace PRSServer
 
             InitializeComponent();
 
-            CheckForUpdates();
+            if (CheckForUpdates())
+            {
+                Close();
+                return;
+            }
 
             DatabaseEngine.MoveUpdateFiles();
 
             Title = string.Format("PRS Server Manager (v{0})", CoreUtils.GetVersion());
-
+            Logger.Send(LogType.Information, "", "Doing refresh");
             Servers.Refresh(true, true);
         }
 
@@ -122,9 +126,9 @@ namespace PRSServer
             Servers.BeforeUpdate();
         }
 
-        private void CheckForUpdates()
+        private bool CheckForUpdates()
         {
-            Update.CheckForUpdates(GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, BeforeUpdate, true, "PRSSetup.exe");
+            return Update.CheckForUpdates(GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, BeforeUpdate, true, "PRSSetup.exe");
         }
 
         #endregion

+ 42 - 18
prs.shared/Update.cs

@@ -9,6 +9,7 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -73,7 +74,15 @@ namespace PRS.Shared
             return form.ShowDialog();
         }
 
-        public static void CheckForUpdates(
+        private static string GenerateInstallerScript(string installerFile)
+        {
+            var commands = new List<string>();
+            commands.Add($"\"{installerFile}\" /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
+            commands.Add($"\"{Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe")}\"");
+            return string.Join('\n', commands);
+        }
+
+        public static bool CheckForUpdates(
             Func<string> getUpdateLocation,
             Func<string, string> getLatestVersion,
             Func<string, string> getReleaseNotes,
@@ -107,7 +116,7 @@ namespace PRS.Shared
                 });
             }
 
-            if (display.IsNullOrWhiteSpace()) return;
+            if (display.IsNullOrWhiteSpace()) return false;
 
             if (ShowReleaseNotes(display, latestVersion, currentVersion) == true)
             {
@@ -115,28 +124,43 @@ namespace PRS.Shared
                 var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);
                 Progress.ShowModal("Retrieving Update", progress =>
                 {
-                    var installer = getInstaller(location);
-                    if (installer?.Any() == true)
+                    try
                     {
-                        File.WriteAllBytes(tempinstall, installer);
-                        bOK = true;
-                    }
+                        var installer = getInstaller(location);
+                        if (installer?.Any() == true)
+                        {
+                            File.WriteAllBytes(tempinstall, installer);
 
-                    beforeUpdate?.Invoke();
-                    progress.Report("Launching Installer");
-                    if (bOK)
+                            var scriptFile = Path.Combine(CoreUtils.GetPath(), "install.bat");
+                            File.WriteAllText(scriptFile, GenerateInstallerScript(tempinstall));
+
+                            bOK = true;
+
+                            beforeUpdate?.Invoke();
+                            progress.Report("Launching Installer");
+                            /*var startInfo = new ProcessStartInfo(tempinstall,
+                                " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");*/
+                            var startInfo = new ProcessStartInfo(scriptFile);
+                            startInfo.Verb = elevated ? "runas" : "open";
+                            startInfo.UseShellExecute = true;
+                            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
+                            var p = Process.Start(startInfo);
+                            p.WaitForExit();
+                        }
+                    }
+                    catch (Exception e)
                     {
-                        var startInfo = new ProcessStartInfo(tempinstall,
-                            " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
-                        startInfo.Verb = elevated ? "runas" : "open";
-                        startInfo.UseShellExecute = true;
-                        var p = Process.Start(startInfo);
-                        p.WaitForExit();
+                        Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
+                        MessageBox.Show("Error during installation!");
                     }
                 });
-                if (!bOK)
-                    MessageBox.Show("Unable to retrieve installer!");
+                if (bOK)
+                    return true;
+
+                MessageBox.Show("Unable to retrieve installer!");
+                return false;
             }
+            return false;
         }
     }
 }