Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 298b3d6

Browse files
authored
Merge pull request #1651 from github/fixes/1620-disable-in-blend
Disable MEF services that are derived from VS services when executing in Blend
2 parents d2f9564 + 16fe07a commit 298b3d6

File tree

12 files changed

+184
-37
lines changed

12 files changed

+184
-37
lines changed

src/GitHub.Exports/Exports/ExportForProcess.cs renamed to src/GitHub.Exports/Exports/ExportForProcessAttribute.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.ComponentModel.Composition;
45

56
namespace GitHub.Exports
@@ -11,24 +12,32 @@ namespace GitHub.Exports
1112
/// This attribute is used to mark exports that mustn't be loaded into Blend.
1213
/// See: https://github.com/github/VisualStudio/pull/1055
1314
/// </remarks>
15+
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "Extended by ExportForVisualStudioProcessAttribute")]
1416
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
15-
public sealed class ExportForProcessAttribute : ExportAttribute
17+
public class ExportForProcessAttribute : ExportAttribute
1618
{
19+
// Unique name for exports that have been disabled
20+
const string DisabledContractName = "GitHub.Disabled";
21+
1722
/// <summary>
1823
/// Define an export that is only exposed in a specific named process.
1924
/// </summary>
20-
/// <param name="contractType">The contract type to expose.</param>
2125
/// <param name="processName">Name of the process to expose export from (e.g. 'devenv').</param>
22-
public ExportForProcessAttribute(Type contractType, string processName) : base(ExportForProcess(contractType, processName))
26+
/// <param name="contractType">The contract type to expose.</param>
27+
public ExportForProcessAttribute(string processName, Type contractType = null) :
28+
base(ContractNameForProcess(processName), contractType)
2329
{
2430
ProcessName = processName;
2531
}
2632

27-
static Type ExportForProcess(Type contractType, string processName)
33+
static string ContractNameForProcess(string processName)
2834
{
29-
return Process.GetCurrentProcess().ProcessName == processName ? contractType : null;
35+
var enabled = IsProcess(processName);
36+
return enabled ? null : DisabledContractName;
3037
}
3138

39+
public static bool IsProcess(string processName) => Process.GetCurrentProcess().ProcessName == processName;
40+
3241
/// <summary>
3342
/// The process name export will be exposed in.
3443
/// </summary>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace GitHub.Exports
4+
{
5+
/// <summary>
6+
/// Only expose export when executing in Visual Studio (devenv) process.
7+
/// </summary>
8+
/// <remarks>
9+
/// This attribute is used to mark exports that mustn't be loaded into Blend.
10+
/// See: https://github.com/github/VisualStudio/pull/1055
11+
/// </remarks>
12+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
13+
public sealed class ExportForVisualStudioProcessAttribute : ExportForProcessAttribute
14+
{
15+
const string VisualStudioProcessName = "devenv";
16+
17+
/// <summary>
18+
/// Define an export that is only exposed in a Visual Studio (devenv) process.
19+
/// </summary>
20+
/// <param name="contractType">The contract type to expose.</param>
21+
public ExportForVisualStudioProcessAttribute(Type contractType = null) :
22+
base(VisualStudioProcessName, contractType)
23+
{
24+
}
25+
26+
public static bool IsVisualStudioProcess() => IsProcess(VisualStudioProcessName);
27+
}
28+
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@
159159
<Compile Include="Commands\IGoToSolutionOrPullRequestFileCommand.cs" />
160160
<Compile Include="Commands\IVsCommand.cs" />
161161
<Compile Include="Commands\IVsCommandBase.cs" />
162-
<Compile Include="Exports\ExportForProcess.cs" />
162+
<Compile Include="Exports\ExportForVisualStudioProcessAttribute.cs" />
163+
<Compile Include="Exports\ExportForProcessAttribute.cs" />
163164
<Compile Include="Extensions\ConnectionManagerExtensions.cs" />
164165
<Compile Include="GitHubLogicException.cs" />
165166
<Compile Include="Models\CommitMessage.cs" />

src/GitHub.InlineReviews/InlineReviewsPackage.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
using System.ComponentModel.Design;
33
using System.Runtime.InteropServices;
44
using System.Threading;
5+
using GitHub.Exports;
6+
using GitHub.Logging;
57
using GitHub.Commands;
68
using GitHub.Services.Vssdk.Commands;
79
using GitHub.VisualStudio;
810
using Microsoft.VisualStudio.ComponentModelHost;
911
using Microsoft.VisualStudio.Shell;
1012
using Microsoft.VisualStudio.Threading;
13+
using Serilog;
1114
using Task = System.Threading.Tasks.Task;
1215

1316
namespace GitHub.InlineReviews
@@ -18,6 +21,8 @@ namespace GitHub.InlineReviews
1821
[ProvideMenuResource("Menus.ctmenu", 1)]
1922
public class InlineReviewsPackage : AsyncPackage
2023
{
24+
static readonly ILogger log = LogManager.ForContext<InlineReviewsPackage>();
25+
2126
protected override async Task InitializeAsync(
2227
CancellationToken cancellationToken,
2328
IProgress<ServiceProgressData> progress)
@@ -33,6 +38,12 @@ protected override async Task InitializeAsync(
3338

3439
async Task InitializeMenus()
3540
{
41+
if (!ExportForVisualStudioProcessAttribute.IsVisualStudioProcess())
42+
{
43+
log.Warning("Don't initialize menus for non-Visual Studio process");
44+
return;
45+
}
46+
3647
var componentModel = (IComponentModel)(await GetServiceAsync(typeof(SComponentModel)));
3748
var exports = componentModel.DefaultExportProvider;
3849
var commands = new IVsCommandBase[]

src/GitHub.TeamFoundation.14/Base/TeamExplorerServiceHolder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void NotifyActiveRepo()
138138

139139
void UpdateActiveRepo()
140140
{
141-
// NOTE: gitService will be null in Expression Blend or Safe Mode
141+
// NOTE: gitService might be null in Blend or Safe Mode
142142
var repo = gitService?.ActiveRepositories.FirstOrDefault();
143143

144144
if (!Equals(repo, ActiveRepo))

src/GitHub.VisualStudio.UI/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GitHub.VisualStudio.UI/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@
425425
<data name="Options_EnableTraceLoggingText" xml:space="preserve">
426426
<value>Enable Trace Logging</value>
427427
</data>
428+
<data name="BlendDialogText" xml:space="preserve">
429+
<value>The GitHub extension is not available inside Blend</value>
430+
</data>
428431
<data name="Options_ForkButtonLabel" xml:space="preserve">
429432
<value>Show Fork button in Team Explorer</value>
430433
</data>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using GitHub.Services.Vssdk.Commands;
3+
using Microsoft.VisualStudio;
4+
using Microsoft.VisualStudio.Shell;
5+
using Microsoft.VisualStudio.Shell.Interop;
6+
using Task = System.Threading.Tasks.Task;
7+
8+
namespace GitHub.VisualStudio.Commands
9+
{
10+
/// <summary>
11+
/// Show an info message dialog when a command is executed.
12+
/// </summary>
13+
public class ShowMessageBoxCommand : VsCommand
14+
{
15+
readonly IServiceProvider serviceProvider;
16+
readonly string message;
17+
18+
public ShowMessageBoxCommand(Guid commandSet, int commandId,
19+
IServiceProvider serviceProvider, string message) : base(commandSet, commandId)
20+
{
21+
this.serviceProvider = serviceProvider;
22+
this.message = message;
23+
}
24+
25+
public override Task Execute()
26+
{
27+
ShowInfoMessage(message);
28+
return Task.CompletedTask;
29+
}
30+
31+
void ShowInfoMessage(string infoMessage)
32+
{
33+
ErrorHandler.ThrowOnFailure(VsShellUtilities.ShowMessageBox(serviceProvider, infoMessage, null,
34+
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST));
35+
}
36+
}
37+
}

src/GitHub.VisualStudio/GitContextPackage.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
22
using System.Threading;
33
using System.Runtime.InteropServices;
4-
using Microsoft.VisualStudio.Shell;
4+
using GitHub.Exports;
5+
using GitHub.Logging;
56
using GitHub.Services;
7+
using Microsoft.VisualStudio.Shell;
8+
using Serilog;
69
using Task = System.Threading.Tasks.Task;
710

811
namespace GitHub.VisualStudio
@@ -17,8 +20,16 @@ namespace GitHub.VisualStudio
1720
[ProvideAutoLoad(Guids.GitSccProviderId, PackageAutoLoadFlags.BackgroundLoad)]
1821
public class GitContextPackage : AsyncPackage
1922
{
23+
static readonly ILogger log = LogManager.ForContext<GitContextPackage>();
24+
2025
protected async override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
2126
{
27+
if (!ExportForVisualStudioProcessAttribute.IsVisualStudioProcess())
28+
{
29+
log.Warning("Don't activate 'UIContext_Git' for non-Visual Studio process");
30+
return;
31+
}
32+
2233
var gitExt = (IVSGitExt)await GetServiceAsync(typeof(IVSGitExt));
2334
var context = UIContext.FromUIContextGuid(new Guid(Guids.UIContext_Git));
2435
RefreshContext(context, gitExt);

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
<Compile Include="AssemblyResolverPackage.cs" />
317317
<Compile Include="Commands\BlameLinkCommand.cs" />
318318
<Compile Include="Commands\CreateGistCommand.cs" />
319+
<Compile Include="Commands\ShowMessageBoxCommand.cs" />
319320
<Compile Include="Commands\LinkCommandBase.cs" />
320321
<Compile Include="Commands\CopyLinkCommand.cs" />
321322
<Compile Include="Commands\OpenLinkCommand.cs" />

0 commit comments

Comments
 (0)