-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMoveFileCommand.cs
95 lines (80 loc) · 4.26 KB
/
MoveFileCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}
}
}