-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathShowActiveProjectCommand.cs
More file actions
73 lines (65 loc) · 3.28 KB
/
ShowActiveProjectCommand.cs
File metadata and controls
73 lines (65 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace VSProjectQueryAPISample
{
using System.Diagnostics;
using System.Text;
using Microsoft;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.Shell;
using Microsoft.VisualStudio.ProjectSystem.Query;
/// <summary>
/// ShowActiveProjectCommand handler.
/// </summary>
[VisualStudioContribution]
internal class ShowActiveProjectCommand : Command
{
private readonly TraceSource logger;
/// <summary>
/// Initializes a new instance of the <see cref="ShowActiveProjectCommand"/> class.
/// </summary>
/// <param name="traceSource">Trace source instance to utilize.</param>
public ShowActiveProjectCommand(TraceSource traceSource)
{
// This optional TraceSource can be used for logging in the command. You can use dependency injection to access
// other services here as well.
this.logger = Requires.NotNull(traceSource, nameof(traceSource));
}
/// <inheritdoc />
public override CommandConfiguration CommandConfiguration => new(displayName: "%VSProjectQueryAPISample.ShowActiveProjectCommand.DisplayName%")
{
// Use this object initializer to set optional parameters for the command. The required parameter,
// displayName, is set above. To localize the displayName, add an entry in .vsextension\string-resources.json
// and reference it here by passing "%VSProjectQueryAPISample.Command1.DisplayName%" as a constructor parameter.
Placements = [CommandPlacement.KnownPlacements.ToolsMenu],
Icon = new(ImageMoniker.KnownValues.Extension, IconSettings.IconAndText),
};
/// <inheritdoc />
public override Task InitializeAsync(CancellationToken cancellationToken)
{
// Use InitializeAsync for any one-time setup or initialization.
return base.InitializeAsync(cancellationToken);
}
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
var activeProject = await context.GetActiveProjectAsync(cancellationToken);
if (activeProject == null)
{
await this.Extensibility.Shell().ShowPromptAsync("There is no active project defined.", PromptOptions.OK, cancellationToken);
}
else
{
var newResult = await activeProject.AsQueryable()
.With(p => p.Name)
.With(p => p.Path)
.ExecuteQueryAsync();
var displayMessageSb = new StringBuilder();
displayMessageSb.AppendLine("The name of the current active project is: " + newResult.First().Name);
displayMessageSb.AppendLine("The path of the current active project is: " + newResult.First().Path);
await this.Extensibility.Shell().ShowPromptAsync(displayMessageSb.ToString(), PromptOptions.OK, cancellationToken);
}
}
}
}