Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f36f0de

Browse files
Merge remote-tracking branch 'remotes/origin/enhancements/async-git-setup-rollup' into features/unzip-task-extract-md5
2 parents a1ec1cb + 25b32d8 commit f36f0de

File tree

14 files changed

+668
-18
lines changed

14 files changed

+668
-18
lines changed

GitHub.Unity.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@
335335
</TypePattern>
336336
&lt;/Patterns&gt;</s:String>
337337
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
338+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MD/@EntryIndexedValue">MD</s:String>
338339
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SSH/@EntryIndexedValue">SSH</s:String>
339340
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
340341
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

src/GitHub.Api/Extensions/FileSystemExtensions.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
2+
using System.IO;
3+
using System.Linq;
24
using System.Security.Cryptography;
35
using System.Text;
46

57
namespace GitHub.Unity
68
{
79
static class FileSystemExtensions
810
{
9-
public static string CalculateMD5(this IFileSystem fileSystem, string file)
11+
public static string CalculateFileMD5(this IFileSystem fileSystem, string file)
1012
{
1113
byte[] computeHash;
1214
using (var md5 = MD5.Create())
@@ -17,7 +19,39 @@ public static string CalculateMD5(this IFileSystem fileSystem, string file)
1719
}
1820
}
1921

20-
return BitConverter.ToString(computeHash).Replace("-", string.Empty);
22+
return BitConverter.ToString(computeHash).Replace("-", string.Empty).ToLower();
23+
}
24+
25+
public static string CalculateFolderMD5(this IFileSystem fileSystem, string path, bool includeContents = true)
26+
{
27+
//https://stackoverflow.com/questions/3625658/creating-hash-for-folder
28+
29+
var filePaths = fileSystem.GetFiles(path, "*", SearchOption.AllDirectories)
30+
.OrderBy(p => p)
31+
.ToArray();
32+
33+
using (var md5 = MD5.Create())
34+
{
35+
foreach (var filePath in filePaths)
36+
{
37+
// hash path
38+
var relativeFilePath = filePath.Substring(path.Length + 1);
39+
var pathBytes = Encoding.UTF8.GetBytes(relativeFilePath);
40+
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
41+
42+
if (includeContents)
43+
{
44+
// hash contents
45+
var contentBytes = File.ReadAllBytes(filePath);
46+
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
47+
}
48+
}
49+
50+
//Handles empty filePaths case
51+
md5.TransformFinalBlock(new byte[0], 0, 0);
52+
53+
return BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
54+
}
2155
}
2256
}
2357
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<Compile Include="Application\ApplicationManagerBase.cs" />
118118
<Compile Include="Helpers\Constants.cs" />
119119
<Compile Include="Helpers\Validation.cs" />
120+
<Compile Include="Installer\UnzipTask.cs" />
120121
<Compile Include="OutputProcessors\GitAheadBehindStatusOutputProcessor.cs" />
121122
<Compile Include="OutputProcessors\LfsVersionOutputProcessor.cs" />
122123
<Compile Include="OutputProcessors\VersionOutputProcessor.cs" />
@@ -139,6 +140,7 @@
139140
<Compile Include="Tasks\BaseOutputProcessor.cs" />
140141
<Compile Include="Tasks\ConcurrentExclusiveInterleave.cs" />
141142
<Compile Include="Tasks\ConfigOutputProcessor.cs" />
143+
<Compile Include="Tasks\DownloadTask.cs" />
142144
<Compile Include="Tasks\ITaskManager.cs" />
143145
<Compile Include="Tasks\ProcessTask.cs" />
144146
<Compile Include="Tasks\TaskBase.cs" />

src/GitHub.Api/IO/FileSystem.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public bool FileExists(string filename)
3434
return File.Exists(filename);
3535
}
3636

37+
public long FileLength(string path)
38+
{
39+
var fileInfo = new FileInfo(path);
40+
return fileInfo.Length;
41+
}
42+
3743
public IEnumerable<string> GetDirectories(string path)
3844
{
3945
return Directory.GetDirectories(path);
@@ -167,6 +173,11 @@ public void WriteAllText(string path, string contents, Encoding encoding)
167173
File.WriteAllText(path, contents, encoding);
168174
}
169175

176+
public byte[] ReadAllBytes(string path)
177+
{
178+
return File.ReadAllBytes(path);
179+
}
180+
170181
public string ReadAllText(string path)
171182
{
172183
return File.ReadAllText(path);
@@ -201,5 +212,10 @@ public Stream OpenRead(string path)
201212
{
202213
return File.OpenRead(path);
203214
}
215+
216+
public Stream OpenWrite(string path, FileMode mode)
217+
{
218+
return new FileStream(path, mode);
219+
}
204220
}
205221
}

src/GitHub.Api/IO/IFileSystem.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace GitHub.Unity
77
public interface IFileSystem
88
{
99
bool FileExists(string path);
10+
long FileLength(string path);
1011
string Combine(string path1, string path2);
1112
string Combine(string path1, string path2, string path3);
1213
string GetFullPath(string path);
@@ -34,12 +35,15 @@ public interface IFileSystem
3435
void WriteAllText(string path, string contents);
3536
void WriteAllText(string path, string contents, Encoding encoding);
3637
void WriteAllLines(string path, string[] contents);
38+
byte[] ReadAllBytes(string path);
3739
string ReadAllText(string path);
3840
string ReadAllText(string path, Encoding encoding);
3941
Stream OpenRead(string path);
42+
Stream OpenWrite(string path, FileMode mode);
4043
string[] ReadAllLines(string path);
4144
char DirectorySeparatorChar { get; }
4245
bool ExistingPathIsDirectory(string path);
4346
void SetCurrentDirectory(string currentDirectory);
47+
byte[] ReadAllBytes(string path);
4448
}
4549
}

src/GitHub.Api/IO/NiceIO.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,12 @@ public NPath WriteAllText(string contents, Encoding encoding)
907907
return this;
908908
}
909909

910+
public byte[] ReadAllBytes()
911+
{
912+
ThrowIfRelative();
913+
return FileSystem.ReadAllBytes(ToString());
914+
}
915+
910916
public string ReadAllText()
911917
{
912918
ThrowIfRelative();

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@ class GitInstaller : IGitInstaller
1818
private readonly CancellationToken cancellationToken;
1919
private readonly IEnvironment environment;
2020
private readonly ILogging logger;
21-
22-
private delegate void ExtractZipFile(string archive, string outFolder, CancellationToken cancellationToken,
23-
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null);
24-
private ExtractZipFile extractCallback;
21+
private readonly IZipHelper zipHelper;
2522

2623
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken)
27-
: this(environment, null, cancellationToken)
24+
: this(environment, ZipHelper.Instance, cancellationToken)
2825
{
2926
}
3027

31-
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken)
28+
public GitInstaller(IEnvironment environment, IZipHelper zipHelper, CancellationToken cancellationToken)
3229
{
3330
Guard.ArgumentNotNull(environment, nameof(environment));
3431

3532
logger = Logging.GetLogger(GetType());
3633
this.cancellationToken = cancellationToken;
3734

3835
this.environment = environment;
39-
this.extractCallback = sharpZipLibHelper != null
40-
? (ExtractZipFile)sharpZipLibHelper.Extract
41-
: ZipHelper.ExtractZipFile;
42-
36+
this.zipHelper = zipHelper;
4337

4438
GitInstallationPath = environment.GetSpecialFolder(Environment.SpecialFolder.LocalApplicationData)
4539
.ToNPath().Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion);
@@ -96,7 +90,7 @@ public bool IsGitLfsExtracted()
9690
return false;
9791
}
9892

99-
var calculateMd5 = environment.FileSystem.CalculateMD5(GitLfsExecutablePath);
93+
var calculateMd5 = environment.FileSystem.CalculateFileMD5(GitLfsExecutablePath);
10094
logger.Trace("GitLFS MD5: {0}", calculateMd5);
10195
var md5 = environment.IsWindows ? WindowsGitLfsExecutableMD5 : MacGitLfsExecutableMD5;
10296
if (String.Compare(calculateMd5, md5, true) != 0)
@@ -184,7 +178,7 @@ public Task<bool> SetupGitIfNeeded(NPath tempPath, IProgress<float> zipFileProgr
184178
{
185179
logger.Trace("Extracting \"{0}\" to \"{1}\"", archiveFilePath, unzipPath);
186180

187-
extractCallback(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
181+
zipHelper.Extract(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
188182
estimatedDurationProgress);
189183
}
190184
catch (Exception ex)
@@ -249,7 +243,7 @@ public Task<bool> SetupGitLfsIfNeeded(NPath tempPath, IProgress<float> zipFilePr
249243
{
250244
logger.Trace("Extracting \"{0}\" to \"{1}\"", archiveFilePath, unzipPath);
251245

252-
extractCallback(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
246+
zipHelper.Extract(archiveFilePath, unzipPath, cancellationToken, zipFileProgress,
253247
estimatedDurationProgress);
254248
}
255249
catch (Exception ex)

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
namespace GitHub.Unity
6+
{
7+
class UnzipTask: TaskBase
8+
{
9+
private readonly string archiveFilePath;
10+
private readonly string extractedPath;
11+
private readonly IZipHelper zipHelper;
12+
private readonly IProgress<float> zipFileProgress;
13+
private readonly IProgress<long> estimatedDurationProgress;
14+
15+
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath,
16+
IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null) :
17+
this(token, archiveFilePath, extractedPath, ZipHelper.Instance, zipFileProgress, estimatedDurationProgress)
18+
{
19+
20+
}
21+
22+
public UnzipTask(CancellationToken token, string archiveFilePath, string extractedPath, IZipHelper zipHelper, IProgress<float> zipFileProgress = null, IProgress<long> estimatedDurationProgress = null)
23+
: base(token)
24+
{
25+
this.archiveFilePath = archiveFilePath;
26+
this.extractedPath = extractedPath;
27+
this.zipHelper = zipHelper;
28+
this.zipFileProgress = zipFileProgress;
29+
this.estimatedDurationProgress = estimatedDurationProgress;
30+
}
31+
32+
protected override void Run(bool success)
33+
{
34+
base.Run(success);
35+
36+
UnzipArchive();
37+
}
38+
39+
private void UnzipArchive()
40+
{
41+
Logger.Trace("Unzip File: {0} to Path: {1}", archiveFilePath, extractedPath);
42+
43+
zipHelper.Extract(archiveFilePath, extractedPath, Token, zipFileProgress, estimatedDurationProgress);
44+
45+
Logger.Trace("Completed Unzip");
46+
}
47+
}
48+
}

src/GitHub.Api/Installer/ZipHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ namespace GitHub.Unity
88
{
99
class ZipHelper : IZipHelper
1010
{
11+
private static IZipHelper instance;
12+
13+
public static IZipHelper Instance
14+
{
15+
get
16+
{
17+
if (instance == null)
18+
{
19+
instance = new ZipHelper();
20+
}
21+
22+
return instance;
23+
}
24+
}
25+
1126
public static bool Copy(Stream source, Stream destination, int chunkSize, long totalSize,
1227
Func<long, long, bool> progress, int progressUpdateRate)
1328
{

0 commit comments

Comments
 (0)