Update.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using InABox.DynamicGrid;
  5. using InABox.WPF;
  6. using RestSharp;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace PRS.Shared
  16. {
  17. /// <summary>
  18. /// Manages updating the app for both PRSDesktop and PRSServer
  19. /// </summary>
  20. public static class Update
  21. {
  22. public static RestResponse GetRemoteFile(string location)
  23. {
  24. var client = StaticRestClients.GetClient(Uri.EscapeUriString(location));
  25. var versionrequest = new RestRequest(Uri.EscapeUriString(location), Method.Get);
  26. return client.Execute(versionrequest);
  27. }
  28. private static string ParseReleaseNotes(string releaseNotes, string latestVersion, string currentVersion)
  29. {
  30. var display = new List<string>();
  31. var latest = "Changes in " + latestVersion;
  32. var current = "Changes in " + currentVersion;
  33. var notes = releaseNotes.Split('\n').ToList();
  34. var bActive = false;
  35. foreach (var note in notes)
  36. {
  37. if (!string.IsNullOrWhiteSpace(note))
  38. {
  39. if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&
  40. string.Compare(latest.Trim().ToUpper(), note.Trim().ToUpper()) <= 0)
  41. bActive = true;
  42. if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&
  43. string.Compare(current.Trim().ToUpper(), note.Trim().ToUpper()) >= 0)
  44. bActive = false;
  45. if (bActive && !note.ToUpper().StartsWith("CHANGES IN") && !note.StartsWith("="))
  46. display.Add(note);
  47. }
  48. }
  49. if (!display.Any())
  50. display.Add("Various Internal Updates");
  51. return string.Join('\n', display);
  52. }
  53. private static bool? ShowReleaseNotes(string display, string latestVersion, string currentVersion, bool allowCancel = false)
  54. {
  55. var form = new NotesForm
  56. {
  57. Caption = string.Format("Release {0} is available!", latestVersion),
  58. CancelEnabled = allowCancel,
  59. Text = string.Format(
  60. "The following changes and improvements have been made since your last update (v{0}). Please review and click [OK] to update your software.\n\n{1} ",
  61. currentVersion,
  62. display
  63. )
  64. };
  65. return form.ShowDialog();
  66. }
  67. private static string GenerateInstallerScript(string installerFile)
  68. {
  69. var commands = new List<string>();
  70. commands.Add($"\"{installerFile}\" /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
  71. commands.Add($"\"{Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe")}\"");
  72. return string.Join('\n', commands);
  73. }
  74. public static bool CheckForUpdates(
  75. Func<string> getUpdateLocation,
  76. Func<string, string> getLatestVersion,
  77. Func<string, string> getReleaseNotes,
  78. Func<string, byte[]?>? getInstaller,
  79. Action? beforeUpdate,
  80. bool elevated,
  81. string tempName,
  82. bool allowCancel = false)
  83. {
  84. var display = "";
  85. var latestVersion = "";
  86. var currentVersion = CoreUtils.GetVersion();
  87. var location = getUpdateLocation();
  88. if (!String.IsNullOrWhiteSpace(location))
  89. {
  90. Progress.ShowModal("Checking for Updates", progress =>
  91. {
  92. if (!string.Equals(currentVersion, "???"))
  93. {
  94. latestVersion = getLatestVersion(location);
  95. Logger.Send(LogType.Information, "",
  96. $"Update Check: Current: {currentVersion} Latest: {latestVersion}");
  97. if (!string.IsNullOrWhiteSpace(latestVersion))
  98. if (string.Compare(currentVersion, latestVersion) < 0)
  99. {
  100. var releasenotes = getReleaseNotes(location);
  101. display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);
  102. }
  103. }
  104. });
  105. }
  106. if (display.IsNullOrWhiteSpace()) return false;
  107. if (ShowReleaseNotes(display, latestVersion, currentVersion, allowCancel) == true)
  108. {
  109. if (getInstaller == null)
  110. return true;
  111. var bOK = false;
  112. var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);
  113. Progress.ShowModal("Retrieving Update", progress =>
  114. {
  115. try
  116. {
  117. var installer = getInstaller(location);
  118. if (installer?.Any() == true)
  119. {
  120. File.WriteAllBytes(tempinstall, installer);
  121. var scriptFile = Path.Combine(CoreUtils.GetPath(), "install.bat");
  122. File.WriteAllText(scriptFile, GenerateInstallerScript(tempinstall));
  123. bOK = true;
  124. beforeUpdate?.Invoke();
  125. progress.Report("Launching Installer");
  126. /*var startInfo = new ProcessStartInfo(tempinstall,
  127. " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");*/
  128. var startInfo = new ProcessStartInfo(scriptFile);
  129. startInfo.Verb = elevated ? "runas" : "open";
  130. startInfo.UseShellExecute = true;
  131. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  132. var p = Process.Start(startInfo);
  133. p.WaitForExit();
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
  139. MessageBox.Show("Error during installation!");
  140. }
  141. });
  142. if (bOK)
  143. return true;
  144. MessageBox.Show("Unable to retrieve installer!");
  145. return false;
  146. }
  147. return false;
  148. }
  149. }
  150. }