copy and move directories

This commit is contained in:
Matt Parker
2025-10-22 00:28:17 +10:00
parent e47bebffca
commit dc00618d69
7 changed files with 102 additions and 15 deletions

View File

@@ -28,6 +28,36 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
await _sharpIdeSolutionModificationService.RemoveDirectory(folder);
}
public async Task CopyDirectory(IFolderOrProject destinationParentNode, string sourceDirectoryPath, string newDirectoryName)
{
var newDirectoryPath = Path.Combine(destinationParentNode.ChildNodeBasePath, newDirectoryName);
CopyAll(new DirectoryInfo(sourceDirectoryPath), new DirectoryInfo(newDirectoryPath));
var newFolder = await _sharpIdeSolutionModificationService.AddDirectory(destinationParentNode, newDirectoryName);
return;
static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
foreach (var fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.FullName, fi.Name));
}
foreach (var diSourceSubDir in source.GetDirectories())
{
var nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
}
public async Task MoveDirectory(IFolderOrProject destinationParentNode, SharpIdeFolder folderToMove)
{
var newDirectoryPath = Path.Combine(destinationParentNode.ChildNodeBasePath, folderToMove.Name);
Directory.Move(folderToMove.Path, newDirectoryPath);
await _sharpIdeSolutionModificationService.MoveDirectory(destinationParentNode, folderToMove);
}
public async Task DeleteFile(SharpIdeFile file)
{
File.Delete(file.Path);

View File

@@ -60,9 +60,37 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
}
}
public async Task MoveDirectory(SharpIdeFolder folder, string newDirectoryPath)
public async Task MoveDirectory(IFolderOrProject destinationParentNode, SharpIdeFolder folderToMove)
{
var oldFolderPath = folderToMove.Path;
var newFolderPath = Path.Combine(destinationParentNode.ChildNodeBasePath, folderToMove.Name);
var parentFolderOrProject = (IFolderOrProject)folderToMove.Parent;
parentFolderOrProject.Folders.Remove(folderToMove);
destinationParentNode.Folders.Add(folderToMove);
folderToMove.Parent = destinationParentNode;
folderToMove.Path = newFolderPath;
var stack = new Stack<SharpIdeFolder>();
stack.Push(folderToMove);
while (stack.Count > 0)
{
var current = stack.Pop();
foreach (var subfolder in current.Folders)
{
subfolder.Path = Path.Combine(current.Path, subfolder.Name);
stack.Push(subfolder);
}
foreach (var file in current.Files)
{
var oldPath = file.Path;
file.Path = Path.Combine(current.Path, file.Name);
await _fileChangedService.SharpIdeFileMoved(file, oldPath);
}
}
}
public async Task RenameDirectory(SharpIdeFolder folder, string renamedFolderName)

View File

@@ -6,7 +6,7 @@ using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.SolutionDiscovery;
public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode
public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode, IFileOrFolder
{
public required IExpandableSharpIdeNode Parent { get; set; }
public required string Path { get; set; }

View File

@@ -5,7 +5,7 @@ using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.SolutionDiscovery;
public class SharpIdeFolder : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode, IFolderOrProject
public class SharpIdeFolder : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode, IFolderOrProject, IFileOrFolder
{
public required IExpandableSharpIdeNode Parent { get; set; }
public required string Path { get; set; }

View File

@@ -22,6 +22,11 @@ public interface IFolderOrProject : IExpandableSharpIdeNode, IChildSharpIdeNode
public string Name { get; set; }
public string ChildNodeBasePath { get; }
}
public interface IFileOrFolder : IChildSharpIdeNode
{
public string Path { get; set; }
public string Name { get; set; }
}
public interface IChildSharpIdeNode
{
public IExpandableSharpIdeNode Parent { get; set; }