rework mapping

This commit is contained in:
Matt Parker
2025-08-17 14:18:25 +10:00
parent 5bd6b64019
commit 7f01df7369
5 changed files with 95 additions and 73 deletions

View File

@@ -1,9 +1,19 @@
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using System.Diagnostics.CodeAnalysis;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.SolutionDiscovery;
public class SharpIdeFile : ISharpIdeNode
public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode
{
public required IExpandableSharpIdeNode Parent { get; set; }
public required string Path { get; set; }
public required string Name { get; set; }
[SetsRequiredMembers]
internal SharpIdeFile(string fullPath, string name, IExpandableSharpIdeNode parent)
{
Path = fullPath;
Name = name;
Parent = parent;
}
}

View File

@@ -1,12 +1,29 @@
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using System.Diagnostics.CodeAnalysis;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.SolutionDiscovery;
public class SharpIdeFolder : ISharpIdeNode
public class SharpIdeFolder : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode
{
public required IExpandableSharpIdeNode Parent { get; set; }
public required string Path { get; set; }
public required string Name { get; set; }
public required List<SharpIdeFile> Files { get; set; }
public required List<SharpIdeFolder> Folders { get; set; }
public bool Expanded { get; set; }
[SetsRequiredMembers]
public SharpIdeFolder(DirectoryInfo folderInfo, IExpandableSharpIdeNode parent)
{
Parent = parent;
Path = folderInfo.FullName;
Name = folderInfo.Name;
Files = folderInfo.GetFiles(this);
Folders = this.GetSubFolders(this);
}
public SharpIdeFolder()
{
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.SolutionDiscovery;
@@ -9,22 +10,23 @@ public static class TreeMapperV2
return folder.Files
.Concat(folder.Folders.SelectMany(sub => sub.GetAllFiles()));
}
public static List<SharpIdeFolder> GetSubFolders(string csprojectPath)
public static List<SharpIdeFolder> GetSubFolders(string csprojectPath, SharpIdeProjectModel sharpIdeProjectModel)
{
var projectDirectory = Path.GetDirectoryName(csprojectPath)!;
var rootFolder = new SharpIdeFolder
{
Parent = sharpIdeProjectModel,
Path = projectDirectory,
Name = null!,
Files = [],
Folders = []
};
var subFolders = rootFolder.GetSubFolders();
var subFolders = rootFolder.GetSubFolders(sharpIdeProjectModel);
return subFolders;
}
private static readonly string[] _excludedFolders = ["bin", "obj", "node_modules"];
public static List<SharpIdeFolder> GetSubFolders(this SharpIdeFolder folder)
public static List<SharpIdeFolder> GetSubFolders(this SharpIdeFolder folder, IExpandableSharpIdeNode parent)
{
var directoryInfo = new DirectoryInfo(folder.Path);
ConcurrentBag<SharpIdeFolder> subFolders = [];
@@ -45,33 +47,20 @@ public static class TreeMapperV2
Parallel.ForEach(subFolderInfos, subFolderInfo =>
{
var subFolder = new SharpIdeFolder
{
Path = subFolderInfo.FullName,
Name = subFolderInfo.Name,
Files = GetFiles(subFolderInfo),
Folders = new SharpIdeFolder
{
Path = subFolderInfo.FullName,
Name = subFolderInfo.Name,
Files = [],
Folders = []
}.GetSubFolders()
};
var subFolder = new SharpIdeFolder(subFolderInfo, parent);
subFolders.Add(subFolder);
});
return subFolders.ToList();
}
public static List<SharpIdeFile> GetFiles(string csprojectPath)
public static List<SharpIdeFile> GetFiles(string csprojectPath, SharpIdeProjectModel sharpIdeProjectModel)
{
var projectDirectory = Path.GetDirectoryName(csprojectPath)!;
var directoryInfo = new DirectoryInfo(projectDirectory);
return GetFiles(directoryInfo);
return GetFiles(directoryInfo, sharpIdeProjectModel);
}
public static List<SharpIdeFile> GetFiles(DirectoryInfo directoryInfo)
public static List<SharpIdeFile> GetFiles(this DirectoryInfo directoryInfo, IExpandableSharpIdeNode parent)
{
List<FileInfo> fileInfos;
try
@@ -83,10 +72,11 @@ public static class TreeMapperV2
return [];
}
return fileInfos.Select(f => new SharpIdeFile
return fileInfos.Select(f => new SharpIdeFile(f.FullName, f.Name, parent)
{
Path = f.FullName,
Name = f.Name
Name = f.Name,
Parent = parent
}).ToList();
}
}

View File

@@ -1,37 +1,84 @@
using System.Threading.Channels;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Channels;
using Microsoft.Build.Evaluation;
using SharpIDE.Application.Features.Evaluation;
namespace SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
public interface ISharpIdeNode;
public class SharpIdeSolutionModel : ISharpIdeNode
public interface IExpandableSharpIdeNode
{
public bool Expanded { get; set; }
}
public interface IChildSharpIdeNode
{
public IExpandableSharpIdeNode Parent { get; set; }
}
public class SharpIdeSolutionModel : ISharpIdeNode, IExpandableSharpIdeNode
{
public required string Name { get; set; }
public required string FilePath { get; set; }
public required List<SharpIdeProjectModel> Projects { get; set; }
public required List<SharpIdeSolutionFolder> Folders { get; set; }
public required HashSet<SharpIdeProjectModel> AllProjects { get; set; }
public bool Expanded { get; set; }
[SetsRequiredMembers]
internal SharpIdeSolutionModel(string solutionFilePath, IntermediateSolutionModel intermediateModel)
{
var solutionName = Path.GetFileName(solutionFilePath);
AllProjects = [];
Name = solutionName;
FilePath = solutionFilePath;
Projects = intermediateModel.Projects.Select(s => new SharpIdeProjectModel(s, AllProjects, this)).ToList();
Folders = intermediateModel.SolutionFolders.Select(s => new SharpIdeSolutionFolder(s, AllProjects, this)).ToList();
}
}
public class SharpIdeSolutionFolder : ISharpIdeNode
public class SharpIdeSolutionFolder : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode
{
public required string Name { get; set; }
public required List<SharpIdeSolutionFolder> Folders { get; set; }
public required List<SharpIdeProjectModel> Projects { get; set; }
public required List<SharpIdeFile> Files { get; set; }
public bool Expanded { get; set; }
public required IExpandableSharpIdeNode Parent { get; set; }
[SetsRequiredMembers]
internal SharpIdeSolutionFolder(IntermediateSlnFolderModel intermediateModel, HashSet<SharpIdeProjectModel> allProjects, IExpandableSharpIdeNode parent)
{
Name = intermediateModel.Model.Name;
Parent = parent;
Files = intermediateModel.Files.Select(s => new SharpIdeFile(s.FullPath, s.Name, this)).ToList();
Folders = intermediateModel.Folders.Select(x => new SharpIdeSolutionFolder(x, allProjects, this)).ToList();
Projects = intermediateModel.Projects.Select(x => new SharpIdeProjectModel(x, allProjects, this)).ToList();
}
}
public class SharpIdeProjectModel : ISharpIdeNode
public class SharpIdeProjectModel : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode
{
public required string Name { get; set; }
public required string FilePath { get; set; }
public required List<SharpIdeFolder> Folders { get; set; }
public required List<SharpIdeFile> Files { get; set; }
public bool Expanded { get; set; }
public required IExpandableSharpIdeNode Parent { get; set; }
public bool Running { get; set; }
public CancellationTokenSource? RunningCancellationTokenSource { get; set; }
public required Task<Project> MsBuildEvaluationProjectTask { get; set; }
[SetsRequiredMembers]
internal SharpIdeProjectModel(IntermediateProjectModel projectModel, HashSet<SharpIdeProjectModel> allProjects, IExpandableSharpIdeNode parent)
{
Parent = parent;
Name = projectModel.Model.ActualDisplayName;
FilePath = projectModel.FullFilePath;
Files = TreeMapperV2.GetFiles(projectModel.FullFilePath, this);
Folders = TreeMapperV2.GetSubFolders(projectModel.FullFilePath, this);
MsBuildEvaluationProjectTask = ProjectEvaluation.GetProject(projectModel.FullFilePath);
allProjects.Add(this);
}
public Project MsBuildEvaluationProject => MsBuildEvaluationProjectTask.IsCompletedSuccessfully
? MsBuildEvaluationProjectTask.Result
: throw new InvalidOperationException("Do not attempt to access the MsBuildEvaluationProject before it has been loaded");

View File

@@ -11,53 +11,11 @@ public static class VsPersistenceMapper
// This intermediate model is pretty much useless, but I have left it around as we grab the project nodes with it, which we might use later.
var intermediateModel = await IntermediateMapper.GetIntermediateModel(solutionFilePath, cancellationToken);
var solutionName = Path.GetFileName(solutionFilePath);
var allProjects = new HashSet<SharpIdeProjectModel>();
var solutionModel = new SharpIdeSolutionModel
{
Name = solutionName,
FilePath = solutionFilePath,
Projects = intermediateModel.Projects.Select(s => GetSharpIdeProjectModel(s, allProjects)).ToList(),
AllProjects = allProjects,
Folders = intermediateModel.SolutionFolders.Select(s => new SharpIdeSolutionFolder
{
Name = s.Model.Name,
Files = s.Files.Select(GetSharpIdeFile).ToList(),
Folders = s.Folders.Select(x => GetSharpIdeSolutionFolder(x, allProjects)).ToList(),
Projects = s.Projects.Select(x => GetSharpIdeProjectModel(x, allProjects)).ToList()
}).ToList(),
};
var solutionModel = new SharpIdeSolutionModel(solutionFilePath, intermediateModel);
timer.Stop();
Console.WriteLine($"Solution model fully created in {timer.ElapsedMilliseconds} ms");
return solutionModel;
}
private static SharpIdeProjectModel GetSharpIdeProjectModel(IntermediateProjectModel projectModel, HashSet<SharpIdeProjectModel> allProjects)
{
var project = new SharpIdeProjectModel
{
Name = projectModel.Model.ActualDisplayName,
FilePath = projectModel.FullFilePath,
Files = TreeMapperV2.GetFiles(projectModel.FullFilePath),
Folders = TreeMapperV2.GetSubFolders(projectModel.FullFilePath),
MsBuildEvaluationProjectTask = ProjectEvaluation.GetProject(projectModel.FullFilePath)
};
allProjects.Add(project);
return project;
}
private static SharpIdeSolutionFolder GetSharpIdeSolutionFolder(IntermediateSlnFolderModel folderModel, HashSet<SharpIdeProjectModel> allProjects) => new SharpIdeSolutionFolder()
{
Name = folderModel.Model.Name,
Files = folderModel.Files.Select(GetSharpIdeFile).ToList(),
Folders = folderModel.Folders.Select(s => GetSharpIdeSolutionFolder(s, allProjects)).ToList(),
Projects = folderModel.Projects.Select(s => GetSharpIdeProjectModel(s, allProjects)).ToList()
};
private static SharpIdeFile GetSharpIdeFile(IntermediateSlnFolderFileModel fileModel) => new SharpIdeFile
{
Path = fileModel.FullPath,
Name = fileModel.Name
};
}