Update.cs 6.6 KB

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