diff --git a/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs b/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs index e0ed85e..c55cdb7 100644 --- a/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs +++ b/src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs @@ -80,12 +80,24 @@ private void RenameProject(Project project, DTE2 dte) var newName = dialog.NewProjectName; var projectFilePath = project.FullName; + // Remove project from solution before file operations + dte.Solution.Remove(project); + // Update RootNamespace and AssemblyName in .csproj ProjectFileService.UpdateProjectFile(projectFilePath, currentName, newName); // Update namespace declarations in source files SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName); + // Rename the project file on disk + var newProjectFilePath = ProjectFileService.RenameProjectFile(projectFilePath, newName); + + // Rename parent directory if it matches the old project name + newProjectFilePath = ProjectFileService.RenameParentDirectoryIfMatches(newProjectFilePath, currentName, newName); + + // Re-add project to solution with new path + dte.Solution.AddFromFile(newProjectFilePath); + // TODO: Implement remaining rename operations // See open issues for requirements: // - #9: Update using statements across solution diff --git a/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs b/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs index 0d909c3..f430fdf 100644 --- a/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs +++ b/src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs @@ -1,3 +1,4 @@ +using System.IO; using System.Xml; namespace CodingWithCalvin.ProjectRenamifier.Services @@ -7,6 +8,57 @@ namespace CodingWithCalvin.ProjectRenamifier.Services /// internal static class ProjectFileService { + /// + /// Renames the project file on disk. + /// + /// Full path to the current .csproj file. + /// The new project name (without extension). + /// The new full path to the renamed project file. + public static string RenameProjectFile(string projectFilePath, string newName) + { + var directory = Path.GetDirectoryName(projectFilePath); + var extension = Path.GetExtension(projectFilePath); + var newFileName = newName + extension; + var newFilePath = Path.Combine(directory, newFileName); + + File.Move(projectFilePath, newFilePath); + + 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.