DownloadAndInstallOperation.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System.Diagnostics;
  2. using System.IO;
  3. using System.Net;
  4. using Codice.Client.Common.Threading;
  5. using Codice.CM.Common;
  6. using Codice.Utils;
  7. using PlasticGui;
  8. using PlasticGui.Help.NewVersions;
  9. using PlasticGui.WorkspaceWindow;
  10. using Unity.PlasticSCM.Editor.Tool;
  11. using Unity.PlasticSCM.Editor.UI.UIElements;
  12. using Unity.PlasticSCM.Editor.WebApi;
  13. namespace Unity.PlasticSCM.Editor.Views.Welcome
  14. {
  15. class DownloadAndInstallOperation
  16. {
  17. internal static void Run(
  18. Edition plasticEdition,
  19. string installerDestinationPath,
  20. ProgressControlsForDialogs progressControls)
  21. {
  22. ((IProgressControls)progressControls).ShowProgress(
  23. PlasticLocalization.GetString(PlasticLocalization.Name.DownloadingProgress));
  24. NewVersionResponse plasticVersion = null;
  25. IThreadWaiter waiter = ThreadWaiter.GetWaiter();
  26. waiter.Execute(
  27. /*threadOperationDelegate*/ delegate
  28. {
  29. plasticVersion =
  30. PlasticScmRestApiClient.GetLastVersion(plasticEdition);
  31. if (plasticVersion == null)
  32. return;
  33. string installerUrl = GetInstallerUrl(
  34. plasticVersion.Version,
  35. plasticEdition == Edition.Cloud);
  36. DownloadInstaller(
  37. installerUrl,
  38. installerDestinationPath,
  39. progressControls);
  40. if (!PlatformIdentifier.IsMac())
  41. return;
  42. installerDestinationPath = UnZipMacOsPackage(
  43. installerDestinationPath);
  44. },
  45. /*afterOperationDelegate*/ delegate
  46. {
  47. ((IProgressControls)progressControls).HideProgress();
  48. if (waiter.Exception != null)
  49. {
  50. ((IProgressControls)progressControls).ShowError(
  51. waiter.Exception.Message);
  52. return;
  53. }
  54. if (plasticVersion == null)
  55. {
  56. ((IProgressControls)progressControls).ShowError(
  57. PlasticLocalization.GetString(PlasticLocalization.Name.ConnectingError));
  58. return;
  59. }
  60. if (!File.Exists(installerDestinationPath))
  61. return;
  62. RunInstaller(
  63. installerDestinationPath,
  64. progressControls);
  65. });
  66. }
  67. static void RunInstaller(
  68. string installerPath,
  69. ProgressControlsForDialogs progressControls)
  70. {
  71. progressControls.ProgressData.ProgressPercent = -1;
  72. ((IProgressControls)progressControls).ShowProgress(
  73. PlasticLocalization.GetString(PlasticLocalization.Name.InstallingProgress));
  74. MacOSConfigWorkaround configWorkaround = new MacOSConfigWorkaround();
  75. IThreadWaiter waiter = ThreadWaiter.GetWaiter();
  76. waiter.Execute(
  77. /*threadOperationDelegate*/ delegate
  78. {
  79. configWorkaround.CreateClientConfigIfNeeded();
  80. Process installerProcess =
  81. LaunchInstaller.ForPlatform(installerPath);
  82. if (installerProcess != null)
  83. installerProcess.WaitForExit();
  84. configWorkaround.DeleteClientConfigIfNeeded();
  85. },
  86. /*afterOperationDelegate*/ delegate
  87. {
  88. ((IProgressControls)progressControls).HideProgress();
  89. if (waiter.Exception != null)
  90. {
  91. ((IProgressControls)progressControls).ShowError(
  92. waiter.Exception.Message);
  93. return;
  94. }
  95. File.Delete(installerPath);
  96. });
  97. }
  98. static void DownloadInstaller(
  99. string url,
  100. string destinationPath,
  101. ProgressControlsForDialogs progressControls)
  102. {
  103. int bytesProcessed = 0;
  104. Stream remoteStream = null;
  105. Stream localStream = null;
  106. WebResponse response = null;
  107. try
  108. {
  109. WebRequest request = WebRequest.Create(url);
  110. response = request.GetResponse();
  111. long totalBytes = response.ContentLength;
  112. if (File.Exists(destinationPath) &&
  113. new FileInfo(destinationPath).Length == totalBytes)
  114. {
  115. UnityEngine.Debug.LogFormat(
  116. PlasticLocalization.GetString(PlasticLocalization.Name.SkippingDownloadFileExists),
  117. destinationPath);
  118. return;
  119. }
  120. remoteStream = response.GetResponseStream();
  121. localStream = File.Create(destinationPath);
  122. byte[] buffer = new byte[100 * 1024];
  123. int bytesRead;
  124. do
  125. {
  126. bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
  127. localStream.Write(buffer, 0, bytesRead);
  128. bytesProcessed += bytesRead;
  129. progressControls.ProgressData.ProgressPercent =
  130. GetProgressBarPercent.ForTransfer(
  131. bytesProcessed,
  132. totalBytes) / 100f;
  133. } while (bytesRead > 0);
  134. }
  135. finally
  136. {
  137. if (response != null)
  138. response.Close();
  139. if (remoteStream != null)
  140. remoteStream.Close();
  141. if (localStream != null)
  142. localStream.Close();
  143. }
  144. }
  145. static string UnZipMacOsPackage(
  146. string zipInstallerPath)
  147. {
  148. try
  149. {
  150. string pkgInstallerPath = zipInstallerPath.Substring(
  151. 0, zipInstallerPath.Length - ".zip".Length);
  152. string unzipCommand = string.Format(
  153. "unzip -p \"{0}\" > \"{1}\"",
  154. zipInstallerPath, pkgInstallerPath);
  155. unzipCommand = unzipCommand.Replace("\"", "\"\"");
  156. ProcessStartInfo processStartInfo = new ProcessStartInfo();
  157. processStartInfo.FileName = "/bin/bash";
  158. processStartInfo.Arguments = string.Format("-c \"{0}\"", unzipCommand);
  159. processStartInfo.UseShellExecute = false;
  160. processStartInfo.RedirectStandardOutput = true;
  161. processStartInfo.CreateNoWindow = true;
  162. Process process = Process.Start(processStartInfo);
  163. process.WaitForExit();
  164. return pkgInstallerPath;
  165. }
  166. finally
  167. {
  168. File.Delete(zipInstallerPath);
  169. }
  170. }
  171. static string GetInstallerUrl(
  172. string version,
  173. bool isCloudEdition)
  174. {
  175. string edition = isCloudEdition ?
  176. "cloudedition" : "full";
  177. string platform = PlatformIdentifier.IsMac() ?
  178. "macosx" : "windows";
  179. return string.Format(
  180. @"https://www.plasticscm.com/download/downloadinstaller/{0}/plasticscm/{1}/{2}",
  181. version,
  182. platform,
  183. edition);
  184. }
  185. }
  186. }