// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using Progmasoft.Tazel.Dsl.Build.Prelude; namespace Progmasoft.Tazel.Project; /// What tazel limited-time C# changed, or would have changed and could not. internal sealed record MigrationResult(IReadOnlyList Renamed, IReadOnlyList Rewritten, IReadOnlyList Problems); /// /// tazel limited-time C#, until Tazel 1.0.0: renames the project files of earlier names to BUILD.csx /// and writes the targets that Depends names by their variables as labels. It reads the files with Roslyn and /// runs none of them. Everything is checked before anything is written, so a problem changes no file; a second run /// finds nothing to do. /// internal static class CsxMigration { /// The names a package's project file had before only BUILD.csx was one. public static IReadOnlyList LegacyNames { get; } = ["BUILD.tzl", "BUILD.tazel", "BUILD", "Tazel.build"]; // The families whose declarations return a Target, and so may be named by a variable in Depends. private static readonly HashSet Families = new(StringComparer.Ordinal) { "CC", "ObjectiveC", "Zig" }; /// Migrates every package below , in every module there. public static MigrationResult Run(string directory) => Run(directory, write: true); /// Migrates every package below , or with false only says what it would do. public static MigrationResult Run(string directory, bool write) { var start = Path.GetFullPath(directory); var problems = new List(); var plans = new List(); foreach (var packageDirectory in Directories(start)) { if (PlanOf(start, packageDirectory, problems) is { } plan) { plans.Add(plan); } } if (problems.Count == 0 && write) { plans.ForEach(Apply); } return Result(start, plans, problems); } // What becomes of one package: Legacy is the file to remove, or null when BUILD.csx is rewritten in place. private sealed record Plan(string? Legacy, string Target, string Text, bool Changed); // The plan of one directory, or null when it has nothing to change or a problem was added. private static Plan? PlanOf(string start, string packageDirectory, List problems) { var names = new HashSet( Directory.EnumerateFiles(packageDirectory).Select(Path.GetFileName).OfType(), StringComparer.Ordinal); var legacy = LegacyNames.Where(names.Contains).ToList(); var shown = Shown(start, packageDirectory); var csx = Path.Combine(packageDirectory, "BUILD.csx"); if (legacy.Count > 1) { problems.Add($"{shown}: {string.Join(" and ", legacy)} are both here; keep one, and run this again"); return null; } var source = legacy.Count == 1 ? Path.Combine(packageDirectory, legacy[0]) : csx; if (!File.Exists(source)) { return null; } var text = File.ReadAllText(source); var rewritten = Rewrite(text, Package(packageDirectory), Shown(start, source), problems); if (rewritten is null) { return null; } if (legacy.Count == 0) { return rewritten == text ? null : new Plan(null, csx, rewritten, Changed: true); } if (!names.Contains("BUILD.csx")) { return new Plan(source, csx, rewritten, Changed: true); } // What an interrupted run leaves: BUILD.csx already holds what this file becomes. if (File.ReadAllText(csx) == rewritten) { return new Plan(source, csx, rewritten, Changed: false); } problems.Add($"{shown}: {legacy[0]} and BUILD.csx are both here; keep one, and run this again"); return null; } private static void Apply(Plan plan) { if (plan.Changed) { var partial = plan.Target + ".partial"; File.WriteAllText(partial, plan.Text, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); File.Move(partial, plan.Target, overwrite: plan.Legacy is null); } if (plan.Legacy is not null) { File.Delete(plan.Legacy); } } private static MigrationResult Result(string start, List plans, List problems) => new(plans.Where(plan => plan.Legacy is not null).Select(plan => $"{Shown(start, plan.Legacy!)} -> BUILD.csx").ToList(), plans.Where(plan => plan.Legacy is null).Select(plan => Shown(start, plan.Target)).ToList(), problems); // The directories below start, without hidden ones (.tazel, .git), the stores of fetched modules, or links. private static IEnumerable Directories(string start) { var pending = new Stack([start]); while (pending.Count > 0) { var directory = pending.Pop(); yield return directory; foreach (var child in new DirectoryInfo(directory).EnumerateDirectories().OrderByDescending(d => d.Name, StringComparer.Ordinal)) { if (!child.Name.StartsWith('.') && child.LinkTarget is null) { pending.Push(child.FullName); } } } } // The package path of a directory: from the nearest MODULE.csx at or above it. private static string Package(string packageDirectory) { var root = ModuleFiles.FindRoot(packageDirectory); var relative = Path.GetRelativePath(root, packageDirectory).Replace('\\', '/'); return relative == "." ? "" : relative; } private static string Shown(string start, string path) { var relative = Path.GetRelativePath(start, path).Replace('\\', '/'); return relative == "." ? "./" : relative; } /// /// with every target that Depends names by a variable named by its label, and the /// variables nothing else uses left out; null, with the reasons in , when a reference /// cannot be resolved. /// internal static string? Rewrite(string text, string package, string shown, List problems) { var options = ScriptHost.BaseOptions.WithFilePath(shown).WithFileEncoding(Encoding.UTF8); var compilation = CSharpScript.Create(text, options, typeof(BuildGlobals)).GetCompilation(); var tree = compilation.SyntaxTrees.Single(); var model = compilation.GetSemanticModel(tree); var root = tree.GetRoot(); var changes = new List(); var replaced = new Dictionary(SymbolEqualityComparer.Default); var before = problems.Count; foreach (var element in DependsElements(root)) { if (Label(element, model, package, shown, problems) is { } label) { changes.Add(new TextChange(element.Span, Literal(label.Text))); replaced[label.Symbol] = replaced.GetValueOrDefault(label.Symbol) + 1; } } if (problems.Count > before) { return null; } changes.AddRange(UnusedDeclarations(root, model, replaced)); return changes.Count == 0 ? text : SourceText.From(text).WithChanges(changes).ToString(); } // The label an element of Depends becomes, with its variable; null for an element that is no Target, or when it // cannot be resolved, with the reason added to problems. private static (string Text, ISymbol Symbol)? Label( ExpressionSyntax element, SemanticModel model, string package, string shown, List problems) { var type = model.GetTypeInfo(element).Type; if (type is null || type.SpecialType == SpecialType.System_String || type.Name != "Target") { return null; } if (element is IdentifierNameSyntax identifier && model.GetSymbolInfo(identifier).Symbol is { } symbol && DeclaredName(symbol) is { } name) { return ($"//{package}:{name}", symbol); } var line = element.GetLocation().GetLineSpan().StartLinePosition.Line + 1; problems.Add($"{shown}:{line}: Depends names '{element}', which is not a variable a declaration of this " + "file set; write the label of the target instead"); return null; } // A variable that only Depends used is left out: its declaration stays, as a statement of its own. private static IEnumerable UnusedDeclarations(SyntaxNode root, SemanticModel model, Dictionary replaced) { foreach (var (symbol, count) in replaced) { var uses = root.DescendantNodes().OfType() .Count(name => name.Identifier.ValueText == symbol.Name && SymbolEqualityComparer.Default.Equals(model.GetSymbolInfo(name).Symbol, symbol)); if (uses == count && Declarator(symbol) is { Parent: VariableDeclarationSyntax { Variables.Count: 1 } declaration } declarator) { yield return new TextChange(TextSpan.FromBounds(declaration.SpanStart, declarator.Initializer!.Value.SpanStart), ""); } } } // The elements of every Depends = [...] (and Depends = new() {...}) in object initializers. private static IEnumerable DependsElements(SyntaxNode root) => root.DescendantNodes().OfType() .Where(assignment => assignment.Left is IdentifierNameSyntax { Identifier.ValueText: "Depends" }) .SelectMany(assignment => assignment.Right is CollectionExpressionSyntax collection ? collection.Elements.OfType().Select(e => e.Expression) : []); // The name in the first argument of the declaration a variable was set to: CC.StaticLibrary("math", ...). private static string? DeclaredName(ISymbol symbol) { if (Declarator(symbol)?.Initializer?.Value is not InvocationExpressionSyntax { Expression: MemberAccessExpressionSyntax { Expression: IdentifierNameSyntax family }, ArgumentList.Arguments: [{ Expression: LiteralExpressionSyntax literal }, ..], } || !Families.Contains(family.Identifier.ValueText) || !literal.IsKind(SyntaxKind.StringLiteralExpression)) { return null; } return literal.Token.ValueText; } private static VariableDeclaratorSyntax? Declarator(ISymbol symbol) => symbol.DeclaringSyntaxReferences.Select(reference => reference.GetSyntax()).OfType().FirstOrDefault(); private static string Literal(string text) => SymbolDisplay.FormatLiteral(text, quote: true); }