Update.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.DynamicGrid;
  4. using InABox.WPF;
  5. using RestSharp;
  6. using Syncfusion.Windows.Shared;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace PRS.Shared
  15. {
  16. /// <summary>
  17. /// Manages updating the app for both PRSDesktop and PRSServer
  18. /// </summary>
  19. public static class Update
  20. {
  21. public static IRestResponse GetRemoteFile(string location)
  22. {
  23. var uri = new Uri(location);
  24. var client = new RestClient(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. public static void CheckForUpdates(
  68. Func<string> getUpdateLocation,
  69. Func<string, string> getLatestVersion,
  70. Func<string, string> getReleaseNotes,
  71. Func<string, byte[]?> getInstaller,
  72. Action? beforeUpdate,
  73. bool elevated,
  74. string tempName)
  75. {
  76. var display = "";
  77. var latestVersion = "";
  78. var currentVersion = CoreUtils.GetVersion();
  79. var location = getUpdateLocation();
  80. if (!String.IsNullOrWhiteSpace(location))
  81. {
  82. Progress.ShowModal("Checking for Updates", progress =>
  83. {
  84. if (!string.Equals(currentVersion, "???"))
  85. {
  86. latestVersion = getLatestVersion(location);
  87. Logger.Send(LogType.Information, "",
  88. $"Update Check: Current: {currentVersion} Latest: {latestVersion}");
  89. if (!string.IsNullOrWhiteSpace(latestVersion))
  90. if (string.Compare(currentVersion, latestVersion) < 0)
  91. {
  92. var releasenotes = getReleaseNotes(location);
  93. display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);
  94. }
  95. }
  96. });
  97. }
  98. if (display.IsNullOrWhiteSpace()) return;
  99. if (ShowReleaseNotes(display, latestVersion, currentVersion) == true)
  100. {
  101. var bOK = false;
  102. var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);
  103. Progress.ShowModal("Retrieving Update", progress =>
  104. {
  105. var installer = getInstaller(location);
  106. if (installer?.Any() == true)
  107. {
  108. File.WriteAllBytes(tempinstall, installer);
  109. bOK = true;
  110. }
  111. beforeUpdate?.Invoke();
  112. progress.Report("Launching Installer");
  113. if (bOK)
  114. {
  115. var startInfo = new ProcessStartInfo(tempinstall,
  116. " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
  117. startInfo.Verb = elevated ? "runas" : "open";
  118. startInfo.UseShellExecute = true;
  119. var p = Process.Start(startInfo);
  120. p.WaitForExit();
  121. }
  122. });
  123. if (!bOK)
  124. MessageBox.Show("Unable to retrieve installer!");
  125. }
  126. }
  127. }
  128. }