diff --git a/New_Extensibility_Model/Samples/Directory.Build.props b/New_Extensibility_Model/Samples/Directory.Build.props index aa044f9..a77b037 100644 --- a/New_Extensibility_Model/Samples/Directory.Build.props +++ b/New_Extensibility_Model/Samples/Directory.Build.props @@ -9,6 +9,8 @@ true $(MSBuildThisFileDirectory)shipping.ruleset true + true + latest @@ -40,8 +42,7 @@ - - + diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/MoveFileCommand.cs b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/MoveFileCommand.cs new file mode 100644 index 0000000..0ae7b4c --- /dev/null +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/MoveFileCommand.cs @@ -0,0 +1,95 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace VSProjectQueryAPISample +{ + using System.Diagnostics; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using Microsoft; + using Microsoft.VisualStudio.Extensibility; + using Microsoft.VisualStudio.Extensibility.Commands; + using Microsoft.VisualStudio.Extensibility.Shell; + using Microsoft.VisualStudio.ProjectSystem.Query; + + /// + /// MoveFile handler. + /// + [VisualStudioContribution] + internal class MoveFileCommand : Command + { + private readonly TraceSource logger; + + /// + /// Initializes a new instance of the class. + /// + /// Trace source instance to utilize. + public MoveFileCommand(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.MoveFileCommand.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.MoveFile.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) + { + // 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 consoleApp1QueryResults = await querySpace.QueryProjectsAsync( + project => project.Where(p => p.Name == "ConsoleApp1") + .With(project => project.Path) + .With(project => project.Files + .With(f => f.FileName) + .With(f => f.Path)), + cancellationToken); + + IFileSnapshot sourceFile = consoleApp1QueryResults.First().Files.First(); + + var sourceFilePath = sourceFile.Path; + sb.Append(sourceFilePath + " to "); + + // Query the destination project to retrieve its path. + IQueryResults destinationProjectQueryResults = await querySpace.QueryProjectsAsync( + project => project.Where(p => p.Name == "ConsoleApp2") + .With(project => project.Path), + cancellationToken); + + var destinationProject = Directory.GetParent(destinationProjectQueryResults.First().Path!)!.ToString(); + + sb.Append(destinationProject); + + // Add the source file to the destination project. + await querySpace.UpdateProjectsAsync( + project => project.Where(project => project.Name == "ConsoleApp2"), + project => project.AddFileFromCopy(sourceFilePath, destinationProject), + cancellationToken); + + // Action query to delete the source file from the source project. + await sourceFile.AsUpdatable().Delete().ExecuteAsync(); + + await this.Extensibility.Shell().ShowPromptAsync(sb.ToString(), PromptOptions.OK, cancellationToken); + } + } +} diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/README.md b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/README.md index 3b5244c..fcfb584 100644 --- a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/README.md +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/README.md @@ -212,7 +212,7 @@ While building on the project level, determine the selected project you want to await result.First().BuildAsync(cancellationToken); ``` -### Rename Project +### Renaming a Project 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. @@ -224,6 +224,17 @@ var result = await querySpace.Projects .ExecuteAsync(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( + project => project.Where(project => project.Name == "ConsoleApp1"), + project => project.RenameFile(filePath, "newName.cs"), + cancellationToken); +``` + ### Skip 1 Project 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. @@ -244,4 +255,21 @@ var unsubscriber = await singleProject .Files .With(f => f.FileName) .TrackUpdatesAsync(new TrackerObserver(), CancellationToken.None); +``` + +### Moving a file +This example offers a temporary workaround to move a file by copying it to the new location and then deleting the original file. Currently, a `MoveFile` API is not available in the Project Query API. In this specific case, we are transferring a file from the `ConsoleApp1` project to the `ConsoleApp2` project. + +The first step is to copy the original file to the new destination. +```csharp +var result = await querySpace.UpdateProjectsAsync( + project => project.Where(project => project.Name == "ConsoleApp2"), + project => project.AddFileFromCopy(sourceFilePath, destinationProject), + cancellationToken); +``` + +Next, delete the original file by obtaining an `IFileSnapshot` instance of the file and proceeding with its deletion. The `AsUpdatable()` method indicates that an action will be performed on the project system. This is followed by the `Delete()` action and its execution using `ExecuteAsync()`. + +```csharp +await sourceFile.AsUpdatable().Delete().ExecuteAsync(); ``` \ No newline at end of file diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/RenameFileCommand.cs b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/RenameFileCommand.cs new file mode 100644 index 0000000..94b1e1c --- /dev/null +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/RenameFileCommand.cs @@ -0,0 +1,71 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace VSProjectQueryAPISample +{ + using System.Diagnostics; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using Microsoft; + using Microsoft.VisualStudio.Extensibility; + using Microsoft.VisualStudio.Extensibility.Commands; + using Microsoft.VisualStudio.Extensibility.Shell; + using Microsoft.VisualStudio.ProjectSystem.Query; + + /// + /// RenameFile handler. + /// + [VisualStudioContribution] + internal class RenameFileCommand : Command + { + private readonly TraceSource logger; + + /// + /// Initializes a new instance of the class. + /// + /// Trace source instance to utilize. + public RenameFileCommand(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.RenameFileCommand.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.RenameFile.DisplayName%" as a constructor parameter. + Placements = [CommandPlacement.KnownPlacements.ExtensionsMenu], + Icon = new(ImageMoniker.KnownValues.Extension, IconSettings.IconAndText), + }; + + /// + public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken) + { + WorkspacesExtensibility querySpace = this.Extensibility.Workspaces(); + StringBuilder sb = new StringBuilder("Renamed file at "); + + IQueryResults consoleApp1QueryResults = await querySpace.QueryProjectsAsync( + project => project.Where(p => p.Name == "ConsoleApp1") + .With(project => project.Files + .With(f => f.FileName) + .With(f => f.Path)), + cancellationToken); + + var filePath = consoleApp1QueryResults.First().Files.First().Path; + sb.Append(filePath); + + await querySpace.UpdateProjectsAsync( + project => project.Where(project => project.Name == "ConsoleApp1"), + project => project.RenameFile(filePath, "newName.cs"), + cancellationToken); + + sb.Append(" to newName.cs"); + + await this.Extensibility.Shell().ShowPromptAsync(sb.ToString(), PromptOptions.OK, cancellationToken); + } + } +} diff --git a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs index 9fee8aa..eb72d9f 100644 --- a/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs +++ b/New_Extensibility_Model/Samples/VSProjectQueryAPISample/ShowActiveProjectCommand.cs @@ -1,4 +1,7 @@ -namespace VSProjectQueryAPISample +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace VSProjectQueryAPISample { using System.Diagnostics; using System.Text;