Skip to content

Commit 2d2b939

Browse files
committed
feat(rename): rename project file and parent directory
- Add RenameProjectFile method to rename .csproj on disk - Add RenameParentDirectoryIfMatches method to rename folder if it matches project name - Uses full paths via Path and DirectoryInfo classes - Directory rename only occurs if folder name matches old project name (case-insensitive) - Both methods return new path for subsequent operations Closes #20 Closes #21
1 parent 881b14e commit 2d2b939

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ private void RenameProject(Project project, DTE2 dte)
8686
// Update namespace declarations in source files
8787
SourceFileService.UpdateNamespacesInProject(projectFilePath, currentName, newName);
8888

89+
// Rename parent directory if it matches project name
90+
projectFilePath = ProjectFileService.RenameParentDirectoryIfMatches(projectFilePath, currentName, newName);
91+
92+
// Rename the project file on disk
93+
projectFilePath = ProjectFileService.RenameProjectFile(projectFilePath, newName);
94+
8995
// TODO: Implement remaining rename operations
9096
// See open issues for requirements:
97+
// - #22: Remove and re-add project to solution
98+
// - #23: Update project references
9199
// - #9: Update using statements across solution
92100
// - #11: Solution folder support
93101
// - #12: Progress indication

src/CodingWithCalvin.ProjectRenamifier/Services/ProjectFileService.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using System.Xml;
23

34
namespace CodingWithCalvin.ProjectRenamifier.Services
@@ -7,6 +8,57 @@ namespace CodingWithCalvin.ProjectRenamifier.Services
78
/// </summary>
89
internal static class ProjectFileService
910
{
11+
/// <summary>
12+
/// Renames the project file on disk.
13+
/// </summary>
14+
/// <param name="projectFilePath">Full path to the current .csproj file.</param>
15+
/// <param name="newName">The new project name (without extension).</param>
16+
/// <returns>The new full path to the renamed project file.</returns>
17+
public static string RenameProjectFile(string projectFilePath, string newName)
18+
{
19+
var directory = Path.GetDirectoryName(projectFilePath);
20+
var extension = Path.GetExtension(projectFilePath);
21+
var newFileName = newName + extension;
22+
var newFilePath = Path.Combine(directory, newFileName);
23+
24+
File.Move(projectFilePath, newFilePath);
25+
26+
return newFilePath;
27+
}
28+
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+
1062
/// <summary>
1163
/// Updates the RootNamespace and AssemblyName elements in a project file
1264
/// if they match the old project name.

0 commit comments

Comments
 (0)