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

Commit 3d330a8

Browse files
committed
Use StartPagePackage as ICodeContainerProvider factory
Simplify by using the built in CreateCodeContainerProvider method.
1 parent 55c551a commit 3d330a8

File tree

2 files changed

+58
-89
lines changed

2 files changed

+58
-89
lines changed

src/GitHub.VisualStudio.16/InBoxExtensionServices.cs

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.VisualStudio.ComponentModelHost;
1010
using CodeContainer = Microsoft.VisualStudio.Shell.CodeContainerManagement.CodeContainer;
1111
using ICodeContainerProvider = Microsoft.VisualStudio.Shell.CodeContainerManagement.ICodeContainerProvider;
12+
using System.Runtime.InteropServices;
1213

1314
namespace GitHub.VisualStudio
1415
{
@@ -18,71 +19,72 @@ public ICodeContainerProvider GetGitHubContainerProvider()
1819
{
1920
return new InBoxGitHubContainerProvider();
2021
}
22+
}
2123

22-
class InBoxGitHubContainerProvider : ICodeContainerProvider
24+
[Guid(Guids.CodeContainerProviderId)]
25+
public class InBoxGitHubContainerProvider : ICodeContainerProvider
26+
{
27+
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
2328
{
24-
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
25-
{
26-
return await RunAcquisitionAsync(downloadProgress, cancellationToken, null);
27-
}
28-
29-
public async Task<CodeContainer> AcquireCodeContainerAsync(RemoteCodeContainer onlineCodeContainer, IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
30-
{
31-
var url = onlineCodeContainer.DisplayUrl.ToString();
32-
return await RunAcquisitionAsync(downloadProgress, cancellationToken, url);
33-
}
29+
return await RunAcquisitionAsync(downloadProgress, cancellationToken, null);
30+
}
3431

35-
async Task<CodeContainer> RunAcquisitionAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken, string url = null)
36-
{
37-
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
32+
public async Task<CodeContainer> AcquireCodeContainerAsync(RemoteCodeContainer onlineCodeContainer, IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
33+
{
34+
var url = onlineCodeContainer.DisplayUrl.ToString();
35+
return await RunAcquisitionAsync(downloadProgress, cancellationToken, url);
36+
}
3837

39-
var componentModel = await ServiceProvider.GetGlobalServiceAsync<SComponentModel, IComponentModel>();
40-
Assumes.Present(componentModel);
38+
async Task<CodeContainer> RunAcquisitionAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken, string url = null)
39+
{
40+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
4141

42-
var result = await ShowCloneDialogAsync(componentModel, downloadProgress, cancellationToken, url);
43-
if (result == null)
44-
{
45-
return null;
46-
}
42+
var componentModel = await ServiceProvider.GetGlobalServiceAsync<SComponentModel, IComponentModel>();
43+
Assumes.Present(componentModel);
4744

48-
var repositoryName = result.Url.RepositoryName;
49-
var repositoryRootFullPath = result.Path;
50-
var sccProvider = new Guid(Guids.GitSccProviderId);
51-
var codeContainerProvider = new Guid(Guids.CodeContainerProviderId);
52-
var displayUrl = result.Url.ToRepositoryUrl();
53-
var browseOnlineUrl = new Uri(displayUrl.ToString().TrimSuffix(".git"));
54-
var lastAccessed = DateTimeOffset.UtcNow;
45+
var result = await ShowCloneDialogAsync(componentModel, downloadProgress, cancellationToken, url);
46+
if (result == null)
47+
{
48+
return null;
49+
}
5550

56-
var codeContainer = new CodeContainer(
57-
localProperties: new CodeContainerLocalProperties(repositoryRootFullPath, CodeContainerType.Folder,
58-
new CodeContainerSourceControlProperties(repositoryName, repositoryRootFullPath, sccProvider)),
59-
remote: new RemoteCodeContainer(repositoryName, codeContainerProvider, displayUrl, browseOnlineUrl, lastAccessed),
60-
isFavorite: false,
61-
lastAccessed: lastAccessed);
51+
var repositoryName = result.Url.RepositoryName;
52+
var repositoryRootFullPath = result.Path;
53+
var sccProvider = new Guid(Guids.GitSccProviderId);
54+
var codeContainerProvider = new Guid(Guids.CodeContainerProviderId);
55+
var displayUrl = result.Url.ToRepositoryUrl();
56+
var browseOnlineUrl = new Uri(displayUrl.ToString().TrimSuffix(".git"));
57+
var lastAccessed = DateTimeOffset.UtcNow;
6258

63-
// Report all steps complete before returning a CodeContainer
64-
downloadProgress.Report(new ServiceProgressData(string.Empty, string.Empty, 1, 1));
59+
var codeContainer = new CodeContainer(
60+
localProperties: new CodeContainerLocalProperties(repositoryRootFullPath, CodeContainerType.Folder,
61+
new CodeContainerSourceControlProperties(repositoryName, repositoryRootFullPath, sccProvider)),
62+
remote: new RemoteCodeContainer(repositoryName, codeContainerProvider, displayUrl, browseOnlineUrl, lastAccessed),
63+
isFavorite: false,
64+
lastAccessed: lastAccessed);
6565

66-
return codeContainer;
67-
}
66+
// Report all steps complete before returning a CodeContainer
67+
downloadProgress.Report(new ServiceProgressData(string.Empty, string.Empty, 1, 1));
6868

69-
static async Task<CloneDialogResult> ShowCloneDialogAsync(IComponentModel componentModel,
70-
IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken, string url = null)
71-
{
72-
var compositionServices = new CompositionServices();
73-
var compositionContainer = compositionServices.CreateVisualStudioCompositionContainer(componentModel.DefaultExportProvider);
69+
return codeContainer;
70+
}
7471

75-
var dialogService = compositionContainer.GetExportedValue<IDialogService>();
76-
var cloneDialogResult = await dialogService.ShowCloneDialog(null, url);
77-
if (cloneDialogResult != null)
78-
{
79-
var repositoryCloneService = compositionContainer.GetExportedValue<IRepositoryCloneService>();
80-
await repositoryCloneService.CloneOrOpenRepository(cloneDialogResult, downloadProgress, cancellationToken);
81-
return cloneDialogResult;
82-
}
72+
static async Task<CloneDialogResult> ShowCloneDialogAsync(IComponentModel componentModel,
73+
IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken, string url = null)
74+
{
75+
var compositionServices = new CompositionServices();
76+
var compositionContainer = compositionServices.CreateVisualStudioCompositionContainer(componentModel.DefaultExportProvider);
8377

84-
return null;
78+
var dialogService = compositionContainer.GetExportedValue<IDialogService>();
79+
var cloneDialogResult = await dialogService.ShowCloneDialog(null, url);
80+
if (cloneDialogResult != null)
81+
{
82+
var repositoryCloneService = compositionContainer.GetExportedValue<IRepositoryCloneService>();
83+
await repositoryCloneService.CloneOrOpenRepository(cloneDialogResult, downloadProgress, cancellationToken);
84+
return cloneDialogResult;
8585
}
86+
87+
return null;
8688
}
8789
}
8890
}
Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using System;
2-
using System.Threading;
3-
using System.Threading.Tasks;
42
using System.Runtime.InteropServices;
53
using GitHub.VisualStudio;
64
using Microsoft.VisualStudio.Shell;
7-
using Microsoft.VisualStudio.Shell.CodeContainerManagement;
8-
using CodeContainer = Microsoft.VisualStudio.Shell.CodeContainerManagement.CodeContainer;
95
using ICodeContainerProvider = Microsoft.VisualStudio.Shell.CodeContainerManagement.ICodeContainerProvider;
106

117
namespace GitHub.StartPage
@@ -14,47 +10,18 @@ namespace GitHub.StartPage
1410
// This is required for .imagemanifest files, XAML and when using unsigned assemblies.
1511
// See: https://github.com/github/VisualStudio/pull/1236/
1612
[ProvideBindingPath(SubPath = "UI")]
17-
1813
[PackageRegistration(UseManagedResourcesOnly = true)]
1914
[Guid(Guids.StartPagePackageId)]
20-
[ProvideCodeContainerProvider("GitHub Container", Guids.StartPagePackageId, Images.ImageMonikerGuid, Images.Logo, "#110", "#111", typeof(GitHubContainerProvider))]
15+
[ProvideCodeContainerProvider("GitHub Container", Guids.StartPagePackageId, Images.ImageMonikerGuid, Images.Logo, "#110", "#111", typeof(InBoxGitHubContainerProvider))]
2116
public sealed class StartPagePackage : ExtensionPointPackage
2217
{
23-
protected override void Initialize()
24-
{
25-
base.Initialize();
26-
ServiceProvider = this;
27-
}
28-
29-
internal static IServiceProvider ServiceProvider { get; private set; }
30-
}
31-
32-
[Guid(Guids.CodeContainerProviderId)]
33-
public class GitHubContainerProvider : ICodeContainerProvider
34-
{
35-
readonly ICodeContainerProvider provider;
36-
37-
public GitHubContainerProvider() : this(StartPagePackage.ServiceProvider)
38-
{
39-
}
40-
41-
public GitHubContainerProvider(IServiceProvider serviceProvider)
18+
protected override ICodeContainerProvider CreateCodeContainerProvider(Guid provider)
4219
{
4320
ThreadHelper.ThrowIfNotOnUIThread();
4421

45-
var factory = new ExtensionServicesFactory(serviceProvider);
22+
var factory = new ExtensionServicesFactory(this);
4623
var services = factory.Create();
47-
provider = services.GetGitHubContainerProvider();
48-
}
49-
50-
public Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
51-
{
52-
return provider.AcquireCodeContainerAsync(downloadProgress, cancellationToken);
53-
}
54-
55-
public Task<CodeContainer> AcquireCodeContainerAsync(RemoteCodeContainer onlineCodeContainer, IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
56-
{
57-
return provider.AcquireCodeContainerAsync(onlineCodeContainer, downloadProgress, cancellationToken);
24+
return services.GetGitHubContainerProvider();
5825
}
5926
}
6027
}

0 commit comments

Comments
 (0)