No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DownloadAndInstallOperation.cs 7.9KB

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