-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenamifyProjectCommand.cs
More file actions
110 lines (87 loc) · 3.58 KB
/
RenamifyProjectCommand.cs
File metadata and controls
110 lines (87 loc) · 3.58 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System.ComponentModel.Design;
using System.IO;
using System.Windows.Interop;
using CodingWithCalvin.ProjectRenamifier.Dialogs;
using CodingWithCalvin.ProjectRenamifier.Services;
using EnvDTE;
using EnvDTE80;
namespace CodingWithCalvin.ProjectRenamifier
{
internal sealed class RenamifyProjectCommand
{
private readonly Package _package;
private IServiceProvider ServiceProvider => _package;
public static void Initialize(Package package)
{
_ = new RenamifyProjectCommand(package);
}
private RenamifyProjectCommand(Package package)
{
_package = package;
var commandService = (OleMenuCommandService)
ServiceProvider.GetService(typeof(IMenuCommandService));
if (commandService == null)
{
return;
}
var menuCommandId = new CommandID(
PackageGuids.CommandSetGuid,
PackageIds.RenamifyProjectCommandId
);
var menuItem = new MenuCommand(Execute, menuCommandId);
commandService.AddCommand(menuItem);
}
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (!(ServiceProvider.GetService(typeof(DTE)) is DTE2 dte))
{
return;
}
var selectedItems = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems;
foreach (UIHierarchyItem selectedItem in selectedItems)
{
if (selectedItem.Object is Project project)
{
RenameProject(project, dte);
}
}
}
private void RenameProject(Project project, DTE2 dte)
{
ThreadHelper.ThrowIfNotOnUIThread();
var currentName = Path.GetFileNameWithoutExtension(project.FullName);
var dialog = new RenameProjectDialog(currentName);
// Set the owner to the VS main window for proper modal behavior
var helper = new WindowInteropHelper(dialog)
{
Owner = dte.MainWindow.HWnd
};
if (dialog.ShowDialog() != true)
{
return;
}
var newName = dialog.NewProjectName;
var projectFilePath = project.FullName;
// Remove project from solution before file operations
dte.Solution.Remove(project);
// Update RootNamespace and AssemblyName in .csproj
ProjectFileService.UpdateProjectFile(projectFilePath, currentName, newName);
// Update namespace declarations in source files
SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName);
// Rename the project file on disk
projectFilePath = ProjectFileService.RenameProjectFile(projectFilePath, newName);
// Rename parent directory if it matches the old project name
projectFilePath = ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName);
// Re-add project to solution with new path
dte.Solution.AddFromFile(projectFilePath);
// TODO: Implement remaining rename operations
// See open issues for requirements:
// - #23: Update project references
// - #9: Update using statements across solution
// - #11: Solution folder support
// - #12: Progress indication
// - #13: Error handling and rollback
}
}
}