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

Commit 95f1cb7

Browse files
committed
Fix a bunch of bugs in the git installation sequence and update progress
1 parent 4bc9602 commit 95f1cb7

File tree

14 files changed

+307
-253
lines changed

14 files changed

+307
-253
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ abstract class ApplicationManagerBase : IApplicationManager
1212
protected static ILogging Logger { get; } = LogHelper.GetLogger<IApplicationManager>();
1313

1414
private RepositoryManager repositoryManager;
15+
private Progress progressReporter;
1516
protected bool isBusy;
16-
public event Action<IProgress> OnProgress;
17+
public event Action<IProgress> OnProgress
18+
{
19+
add { progressReporter.OnProgress += value; }
20+
remove { progressReporter.OnProgress -= value; }
21+
}
1722

1823
public ApplicationManagerBase(SynchronizationContext synchronizationContext)
1924
{
25+
progressReporter = new Progress();
2026
SynchronizationContext = synchronizationContext;
2127
SynchronizationContext.SetSynchronizationContext(SynchronizationContext);
2228
ThreadingHelper.SetUIThread();
@@ -60,7 +66,8 @@ public void Run(bool firstRun)
6066
Logger.Trace("No git path found in settings");
6167

6268
isBusy = true;
63-
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken, (b, path) => InitializeEnvironment(path)) { Affinity = TaskAffinity.UI };
69+
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken,
70+
(b, path) => InitializeEnvironment(path)) { Affinity = TaskAffinity.UI };
6471
var findExecTask = new FindExecTask("git", CancellationToken)
6572
.FinallyInUI((b, ex, path) =>
6673
{
@@ -80,7 +87,15 @@ public void Run(bool firstRun)
8087
var gitInstaller = new GitInstaller(Environment, CancellationToken);
8188

8289
// if successful, continue with environment initialization, otherwise try to find an existing git installation
83-
gitInstaller.SetupGitIfNeeded(initEnvironmentTask, findExecTask);
90+
var setupTask = gitInstaller.SetupGitIfNeeded();
91+
setupTask.Progress(progressReporter.UpdateProgress);
92+
setupTask.OnEnd += (thisTask, result, success, exception) =>
93+
{
94+
if (success && result != null)
95+
thisTask.Then(initEnvironmentTask);
96+
else
97+
thisTask.Then(findExecTask);
98+
};
8499
}
85100
}
86101

src/GitHub.Api/Helpers/Progress.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitHub.Logging;
12
using System;
23

34
namespace GitHub.Unity
@@ -11,25 +12,41 @@ public interface IProgress
1112
float Percentage { get; }
1213
long Value { get; }
1314
long Total { get; }
15+
string Message { get; }
16+
event Action<IProgress> OnProgress;
1417
}
1518

1619
public class Progress : IProgress
1720
{
21+
private static ILogging Logger = LogHelper.GetLogger<Progress>();
1822
public ITask Task { get; internal set; }
1923
public float Percentage { get { return Total > 0 ? (float)(double)Value / Total : 0f; } }
2024
public long Value { get; internal set; }
2125
public long Total { get; internal set; }
26+
public string Message { get; internal set; }
2227

2328
private long previousValue;
2429
private float averageSpeed = -1f;
2530
private float lastSpeed = 0f;
2631
private float smoothing = 0.005f;
32+
public event Action<IProgress> OnProgress;
2733

28-
public void UpdateProgress(long value, long total)
34+
public void UpdateProgress(IProgress progress)
35+
{
36+
Task = progress.Task;
37+
UpdateProgress(progress.Value, progress.Total, progress.Message);
38+
}
39+
40+
public void UpdateProgress(long value, long total, string message = null)
2941
{
30-
previousValue = Value;
3142
Total = total;
3243
Value = value;
44+
Message = message ?? Message;
45+
if (Total == 0 || ((float)(double)Value / Total) - ((float)(double)previousValue / Total) > 1f / 100f)
46+
{ // signal progress in 1% increments or if we don't know what the total is
47+
previousValue = Value;
48+
OnProgress?.Invoke(this);
49+
}
3350
}
3451
}
3552
}

src/GitHub.Api/IO/Utils.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public static bool Copy(Stream source, Stream destination,
1414
Func<long, long, bool> progress = null,
1515
int progressUpdateRate = 100)
1616
{
17-
var logger = LogHelper.GetLogger("Copy");
1817
byte[] buffer = new byte[chunkSize];
1918
int bytesRead = 0;
2019
long totalRead = 0;
@@ -62,7 +61,6 @@ public static bool Copy(Stream source, Stream destination,
6261
timeToFinish = Math.Max(1L,
6362
(long)((totalSize - totalRead) / (averageSpeed / progressUpdateRate)));
6463

65-
logger.Trace($"totalRead: {totalRead} of {totalSize}");
6664
success = progress(totalRead, timeToFinish);
6765
if (!success)
6866
break;

0 commit comments

Comments
 (0)