|
1 | 1 | using System; |
| 2 | +using System.ComponentModel.Design; |
2 | 3 | using System.Diagnostics; |
| 4 | +using GitHub.Primitives; |
3 | 5 | using GitHub.Services; |
| 6 | +using LibGit2Sharp; |
4 | 7 | using Microsoft.TeamFoundation.Controls; |
| 8 | +using Microsoft.VisualStudio; |
| 9 | +using Microsoft.VisualStudio.Shell; |
| 10 | +using Microsoft.VisualStudio.Shell.Interop; |
5 | 11 |
|
6 | 12 | namespace GitHub.Extensions |
7 | 13 | { |
8 | | - public static class VSExtensions |
| 14 | + public static class IServiceProviderExtensions |
9 | 15 | { |
10 | 16 | static IUIProvider cachedUIProvider = null; |
11 | 17 |
|
@@ -75,5 +81,64 @@ static T GetExportedValueAndCache<T, CacheType>(ref CacheType cache) |
75 | 81 | cache = (CacheType)(object)ret; |
76 | 82 | return ret; |
77 | 83 | } |
| 84 | + |
| 85 | + public static void AddTopLevelMenuItem(this IServiceProvider provider, |
| 86 | + Guid guid, |
| 87 | + int cmdId, |
| 88 | + EventHandler eventHandler) |
| 89 | + { |
| 90 | + var mcs = provider.GetService(typeof(IMenuCommandService)) as IMenuCommandService; |
| 91 | + Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky"); |
| 92 | + if (mcs == null) |
| 93 | + return; |
| 94 | + var id = new CommandID(guid, cmdId); |
| 95 | + var item = new MenuCommand(eventHandler, id); |
| 96 | + mcs.AddCommand(item); |
| 97 | + } |
| 98 | + |
| 99 | + public static void AddDynamicMenuItem(this IServiceProvider provider, |
| 100 | + Guid guid, |
| 101 | + int cmdId, |
| 102 | + Func<bool> canEnable, |
| 103 | + Action execute) |
| 104 | + { |
| 105 | + var mcs = provider.GetService(typeof(IMenuCommandService)) as IMenuCommandService; |
| 106 | + Debug.Assert(mcs != null, "No IMenuCommandService? Something is wonky"); |
| 107 | + if (mcs == null) |
| 108 | + return; |
| 109 | + var id = new CommandID(guid, cmdId); |
| 110 | + var item = new OleMenuCommand( |
| 111 | + (s, e) => execute(), |
| 112 | + (s, e) => { }, |
| 113 | + (s, e) => |
| 114 | + { |
| 115 | + ((OleMenuCommand)s).Visible = canEnable(); |
| 116 | + }, |
| 117 | + id); |
| 118 | + mcs.AddCommand(item); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static class ISolutionExtensions |
| 123 | + { |
| 124 | + public static UriString GetRepoUrlFromSolution(IVsSolution solution) |
| 125 | + { |
| 126 | + string solutionDir, solutionFile, userFile; |
| 127 | + if (!ErrorHandler.Succeeded(solution.GetSolutionInfo(out solutionDir, out solutionFile, out userFile))) |
| 128 | + return null; |
| 129 | + if (solutionDir == null) |
| 130 | + return null; |
| 131 | + return GitService.GitServiceHelper.GetUri(solutionDir); |
| 132 | + } |
| 133 | + |
| 134 | + public static IRepository GetRepoFromSolution(this IVsSolution solution) |
| 135 | + { |
| 136 | + string solutionDir, solutionFile, userFile; |
| 137 | + if (!ErrorHandler.Succeeded(solution.GetSolutionInfo(out solutionDir, out solutionFile, out userFile))) |
| 138 | + return null; |
| 139 | + if (solutionDir == null) |
| 140 | + return null; |
| 141 | + return GitService.GitServiceHelper.GetRepo(solutionDir); |
| 142 | + } |
78 | 143 | } |
79 | 144 | } |
0 commit comments