-
Notifications
You must be signed in to change notification settings - Fork 60
[Project Query] Rename File/ Move File Command Sample #436
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4036a1
Command to rename a file
Jxwoon 5b85c35
Move file workaround sample
Jxwoon af4d889
Renamed variables
Jxwoon 38d14b2
small rename
Jxwoon 78df029
removed extra query
Jxwoon c9393fa
nit typo
Jxwoon 29b8227
Merge remote-tracking branch 'origin/main' into dev/jasminewoon/Renam…
Jxwoon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
New_Extensibility_Model/Samples/VSProjectQueryAPISample/MoveFileCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
|
||
| /// <summary> | ||
| /// MoveFile handler. | ||
| /// </summary> | ||
| [VisualStudioContribution] | ||
| internal class MoveFileCommand : Command | ||
| { | ||
| private readonly TraceSource logger; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MoveFileCommand"/> class. | ||
| /// </summary> | ||
| /// <param name="traceSource">Trace source instance to utilize.</param> | ||
| 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)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| 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), | ||
| }; | ||
|
|
||
| /// <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) | ||
| { | ||
| // 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( | ||
| 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<IProjectSnapshot> 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
New_Extensibility_Model/Samples/VSProjectQueryAPISample/RenameFileCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
|
|
||
| /// <summary> | ||
| /// RenameFile handler. | ||
| /// </summary> | ||
| [VisualStudioContribution] | ||
| internal class RenameFileCommand : Command | ||
| { | ||
| private readonly TraceSource logger; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="RenameFileCommand"/> class. | ||
| /// </summary> | ||
| /// <param name="traceSource">Trace source instance to utilize.</param> | ||
| 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)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| 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), | ||
| }; | ||
|
|
||
| /// <inheritdoc /> | ||
| public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken) | ||
| { | ||
| WorkspacesExtensibility querySpace = this.Extensibility.Workspaces(); | ||
| StringBuilder sb = new StringBuilder("Renamed file at "); | ||
|
|
||
| IQueryResults<IProjectSnapshot> 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); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.