From 6868f105290dd8b5e1f57920628abc81fb26d5eb Mon Sep 17 00:00:00 2001 From: "Tina Schrepfer (LI)" Date: Mon, 9 Sep 2024 15:22:17 -0700 Subject: [PATCH] Update samples to include a command to show current active project. Update resources to add missing command labels. --- .../.vsextension/string-resources.json | 5 +- .../ShowActiveProjectCommand.cs | 70 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/.vsextension/string-resources.json b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/.vsextension/string-resources.json index 2ddadd7b..2229ecb3 100644 --- a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/.vsextension/string-resources.json +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/.vsextension/string-resources.json @@ -3,5 +3,8 @@ "VSProjectQueryAPISample.QueryFileCommand.DisplayName": "Query File", "VSProjectQueryAPISample.QueryOutputGroupByIdCommand.DisplayName": "Query Output Group by ID", "VSProjectQueryAPISample.QueryOutputGroupByNameCommand.DisplayName": "Query Output Group by Name", - "VSProjectQueryAPISample.QueryOutputGroupByProjectCommand.DisplayName": "Query Output Group by Project" + "VSProjectQueryAPISample.QueryOutputGroupByProjectCommand.DisplayName": "Query Output Group by Project", + "VSProjectQueryAPISample.SkipOfNCommand.DisplayName": "Query Project And Skip Over 1", + "VSProjectQueryAPISample.TrackProjectQueryCommand.DisplayName": "Track Project Query", + "VSProjectQueryAPISample.ShowActiveProjectCommand.DisplayName": "Show Current Active Project" } diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs new file mode 100644 index 00000000..9fee8aaa --- /dev/null +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs @@ -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; + + /// + /// ShowActiveProjectCommand handler. + /// + [VisualStudioContribution] + internal class ShowActiveProjectCommand : Command + { + private readonly TraceSource logger; + + /// + /// Initializes a new instance of the class. + /// + /// Trace source instance to utilize. + 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)); + } + + /// + 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), + }; + + /// + public override Task InitializeAsync(CancellationToken cancellationToken) + { + // Use InitializeAsync for any one-time setup or initialization. + return base.InitializeAsync(cancellationToken); + } + + /// + 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); + } + } + } +}