-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenamifyProjectCommand.cs
More file actions
267 lines (226 loc) · 10.3 KB
/
RenamifyProjectCommand.cs
File metadata and controls
267 lines (226 loc) · 10.3 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.IO;
using System.Windows.Interop;
using System.Windows.Threading;
using CodingWithCalvin.ProjectRenamifier.Dialogs;
using CodingWithCalvin.ProjectRenamifier.Services;
using CodingWithCalvin.Otel4Vsix;
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();
using var activity = VsixTelemetry.StartCommandActivity("ProjectRenamifier.Execute");
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();
using var activity = VsixTelemetry.StartCommandActivity("ProjectRenamifier.RenameProject");
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)
{
VsixTelemetry.LogInformation("Rename cancelled by user");
return;
}
var newName = dialog.NewProjectName;
var projectFilePath = project.FullName;
var originalProjectFilePath = projectFilePath;
VsixTelemetry.LogInformation("Renaming project");
// Show progress dialog
var progressDialog = new RenameProgressDialog(currentName);
var progressHelper = new WindowInteropHelper(progressDialog)
{
Owner = dte.MainWindow.HWnd
};
progressDialog.Show();
var stepIndex = 0;
var projectRemovedFromSolution = false;
var projectReaddedToSolution = false;
List<string> referencingProjects = null;
Project parentSolutionFolder = null;
try
{
// Step 1: Collect projects that reference this project before removal
ExecuteStep(progressDialog, stepIndex++, () =>
{
return ProjectReferenceService.FindProjectsReferencingTarget(dte.Solution, projectFilePath);
}, out referencingProjects);
var oldProjectFilePath = projectFilePath;
// Step 2: Capture the parent solution folder before removal
ExecuteStep(progressDialog, stepIndex++, () =>
{
return SolutionFolderService.GetParentSolutionFolder(project);
}, out parentSolutionFolder);
// Step 3: Remove project from solution before file operations
ExecuteStep(progressDialog, stepIndex++, () =>
{
dte.Solution.Remove(project);
projectRemovedFromSolution = true;
});
// Step 4: Update RootNamespace and AssemblyName in .csproj
ExecuteStep(progressDialog, stepIndex++, () =>
{
ProjectFileService.UpdateProjectFile(projectFilePath, currentName, newName);
});
// Step 5: Update namespace declarations in source files
ExecuteStep(progressDialog, stepIndex++, () =>
{
SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName);
});
// Step 6: Rename the project file on disk
ExecuteStep(progressDialog, stepIndex++, () =>
{
return ProjectFileService.RenameProjectFile(projectFilePath, newName);
}, out projectFilePath);
// Step 7: Rename parent directory if it matches the old project name
ExecuteStep(progressDialog, stepIndex++, () =>
{
return ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName);
}, out projectFilePath);
// Step 8: Update references in projects that referenced this project
ExecuteStep(progressDialog, stepIndex++, () =>
{
ProjectReferenceService.UpdateProjectReferences(referencingProjects, oldProjectFilePath, projectFilePath);
});
// Step 9: Re-add project to solution, preserving solution folder location
ExecuteStep(progressDialog, stepIndex++, () =>
{
SolutionFolderService.AddProjectToSolution(dte.Solution, projectFilePath, parentSolutionFolder);
projectReaddedToSolution = true;
});
// Step 10: Update using statements across the entire solution
ExecuteStep(progressDialog, stepIndex++, () =>
{
SourceFileService.UpdateUsingStatementsInSolution(dte.Solution, currentName, newName);
});
// Step 11: Update fully qualified type references across the solution
ExecuteStep(progressDialog, stepIndex++, () =>
{
SourceFileService.UpdateFullyQualifiedReferencesInSolution(dte.Solution, currentName, newName);
});
// Mark as complete and close after a brief delay
progressDialog.Complete();
DoEvents();
System.Threading.Thread.Sleep(500);
progressDialog.Close();
activity?.SetTag("rename.success", true);
activity?.SetTag("rename.steps_completed", stepIndex);
VsixTelemetry.LogInformation("Successfully renamed project");
}
catch (Exception ex)
{
// Mark the current step as failed
progressDialog.FailStep(stepIndex, ex.Message);
DoEvents();
activity?.RecordError(ex);
activity?.SetTag("rename.success", false);
activity?.SetTag("rename.failed_step", stepIndex);
VsixTelemetry.TrackException(ex, new Dictionary<string, object>
{
{ "operation.name", "RenameProject" },
{ "step.index", stepIndex }
});
// Attempt rollback if project was removed but not re-added
if (projectRemovedFromSolution && !projectReaddedToSolution)
{
try
{
// Try to re-add the project at its current location
var currentProjectPath = File.Exists(projectFilePath) ? projectFilePath : originalProjectFilePath;
if (File.Exists(currentProjectPath))
{ SolutionFolderService.AddProjectToSolution(dte.Solution, currentProjectPath, parentSolutionFolder);
VsixTelemetry.LogInformation("Rollback: Re-added project");
}
}
catch (Exception rollbackEx)
{
VsixTelemetry.TrackException(rollbackEx, new Dictionary<string, object>
{
{ "operation.name", "RenameProject.Rollback" }
});
}
}
// Show error message
System.Windows.MessageBox.Show(
$"An error occurred while renaming the project:\n\n{ex.Message}\n\n" +
"The operation has been aborted. The project may be in a partially renamed state.",
"Rename Failed",
System.Windows.MessageBoxButton.OK,
System.Windows.MessageBoxImage.Error);
progressDialog.Close();
}
}
private void ExecuteStep(RenameProgressDialog dialog, int stepIndex, Action action)
{
dialog.StartStep(stepIndex);
DoEvents();
action();
dialog.CompleteStep(stepIndex);
DoEvents();
}
private void ExecuteStep<T>(RenameProgressDialog dialog, int stepIndex, Func<T> func, out T result)
{
dialog.StartStep(stepIndex);
DoEvents();
result = func();
dialog.CompleteStep(stepIndex);
DoEvents();
}
private void DoEvents()
{
var frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(obj =>
{
((DispatcherFrame)obj).Continue = false;
return null;
}), frame);
Dispatcher.PushFrame(frame);
}
}
}