This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathStartPagePackage.cs
More file actions
163 lines (141 loc) · 6.55 KB
/
Copy pathStartPagePackage.cs
File metadata and controls
163 lines (141 loc) · 6.55 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
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using GitHub.Logging;
using GitHub.Models;
using GitHub.Primitives;
using GitHub.Services;
using GitHub.VisualStudio;
using Microsoft.TeamFoundation.Controls;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.CodeContainerManagement;
using Microsoft.VisualStudio.Threading;
using Serilog;
using CodeContainer = Microsoft.VisualStudio.Shell.CodeContainerManagement.CodeContainer;
using ICodeContainerProvider = Microsoft.VisualStudio.Shell.CodeContainerManagement.ICodeContainerProvider;
using Task = System.Threading.Tasks.Task;
namespace GitHub.StartPage
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[Guid(Guids.StartPagePackageId)]
[ProvideCodeContainerProvider("GitHub Container", Guids.StartPagePackageId, Guids.ImagesId, 1, "#110", "#111", typeof(GitHubContainerProvider))]
public sealed class StartPagePackage : ExtensionPointPackage
{
static IServiceProvider serviceProvider;
internal static IServiceProvider ServiceProvider { get { return serviceProvider; } }
public StartPagePackage()
{
serviceProvider = this;
}
}
[Guid(Guids.CodeContainerProviderId)]
public class GitHubContainerProvider : ICodeContainerProvider
{
static readonly ILogger log = LogManager.ForContext<GitHubContainerProvider>();
public async Task<CodeContainer> AcquireCodeContainerAsync(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
{
return await RunAcquisition(downloadProgress, cancellationToken, null);
}
public async Task<CodeContainer> AcquireCodeContainerAsync(RemoteCodeContainer onlineCodeContainer, IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken)
{
var repository = new RepositoryModel(onlineCodeContainer.Name, UriString.ToUriString(onlineCodeContainer.DisplayUrl));
return await RunAcquisition(downloadProgress, cancellationToken, repository);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "cancellationToken")]
async Task<CodeContainer> RunAcquisition(IProgress<ServiceProgressData> downloadProgress, CancellationToken cancellationToken, IRepositoryModel repository)
{
CloneDialogResult request = null;
try
{
var uiProvider = await Task.Run(() => Package.GetGlobalService(typeof(IGitHubServiceProvider)) as IGitHubServiceProvider);
await ShowTeamExplorerPage(uiProvider);
request = await ShowCloneDialog(uiProvider, downloadProgress, repository);
}
catch (Exception e)
{
log.Error(e, "Error showing Start Page clone dialog");
}
if (request == null)
return null;
var path = Path.Combine(request.BasePath, request.Repository.Name);
var uri = request.Repository.CloneUrl.ToRepositoryUrl();
return new CodeContainer(
localProperties: new CodeContainerLocalProperties(path, CodeContainerType.Folder,
new CodeContainerSourceControlProperties(request.Repository.Name, path, new Guid(Guids.GitSccProviderId))),
remote: new RemoteCodeContainer(request.Repository.Name,
new Guid(Guids.CodeContainerProviderId),
uri,
new Uri(uri.ToString().TrimSuffix(".git")),
DateTimeOffset.UtcNow),
isFavorite: false,
lastAccessed: DateTimeOffset.UtcNow);
}
async Task ShowTeamExplorerPage(IGitHubServiceProvider gitHubServiceProvider)
{
var te = gitHubServiceProvider?.GetService(typeof(ITeamExplorer)) as ITeamExplorer;
if (te != null)
{
var page = te.NavigateToPage(new Guid(TeamExplorerPageIds.Connect), null);
if (page == null)
{
var tcs = new TaskCompletionSource<ITeamExplorerPage>();
PropertyChangedEventHandler handler = null;
handler = new PropertyChangedEventHandler((s, e) =>
{
if (e.PropertyName == "CurrentPage")
{
tcs.SetResult(te.CurrentPage);
te.PropertyChanged -= handler;
}
});
te.PropertyChanged += handler;
page = await tcs.Task;
}
}
}
async Task<CloneDialogResult> ShowCloneDialog(
IGitHubServiceProvider gitHubServiceProvider,
IProgress<ServiceProgressData> progress,
IRepositoryModel repository = null)
{
var dialogService = gitHubServiceProvider.GetService<IDialogService>();
var cloneService = gitHubServiceProvider.GetService<IRepositoryCloneService>();
var usageTracker = gitHubServiceProvider.GetService<IUsageTracker>();
CloneDialogResult result = null;
if (repository == null)
{
result = await dialogService.ShowCloneDialog(null);
}
else
{
var basePath = await dialogService.ShowReCloneDialog(repository);
if (basePath != null)
{
result = new CloneDialogResult(basePath, repository);
}
}
if (result != null)
{
try
{
await cloneService.CloneRepository(
result.Repository.CloneUrl,
result.Repository.Name,
result.BasePath,
progress);
usageTracker.IncrementCounter(x => x.NumberOfStartPageClones).Forget();
}
catch
{
var teServices = gitHubServiceProvider.TryGetService<ITeamExplorerServices>();
teServices.ShowError($"Failed to clone the repository '{result.Repository.Name}'");
result = null;
}
}
return result;
}
}
}