Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ public static string RenameProjectFile(string projectFilePath, string newName)
return newFilePath;
}

/// <summary>
/// Renames the parent directory if its name matches the old project name.
/// </summary>
/// <param name="projectFilePath">Full path to the .csproj file.</param>
/// <param name="oldName">The old project name to match against.</param>
/// <param name="newName">The new project name.</param>
/// <returns>The new full path to the project file after directory rename, or the original path if no rename occurred.</returns>
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);
}

/// <summary>
/// Updates the RootNamespace and AssemblyName elements in a project file
/// if they match the old project name.
Expand Down