Update.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. bool elevated,
  73. string tempName)
  74. {
  75. var display = "";
  76. var latestVersion = "";
  77. var currentVersion = CoreUtils.GetVersion();
  78. var location = getUpdateLocation();
  79. if (!String.IsNullOrWhiteSpace(location))
  80. {
  81. Progress.ShowModal("Checking for Updates", progress =>
  82. {
  83. if (!string.Equals(currentVersion, "???"))
  84. {
  85. latestVersion = getLatestVersion(location);
  86. Logger.Send(LogType.Information, "",
  87. $"Update Check: Current: {currentVersion} Latest: {latestVersion}");
  88. if (!string.IsNullOrWhiteSpace(latestVersion))
  89. if (string.Compare(currentVersion, latestVersion) < 0)
  90. {
  91. var releasenotes = getReleaseNotes(location);
  92. display = ParseReleaseNotes(releasenotes, latestVersion, currentVersion);
  93. }
  94. }
  95. });
  96. }
  97. if (display.IsNullOrWhiteSpace()) return;
  98. if (ShowReleaseNotes(display, latestVersion, currentVersion) == true)
  99. {
  100. var bOK = false;
  101. var tempinstall = Path.Combine(CoreUtils.GetPath(), tempName);
  102. Progress.ShowModal("Retrieving Update", progress =>
  103. {
  104. var installer = getInstaller(location);
  105. if (installer?.Any() == true)
  106. {
  107. File.WriteAllBytes(tempinstall, installer);
  108. bOK = true;
  109. }
  110. progress.Report("Launching Installer");
  111. if (bOK)
  112. {
  113. var startInfo = new ProcessStartInfo(tempinstall,
  114. " /SILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS /RESTARTAPPLICATIONS");
  115. startInfo.Verb = elevated ? "runas" : "open";
  116. startInfo.UseShellExecute = true;
  117. var p = Process.Start(startInfo);
  118. p.WaitForExit();
  119. }
  120. });
  121. if (!bOK)
  122. MessageBox.Show("Unable to retrieve installer!");
  123. }
  124. }
  125. }
  126. }