11using System ;
2- using System . IO ;
32using System . Windows ;
4- using System . Threading . Tasks ;
53using System . ComponentModel . Composition ;
64using GitHub . Commands ;
75using GitHub . Services ;
86using GitHub . App . Services ;
97using GitHub . Services . Vssdk . Commands ;
10- using EnvDTE ;
11- using Microsoft . VisualStudio ;
12- using Microsoft . VisualStudio . Shell ;
13- using Microsoft . VisualStudio . Shell . Interop ;
148using Task = System . Threading . Tasks . Task ;
15- using SVsServiceProvider = Microsoft . VisualStudio . Shell . SVsServiceProvider ;
169
1710namespace GitHub . VisualStudio . Commands
1811{
1912 [ Export ( typeof ( IOpenFromClipboardCommand ) ) ]
2013 public class OpenFromClipboardCommand : VsCommand < string > , IOpenFromClipboardCommand
2114 {
2215 readonly Lazy < GitHubContextService > gitHubContextService ;
23- readonly Lazy < IRepositoryCloneService > repositoryCloneService ;
24- readonly Lazy < IPullRequestEditorService > pullRequestEditorService ;
2516 readonly Lazy < ITeamExplorerContext > teamExplorerContext ;
26- readonly Lazy < IGitHubToolWindowManager > gitHubToolWindowManager ;
27- readonly Lazy < DTE > dte ;
28- readonly IServiceProvider serviceProvider ;
2917
3018 /// <summary>
3119 /// Gets the GUID of the group the command belongs to.
@@ -40,154 +28,44 @@ public class OpenFromClipboardCommand : VsCommand<string>, IOpenFromClipboardCom
4028 [ ImportingConstructor ]
4129 public OpenFromClipboardCommand (
4230 Lazy < GitHubContextService > gitHubContextService ,
43- Lazy < IRepositoryCloneService > repositoryCloneService ,
44- Lazy < IPullRequestEditorService > pullRequestEditorService ,
45- Lazy < ITeamExplorerContext > teamExplorerContext ,
46- [ Import ( typeof ( SVsServiceProvider ) ) ] IServiceProvider serviceProvider ) :
47- base ( CommandSet , CommandId )
31+ Lazy < ITeamExplorerContext > teamExplorerContext )
32+ : base ( CommandSet , CommandId )
4833 {
4934 this . gitHubContextService = gitHubContextService ;
50- this . repositoryCloneService = repositoryCloneService ;
51- this . pullRequestEditorService = pullRequestEditorService ;
5235 this . teamExplorerContext = teamExplorerContext ;
53- this . serviceProvider = serviceProvider ;
54- dte = new Lazy < DTE > ( ( ) => ( DTE ) serviceProvider . GetService ( typeof ( DTE ) ) ) ;
55- gitHubToolWindowManager = new Lazy < IGitHubToolWindowManager > (
56- ( ) => ( IGitHubToolWindowManager ) serviceProvider . GetService ( typeof ( IGitHubToolWindowManager ) ) ) ;
5736
5837 // See https://code.msdn.microsoft.com/windowsdesktop/AllowParams-2005-9442298f
5938 ParametersDescription = "u" ; // accept a single url
6039 }
6140
62- public override async Task Execute ( string url )
41+ public override Task Execute ( string url )
6342 {
6443 if ( string . IsNullOrEmpty ( url ) )
6544 {
6645 url = Clipboard . GetText ( TextDataFormat . Text ) ;
6746 }
6847
6948 var context = gitHubContextService . Value . FindContextFromUrl ( url ) ;
70- context = context ?? gitHubContextService . Value . FindContextFromBrowser ( ) ;
71-
7249 if ( context == null )
7350 {
74- return ;
51+ // Couldn't find URL in clipboard
52+ return Task . CompletedTask ;
7553 }
7654
7755 var activeDir = teamExplorerContext . Value . ActiveRepository ? . LocalPath ;
78- if ( activeDir != null )
79- {
80- // Try opening file in current context
81- if ( gitHubContextService . Value . TryOpenFile ( activeDir , context ) )
82- {
83- return ;
84- }
85- }
86-
87- // Keep repos in unique dir while testing
88- var defaultSubPath = "GitHubCache" ;
89-
90- var cloneUrl = gitHubContextService . Value . ToRepositoryUrl ( context ) . ToString ( ) ;
91- var targetDir = Path . Combine ( repositoryCloneService . Value . DefaultClonePath , defaultSubPath , context . Owner ) ;
92- var repositoryDirName = context . RepositoryName ;
93- var repositoryDir = Path . Combine ( targetDir , repositoryDirName ) ;
94-
95- if ( ! Directory . Exists ( repositoryDir ) )
96- {
97- var result = ShowInfoMessage ( $ "Clone { cloneUrl } to '{ repositoryDir } '?") ;
98- switch ( result )
99- {
100- case VSConstants . MessageBoxResult . IDYES :
101- await repositoryCloneService . Value . CloneRepository ( cloneUrl , repositoryDirName , targetDir ) ;
102- // Open the cloned repository
103- dte . Value . ExecuteCommand ( "File.OpenFolder" , repositoryDir ) ;
104- dte . Value . ExecuteCommand ( "View.TfsTeamExplorer" ) ;
105- break ;
106- case VSConstants . MessageBoxResult . IDNO :
107- // Target the current solution
108- repositoryDir = FindSolutionDirectory ( dte . Value . Solution ) ;
109- if ( repositoryDir == null )
110- {
111- // No current solution to use
112- return ;
113- }
114-
115- break ;
116- case VSConstants . MessageBoxResult . IDCANCEL :
117- return ;
118- }
119- }
120-
121- var solutionDir = FindSolutionDirectory ( dte . Value . Solution ) ;
122- if ( solutionDir == null || ! ContainsDirectory ( repositoryDir , solutionDir ) )
123- {
124- var result = ShowInfoMessage ( $ "Open repository at '{ repositoryDir } '?") ;
125- switch ( result )
126- {
127- case VSConstants . MessageBoxResult . IDYES :
128- // Open if current solution isn't in repository directory
129- dte . Value . ExecuteCommand ( "File.OpenFolder" , repositoryDir ) ;
130- dte . Value . ExecuteCommand ( "View.TfsTeamExplorer" ) ;
131- break ;
132- case VSConstants . MessageBoxResult . IDNO :
133- break ;
134- case VSConstants . MessageBoxResult . IDCANCEL :
135- return ;
136- }
137- }
138-
139- await TryOpenPullRequest ( context ) ;
140- gitHubContextService . Value . TryOpenFile ( repositoryDir , context ) ;
141- }
142-
143- VSConstants . MessageBoxResult ShowInfoMessage ( string message )
144- {
145- return ( VSConstants . MessageBoxResult ) VsShellUtilities . ShowMessageBox ( serviceProvider , message , null ,
146- OLEMSGICON . OLEMSGICON_QUERY , OLEMSGBUTTON . OLEMSGBUTTON_YESNOCANCEL , OLEMSGDEFBUTTON . OLEMSGDEFBUTTON_FIRST ) ;
147- }
148-
149- static bool ContainsDirectory ( string repositoryDir , string solutionDir )
150- {
151- if ( solutionDir . Equals ( repositoryDir , StringComparison . OrdinalIgnoreCase ) )
152- {
153- return true ;
154- }
155-
156- if ( solutionDir . StartsWith ( repositoryDir + '\\ ' , StringComparison . OrdinalIgnoreCase ) )
157- {
158- return true ;
159- }
160-
161- return false ;
162- }
163-
164- static string FindSolutionDirectory ( Solution solution )
165- {
166- var solutionPath = solution . FileName ;
167- if ( File . Exists ( solutionPath ) )
168- {
169- return Path . GetDirectoryName ( solutionPath ) ;
170- }
171-
172- if ( Directory . Exists ( solutionPath ) )
56+ if ( context == null )
17357 {
174- return solutionPath ;
58+ // No active repository
59+ return Task . CompletedTask ;
17560 }
17661
177- return null ;
178- }
179-
180- async Task < bool > TryOpenPullRequest ( GitHubContext context )
181- {
182- var pullRequest = context . PullRequest ;
183- if ( pullRequest == null )
62+ if ( ! gitHubContextService . Value . TryOpenFile ( activeDir , context ) )
18463 {
185- return false ;
64+ // Couldn't open file
65+ return Task . CompletedTask ;
18666 }
18767
188- var host = await gitHubToolWindowManager . Value . ShowGitHubPane ( ) ;
189- await host . ShowPullRequest ( context . Owner , context . RepositoryName , pullRequest . Value ) ;
190- return true ;
68+ return Task . CompletedTask ;
19169 }
19270 }
19371}
0 commit comments