Skip to content

Commit b1079ee

Browse files
authored
feat(rename): rename sibling files sharing the project file name (#73)
1 parent b3e84cc commit b1079ee

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,38 @@ private void RenameProject(Project project, DTE2 dte)
215215
return ProjectFileService.RenameProjectFile(projectFilePath, newName);
216216
}, out projectFilePath);
217217

218-
// Step 7: Rename parent directory if it matches the old project name
218+
// Step 7: Rename sibling files (e.g., .csproj.user, .vcxproj.filters)
219+
ExecuteStep(progressDialog, stepIndex++, () =>
220+
{
221+
ProjectFileService.RenameSiblingFiles(projectFilePath, currentName);
222+
});
223+
224+
// Step 8: Rename parent directory if it matches the old project name
219225
ExecuteStep(progressDialog, stepIndex++, () =>
220226
{
221227
return ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName);
222228
}, out projectFilePath);
223229

224-
// Step 8: Update references in projects that referenced this project
230+
// Step 9: Update references in projects that referenced this project
225231
ExecuteStep(progressDialog, stepIndex++, () =>
226232
{
227233
ProjectReferenceService.UpdateProjectReferences(referencingProjects, oldProjectFilePath, projectFilePath);
228234
});
229235

230-
// Step 9: Re-add project to solution, preserving solution folder location
236+
// Step 10: Re-add project to solution, preserving solution folder location
231237
ExecuteStep(progressDialog, stepIndex++, () =>
232238
{
233239
SolutionFolderService.AddProjectToSolution(dte.Solution, projectFilePath, parentSolutionFolder);
234240
projectReaddedToSolution = true;
235241
});
236242

237-
// Step 10: Update using statements across the entire solution
243+
// Step 11: Update using statements across the entire solution
238244
ExecuteStep(progressDialog, stepIndex++, () =>
239245
{
240246
SourceFileService.UpdateUsingStatementsInSolution(dte.Solution, currentName, newName);
241247
});
242248

243-
// Step 11: Update fully qualified type references across the solution
249+
// Step 12: Update fully qualified type references across the solution
244250
ExecuteStep(progressDialog, stepIndex++, () =>
245251
{
246252
SourceFileService.UpdateFullyQualifiedReferencesInSolution(dte.Solution, currentName, newName);

src/CodingWithCalvin.ProjectRenamifier/Dialogs/RenameProgressDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
Title="Renaming Project"
55
Width="400"
6-
Height="360"
6+
Height="384"
77
ResizeMode="NoResize"
88
WindowStartupLocation="CenterOwner"
99
ShowInTaskbar="False">

src/CodingWithCalvin.ProjectRenamifier/Dialogs/RenameProgressDialog.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public RenameProgressDialog(string projectName)
2323
new ProgressStep("Updating project file"),
2424
new ProgressStep("Updating namespace declarations"),
2525
new ProgressStep("Renaming project file"),
26+
new ProgressStep("Renaming sibling files"),
2627
new ProgressStep("Renaming project directory"),
2728
new ProgressStep("Updating project references"),
2829
new ProgressStep("Re-adding project to solution"),

src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections.Generic;
12
using System.IO;
3+
using System.Linq;
24
using System.Xml;
35

46
namespace CodingWithCalvin.ProjectRenamifier.Services
@@ -26,6 +28,45 @@ public static string RenameProjectFile(string projectFilePath, string newName)
2628
return newFilePath;
2729
}
2830

31+
/// <summary>
32+
/// Renames sibling files that share the project file name as a prefix.
33+
/// For example, renaming Foo.csproj also renames Foo.csproj.user, Foo.csproj.filters, etc.
34+
/// </summary>
35+
/// <param name="projectFilePath">Full path to the already-renamed project file.</param>
36+
/// <param name="oldName">The old project name (without extension).</param>
37+
/// <returns>A list of the old file paths that were renamed.</returns>
38+
public static IReadOnlyList<string> RenameSiblingFiles(string projectFilePath, string oldName)
39+
{
40+
var directory = Path.GetDirectoryName(projectFilePath);
41+
var extension = Path.GetExtension(projectFilePath);
42+
var newName = Path.GetFileNameWithoutExtension(projectFilePath);
43+
var oldProjectFileName = oldName + extension;
44+
var newProjectFileName = newName + extension;
45+
46+
var siblingFiles = Directory.GetFiles(directory)
47+
.Where(f =>
48+
{
49+
var fileName = Path.GetFileName(f);
50+
return fileName.StartsWith(oldProjectFileName + ".", StringComparison.OrdinalIgnoreCase);
51+
})
52+
.ToList();
53+
54+
var renamed = new List<string>();
55+
56+
foreach (var filePath in siblingFiles)
57+
{
58+
var fileName = Path.GetFileName(filePath);
59+
var suffix = fileName.Substring(oldProjectFileName.Length);
60+
var newFileName = newProjectFileName + suffix;
61+
var newFilePath = Path.Combine(directory, newFileName);
62+
63+
File.Move(filePath, newFilePath);
64+
renamed.Add(filePath);
65+
}
66+
67+
return renamed;
68+
}
69+
2970
/// <summary>
3071
/// Renames the parent directory if its name matches the old project name.
3172
/// </summary>

0 commit comments

Comments
 (0)