Skip to content

Commit 124accb

Browse files
authored
feat(rename): rename parent directory if it matches project name (#27)
1 parent 8d008c7 commit 124accb

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ private void RenameProject(Project project, DTE2 dte)
8989
// Rename the project file on disk
9090
projectFilePath = ProjectFileService.RenameProjectFile(projectFilePath, newName);
9191

92+
// Rename parent directory if it matches the old project name
93+
projectFilePath = ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName);
94+
9295
// TODO: Implement remaining rename operations
9396
// See open issues for requirements:
94-
// - #21: Rename parent directory if it matches project name
9597
// - #22: Remove and re-add project to solution
9698
// - #23: Update project references
9799
// - #9: Update using statements across solution

src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@ public static string RenameProjectFile(string projectFilePath, string newName)
2626
return newFilePath;
2727
}
2828

29+
/// <summary>
30+
/// Renames the parent directory if its name matches the old project name.
31+
/// </summary>
32+
/// <param name="projectFilePath">Full path to the .csproj file.</param>
33+
/// <param name="oldName">The old project name to match against.</param>
34+
/// <param name="newName">The new project name.</param>
35+
/// <returns>The new full path to the project file after directory rename, or the original path if no rename occurred.</returns>
36+
public static string RenameParentDirectoryIfMatches(string projectFilePath, string oldName, string newName)
37+
{
38+
var projectDirectory = Path.GetDirectoryName(projectFilePath);
39+
var parentDirectory = Directory.GetParent(projectDirectory);
40+
41+
if (parentDirectory == null)
42+
{
43+
return projectFilePath;
44+
}
45+
46+
var directoryName = new DirectoryInfo(projectDirectory).Name;
47+
48+
// Only rename if directory name matches the old project name
49+
if (!directoryName.Equals(oldName, StringComparison.OrdinalIgnoreCase))
50+
{
51+
return projectFilePath;
52+
}
53+
54+
var newDirectoryPath = Path.Combine(parentDirectory.FullName, newName);
55+
Directory.Move(projectDirectory, newDirectoryPath);
56+
57+
// Return the new project file path
58+
var fileName = Path.GetFileName(projectFilePath);
59+
return Path.Combine(newDirectoryPath, fileName);
60+
}
61+
2962
/// <summary>
3063
/// Updates the RootNamespace and AssemblyName elements in a project file
3164
/// if they match the old project name.

0 commit comments

Comments
 (0)