// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Progmasoft.Tazel.Project; /// Where the project files of a module are. internal static class ModuleFiles { /// The file at the root of a module. public const string ModuleFile = "MODULE.csx"; /// The directory below the root where Tazel keeps what it makes. public const string StateDirectory = ".tazel"; /// The name of a package's project file, the only one: case-sensitive on every system. public const string BuildFile = "BUILD.csx"; /// The root of the module is in: the nearest directory with a MODULE.csx. public static string FindRoot(string directory) => RootOf(directory) ?? throw new ProjectException($"there is no {ModuleFile} in {Path.GetFullPath(directory)} or above it"); /// The root of the module is in, or null when it is in none. public static string? RootOf(string directory) { for (var current = new DirectoryInfo(Path.GetFullPath(directory)); current is not null; current = current.Parent) { if (File.Exists(Path.Combine(current.FullName, ModuleFile))) { return current.FullName; } } return null; } /// The project file of the package at , or null when it has none. public static string? BuildFileIn(string directory) { // The lookup is case-sensitive on every system, so build.csx is not a project file anywhere. var names = new HashSet( Directory.EnumerateFiles(directory).Select(Path.GetFileName).OfType(), StringComparer.Ordinal); if (names.Contains(BuildFile)) { return Path.Combine(directory, BuildFile); } // A file of an earlier name would leave its targets out without a word; it is refused instead. if (CsxMigration.LegacyNames.FirstOrDefault(names.Contains) is { } legacy) { throw new ProjectException( $"{Path.Combine(directory, legacy)}: a project file is named BUILD.csx and nothing else; rename it, or let " + "tazel limited-time C# rename it and write its Depends as labels"); } return null; } /// /// Every package of the module at , by its path: directories with a project file, less /// hidden ones, the directories of other modules and what its .tazelignore files ignore. An ignored /// directory is not entered. /// public static IReadOnlyList<(string Path, string File)> Packages(string root) { var found = new List<(string, string)>(); var ignore = Ignore.IgnoreRules.For(root); Visit(root, ""); return found.OrderBy(entry => entry.Item1, StringComparer.Ordinal).ToList(); void Visit(string directory, string path) { if (BuildFileIn(directory) is { } file) { found.Add((path, file)); } foreach (var child in new DirectoryInfo(directory).EnumerateDirectories()) { var childPath = path.Length == 0 ? child.Name : $"{path}/{child.Name}"; if (child.Name.StartsWith('.') || child.LinkTarget is not null || File.Exists(Path.Combine(child.FullName, ModuleFile)) || ignore.Ignored(childPath, directory: true)) { continue; } Visit(child.FullName, childPath); } } } /// /// The package at of the module at , whatever its .tazelignore files /// say, as an explicit label names it; null when there is none there: no project file, a hidden or linked /// directory on the way, or another module's. /// public static (string Path, string File)? PackageAt(string root, string path) { var directory = root; foreach (var part in path.Length == 0 ? [] : path.Split('/')) { if (part.Length == 0 || part.StartsWith('.')) { return null; } directory = Path.Combine(directory, part); if (!Directory.Exists(directory) || new DirectoryInfo(directory).LinkTarget is not null || File.Exists(Path.Combine(directory, ModuleFile))) { return null; } } return BuildFileIn(directory) is { } file ? (path, file) : null; } }