This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathGitHubPackage.cs
More file actions
299 lines (271 loc) · 13.3 KB
/
Copy pathGitHubPackage.cs
File metadata and controls
299 lines (271 loc) · 13.3 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using System;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel.Design;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;
using GitHub.Api;
using GitHub.Commands;
using GitHub.Info;
using GitHub.Exports;
using GitHub.Logging;
using GitHub.Services;
using GitHub.Settings;
using GitHub.VisualStudio.Commands;
using GitHub.Services.Vssdk.Commands;
using GitHub.ViewModels.GitHubPane;
using GitHub.VisualStudio.Settings;
using GitHub.VisualStudio.UI;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Serilog;
using Task = System.Threading.Tasks.Task;
namespace GitHub.VisualStudio
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[InstalledProductRegistration("#110", "#112", AssemblyVersionInformation.Version, IconResourceID = 400)]
[Guid(Guids.guidGitHubPkgString)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideAutoLoad(Guids.UIContext_Git, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideOptionPage(typeof(OptionsPage), "GitHub for Visual Studio", "General", 0, 0, supportsAutomation: true)]
public class GitHubPackage : AsyncPackage
{
static readonly ILogger log = LogManager.ForContext<GitHubPackage>();
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
LogVersionInformation();
await base.InitializeAsync(cancellationToken, progress);
await InitializeLoggingAsync();
await GetServiceAsync(typeof(IUsageTracker));
// Avoid delays when there is ongoing UI activity.
// See: https://github.com/github/VisualStudio/issues/1537
await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeMenus);
}
async Task InitializeLoggingAsync()
{
var packageSettings = await GetServiceAsync(typeof(IPackageSettings)) as IPackageSettings;
LogManager.EnableTraceLogging(packageSettings?.EnableTraceLogging ?? false);
if (packageSettings != null)
{
packageSettings.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(packageSettings.EnableTraceLogging))
{
LogManager.EnableTraceLogging(packageSettings.EnableTraceLogging);
}
};
}
}
void LogVersionInformation()
{
var packageVersion = ApplicationInfo.GetPackageVersion(this);
var hostVersionInfo = ApplicationInfo.GetHostVersionInfo();
log.Information("Initializing GitHub Extension v{PackageVersion} in {$FileDescription} ({$ProductVersion})",
packageVersion, hostVersionInfo.FileDescription, hostVersionInfo.ProductVersion);
}
async Task InitializeMenus()
{
IVsCommandBase[] commands;
if (ExportForVisualStudioProcessAttribute.IsVisualStudioProcess())
{
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
var exports = componentModel.DefaultExportProvider;
commands = new IVsCommandBase[]
{
exports.GetExportedValue<IAddConnectionCommand>(),
exports.GetExportedValue<IBlameLinkCommand>(),
exports.GetExportedValue<ICopyLinkCommand>(),
exports.GetExportedValue<ICreateGistCommand>(),
exports.GetExportedValue<ICreateGistEnterpriseCommand>(),
exports.GetExportedValue<IOpenLinkCommand>(),
exports.GetExportedValue<IOpenPullRequestsCommand>(),
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),
exports.GetExportedValue<IShowGitHubPaneCommand>(),
exports.GetExportedValue<IGoToSolutionOrPullRequestFileCommand>(),
exports.GetExportedValue<ISyncSubmodulesCommand>(),
exports.GetExportedValue<IOpenFromUrlCommand>(),
exports.GetExportedValue<IOpenFromClipboardCommand>()
};
}
else
{
// Show info message box when executed in non-Visual Studio process
var message = Resources.BlendDialogText;
commands = new IVsCommandBase[]
{
new ShowMessageBoxCommand(AddConnectionCommand.CommandSet, AddConnectionCommand.CommandId, this, message),
new ShowMessageBoxCommand(ShowGitHubPaneCommand.CommandSet, ShowGitHubPaneCommand.CommandId, this, message)
};
}
await JoinableTaskFactory.SwitchToMainThreadAsync();
var menuService = (IMenuCommandService)(await GetServiceAsync(typeof(IMenuCommandService)));
menuService.AddCommands(commands);
}
async Task EnsurePackageLoaded(Guid packageGuid)
{
var shell = await GetServiceAsync(typeof(SVsShell)) as IVsShell;
if (shell != null)
{
IVsPackage vsPackage;
ErrorHandler.ThrowOnFailure(shell.LoadPackage(ref packageGuid, out vsPackage));
}
}
}
[PartCreationPolicy(CreationPolicy.Shared)]
public class ServiceProviderExports
{
readonly IServiceProvider serviceProvider;
[ImportingConstructor]
public ServiceProviderExports([Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
[ExportForVisualStudioProcess]
public ILoginManager LoginManager => GetService<ILoginManager>();
[ExportForVisualStudioProcess]
public IGitHubServiceProvider GitHubServiceProvider => GetService<IGitHubServiceProvider>();
[ExportForVisualStudioProcess]
public IUsageTracker UsageTracker => GetService<IUsageTracker>();
[ExportForVisualStudioProcess]
public IVSGitExt VSGitExt => GetService<IVSGitExt>();
[ExportForVisualStudioProcess]
public IPackageSettings PackageSettings => GetService<IPackageSettings>();
T GetService<T>() => (T)serviceProvider.GetService(typeof(T));
}
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideService(typeof(ILoginManager), IsAsyncQueryable = true)]
[ProvideService(typeof(IGitHubServiceProvider), IsAsyncQueryable = true)]
[ProvideService(typeof(IUsageTracker), IsAsyncQueryable = true)]
[ProvideService(typeof(IPackageSettings), IsAsyncQueryable = true)]
[ProvideService(typeof(IUsageService), IsAsyncQueryable = true)]
[ProvideService(typeof(IVSGitExt), IsAsyncQueryable = true)]
[ProvideService(typeof(IGitHubToolWindowManager))]
[Guid(ServiceProviderPackageId)]
public sealed class ServiceProviderPackage : AsyncPackage, IServiceProviderPackage, IGitHubToolWindowManager
{
public const string ServiceProviderPackageId = "D5CE1488-DEDE-426D-9E5B-BFCCFBE33E53";
static readonly ILogger log = LogManager.ForContext<ServiceProviderPackage>();
protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
AddService(typeof(IGitHubServiceProvider), CreateService, true);
AddService(typeof(IVSGitExt), CreateService, true);
AddService(typeof(IUsageTracker), CreateService, true);
AddService(typeof(IUsageService), CreateService, true);
AddService(typeof(ILoginManager), CreateService, true);
AddService(typeof(IGitHubToolWindowManager), CreateService, true);
AddService(typeof(IPackageSettings), CreateService, true);
return Task.CompletedTask;
}
public async Task<IGitHubPaneViewModel> ShowGitHubPane()
{
var pane = ShowToolWindow(new Guid(GitHubPane.GitHubPaneGuid));
if (pane == null)
return null;
var frame = pane.Frame as IVsWindowFrame;
if (frame != null)
{
ErrorHandler.Failed(frame.Show());
}
var gitHubPane = (GitHubPane)pane;
return await gitHubPane.GetViewModelAsync();
}
static ToolWindowPane ShowToolWindow(Guid windowGuid)
{
IVsWindowFrame frame;
if (ErrorHandler.Failed(Services.UIShell.FindToolWindow((uint)__VSCREATETOOLWIN.CTW_fForceCreate,
ref windowGuid, out frame)))
{
log.Error("Unable to find or create GitHubPane '{Guid}'", UI.GitHubPane.GitHubPaneGuid);
return null;
}
if (ErrorHandler.Failed(frame.Show()))
{
log.Error("Unable to show GitHubPane '{Guid}'", UI.GitHubPane.GitHubPaneGuid);
return null;
}
object docView = null;
if (ErrorHandler.Failed(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out docView)))
{
log.Error("Unable to grab instance of GitHubPane '{Guid}'", UI.GitHubPane.GitHubPaneGuid);
return null;
}
return docView as GitHubPane;
}
async Task<object> CreateService(IAsyncServiceContainer container, CancellationToken cancellationToken, Type serviceType)
{
if (serviceType == null)
return null;
if (container != this)
return null;
if (serviceType == typeof(IGitHubServiceProvider))
{
//var sp = await GetServiceAsync(typeof(SVsServiceProvider)) as IServiceProvider;
var result = new GitHubServiceProvider(this, this);
await result.Initialize();
return result;
}
else if (serviceType == typeof(ILoginManager))
{
// These services are got through MEF and we will take a performance hit if ILoginManager is requested during
// InitializeAsync. TODO: We can probably make LoginManager a normal MEF component rather than a service.
var serviceProvider = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
var keychain = serviceProvider.GetService<IKeychain>();
var oauthListener = serviceProvider.GetService<IOAuthCallbackListener>();
// HACK: We need to make sure this is run on the main thread. We really
// shouldn't be injecting a view model concern into LoginManager - this
// needs to be refactored. See #1398.
var lazy2Fa = new Lazy<ITwoFactorChallengeHandler>(() =>
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
return serviceProvider.GetService<ITwoFactorChallengeHandler>();
}));
return new LoginManager(
keychain,
lazy2Fa,
oauthListener,
ApiClientConfiguration.ClientId,
ApiClientConfiguration.ClientSecret,
ApiClientConfiguration.RequiredScopes,
ApiClientConfiguration.AuthorizationNote,
ApiClientConfiguration.MachineFingerprint);
}
else if (serviceType == typeof(IUsageService))
{
var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
var environment = new Rothko.Environment();
return new UsageService(sp, environment);
}
else if (serviceType == typeof(IUsageTracker))
{
var usageService = await GetServiceAsync(typeof(IUsageService)) as IUsageService;
var serviceProvider = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
var settings = await GetServiceAsync(typeof(IPackageSettings)) as IPackageSettings;
return new UsageTracker(serviceProvider, usageService, settings);
}
else if (serviceType == typeof(IVSGitExt))
{
var vsVersion = ApplicationInfo.GetHostVersionInfo().FileMajorPart;
return new VSGitExtFactory(vsVersion, this).Create();
}
else if (serviceType == typeof(IGitHubToolWindowManager))
{
return this;
}
else if (serviceType == typeof(IPackageSettings))
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var sp = new ServiceProvider(Services.Dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
return new PackageSettings(sp);
}
// go the mef route
else
{
var sp = await GetServiceAsync(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider;
return sp.TryGetService(serviceType);
}
}
}
}