using System.Xml.Linq;
namespace Khaemhat.Core.Tests.Architecture;
/// Reads the solution's project files from the repository, without loading the projects.
internal static class RepositoryLayout
{
private static readonly Lazy RootPath = new(FindRoot);
/// The repository root (the folder holding Khaemhat.sln).
public static string Root => RootPath.Value;
/// Every project file under src/, tests/ and tools/, by project name.
public static IReadOnlyDictionary Projects()
{
var result = new Dictionary(StringComparer.Ordinal);
foreach (var folder in new[] { "src", "tests", "tools" })
{
var path = Path.Combine(Root, folder);
if (!Directory.Exists(path))
{
continue;
}
foreach (var file in Directory.EnumerateFiles(path, "*.csproj", SearchOption.AllDirectories))
{
if (!IsBuildOutput(file))
{
result[Path.GetFileNameWithoutExtension(file)] = file;
}
}
}
return result;
}
/// Names of the projects referenced by a project file.
public static IReadOnlySet ProjectReferences(string projectFile) =>
Includes(projectFile, "ProjectReference")
.Select(include => Path.GetFileNameWithoutExtension(include.Replace('\\', '/')))
.ToHashSet(StringComparer.Ordinal);
/// Package ids referenced by a project or props file.
public static IReadOnlySet PackageReferences(string file) =>
Includes(file, "PackageReference").ToHashSet(StringComparer.OrdinalIgnoreCase);
/// Every MSBuild file (projects, props, targets) of the repository that could declare packages.
public static IEnumerable MsBuildFiles() =>
Directory.EnumerateFiles(Root, "*.*", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".props", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".targets", StringComparison.OrdinalIgnoreCase))
.Where(f => !IsBuildOutput(f) && !IsUnderIgnoredFolder(f));
/// The path of a file relative to the repository root, with forward slashes.
public static string Relative(string file) => Path.GetRelativePath(Root, file).Replace('\\', '/');
private static IEnumerable Includes(string file, string element) =>
XDocument.Load(file).Descendants()
.Where(e => e.Name.LocalName == element)
.Select(e => (string?)e.Attribute("Include") ?? (string?)e.Attribute("Update") ?? string.Empty)
.Where(s => s.Length > 0);
private static bool IsBuildOutput(string path)
{
var p = path.Replace('\\', '/');
return p.Contains("/bin/", StringComparison.Ordinal) || p.Contains("/obj/", StringComparison.Ordinal);
}
private static bool IsUnderIgnoredFolder(string path)
{
var relative = Relative(path);
return relative.StartsWith(".git/", StringComparison.Ordinal)
|| relative.StartsWith(".claude/", StringComparison.Ordinal)
|| relative.StartsWith("out/", StringComparison.Ordinal);
}
private static string FindRoot()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "Khaemhat.sln")))
{
dir = dir.Parent;
}
return dir?.FullName ?? throw new InvalidOperationException("Repository root (Khaemhat.sln) not found.");
}
}