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

Commit 434d1d1

Browse files
Merge pull request #549 from github-for-unity/enhancements/unzip-task
Creating a ZipHelper singleton and UnzipTask
2 parents e4284b4 + 693c67b commit 434d1d1

File tree

6 files changed

+116
-12
lines changed

6 files changed

+116
-12
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 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" />

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 6 additions & 12 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);
@@ -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
{

src/tests/IntegrationTests/IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Compile Include="Properties\AssemblyInfo.cs" />
8989
<Compile Include="SetUpFixture.cs" />
9090
<Compile Include="ThreadSynchronizationContext.cs" />
91+
<Compile Include="UnzipTaskTests.cs" />
9192
</ItemGroup>
9293
<ItemGroup>
9394
<ProjectReference Include="$(SolutionDir)src\GitHub.Api\GitHub.Api.csproj">
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using GitHub.Unity;
4+
using Microsoft.Win32.SafeHandles;
5+
using NSubstitute;
6+
using NUnit.Framework;
7+
using Rackspace.Threading;
8+
9+
namespace IntegrationTests
10+
{
11+
[TestFixture]
12+
class UnzipTaskTests : BaseTaskManagerTest
13+
{
14+
[Test]
15+
public void UnzipTest()
16+
{
17+
InitializeTaskManager();
18+
19+
var cacheContainer = Substitute.For<ICacheContainer>();
20+
Environment = new IntegrationTestEnvironment(cacheContainer, TestBasePath, SolutionDirectory);
21+
22+
var destinationPath = TestBasePath.Combine("git_zip").CreateDirectory();
23+
var archiveFilePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", destinationPath, Environment);
24+
25+
Logger.Trace("ArchiveFilePath: {0}", archiveFilePath);
26+
Logger.Trace("TestBasePath: {0}", TestBasePath);
27+
28+
var extractedPath = TestBasePath.Combine("git_zip_extracted").CreateDirectory();
29+
30+
var zipProgress = 0;
31+
Logger.Trace("Pct Complete {0}%", zipProgress);
32+
var unzipTask = new UnzipTask(CancellationToken.None, archiveFilePath, extractedPath,
33+
new Progress<float>(zipFileProgress => {
34+
var zipFileProgressInteger = (int) (zipFileProgress * 100);
35+
if (zipProgress != zipFileProgressInteger)
36+
{
37+
zipProgress = zipFileProgressInteger;
38+
Logger.Trace("Pct Complete {0}%", zipProgress);
39+
}
40+
}));
41+
42+
unzipTask.Start().Wait();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)