-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update samples to include a command to show current active project. U…
…pdate resources to add missing command labels.
- Loading branch information
Tina Schrepfer (LI)
committed
Sep 9, 2024
1 parent
2331763
commit 6868f10
Showing
2 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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.ExtensionsMenu], | ||
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); | ||
} | ||
} | ||
} | ||
} |