refactor file creation for a project parent
This commit is contained in:
@@ -26,13 +26,20 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
|
||||
await _sharpIdeSolutionModificationService.RemoveFile(file);
|
||||
}
|
||||
|
||||
public async Task CreateCsFile(SharpIdeFolder parentFolder, string newFileName)
|
||||
public async Task CreateCsFile(IFolderOrProject parentNode, string newFileName)
|
||||
{
|
||||
var newFilePath = Path.Combine(parentFolder.Path, newFileName);
|
||||
var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName);
|
||||
var className = Path.GetFileNameWithoutExtension(newFileName);
|
||||
var @namespace = NewFileTemplates.ComputeNamespace(parentFolder);
|
||||
var @namespace = NewFileTemplates.ComputeNamespace(parentNode);
|
||||
var fileText = NewFileTemplates.CsharpClass(className, @namespace);
|
||||
await File.WriteAllTextAsync(newFilePath, fileText);
|
||||
await _sharpIdeSolutionModificationService.CreateFile(parentFolder, newFileName, fileText);
|
||||
await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText);
|
||||
}
|
||||
|
||||
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")
|
||||
};
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ public static class NewFileTemplates
|
||||
return text;
|
||||
}
|
||||
|
||||
public static string ComputeNamespace(SharpIdeFolder folder)
|
||||
public static string ComputeNamespace(IFolderOrProject folder)
|
||||
{
|
||||
var names = new List<string>();
|
||||
IFolderOrProject? current = folder;
|
||||
|
||||
@@ -60,11 +60,10 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateFile(SharpIdeFolder parentFolder, string fileName, string contents)
|
||||
public async Task CreateFile(IFolderOrProject parentNode, string newFilePath, string fileName, string contents)
|
||||
{
|
||||
var newFilePath = Path.Combine(parentFolder.Path, fileName);
|
||||
var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentFolder, []);
|
||||
parentFolder.Files.Add(sharpIdeFile);
|
||||
var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentNode, []);
|
||||
parentNode.Files.Add(sharpIdeFile);
|
||||
SolutionModel.AllFiles.Add(sharpIdeFile);
|
||||
await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, contents);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user