Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update samples to include a command to show current active project. #416

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
Loading