Skip to content
Closed
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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.IO;
using System.Xml;

namespace CodingWithCalvin.ProjectRenamifier.Services
Expand All @@ -7,6 +8,57 @@ namespace CodingWithCalvin.ProjectRenamifier.Services
/// </summary>
internal static class ProjectFileService
{
/// <summary>
/// Renames the project file on disk.
/// </summary>
/// <param name="projectFilePath">Full path to the current .csproj file.</param>
/// <param name="newName">The new project name (without extension).</param>
/// <returns>The new full path to the renamed project file.</returns>
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;
}

/// <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