Update.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Progress.ShowModal("Checking for Updates", progress =>
  89. {
  90. if (!string.Equals(currentVersion, "???"))
  91. {
  92. latestVersion = getLatestVersion(location);
  93. Logger.Send(LogType.Information, "",
  94. $"Update Check: Current: {currentVersion} Latest: {latestVersion}");
  95. if (!string.IsNullOrWhiteSpace(latestVersion))
  96. if (string.Compare(currentVersion, latestVersion) < 0)
  97. {
  98. var releasenotes = getReleaseNotes(location);
  99. display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);
  100. }
  101. }
  102. });
  103. if (display.IsNullOrWhiteSpace()) return false;
  104. if (ShowReleaseNotes(display, latestVersion, currentVersion, allowCancel) == true)
  105. {
  106. if (getInstaller == null)
  107. return true;
  108. var bOK = false;
  109. var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);
  110. Progress.ShowModal("Retrieving Update", progress =>
  111. {
  112. try
  113. {
  114. var installer = getInstaller(location);
  115. if (installer?.Any() == true)
  116. {
  117. File.WriteAllBytes(tempinstall, installer);
  118. var scriptFile = Path.Combine(CoreUtils.GetPath(), "install.bat");
  119. File.WriteAllText(scriptFile, GenerateInstallerScript(tempinstall));
  120. bOK = true;
  121. beforeUpdate?.Invoke();
  122. progress.Report("Launching Installer");
  123. /*var startInfo = new ProcessStartInfo(tempinstall,
  124. " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");*/
  125. var startInfo = new ProcessStartInfo(scriptFile);
  126. startInfo.Verb = elevated ? "runas" : "open";
  127. startInfo.UseShellExecute = true;
  128. startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  129. var p = Process.Start(startInfo);
  130. p.WaitForExit();
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. Logger.Send(LogType.Error, "", CoreUtils.FormatException(e));
  136. MessageBox.Show("Error during installation!");
  137. }
  138. });
  139. if (bOK)
  140. return true;
  141. MessageBox.Show("Unable to retrieve installer!");
  142. return false;
  143. }
  144. return false;
  145. }
  146. }
  147. }