Skip to content

Commit

Permalink
Update samples to include a command to show current active project. U…
Browse files Browse the repository at this point in the history
…pdate resources to add missing command labels.
  • Loading branch information
Tina Schrepfer (LI) committed Sep 9, 2024
1 parent 2331763 commit 6868f10
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
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);
}
}
}
}

0 comments on commit 6868f10

Please sign in to comment.