|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Windows; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.ComponentModel.Composition; |
| 6 | +using GitHub.Commands; |
| 7 | +using GitHub.Services; |
| 8 | +using GitHub.App.Services; |
| 9 | +using GitHub.Services.Vssdk.Commands; |
| 10 | +using EnvDTE; |
| 11 | +using Microsoft.VisualStudio; |
| 12 | +using Microsoft.VisualStudio.Shell; |
| 13 | +using Microsoft.VisualStudio.Shell.Interop; |
| 14 | +using Task = System.Threading.Tasks.Task; |
| 15 | +using SVsServiceProvider = Microsoft.VisualStudio.Shell.SVsServiceProvider; |
| 16 | + |
| 17 | +namespace GitHub.VisualStudio.Commands |
| 18 | +{ |
| 19 | + [Export(typeof(IOpenFromClipboardCommand))] |
| 20 | + public class OpenFromClipboardCommand : VsCommand<string>, IOpenFromClipboardCommand |
| 21 | + { |
| 22 | + readonly Lazy<GitHubContextService> gitHubContextService; |
| 23 | + readonly Lazy<IRepositoryCloneService> repositoryCloneService; |
| 24 | + readonly Lazy<IPullRequestEditorService> pullRequestEditorService; |
| 25 | + readonly Lazy<ITeamExplorerContext> teamExplorerContext; |
| 26 | + readonly Lazy<IGitHubToolWindowManager> gitHubToolWindowManager; |
| 27 | + readonly Lazy<DTE> dte; |
| 28 | + readonly IServiceProvider serviceProvider; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the GUID of the group the command belongs to. |
| 32 | + /// </summary> |
| 33 | + public static readonly Guid CommandSet = Guids.guidGitHubCmdSet; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Gets the numeric identifier of the command. |
| 37 | + /// </summary> |
| 38 | + public const int CommandId = PkgCmdIDList.openFromClipboardCommand; |
| 39 | + |
| 40 | + [ImportingConstructor] |
| 41 | + public OpenFromClipboardCommand( |
| 42 | + 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) |
| 48 | + { |
| 49 | + this.gitHubContextService = gitHubContextService; |
| 50 | + this.repositoryCloneService = repositoryCloneService; |
| 51 | + this.pullRequestEditorService = pullRequestEditorService; |
| 52 | + 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))); |
| 57 | + |
| 58 | + // See https://code.msdn.microsoft.com/windowsdesktop/AllowParams-2005-9442298f |
| 59 | + ParametersDescription = "u"; // accept a single url |
| 60 | + } |
| 61 | + |
| 62 | + public override async Task Execute(string url) |
| 63 | + { |
| 64 | + if (string.IsNullOrEmpty(url)) |
| 65 | + { |
| 66 | + url = Clipboard.GetText(TextDataFormat.Text); |
| 67 | + } |
| 68 | + |
| 69 | + var context = gitHubContextService.Value.FindContextFromUrl(url); |
| 70 | + context = context ?? gitHubContextService.Value.FindContextFromBrowser(); |
| 71 | + |
| 72 | + if (context == null) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + 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)) |
| 173 | + { |
| 174 | + return solutionPath; |
| 175 | + } |
| 176 | + |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + async Task<bool> TryOpenPullRequest(GitHubContext context) |
| 181 | + { |
| 182 | + var pullRequest = context.PullRequest; |
| 183 | + if (pullRequest == null) |
| 184 | + { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + var host = await gitHubToolWindowManager.Value.ShowGitHubPane(); |
| 189 | + await host.ShowPullRequest(context.Owner, context.RepositoryName, pullRequest.Value); |
| 190 | + return true; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments