76 lines
3.5 KiB
C#
76 lines
3.5 KiB
C#
using SharpIDE.Application.Features.SolutionDiscovery;
|
|
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
|
|
|
|
namespace SharpIDE.Application.Features.FileWatching;
|
|
|
|
public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpIdeSolutionModificationService)
|
|
{
|
|
private readonly SharpIdeSolutionModificationService _sharpIdeSolutionModificationService = sharpIdeSolutionModificationService;
|
|
|
|
public async Task RenameDirectory(SharpIdeFolder folder, string newDirectoryName)
|
|
{
|
|
var parentPath = Path.GetDirectoryName(folder.Path)!;
|
|
var newDirectoryPath = Path.Combine(parentPath, newDirectoryName);
|
|
Directory.Move(folder.Path, newDirectoryPath);
|
|
await _sharpIdeSolutionModificationService.RenameDirectory(folder, newDirectoryName);
|
|
}
|
|
|
|
public async Task CreateDirectory(IFolderOrProject parentNode, string newDirectoryName)
|
|
{
|
|
var newDirectoryPath = Path.Combine(parentNode.ChildNodeBasePath, newDirectoryName);
|
|
Directory.CreateDirectory(newDirectoryPath);
|
|
var newFolder = await _sharpIdeSolutionModificationService.AddDirectory(parentNode, newDirectoryName);
|
|
}
|
|
|
|
public async Task DeleteDirectory(SharpIdeFolder folder)
|
|
{
|
|
Directory.Delete(folder.Path, true);
|
|
await _sharpIdeSolutionModificationService.RemoveDirectory(folder);
|
|
}
|
|
|
|
public async Task DeleteFile(SharpIdeFile file)
|
|
{
|
|
File.Delete(file.Path);
|
|
await _sharpIdeSolutionModificationService.RemoveFile(file);
|
|
}
|
|
|
|
// TODO: Pass class/interface/enum type to create different templates
|
|
public async Task<SharpIdeFile> CreateCsFile(IFolderOrProject parentNode, string newFileName)
|
|
{
|
|
var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName);
|
|
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
|
|
var className = Path.GetFileNameWithoutExtension(newFileName);
|
|
var @namespace = NewFileTemplates.ComputeNamespace(parentNode);
|
|
var fileText = NewFileTemplates.CsharpClass(className, @namespace);
|
|
await File.WriteAllTextAsync(newFilePath, fileText);
|
|
var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText);
|
|
return sharpIdeFile;
|
|
}
|
|
|
|
public async Task<SharpIdeFile> CopyFile(IFolderOrProject destinationParentNode, string sourceFilePath, string newFileName)
|
|
{
|
|
var newFilePath = Path.Combine(GetFileParentNodePath(destinationParentNode), newFileName);
|
|
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
|
|
var fileContents = await File.ReadAllTextAsync(sourceFilePath);
|
|
File.Copy(sourceFilePath, newFilePath);
|
|
var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(destinationParentNode, newFilePath, newFileName, fileContents);
|
|
return sharpIdeFile;
|
|
}
|
|
|
|
public async Task<SharpIdeFile> MoveFile(IFolderOrProject destinationParentNode, SharpIdeFile fileToMove)
|
|
{
|
|
var newFilePath = Path.Combine(destinationParentNode.ChildNodeBasePath, fileToMove.Name);
|
|
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
|
|
File.Move(fileToMove.Path, newFilePath);
|
|
var sharpIdeFile = await _sharpIdeSolutionModificationService.MoveFile(destinationParentNode, fileToMove);
|
|
return sharpIdeFile;
|
|
}
|
|
|
|
private static string GetFileParentNodePath(IFolderOrProject parentNode) => parentNode switch
|
|
{
|
|
SharpIdeFolder folder => folder.Path,
|
|
SharpIdeProjectModel project => Path.GetDirectoryName(project.FilePath)!,
|
|
_ => throw new InvalidOperationException("Parent node must be a folder or project")
|
|
};
|
|
}
|