This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathVSGitExt.cs
More file actions
148 lines (128 loc) · 5.29 KB
/
VSGitExt.cs
File metadata and controls
148 lines (128 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using GitHub.Models;
using GitHub.Services;
using GitHub.Logging;
using GitHub.Extensions;
using GitHub.TeamFoundation.Services;
using Serilog;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
using Task = System.Threading.Tasks.Task;
namespace GitHub.VisualStudio.Base
{
/// <summary>
/// This service acts as an always available version of <see cref="IGitExt"/>.
/// </summary>
/// <remarks>
/// Initialization for this service will be done asynchronously and the <see cref="IGitExt" /> service will be
/// retrieved using <see cref="GetServiceAsync" />. This means the service can be constructed and subscribed to from a background thread.
/// </remarks>
public class VSGitExt : IVSGitExt
{
static readonly ILogger log = LogManager.ForContext<VSGitExt>();
readonly IAsyncServiceProvider asyncServiceProvider;
readonly ILocalRepositoryModelFactory repositoryFactory;
readonly object refreshLock = new object();
IGitExt gitService;
IReadOnlyList<ILocalRepositoryModel> activeRepositories;
public VSGitExt(IAsyncServiceProvider asyncServiceProvider)
: this(asyncServiceProvider, new VSUIContextFactory(), new LocalRepositoryModelFactory(), ThreadHelper.JoinableTaskContext)
{
}
public VSGitExt(IAsyncServiceProvider asyncServiceProvider, IVSUIContextFactory factory, ILocalRepositoryModelFactory repositoryFactory,
JoinableTaskContext joinableTaskContext)
{
JoinableTaskCollection = joinableTaskContext.CreateCollection();
JoinableTaskCollection.DisplayName = nameof(VSGitExt);
JoinableTaskFactory = joinableTaskContext.CreateFactory(JoinableTaskCollection);
this.asyncServiceProvider = asyncServiceProvider;
this.repositoryFactory = repositoryFactory;
// Start with empty array until we have a chance to initialize.
ActiveRepositories = Array.Empty<ILocalRepositoryModel>();
// The IGitExt service isn't available when a TFS based solution is opened directly.
// It will become available when moving to a Git based solution (and cause a UIContext event to fire).
// NOTE: I tried using the RepositoryOpen context, but it didn't work consistently.
var context = factory.GetUIContext(new Guid(Guids.GitSccProviderId));
context.WhenActivated(() =>
{
log.Debug("WhenActivated");
JoinableTaskFactory.RunAsync(InitializeAsync).Task.Forget(log);
});
}
async Task InitializeAsync()
{
gitService = await GetServiceAsync<IGitExt>();
if (gitService == null)
{
log.Error("Couldn't find IGitExt service");
return;
}
// Refresh on background thread
await Task.Run(() => RefreshActiveRepositories());
gitService.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(gitService.ActiveRepositories))
{
RefreshActiveRepositories();
}
};
}
public void RefreshActiveRepositories()
{
try
{
lock (refreshLock)
{
log.Debug(
"IGitExt.ActiveRepositories (#{Id}) returned {Repositories}",
gitService.GetHashCode(),
gitService.ActiveRepositories.Select(x => x.RepositoryPath));
if (gitService != null)
{
ActiveRepositories = gitService.ActiveRepositories.Select(x => repositoryFactory.Create(x.RepositoryPath)).ToList();
}
}
}
catch (Exception e)
{
log.Error(e, "Error refreshing repositories");
ActiveRepositories = Array.Empty<ILocalRepositoryModel>();
}
}
public IReadOnlyList<ILocalRepositoryModel> ActiveRepositories
{
get
{
return activeRepositories;
}
private set
{
if (value != activeRepositories)
{
log.Debug("ActiveRepositories changed to {Repositories}", value?.Select(x => x.CloneUrl));
activeRepositories = value;
ActiveRepositoriesChanged?.Invoke();
}
}
}
public void JoinTillEmpty()
{
JoinableTaskFactory.Context.Factory.Run(async () =>
{
await JoinableTaskCollection.JoinTillEmptyAsync();
});
}
async Task<T> GetServiceAsync<T>()
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
return (T)await asyncServiceProvider.GetServiceAsync(typeof(T));
}
public event Action ActiveRepositoriesChanged;
JoinableTaskCollection JoinableTaskCollection { get; }
JoinableTaskFactory JoinableTaskFactory { get; }
}
}