diff --git a/src/GitHub.App/Services/DialogService.cs b/src/GitHub.App/Services/DialogService.cs index 43ae5f98a..aa1abe53e 100644 --- a/src/GitHub.App/Services/DialogService.cs +++ b/src/GitHub.App/Services/DialogService.cs @@ -51,10 +51,19 @@ namespace GitHub.Services return (string)await showDialog.ShowWithFirstConnection(viewModel); } - public async Task ShowCreateGist() + public async Task ShowCreateGist(IConnection connection) { var viewModel = factory.CreateViewModel(); - await showDialog.ShowWithFirstConnection(viewModel); + + if (connection != null) + { + await viewModel.InitializeAsync(connection); + await showDialog.Show(viewModel); + } + else + { + await showDialog.ShowWithFirstConnection(viewModel); + } } public async Task ShowCreateRepositoryDialog(IConnection connection) diff --git a/src/GitHub.Exports/Commands/ICreateGistEnterpriseCommand.cs b/src/GitHub.Exports/Commands/ICreateGistEnterpriseCommand.cs new file mode 100644 index 000000000..702daa422 --- /dev/null +++ b/src/GitHub.Exports/Commands/ICreateGistEnterpriseCommand.cs @@ -0,0 +1,11 @@ +using System; + +namespace GitHub.Commands +{ + /// + /// Creates a GitHub Enterprise gist from the currently selected text. + /// + public interface ICreateGistEnterpriseCommand : IVsCommand + { + } +} \ No newline at end of file diff --git a/src/GitHub.Exports/GitHub.Exports.csproj b/src/GitHub.Exports/GitHub.Exports.csproj index ad7f0e87c..6e03a1fbc 100644 --- a/src/GitHub.Exports/GitHub.Exports.csproj +++ b/src/GitHub.Exports/GitHub.Exports.csproj @@ -149,6 +149,7 @@ + diff --git a/src/GitHub.Exports/Services/IDialogService.cs b/src/GitHub.Exports/Services/IDialogService.cs index c74fad36b..083cbedea 100644 --- a/src/GitHub.Exports/Services/IDialogService.cs +++ b/src/GitHub.Exports/Services/IDialogService.cs @@ -39,7 +39,11 @@ namespace GitHub.Services /// /// Shows the Create Gist dialog. /// - Task ShowCreateGist(); + /// + /// The connection to use. If null, the first connection will be used, or the user promted + /// to log in if there are no connections. + /// + Task ShowCreateGist(IConnection connection); /// /// Shows the Create Repository dialog. diff --git a/src/GitHub.Exports/Settings/PkgCmdID.cs b/src/GitHub.Exports/Settings/PkgCmdID.cs index 55b5706b6..636905c37 100644 --- a/src/GitHub.Exports/Settings/PkgCmdID.cs +++ b/src/GitHub.Exports/Settings/PkgCmdID.cs @@ -19,6 +19,7 @@ namespace GitHub.VisualStudio public const int refreshCommand = 0x302; public const int pullRequestCommand = 0x310; public const int createGistCommand = 0x400; + public const int createGistEnterpriseCommand = 0x401; public const int openLinkCommand = 0x100; public const int copyLinkCommand = 0x101; public const int goToSolutionOrPullRequestFileCommand = 0x102; diff --git a/src/GitHub.VisualStudio/Commands/CreateGistCommand.cs b/src/GitHub.VisualStudio/Commands/CreateGistCommand.cs index 4a9d2e2e9..1fb55e2b7 100644 --- a/src/GitHub.VisualStudio/Commands/CreateGistCommand.cs +++ b/src/GitHub.VisualStudio/Commands/CreateGistCommand.cs @@ -1,28 +1,30 @@ using System; +using System.Linq; using System.ComponentModel.Composition; using System.Threading.Tasks; +using GitHub.Models; using GitHub.Commands; using GitHub.Logging; using GitHub.Services; +using GitHub.Extensions; using GitHub.Services.Vssdk.Commands; namespace GitHub.VisualStudio.Commands { /// - /// Creates a gist from the currently selected text. + /// Creates a GitHub Gist from the currently selected text. /// [Export(typeof(ICreateGistCommand))] - public class CreateGistCommand : VsCommand, ICreateGistCommand + public class CreateGistCommand : CreateGistCommandBase, ICreateGistCommand { - readonly Lazy dialogService; - readonly Lazy selectedTextProvider; - [ImportingConstructor] - protected CreateGistCommand(Lazy dialogService, Lazy selectedTextProvider) - : base(CommandSet, CommandId) + protected CreateGistCommand( + Lazy dialogService, + Lazy selectedTextProvider, + Lazy connectionManager) + : base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, true, + isNotLoggedInDefault: true) { - this.dialogService = dialogService; - this.selectedTextProvider = selectedTextProvider; } /// @@ -34,21 +36,100 @@ namespace GitHub.VisualStudio.Commands /// Gets the numeric identifier of the command. /// public const int CommandId = PkgCmdIDList.createGistCommand; + } + + /// + /// Creates a GitHub Enterprise Gist from the currently selected text. + /// + [Export(typeof(ICreateGistEnterpriseCommand))] + public class CreateGistEnterpriseCommand : CreateGistCommandBase, ICreateGistEnterpriseCommand + { + [ImportingConstructor] + protected CreateGistEnterpriseCommand( + Lazy dialogService, + Lazy selectedTextProvider, + Lazy connectionManager) + : base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, false) + { + } + + /// + /// Gets the GUID of the group the command belongs to. + /// + public static readonly Guid CommandSet = Guids.guidContextMenuSet; + + /// + /// Gets the numeric identifier of the command. + /// + public const int CommandId = PkgCmdIDList.createGistEnterpriseCommand; + } + + /// + /// Creates a GitHub or GitHub Enterprise Gist from the currently selected text. + /// + public abstract class CreateGistCommandBase : VsCommand + { + readonly bool isGitHubDotCom; + readonly bool isNotLoggedInDefault; + readonly Lazy dialogService; + readonly Lazy selectedTextProvider; + readonly Lazy connectionManager; + + protected CreateGistCommandBase( + Guid commandSet, int commandId, + Lazy dialogService, + Lazy selectedTextProvider, + Lazy connectionManager, + bool isGitHubDotCom, + bool isNotLoggedInDefault = false) + : base(commandSet, commandId) + { + this.dialogService = dialogService; + this.selectedTextProvider = selectedTextProvider; + this.connectionManager = connectionManager; + this.isGitHubDotCom = isGitHubDotCom; + this.isNotLoggedInDefault = isNotLoggedInDefault; + } ISelectedTextProvider SelectedTextProvider => selectedTextProvider.Value; /// /// Shows the Create Gist dialog. /// - public override Task Execute() + public override async Task Execute() { - return dialogService.Value.ShowCreateGist(); + var connection = await FindConnectionAsync(); + await dialogService.Value.ShowCreateGist(connection); } protected override void QueryStatus() { Log.Assert(SelectedTextProvider != null, "Could not get an instance of ISelectedTextProvider"); - Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText()); + Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText()) && + (HasConnection() || isNotLoggedInDefault && IsLoggedIn() == false); + } + + bool HasConnection() + { + var task = FindConnectionAsync(); + return task.IsCompleted && task.Result != null; + } + + async Task FindConnectionAsync() + { + var connections = await connectionManager.Value.GetLoadedConnections(); + return connections.FirstOrDefault(x => x.IsLoggedIn && x.HostAddress.IsGitHubDotCom() == isGitHubDotCom); + } + + bool? IsLoggedIn() + { + var task = connectionManager.Value.IsLoggedIn(); + if (task.IsCompleted) + { + return task.Result; + } + + return null; } } } diff --git a/src/GitHub.VisualStudio/GitHub.VisualStudio.vsct b/src/GitHub.VisualStudio/GitHub.VisualStudio.vsct index 27393acb6..4a5900e8e 100644 --- a/src/GitHub.VisualStudio/GitHub.VisualStudio.vsct +++ b/src/GitHub.VisualStudio/GitHub.VisualStudio.vsct @@ -214,6 +214,18 @@ + +