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

Commit c48a2b3

Browse files
committed
Make DownloadTask cancellable and add progress reporting
1 parent 7cf671b commit c48a2b3

File tree

12 files changed

+383
-210
lines changed

12 files changed

+383
-210
lines changed

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
<Compile Include="Tasks\TaskExtensions.cs" />
150150
<Compile Include="Tasks\TaskManager.cs" />
151151
<Compile Include="Platform\FindExecTask.cs" />
152-
<Compile Include="Helpers\ProgressReport.cs" />
152+
<Compile Include="Helpers\Progress.cs" />
153153
<Compile Include="Metrics\IMetricsService.cs" />
154154
<Compile Include="Metrics\IUsageTracker.cs" />
155155
<Compile Include="Metrics\UsageModel.cs" />

src/GitHub.Api/Helpers/Progress.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
public interface IProgress
6+
{
7+
ITask Task { get; }
8+
/// <summary>
9+
/// From 0 to 1
10+
/// </summary>
11+
float Percentage { get; }
12+
long Value { get; }
13+
long Total { get; }
14+
}
15+
16+
public class Progress : IProgress
17+
{
18+
public ITask Task { get; internal set; }
19+
public float Percentage { get { return Total > 0 ? (float)(double)Value / Total : 0f; } }
20+
public long Value { get; internal set; }
21+
public long Total { get; internal set; }
22+
23+
private long previousValue;
24+
private float averageSpeed = -1f;
25+
private float lastSpeed = 0f;
26+
private float smoothing = 0.005f;
27+
28+
public void UpdateProgress(long value, long total)
29+
{
30+
previousValue = Value;
31+
Total = total;
32+
Value = value;
33+
}
34+
}
35+
}

src/GitHub.Api/Helpers/ProgressReport.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,30 +175,36 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
175175
private ITask CreateDownloadTask()
176176
{
177177
var tempZipPath = NPath.CreateTempDirectory("git_zip_paths");
178-
gitArchiveFilePath = tempZipPath.Combine("git.zip");
179-
gitLfsArchivePath = tempZipPath.Combine("git-lfs.zip");
180-
181-
var downloadGitMd5Task = new DownloadTextTask(CancellationToken.None,
182-
"https://ghfvs-installer.github.com/unity/portable_git/git.zip.MD5.txt");
183-
184-
var downloadGitTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
185-
"https://ghfvs-installer.github.com/unity/portable_git/git.zip", gitArchiveFilePath, retryCount: 1);
186-
187-
var downloadGitLfsMd5Task = new DownloadTextTask(CancellationToken.None,
188-
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip.MD5.txt");
189-
190-
var downloadGitLfsTask = new DownloadTask(CancellationToken.None, environment.FileSystem,
191-
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip", gitLfsArchivePath, retryCount: 1);
192-
193-
return downloadGitMd5Task.Then((b, s) => {
194-
downloadGitTask.ValidationHash = s;
195-
})
196-
.Then(downloadGitTask)
197-
.Then(downloadGitLfsMd5Task)
198-
.Then((b, s) => {
199-
downloadGitLfsTask.ValidationHash = s;
200-
})
201-
.Then(downloadGitLfsTask);
178+
gitArchiveFilePath = tempZipPath.Combine("git");
179+
gitLfsArchivePath = tempZipPath.Combine("git-lfs");
180+
181+
var downloadGitMd5Task = new DownloadTextTask(TaskManager.Instance.Token,
182+
environment.FileSystem,
183+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip.MD5.txt",
184+
gitArchiveFilePath);
185+
186+
var downloadGitTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
187+
"https://ghfvs-installer.github.com/unity/portable_git/git.zip",
188+
gitArchiveFilePath, retryCount: 1);
189+
190+
var downloadGitLfsMd5Task = new DownloadTextTask(TaskManager.Instance.Token, environment.FileSystem,
191+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip.MD5.txt",
192+
gitLfsArchivePath);
193+
194+
var downloadGitLfsTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
195+
"https://ghfvs-installer.github.com/unity/portable_git/git-lfs.zip",
196+
gitLfsArchivePath, retryCount: 1);
197+
198+
return downloadGitMd5Task
199+
.Then((b, s) => {
200+
downloadGitTask.ValidationHash = s;
201+
})
202+
.Then(downloadGitTask)
203+
.Then(downloadGitLfsMd5Task)
204+
.Then((b, s) => {
205+
downloadGitLfsTask.ValidationHash = s;
206+
})
207+
.Then(downloadGitLfsTask);
202208
}
203209

204210
private bool IsGitExtracted()

src/GitHub.Api/Primitives/UriString.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ void SetUri(Uri uri)
7171
Host = uri.Host;
7272
if (uri.Segments.Any())
7373
{
74-
RepositoryName = GetRepositoryName(uri.Segments.Last());
74+
Filename = uri.Segments.Last();
75+
RepositoryName = GetRepositoryName(Filename);
7576
}
7677

7778
if (uri.Segments.Length > 2)
@@ -86,15 +87,17 @@ void SetFilePath(Uri uri)
8687
{
8788
Host = "";
8889
Owner = "";
89-
RepositoryName = GetRepositoryName(uri.Segments.Last());
90+
Filename = uri.Segments.Last();
91+
RepositoryName = GetRepositoryName(Filename);
9092
IsFileUri = true;
9193
}
9294

9395
void SetFilePath(string path)
9496
{
9597
Host = "";
9698
Owner = "";
97-
RepositoryName = GetRepositoryName(path.Replace("/", @"\").RightAfterLast(@"\"));
99+
Filename = path.Replace("/", @"\").RightAfterLast(@"\");
100+
RepositoryName = GetRepositoryName(Filename);
98101
IsFileUri = true;
99102
}
100103

@@ -131,6 +134,7 @@ bool ParseScpSyntax(string scpString)
131134

132135
public bool IsValidUri => url != null;
133136
public string Protocol => url?.Scheme;
137+
public string Filename { get; private set; }
134138

135139
/// <summary>
136140
/// Attempts a best-effort to convert the remote origin to a GitHub Repository URL.

0 commit comments

Comments
 (0)