Skip to content
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 @@ -26,15 +26,13 @@ public class AddSolutionConfigurationCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var queryResults = await querySpace.QuerySolutionAsync(
var queryResults = await this.Extensibility.Workspaces().QuerySolutionAsync(
solution => solution.With(solution => solution.BaseName),
cancellationToken);

var solutionName = queryResults.First().BaseName;

await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.AddSolutionConfiguration("Foo", "Debug", false),
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ public class CreateFileCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

await querySpace.UpdateProjectsAsync(
await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(project => project.Name == "ConsoleApp1"),
project => project.CreateFile("CreatedFile.txt"),
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ public class DeleteSolutionConfigurationCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var queryResults = await querySpace.QuerySolutionAsync(
var queryResults = await this.Extensibility.Workspaces().QuerySolutionAsync(
solution => solution.With(solution => solution.BaseName),
cancellationToken);

var solutionName = queryResults.First().BaseName;

await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.DeleteSolutionConfiguration("Foo"),
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ public override Task InitializeAsync(CancellationToken cancellationToken)
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
// Move File Workaround
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();
StringBuilder sb = new StringBuilder("Move file from ");

// Query the source project to retrieve the path of the file to be moved.
IQueryResults<IProjectSnapshot> consoleApp1QueryResults = await querySpace.QueryProjectsAsync(
IQueryResults<IProjectSnapshot> consoleApp1QueryResults = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.Where(p => p.Name == "ConsoleApp1")
.With(project => project.Path)
.With(project => project.Files
Expand All @@ -71,7 +70,7 @@ public override async Task ExecuteCommandAsync(IClientContext context, Cancellat
sb.Append(sourceFilePath + " to ");

// Query the destination project to retrieve its path.
IQueryResults<IProjectSnapshot> destinationProjectQueryResults = await querySpace.QueryProjectsAsync(
IQueryResults<IProjectSnapshot> destinationProjectQueryResults = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.Where(p => p.Name == "ConsoleApp2")
.With(project => project.Path),
cancellationToken);
Expand All @@ -81,7 +80,7 @@ public override async Task ExecuteCommandAsync(IClientContext context, Cancellat
sb.Append(destinationProject);

// Add the source file to the destination project.
await querySpace.UpdateProjectsAsync(
await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(project => project.Name == "ConsoleApp2"),
project => project.AddFileFromCopy(sourceFilePath, destinationProject),
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public class ProjectBuildCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();
const string projectName = "ConsoleApp1";

var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.Where(p => p.Name == projectName),
cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public class QueryFileCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(project => project.Name)
.With(project => project.Path)
.With(project => project.Files.With(file => file.FileName)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public class QueryOutputGroupByIdCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

StringBuilder message = new StringBuilder();

var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations.With(c => c.Name)
.With(c => c.OutputGroups)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public class QueryOutputGroupByNameCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations
.With(c => c.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public class QueryOutputGroupByProjectCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations
.With(c => c.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public class QuerySolutionConfigurations : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

var results = await querySpace.QuerySolutionAsync(
var results = await this.Extensibility.Workspaces().QuerySolutionAsync(
solution => solution.With(solution => solution.SolutionConfigurations
.With(c => c.Name)),
cancellationToken);
Expand Down
70 changes: 34 additions & 36 deletions New_Extensibility_Model/Samples/VSProjectQueryAPISample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,14 @@ This extension demonstrates the most basic usage of the [VS Project Query API](h

The Project System Query API allows you to retrieve and update information in the Project System such as files, build configurations, dependencies, and much more.

## Setting Up IProjectModelQueryableSpace

Add `Microsoft.VisualStudio.ProjectSystem.Query` NuGet Package to the solution's `references` to get access to the API.

The code snippet as seen below sets up the Project Query Service:

```csharp
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();
```

## Accessing API Metadata

Once a `querySpace` is established, you may query information about the Project System on the Solution and/or Project level. Using the keyword `With`, you can specify the property and/or the collection they want to receive.
Using the keyword `With`, you can specify the property and/or the collection they want to receive.

In our example, we call the `QueryProjectsAsync` method to get information from the projects, namely the Project Name, Project Path, Project Files, and File Names.

```csharp
var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(project => project.Name)
.With(project => project.Path)
.With(project => project.Files.With(file => file.FileName)),
Expand All @@ -38,12 +28,12 @@ var result = await querySpace.QueryProjectsAsync(

## Modifying the Project System

Using the same `querySpace`, you may modify data in your project system. `Where` is used to filter the query result based on a predicate. `UpdateProjectsAsync` is to update the object from the query result. These actions modifying the Project System may include creating/deleting files, building/cleaning/deploying solutions, setting properties, and more.
`Where` is used to filter the query result based on a predicate. `UpdateProjectsAsync` is to update the object from the query result. These actions modifying the Project System may include creating/deleting files, building/cleaning/deploying solutions, setting properties, and more.

In our example, we call the `UpdateProjectsAsync` method to create a new file. The file we want to add is called `CreatedFile.txt`, and we want to add it to our project called `ConsoleApp1`.

```csharp
await querySpace.UpdateProjectsAsync(
await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(project => project.Name == "ConsoleApp1"),
project => project.AddFile("CreatedFile.txt"),
cancellationToken);
Expand All @@ -63,7 +53,7 @@ You may filter out metadata By Project in your solution.
In the snippet below, the result will contain information about Output Groups' names about all projects in our Solution.

```csharp
var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations
.With(c => c.Name)
Expand All @@ -78,7 +68,7 @@ If you know which metadata you would like to obtain, you may filter that informa
In the snippet below, we call `OutputGroupsByName` to get specific Output Groups. The Project System Query API will add valid output group to the results, and invalid groups are skipped over. In this case, results will contain three output groups: `Built`, `XmlSerializer`, and `SourceFiles`.

```csharp
var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations
.With(c => c.Name)
Expand All @@ -94,7 +84,7 @@ As usages for project query becomes more complex, you may realize that the requi
In our example, let's say we already queried information about Output Groups.

```csharp
var result = await querySpace.QueryProjectsAsync(
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.With(p => p.ActiveConfigurations.With(c => c.Name)),
cancellationToken);
Expand Down Expand Up @@ -135,16 +125,20 @@ Below is a showcase of queries that are available in the Project Query API
In project query, you also have the ability to invoke build actions on the solution level. These build actions include: `BuildAsync`, `RebuildAsync`, `CleanAsync`, `DebugLaunchAsync`, and `LaunchAsync`.

```csharp
var result = await querySpace.Solutions
.BuildAsync(cancellationToken);
IQueryResults<ISolutionSnapshot> solutions = await this.Extensibility.Workspaces().QuerySolutionAsync(s => s, cancellationToken);

foreach (var solution in solutions)
{
await solution.AsQueryable().BuildAsync(cancellationToken);
}
```

### Loading/Unloading a Project

In the snippet below, we specify the solution we would like to unload the project from and pass in the project path when we make our `UnloadProject` call.

```csharp
await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.UnloadProject(projectPath),
cancellationToken);
Expand All @@ -153,7 +147,7 @@ await querySpace.UpdateSolutionAsync(
Similarly, we can load the project by calling the `ReloadProject` API.

```csharp
await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.ReloadProject(projectPath),
cancellationToken);
Expand All @@ -165,7 +159,12 @@ await querySpace.UpdateSolutionAsync(
`SaveAsync` is an API call that can be used on the solution level.

```csharp
var result = await querySpace.Solutions.SaveAsync(cancellationToken);
IQueryResults<ISolutionSnapshot> solutions = await this.Extensibility.Workspaces().QuerySolutionAsync(s => s, cancellationToken);

foreach (var solution in solutions)
{
await solution.SaveAsync(cancellationToken);
}
```


Expand All @@ -174,7 +173,7 @@ var result = await querySpace.Solutions.SaveAsync(cancellationToken);
Using the Project Query API, you also can select which projects get executed. In the sample below, we added two project paths to be set as the startup project.

```csharp
await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.SetStartupProjects(projectPath1, projectPath2),
cancellationToken);
Expand All @@ -185,7 +184,7 @@ await querySpace.UpdateSolutionAsync(
`AddSolutionConfiguration` is an API call that takes in three parameters. The first parameter is the new name we want to give our new solution configuration. In this scenario, we will call our new solution configuration `Foo`. The next parameter is the configuration to base our new configuration. Below, we based our new solution configuration on the existing solution configuration, `Debug`. Lastly, the boolean represents if the solution configuration should be propagated.

```csharp
await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.AddSolutionConfiguration("Foo", "Debug", false),
cancellationToken);
Expand All @@ -194,7 +193,7 @@ await querySpace.UpdateSolutionAsync(
`DeleteSolutionConfiguration` is an API call that removes the solution configuration. In the example below, we removed the solution configuration called `Foo`.

```csharp
await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.DeleteSolutionConfiguration("Foo"),
cancellationToken);
Expand All @@ -217,19 +216,18 @@ await result.First().BuildAsync(cancellationToken);
In the example below, we specify the name of the project we would like to update. We then call `Rename` while passing in the new name of the project.

```csharp
var result = await querySpace.Projects
.Where(p => p.Name == "ConsoleApp1")
.AsUpdatable()
.Rename("NewProjectName")
.ExecuteAsync(cancellationToken);
var result = await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(p => p.Name == "ConsoleApp1"),
project => project.Rename("NewProjectName"),
cancellationToken);
```


### Renaming a file
`RenameFile` takes the file path of the file you want to rename and the new name of the file. In the example below, we rename a file in a project called `ConsoleApp1` to `newName.cs`.

```csharp
var result = await querySpace.UpdateProjectsAsync(
var result = await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(project => project.Name == "ConsoleApp1"),
project => project.RenameFile(filePath, "newName.cs"),
cancellationToken);
Expand All @@ -240,10 +238,10 @@ var result = await querySpace.UpdateProjectsAsync(
In the code sample, we will query the projects in a solution and skip the first one. Let's say there are 3 projects in the solution. The first result will be skipped and will return the two remaining projects. Note: the order is not guaranteed.

```csharp
var result = await querySpace.QueryProjectsAsync(
project => project.With(p => p.Name)
.Skip(1),
cancellationToken);
var result = await this.Extensibility.Workspaces().QueryProjectsAsync(
project => project.With(p => p.Name)
.Skip(1),
cancellationToken);
```

### Tracking Queries
Expand All @@ -262,7 +260,7 @@ This example offers a temporary workaround to move a file by copying it to the n

The first step is to copy the original file to the new destination.
```csharp
var result = await querySpace.UpdateProjectsAsync(
var result = await this.Extensibility.Workspaces().UpdateProjectsAsync(
project => project.Where(project => project.Name == "ConsoleApp2"),
project => project.AddFileFromCopy(sourceFilePath, destinationProject),
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ public class ReloadProjectCommand : Command
/// <inheritdoc />
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
WorkspacesExtensibility querySpace = this.Extensibility.Workspaces();

const string projectPath = "ConsoleApp1\\\\ConsoleApp1.csproj";

var solutionQueryResults = await querySpace.QuerySolutionAsync(
var solutionQueryResults = await this.Extensibility.Workspaces().QuerySolutionAsync(
solution => solution.With(solution => solution.BaseName),
cancellationToken);

var solutionName = solutionQueryResults.First().BaseName;

await querySpace.UpdateSolutionAsync(
await this.Extensibility.Workspaces().UpdateSolutionAsync(
solution => solution.Where(solution => solution.BaseName == solutionName),
solution => solution.ReloadProject(projectPath),
cancellationToken);
Expand Down
Loading