Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f23fd24

Browse files
committed
Expose IVSGitExt as async VS service
1 parent cf2bc71 commit f23fd24

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

src/GitHub.Exports/Services/IGitHubServiceProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using System.ComponentModel.Composition.Hosting;
34
using System.Runtime.InteropServices;
45
using GitHub.VisualStudio;
@@ -28,5 +29,7 @@ Ret GetService<T, Ret>() where T : class
2829
/// <param name="owner">The owner, which either has to match what was passed to AddService,
2930
/// or if it's null, the service will be removed without checking for ownership</param>
3031
void RemoveService(Type t, object owner);
32+
33+
Task<object> GetServiceAsync(Type serviceType);
3134
}
3235
}

src/GitHub.InlineReviews/PullRequestStatusBarPackage.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Windows;
32
using System.Threading;
43
using System.Runtime.InteropServices;
54
using GitHub.Services;
@@ -22,13 +21,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
2221
{
2322
var usageTracker = (IUsageTracker)await GetServiceAsync(typeof(IUsageTracker));
2423
var serviceProvider = (IGitHubServiceProvider)await GetServiceAsync(typeof(IGitHubServiceProvider));
24+
var gitExt = (IVSGitExt)await GetServiceAsync(typeof(IVSGitExt));
2525

26-
// NOTE: ThreadingHelper.SwitchToMainThreadAsync() doesn't return until a solution is loaded. Using Dispatcher.Invoke instead.
27-
Application.Current.Dispatcher.Invoke(() =>
28-
{
29-
var gitExt = serviceProvider.GetService<IVSGitExt>();
30-
new PullRequestStatusBarManager(gitExt, usageTracker, serviceProvider);
31-
});
26+
new PullRequestStatusBarManager(gitExt, usageTracker, serviceProvider);
3227
}
3328
}
3429
}

src/GitHub.TeamFoundation.14/Services/VSGitExt.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using GitHub.Models;
77
using GitHub.Services;
88
using GitHub.Logging;
9-
using GitHub.Helpers;
109
using GitHub.TeamFoundation.Services;
1110
using Serilog;
1211
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
@@ -20,8 +19,6 @@ namespace GitHub.VisualStudio.Base
2019
/// Initialization for this service will be done asynchronously and the <see cref="IGitExt" /> service will be
2120
/// retrieved on the Main thread. This means the service can be constructed and subscribed to on a background thread.
2221
/// </remarks>
23-
[Export(typeof(IVSGitExt))]
24-
[PartCreationPolicy(CreationPolicy.Shared)]
2522
public class VSGitExt : IVSGitExt
2623
{
2724
static readonly ILogger log = LogManager.ForContext<VSGitExt>();
@@ -99,7 +96,7 @@ async Task ContextChangedAsync()
9996

10097
async Task<bool> TryInitialize()
10198
{
102-
gitService = await GetServiceAsync<IGitExt>();
99+
gitService = (IGitExt)await serviceProvider.GetServiceAsync(typeof(IGitExt));
103100
if (gitService != null)
104101
{
105102
gitService.PropertyChanged += (s, e) =>
@@ -122,13 +119,6 @@ async Task<bool> TryInitialize()
122119
return false;
123120
}
124121

125-
async Task<T> GetServiceAsync<T>() where T : class
126-
{
127-
// GetService must be called from the Main thread.
128-
await ThreadingHelper.SwitchToMainThreadAsync();
129-
return serviceProvider.GetService<T>();
130-
}
131-
132122
void RefreshActiveRepositories()
133123
{
134124
try

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,15 @@
679679
<Private>True</Private>
680680
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup;GetCopyToOutputDirectoryItems;DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIX>
681681
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIXLocalOnly>
682+
<Aliases>TF14</Aliases>
682683
</ProjectReference>
683684
<ProjectReference Include="..\GitHub.TeamFoundation.15\GitHub.TeamFoundation.15.csproj">
684685
<Project>{161dbf01-1dbf-4b00-8551-c5c00f26720e}</Project>
685686
<Name>GitHub.TeamFoundation.15</Name>
686687
<Private>True</Private>
687688
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup;GetCopyToOutputDirectoryItems;DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIX>
688689
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup;</IncludeOutputGroupsInVSIXLocalOnly>
690+
<Aliases>TF15</Aliases>
689691
</ProjectReference>
690692
<ProjectReference Include="..\GitHub.UI.Reactive\GitHub.UI.Reactive.csproj">
691693
<Project>{158b05e8-fdbc-4d71-b871-c96e28d5adf5}</Project>

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System;
1+
extern alias TF14;
2+
extern alias TF15;
3+
4+
using System;
25
using System.ComponentModel.Composition;
36
using System.Diagnostics;
47
using System.Reflection;
@@ -22,6 +25,8 @@
2225
using Octokit;
2326
using Serilog;
2427
using Task = System.Threading.Tasks.Task;
28+
using VSGitExt14 = TF14.GitHub.VisualStudio.Base.VSGitExt;
29+
using VSGitExt15 = TF15.GitHub.VisualStudio.Base.VSGitExt;
2530

2631
namespace GitHub.VisualStudio
2732
{
@@ -107,12 +112,28 @@ public GHClient(IProgram program)
107112
}
108113
}
109114

115+
[PartCreationPolicy(CreationPolicy.Shared)]
116+
public class VSGitExtPart
117+
{
118+
readonly IGitHubServiceProvider serviceProvider;
119+
120+
[ImportingConstructor]
121+
public VSGitExtPart(IGitHubServiceProvider serviceProvider)
122+
{
123+
this.serviceProvider = serviceProvider;
124+
}
125+
126+
[Export(typeof(IVSGitExt))]
127+
public IVSGitExt VSGitExt => serviceProvider.GetService<IVSGitExt>();
128+
}
129+
110130
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
111131
[ProvideService(typeof(ILoginManager), IsAsyncQueryable = true)]
112132
[ProvideService(typeof(IMenuProvider), IsAsyncQueryable = true)]
113133
[ProvideService(typeof(IGitHubServiceProvider), IsAsyncQueryable = true)]
114134
[ProvideService(typeof(IUsageTracker), IsAsyncQueryable = true)]
115135
[ProvideService(typeof(IUsageService), IsAsyncQueryable = true)]
136+
[ProvideService(typeof(IVSGitExt), IsAsyncQueryable = true)]
116137
[ProvideService(typeof(IGitHubToolWindowManager))]
117138
[Guid(ServiceProviderPackageId)]
118139
public sealed class ServiceProviderPackage : AsyncPackage, IServiceProviderPackage, IGitHubToolWindowManager
@@ -150,6 +171,7 @@ Version VSVersion
150171
protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
151172
{
152173
AddService(typeof(IGitHubServiceProvider), CreateService, true);
174+
AddService(typeof(IVSGitExt), CreateService, true);
153175
AddService(typeof(IUsageTracker), CreateService, true);
154176
AddService(typeof(IUsageService), CreateService, true);
155177
AddService(typeof(ILoginManager), CreateService, true);
@@ -258,6 +280,11 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
258280
var serviceProvider = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
259281
return new UsageTracker(serviceProvider, usageService);
260282
}
283+
else if (serviceType == typeof(IVSGitExt))
284+
{
285+
var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
286+
return CreateVSGitExt(sp);
287+
}
261288
else if (serviceType == typeof(IGitHubToolWindowManager))
262289
{
263290
return this;
@@ -269,5 +296,18 @@ async Task<object> CreateService(IAsyncServiceContainer container, CancellationT
269296
return sp.TryGetService(serviceType);
270297
}
271298
}
299+
300+
IVSGitExt CreateVSGitExt(IGitHubServiceProvider sp)
301+
{
302+
switch (VSVersion.Major)
303+
{
304+
case 14:
305+
return new Lazy<IVSGitExt>(() => new VSGitExt14(sp)).Value;
306+
case 15:
307+
return new Lazy<IVSGitExt>(() => new VSGitExt15(sp)).Value;
308+
default:
309+
return null;
310+
}
311+
}
272312
}
273313
}

src/GitHub.VisualStudio/Services/GitHubServiceProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public IServiceProvider GitServiceProvider
7171
public object TryGetService(Type t) => theRealProvider.TryGetService(t);
7272

7373
public T TryGetService<T>() where T : class => theRealProvider.TryGetService<T>();
74+
75+
public Task<object> GetServiceAsync(Type serviceType) => theRealProvider.GetServiceAsync(serviceType);
7476
}
7577

7678
/// <summary>
@@ -309,5 +311,7 @@ public void Dispose()
309311
Dispose(true);
310312
GC.SuppressFinalize(this);
311313
}
314+
315+
public Task<object> GetServiceAsync(Type serviceType) => asyncServiceProvider.GetServiceAsync(serviceType);
312316
}
313317
}

0 commit comments

Comments
 (0)