11using System ;
2+ using System . Linq ;
23using System . ComponentModel . Composition ;
34using System . Threading . Tasks ;
5+ using GitHub . Models ;
46using GitHub . Commands ;
57using GitHub . Logging ;
68using GitHub . Services ;
9+ using GitHub . Extensions ;
710using GitHub . Services . Vssdk . Commands ;
811
912namespace GitHub . VisualStudio . Commands
1013{
1114 /// <summary>
12- /// Creates a gist from the currently selected text.
15+ /// Creates a GitHub Gist from the currently selected text.
1316 /// </summary>
1417 [ Export ( typeof ( ICreateGistCommand ) ) ]
15- public class CreateGistCommand : VsCommand , ICreateGistCommand
18+ public class CreateGistCommand : CreateGistCommandBase , ICreateGistCommand
1619 {
17- readonly Lazy < IDialogService > dialogService ;
18- readonly Lazy < ISelectedTextProvider > selectedTextProvider ;
19-
2020 [ ImportingConstructor ]
21- protected CreateGistCommand ( Lazy < IDialogService > dialogService , Lazy < ISelectedTextProvider > selectedTextProvider )
22- : base ( CommandSet , CommandId )
21+ protected CreateGistCommand (
22+ Lazy < IDialogService > dialogService ,
23+ Lazy < ISelectedTextProvider > selectedTextProvider ,
24+ Lazy < IConnectionManager > connectionManager )
25+ : base ( CommandSet , CommandId , dialogService , selectedTextProvider , connectionManager , true ,
26+ isNotLoggedInDefault : true )
2327 {
24- this . dialogService = dialogService ;
25- this . selectedTextProvider = selectedTextProvider ;
2628 }
2729
2830 /// <summary>
@@ -34,21 +36,100 @@ protected CreateGistCommand(Lazy<IDialogService> dialogService, Lazy<ISelectedTe
3436 /// Gets the numeric identifier of the command.
3537 /// </summary>
3638 public const int CommandId = PkgCmdIDList . createGistCommand ;
39+ }
40+
41+ /// <summary>
42+ /// Creates a GitHub Enterprise Gist from the currently selected text.
43+ /// </summary>
44+ [ Export ( typeof ( ICreateGistEnterpriseCommand ) ) ]
45+ public class CreateGistEnterpriseCommand : CreateGistCommandBase , ICreateGistEnterpriseCommand
46+ {
47+ [ ImportingConstructor ]
48+ protected CreateGistEnterpriseCommand (
49+ Lazy < IDialogService > dialogService ,
50+ Lazy < ISelectedTextProvider > selectedTextProvider ,
51+ Lazy < IConnectionManager > connectionManager )
52+ : base ( CommandSet , CommandId , dialogService , selectedTextProvider , connectionManager , false )
53+ {
54+ }
55+
56+ /// <summary>
57+ /// Gets the GUID of the group the command belongs to.
58+ /// </summary>
59+ public static readonly Guid CommandSet = Guids . guidContextMenuSet ;
60+
61+ /// <summary>
62+ /// Gets the numeric identifier of the command.
63+ /// </summary>
64+ public const int CommandId = PkgCmdIDList . createGistEnterpriseCommand ;
65+ }
66+
67+ /// <summary>
68+ /// Creates a GitHub or GitHub Enterprise Gist from the currently selected text.
69+ /// </summary>
70+ public abstract class CreateGistCommandBase : VsCommand
71+ {
72+ readonly bool isGitHubDotCom ;
73+ readonly bool isNotLoggedInDefault ;
74+ readonly Lazy < IDialogService > dialogService ;
75+ readonly Lazy < ISelectedTextProvider > selectedTextProvider ;
76+ readonly Lazy < IConnectionManager > connectionManager ;
77+
78+ protected CreateGistCommandBase (
79+ Guid commandSet , int commandId ,
80+ Lazy < IDialogService > dialogService ,
81+ Lazy < ISelectedTextProvider > selectedTextProvider ,
82+ Lazy < IConnectionManager > connectionManager ,
83+ bool isGitHubDotCom ,
84+ bool isNotLoggedInDefault = false )
85+ : base ( commandSet , commandId )
86+ {
87+ this . dialogService = dialogService ;
88+ this . selectedTextProvider = selectedTextProvider ;
89+ this . connectionManager = connectionManager ;
90+ this . isGitHubDotCom = isGitHubDotCom ;
91+ this . isNotLoggedInDefault = isNotLoggedInDefault ;
92+ }
3793
3894 ISelectedTextProvider SelectedTextProvider => selectedTextProvider . Value ;
3995
4096 /// <summary>
4197 /// Shows the Create Gist dialog.
4298 /// </summary>
43- public override Task Execute ( )
99+ public override async Task Execute ( )
44100 {
45- return dialogService . Value . ShowCreateGist ( ) ;
101+ var connection = await FindConnectionAsync ( ) ;
102+ await dialogService . Value . ShowCreateGist ( connection ) ;
46103 }
47104
48105 protected override void QueryStatus ( )
49106 {
50107 Log . Assert ( SelectedTextProvider != null , "Could not get an instance of ISelectedTextProvider" ) ;
51- Visible = ! string . IsNullOrWhiteSpace ( SelectedTextProvider ? . GetSelectedText ( ) ) ;
108+ Visible = ! string . IsNullOrWhiteSpace ( SelectedTextProvider ? . GetSelectedText ( ) ) &&
109+ ( HasConnection ( ) || isNotLoggedInDefault && IsLoggedIn ( ) == false ) ;
110+ }
111+
112+ bool HasConnection ( )
113+ {
114+ var task = FindConnectionAsync ( ) ;
115+ return task . IsCompleted && task . Result != null ;
116+ }
117+
118+ async Task < IConnection > FindConnectionAsync ( )
119+ {
120+ var connections = await connectionManager . Value . GetLoadedConnections ( ) ;
121+ return connections . FirstOrDefault ( x => x . IsLoggedIn && x . HostAddress . IsGitHubDotCom ( ) == isGitHubDotCom ) ;
122+ }
123+
124+ bool ? IsLoggedIn ( )
125+ {
126+ var task = connectionManager . Value . IsLoggedIn ( ) ;
127+ if ( task . IsCompleted )
128+ {
129+ return task . Result ;
130+ }
131+
132+ return null ;
52133 }
53134 }
54135}
0 commit comments