Update.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 Syncfusion.Windows.Shared;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. namespace PRS.Shared
  18. {
  19. /// <summary>
  20. /// Manages updating the app for both PRSDesktop and PRSServer
  21. /// </summary>
  22. public static class Update
  23. {
  24. public static RestResponse GetRemoteFile(string location)
  25. {
  26. var client = StaticRestClients.GetClient(Uri.EscapeUriString(location));
  27. var versionrequest = new RestRequest(Uri.EscapeUriString(location), Method.Get);
  28. return client.Execute(versionrequest);
  29. }
  30. private static string ParseReleaseNotes(string releaseNotes, string latestVersion, string currentVersion)
  31. {
  32. var display = new List<string>();
  33. var latest = "Changes in " + latestVersion;
  34. var current = "Changes in " + currentVersion;
  35. var notes = releaseNotes.Split('\n').ToList();
  36. var bActive = false;
  37. foreach (var note in notes)
  38. {
  39. if (!string.IsNullOrWhiteSpace(note))
  40. {
  41. if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&
  42. string.Compare(latest.Trim().ToUpper(), note.Trim().ToUpper()) <= 0)
  43. bActive = true;
  44. if (note.Trim().ToUpper().StartsWith("CHANGES IN ") &&
  45. string.Compare(current.Trim().ToUpper(), note.Trim().ToUpper()) >= 0)
  46. bActive = false;
  47. if (bActive && !note.ToUpper().StartsWith("CHANGES IN") && !note.StartsWith("="))
  48. display.Add(note);
  49. }
  50. }
  51. if (!display.Any())
  52. display.Add("Various Internal Updates");
  53. return string.Join('\n', display);
  54. }
  55. private static bool? ShowReleaseNotes(string display, string latestVersion, string currentVersion)
  56. {
  57. var form = new NotesForm
  58. {
  59. Caption = string.Format("Release {0} is available!", latestVersion),
  60. CancelEnabled = false,
  61. Text = string.Format(
  62. "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} ",
  63. currentVersion,
  64. display
  65. )
  66. };
  67. return form.ShowDialog();
  68. }
  69. private static string GenerateInstallerScript(string installerFile)
  70. {
  71. var commands = new List<string>();
  72. commands.Add($"\"{installerFile}\" /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
  73. commands.Add($"\"{Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe")}\"");
  74. return string.Join('\n', commands);
  75. }
  76. public static bool CheckForUpdates(
  77. Func<string> getUpdateLocation,
  78. Func<string, string> getLatestVersion,
  79. Func<string, string> getReleaseNotes,
  80. Func<string, byte[]?> getInstaller,
  81. Action? beforeUpdate,
  82. bool elevated,
  83. string tempName)
  84. {
  85. var display = "";
  86. var latestVersion = "";
  87. var currentVersion = CoreUtils.GetVersion();
  88. var location = getUpdateLocation();
  89. if (!String.IsNullOrWhiteSpace(location))
  90. {
  91. Progress.ShowModal("Checking for Updates", progress =>
  92. {
  93. if (!string.Equals(currentVersion, "???"))
  94. {
  95. latestVersion = getLatestVersion(location);
  96. Logger.Send(LogType.Information, "",
  97. $"Update Check: Current: {currentVersion} Latest: {latestVersion}");
  98. if (!string.IsNullOrWhiteSpace(latestVersion))
  99. if (string.Compare(currentVersion, latestVersion) < 0)
  100. {
  101. var releasenotes = getReleaseNotes(location);
  102. display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);
  103. }
  104. }
  105. });
  106. }
  107. if (display.IsNullOrWhiteSpace()) return false;
  108. if (ShowReleaseNotes(display, latestVersion, currentVersion) == true)
  109. {
  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. }