|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | + |
| 4 | +namespace GitHub.Unity |
| 5 | +{ |
| 6 | + class GitInstallDetails |
| 7 | + { |
| 8 | + public NPath GitInstallPath { get; } |
| 9 | + public string GitExec { get; } |
| 10 | + public NPath GitExecPath { get; } |
| 11 | + public string GitLfsExec { get; } |
| 12 | + public NPath GitLfsExecPath { get; } |
| 13 | + |
| 14 | + public const string ExtractedMD5 = "65fd0575d3b47d8207b9e19d02faca4f"; |
| 15 | + public const string FileListMD5 = "a152a216b2e76f6c127053251187a278"; |
| 16 | + |
| 17 | + private const string PackageVersion = "f02737a78695063deace08e96d5042710d3e32db"; |
| 18 | + private const string PackageName = "PortableGit"; |
| 19 | + private const string PackageNameWithVersion = PackageName + "_" + PackageVersion; |
| 20 | + |
| 21 | + private readonly bool onWindows; |
| 22 | + |
| 23 | + public GitInstallDetails(NPath targetInstallPath, bool onWindows) |
| 24 | + { |
| 25 | + this.onWindows = onWindows; |
| 26 | + var gitInstallPath = targetInstallPath.Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion); |
| 27 | + GitInstallPath = gitInstallPath; |
| 28 | + |
| 29 | + if (onWindows) |
| 30 | + { |
| 31 | + GitExec += "git.exe"; |
| 32 | + GitLfsExec += "git-lfs.exe"; |
| 33 | + |
| 34 | + GitExecPath = gitInstallPath.Combine("cmd", GitExec); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + GitExec = "git"; |
| 39 | + GitLfsExec = "git-lfs"; |
| 40 | + |
| 41 | + GitExecPath = gitInstallPath.Combine("bin", GitExec); |
| 42 | + } |
| 43 | + |
| 44 | + GitLfsExecPath = GetGitLfsExecPath(gitInstallPath); |
| 45 | + } |
| 46 | + |
| 47 | + public NPath GetGitLfsExecPath(NPath gitInstallRoot) |
| 48 | + { |
| 49 | + return onWindows |
| 50 | + ? gitInstallRoot.Combine("mingw32", "libexec", "git-core", GitLfsExec) |
| 51 | + : gitInstallRoot.Combine("libexec", "git-core", GitLfsExec); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + class GitInstaller |
| 56 | + { |
| 57 | + private static ILogging Logger = Logging.GetLogger<GitInstaller>(); |
| 58 | + |
| 59 | + private readonly IEnvironment environment; |
| 60 | + private readonly IZipHelper sharpZipLibHelper; |
| 61 | + private readonly CancellationToken cancellationToken; |
| 62 | + private readonly GitInstallDetails installDetails; |
| 63 | + |
| 64 | + public GitInstaller(IEnvironment environment, CancellationToken cancellationToken, GitInstallDetails installDetails) |
| 65 | + : this(environment, ZipHelper.Instance, cancellationToken, installDetails) |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken, GitInstallDetails installDetails) |
| 70 | + { |
| 71 | + this.environment = environment; |
| 72 | + this.sharpZipLibHelper = sharpZipLibHelper; |
| 73 | + this.cancellationToken = cancellationToken; |
| 74 | + this.installDetails = installDetails; |
| 75 | + } |
| 76 | + |
| 77 | + public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure) |
| 78 | + { |
| 79 | + Logger.Trace("SetupGitIfNeeded"); |
| 80 | + |
| 81 | + if (!environment.IsWindows) |
| 82 | + { |
| 83 | + onFailure.Start(); |
| 84 | + } |
| 85 | + |
| 86 | + new FuncTask<bool>(cancellationToken, IsGitExtracted) |
| 87 | + .Then((success, isPortableGitExtracted) => { |
| 88 | + |
| 89 | + Logger.Trace("IsPortableGitExtracted: {0}", isPortableGitExtracted); |
| 90 | + |
| 91 | + if (isPortableGitExtracted) |
| 92 | + { |
| 93 | + Logger.Trace("SetupGitIfNeeded: Skipped"); |
| 94 | + |
| 95 | + new FuncTask<NPath>(cancellationToken, () => installDetails.GitExecPath) |
| 96 | + .Then(onSuccess) |
| 97 | + .Start(); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + var tempZipPath = NPath.CreateTempDirectory("git_zip_paths"); |
| 102 | + var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", tempZipPath, environment); |
| 103 | + var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", tempZipPath, environment); |
| 104 | + |
| 105 | + var gitExtractPath = tempZipPath.Combine("git").CreateDirectory(); |
| 106 | + var gitLfsExtractPath = tempZipPath.Combine("git-lfs").CreateDirectory(); |
| 107 | + |
| 108 | + new UnzipTask(cancellationToken, gitArchivePath, gitExtractPath, sharpZipLibHelper) |
| 109 | + .Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath, sharpZipLibHelper)) |
| 110 | + .Then(() => { |
| 111 | + var targetGitLfsExecPath = installDetails.GetGitLfsExecPath(gitExtractPath); |
| 112 | + var extractGitLfsExePath = gitLfsExtractPath.Combine(installDetails.GitLfsExec); |
| 113 | + extractGitLfsExePath.Move(targetGitLfsExecPath); |
| 114 | + |
| 115 | + var extractedMD5 = environment.FileSystem.CalculateFolderMD5(gitExtractPath); |
| 116 | + if (!extractedMD5.Equals(GitInstallDetails.ExtractedMD5, StringComparison.InvariantCultureIgnoreCase)) |
| 117 | + { |
| 118 | + Logger.Warning("MD5 {0} does not match expected {1}", extractedMD5, GitInstallDetails.ExtractedMD5); |
| 119 | + Logger.Warning("Failed PortableGitInstallTask"); |
| 120 | + throw new Exception(); |
| 121 | + } |
| 122 | + |
| 123 | + Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", gitExtractPath, |
| 124 | + installDetails.GitInstallPath); |
| 125 | + |
| 126 | + gitExtractPath.Move(installDetails.GitInstallPath); |
| 127 | + |
| 128 | + Logger.Trace("Deleting tempZipPath:\"{0}\"", tempZipPath); |
| 129 | + tempZipPath.DeleteIfExists(); |
| 130 | + |
| 131 | + }).Finally((b, exception) => { |
| 132 | + if (b) |
| 133 | + { |
| 134 | + Logger.Trace("SetupGitIfNeeded: Success"); |
| 135 | + |
| 136 | + new FuncTask<NPath>(cancellationToken, () => installDetails.GitExecPath) |
| 137 | + .Then(onSuccess) |
| 138 | + .Start(); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + Logger.Trace("SetupGitIfNeeded: Failed"); |
| 143 | + |
| 144 | + onFailure.Start(); |
| 145 | + } |
| 146 | + }).Start(); |
| 147 | + } |
| 148 | + |
| 149 | + }).Start(); |
| 150 | + } |
| 151 | + |
| 152 | + private bool IsGitExtracted() |
| 153 | + { |
| 154 | + if (!installDetails.GitInstallPath.DirectoryExists()) |
| 155 | + { |
| 156 | + Logger.Trace("{0} does not exist", installDetails.GitInstallPath); |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + var fileListMD5 = environment.FileSystem.CalculateFolderMD5(installDetails.GitInstallPath, false); |
| 161 | + if (!fileListMD5.Equals(GitInstallDetails.FileListMD5, StringComparison.InvariantCultureIgnoreCase)) |
| 162 | + { |
| 163 | + Logger.Trace("MD5 {0} does not match expected {1}", fileListMD5, GitInstallDetails.FileListMD5); |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + Logger.Trace("Git Present"); |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments