|
1 | 1 | using System.ComponentModel.Design; |
2 | 2 | using System.IO; |
3 | 3 | using System.Windows.Interop; |
| 4 | +using System.Windows.Threading; |
4 | 5 | using CodingWithCalvin.ProjectRenamifier.Dialogs; |
5 | 6 | using CodingWithCalvin.ProjectRenamifier.Services; |
6 | 7 | using EnvDTE; |
@@ -80,41 +81,116 @@ private void RenameProject(Project project, DTE2 dte) |
80 | 81 | var newName = dialog.NewProjectName; |
81 | 82 | var projectFilePath = project.FullName; |
82 | 83 |
|
83 | | - // Collect projects that reference this project before removal |
84 | | - var referencingProjects = ProjectReferenceService.FindProjectsReferencingTarget(dte.Solution, projectFilePath); |
| 84 | + // Show progress dialog |
| 85 | + var progressDialog = new RenameProgressDialog(currentName); |
| 86 | + var progressHelper = new WindowInteropHelper(progressDialog) |
| 87 | + { |
| 88 | + Owner = dte.MainWindow.HWnd |
| 89 | + }; |
| 90 | + progressDialog.Show(); |
| 91 | + |
| 92 | + var stepIndex = 0; |
| 93 | + |
| 94 | + // Step 1: Collect projects that reference this project before removal |
| 95 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 96 | + { |
| 97 | + return ProjectReferenceService.FindProjectsReferencingTarget(dte.Solution, projectFilePath); |
| 98 | + }, out var referencingProjects); |
85 | 99 | var oldProjectFilePath = projectFilePath; |
86 | 100 |
|
87 | | - // Capture the parent solution folder before removal |
88 | | - var parentSolutionFolder = SolutionFolderService.GetParentSolutionFolder(project); |
| 101 | + // Step 2: Capture the parent solution folder before removal |
| 102 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 103 | + { |
| 104 | + return SolutionFolderService.GetParentSolutionFolder(project); |
| 105 | + }, out var parentSolutionFolder); |
89 | 106 |
|
90 | | - // Remove project from solution before file operations |
91 | | - dte.Solution.Remove(project); |
| 107 | + // Step 3: Remove project from solution before file operations |
| 108 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 109 | + { |
| 110 | + dte.Solution.Remove(project); |
| 111 | + }); |
| 112 | + |
| 113 | + // Step 4: Update RootNamespace and AssemblyName in .csproj |
| 114 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 115 | + { |
| 116 | + ProjectFileService.UpdateProjectFile(projectFilePath, currentName, newName); |
| 117 | + }); |
92 | 118 |
|
93 | | - // Update RootNamespace and AssemblyName in .csproj |
94 | | - ProjectFileService.UpdateProjectFile(projectFilePath, currentName, newName); |
| 119 | + // Step 5: Update namespace declarations in source files |
| 120 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 121 | + { |
| 122 | + SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName); |
| 123 | + }); |
95 | 124 |
|
96 | | - // Update namespace declarations in source files |
97 | | - SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName); |
| 125 | + // Step 6: Rename the project file on disk |
| 126 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 127 | + { |
| 128 | + return ProjectFileService.RenameProjectFile(projectFilePath, newName); |
| 129 | + }, out projectFilePath); |
98 | 130 |
|
99 | | - // Rename the project file on disk |
100 | | - projectFilePath = ProjectFileService.RenameProjectFile(projectFilePath, newName); |
| 131 | + // Step 7: Rename parent directory if it matches the old project name |
| 132 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 133 | + { |
| 134 | + return ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName); |
| 135 | + }, out projectFilePath); |
101 | 136 |
|
102 | | - // Rename parent directory if it matches the old project name |
103 | | - projectFilePath = ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName); |
| 137 | + // Step 8: Update references in projects that referenced this project |
| 138 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 139 | + { |
| 140 | + ProjectReferenceService.UpdateProjectReferences(referencingProjects, oldProjectFilePath, projectFilePath); |
| 141 | + }); |
104 | 142 |
|
105 | | - // Update references in projects that referenced this project |
106 | | - ProjectReferenceService.UpdateProjectReferences(referencingProjects, oldProjectFilePath, projectFilePath); |
| 143 | + // Step 9: Re-add project to solution, preserving solution folder location |
| 144 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 145 | + { |
| 146 | + SolutionFolderService.AddProjectToSolution(dte.Solution, projectFilePath, parentSolutionFolder); |
| 147 | + }); |
107 | 148 |
|
108 | | - // Re-add project to solution, preserving solution folder location |
109 | | - SolutionFolderService.AddProjectToSolution(dte.Solution, projectFilePath, parentSolutionFolder); |
| 149 | + // Step 10: Update using statements across the entire solution |
| 150 | + ExecuteStep(progressDialog, stepIndex++, () => |
| 151 | + { |
| 152 | + SourceFileService.UpdateUsingStatementsInSolution(dte.Solution, currentName, newName); |
| 153 | + }); |
110 | 154 |
|
111 | | - // Update using statements across the entire solution |
112 | | - SourceFileService.UpdateUsingStatementsInSolution(dte.Solution, currentName, newName); |
| 155 | + // Mark as complete and close after a brief delay |
| 156 | + progressDialog.Complete(); |
| 157 | + DoEvents(); |
| 158 | + System.Threading.Thread.Sleep(500); |
| 159 | + progressDialog.Close(); |
113 | 160 |
|
114 | 161 | // TODO: Implement remaining rename operations |
115 | 162 | // See open issues for requirements: |
116 | | - // - #12: Progress indication |
117 | 163 | // - #13: Error handling and rollback |
118 | 164 | } |
| 165 | + |
| 166 | + private void ExecuteStep(RenameProgressDialog dialog, int stepIndex, Action action) |
| 167 | + { |
| 168 | + dialog.StartStep(stepIndex); |
| 169 | + DoEvents(); |
| 170 | + action(); |
| 171 | + dialog.CompleteStep(stepIndex); |
| 172 | + DoEvents(); |
| 173 | + } |
| 174 | + |
| 175 | + private void ExecuteStep<T>(RenameProgressDialog dialog, int stepIndex, Func<T> func, out T result) |
| 176 | + { |
| 177 | + dialog.StartStep(stepIndex); |
| 178 | + DoEvents(); |
| 179 | + result = func(); |
| 180 | + dialog.CompleteStep(stepIndex); |
| 181 | + DoEvents(); |
| 182 | + } |
| 183 | + |
| 184 | + private void DoEvents() |
| 185 | + { |
| 186 | + var frame = new DispatcherFrame(); |
| 187 | + Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, |
| 188 | + new DispatcherOperationCallback(obj => |
| 189 | + { |
| 190 | + ((DispatcherFrame)obj).Continue = false; |
| 191 | + return null; |
| 192 | + }), frame); |
| 193 | + Dispatcher.PushFrame(frame); |
| 194 | + } |
119 | 195 | } |
120 | 196 | } |
0 commit comments