From 9d3d888696c26210c7aec824a6f4f4b6e4ba69b5 Mon Sep 17 00:00:00 2001 From: "Calvin A. Allen" Date: Sat, 20 Dec 2025 11:20:10 -0500 Subject: [PATCH] feat(rename): rename parent directory if it matches project name - Add RenameParentDirectoryIfMatches method to ProjectFileService - Only renames directory if folder name matches old project name (case-insensitive) - Returns updated project file path after directory rename Closes #21 --- .../Commands/RenamifyProjectCommand.cs | 4 ++- .../Services/ProjectFileService.cs | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs b/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs index 3847491..67a79df 100644 --- a/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs +++ b/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs @@ -89,9 +89,11 @@ private void RenameProject(Project project, DTE2 dte) // 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); + // TODO: Implement remaining rename operations // See open issues for requirements: - // - #21: Rename parent directory if it matches project name // - #22: Remove and re-add project to solution // - #23: Update project references // - #9: Update using statements across solution diff --git a/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs b/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs index 8714e5d..f430fdf 100644 --- a/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs +++ b/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs @@ -26,6 +26,39 @@ public static string RenameProjectFile(string projectFilePath, string newName) return newFilePath; } + /// + /// Renames the parent directory if its name matches the old project name. + /// + /// Full path to the .csproj file. + /// The old project name to match against. + /// The new project name. + /// The new full path to the project file after directory rename, or the original path if no rename occurred. + public static string RenameParentDirectoryIfMatches(string projectFilePath, string oldName, string newName) + { + var projectDirectory = Path.GetDirectoryName(projectFilePath); + var parentDirectory = Directory.GetParent(projectDirectory); + + if (parentDirectory == null) + { + return projectFilePath; + } + + var directoryName = new DirectoryInfo(projectDirectory).Name; + + // Only rename if directory name matches the old project name + if (!directoryName.Equals(oldName, StringComparison.OrdinalIgnoreCase)) + { + return projectFilePath; + } + + var newDirectoryPath = Path.Combine(parentDirectory.FullName, newName); + Directory.Move(projectDirectory, newDirectoryPath); + + // Return the new project file path + var fileName = Path.GetFileName(projectFilePath); + return Path.Combine(newDirectoryPath, fileName); + } + /// /// Updates the RootNamespace and AssemblyName elements in a project file /// if they match the old project name.