// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using Progmasoft.Tazel.Driver; using Progmasoft.Tazel.Dsl.Custom; using Progmasoft.Tazel.Dsl.Custom.Lexer; using Progmasoft.Tazel.Dsl.Environment; namespace Progmasoft.Tazel.Dsl.Envs; /// A value of tazel_envs.txt: a string, the output of a program, or the output of a shell line. public abstract record EnvValue { /// A string, as written. /// The string. public sealed record Text(string Value) : EnvValue; /// ("program" "argument"): a program started directly; the value is what it prints. /// The program and its arguments, each string one argument. public sealed record Program(IReadOnlyList Arguments) : EnvValue; /// s("line"): a line for /bin/sh, or cmd.exe on Windows; the value is what it prints. /// The line. public sealed record Shell(string Line) : EnvValue; } /// One entry of tazel_envs.txt, with where it is. /// Its name. /// Its value. /// The line it is on. public sealed record EnvEntry(string Name, EnvValue Value, int Line); /// /// Reads tazel_envs.txt with the lexer of tazel_custom.txt and tazel_options.txt: the same comments /// and strings, name: value; one after another, each value ended by ;. /// public static class TazelEnvsFile { /// The file, beside the MODULE.csx of a module. public static string FileName { get; } = "tazel_envs.txt"; /// The entries of , named tazel_envs.txt in messages. public static IReadOnlyList Parse(string text) => Parse(text, FileName); /// /// The entries of ; names it in messages. Its directives are /// resolved for context (this machine, built for itself, by default), and a quoted include is /// found beside path. /// public static IReadOnlyList Parse(string text, string file) => Parse(text, file, null, null); /// , with includes found beside . public static IReadOnlyList Parse(string text, string file, string? path) => Parse(text, file, path, null); /// , resolved for when it is not null. public static IReadOnlyList Parse(string text, string file, string? path, Configuration.ConfigurationContext? context) { ArgumentNullException.ThrowIfNull(text); var tokens = Configuration.ConfigurationResolver.Resolve(path ?? file, file, text, Configuration.ConfigurationFamily.Envs, context ?? Configuration.ConfigurationContext.ForThisMachine().ForItself()); return new Reader(tokens, text, file).Entries(); } private sealed class Reader(IReadOnlyList tokens, string text, string file) { private int _position; public List Entries() { var entries = new List(); var names = new Dictionary(StringComparer.Ordinal); while (_position < tokens.Count) { var name = Take(TokenKind.Name, "the name of a value"); if (names.TryGetValue(name.Text, out var first)) { throw Fail(Output.ErrorCodes.EnvsTwice, name, $"'{name.Text}' is set twice", "the second one", $"keep one of them; the first is on line {first.Line}", "Two values, one name. Tazel is not going to flip a coin."); } names[name.Text] = name; Take(TokenKind.Colon, $"':' after {name.Text}"); var value = Value(name); // The last value may end the file without a ';'. if (Peek is not null) { Take(TokenKind.Semicolon, $"';' after the value of {name.Text}"); } entries.Add(new EnvEntry(name.Text, value, name.Line)); } return entries; } private Token? Peek => _position < tokens.Count ? tokens[_position] : null; /// A string, s("line") or ("program" "argument"). private EnvValue Value(Token name) { if (Peek is { Kind: TokenKind.String } literal) { _position++; return new EnvValue.Text(literal.Text); } if (Peek is { Kind: TokenKind.Name, Text: "s" }) { _position++; return Shell(name); } if (Peek is { Kind: TokenKind.Open }) { return Program(name); } if (Peek is { } stranger) { throw Fail(Output.ErrorCodes.EnvsNotAValue, stranger, $"expected the value of {name.Text}", "this is not a value", $"a value is a string, a program or a shell line: {name.Text}: '0.1.0'; or {name.Text}: (\"git\" \"describe\"); or {name.Text}: s(`date`);", stranger.Kind == TokenKind.Name ? $"'{stranger.Text}' without quotes is a name, and names are not values. Quotes, please!" : "Tazel expected a value and got something else entirely."); } throw new CustomCommandsException(Diagnostic.After(Output.ErrorCodes.EnvsNoValue, text, file, tokens[_position - 1], $"expected the value of {name.Text}", "the value goes here", $"for example {name.Text}: '0.1.0';", "A name, a colon, and then... silence.")); } private EnvValue Shell(Token name) { var open = Take(TokenKind.Open, "'(' after s"); var strings = Strings(); Take(TokenKind.Close, "')' after the line of s(...)"); return strings.Count == 1 ? new EnvValue.Shell(strings[0]) : throw Fail(Output.ErrorCodes.EnvsShellOneLine, open, "s(...) takes one string, the whole line", "this s(...)", $"write one line: {name.Text}: s(`echo $HOME`);", "One shell, one line. It is not a group chat."); } private EnvValue Program(Token name) { var open = Take(TokenKind.Open, "'('"); var strings = Strings(); Take(TokenKind.Close, "')' after the strings of a program"); return strings.Count > 0 ? new EnvValue.Program(strings) : throw Fail(Output.ErrorCodes.EnvsEmptyProgram, open, "a program names at least itself", "these parentheses are empty", $"name the program first: {name.Text}: (\"rustc\" \"--version\");", "Empty parentheses: a program that forgot its own name."); } private List Strings() { var strings = new List(); while (Peek is { Kind: TokenKind.String } token) { _position++; strings.Add(token.Text); } return Peek is { Kind: TokenKind.Placeholder } placeholder ? throw Fail(Output.ErrorCodes.EnvsPlaceholder, placeholder, "a value of tazel_envs.txt takes no or <*>", "this placeholder", "write the argument itself, in quotes", "Nobody passes arguments to a file. It just sits there.") : strings; } private Token Take(TokenKind kind, string wanted) { if (Peek is { } token && token.Kind == kind) { _position++; return token; } var (code, label, help) = Missing(kind); if (_position > 0 && (Peek is null || kind is TokenKind.Semicolon or TokenKind.Colon)) { throw new CustomCommandsException(Diagnostic.After(code, text, file, tokens[_position - 1], $"expected {wanted}", label, help, Jokes.Missing(kind))); } throw Fail(code, Peek!, $"expected {wanted}", "not this", help, Jokes.Missing(kind)); } /// The code, the label of the gap, and the help for something missing. private static (string Code, string Label, string Help) Missing(TokenKind kind) => kind switch { TokenKind.Semicolon => (Output.ErrorCodes.EnvsNoSemicolon, "a ';' goes here", "end every value with ';': example-version: '0.1.0';"), TokenKind.Colon => (Output.ErrorCodes.EnvsNoColon, "a ':' goes here", "a name and its value are joined by ':': example-version: '0.1.0';"), TokenKind.Close => (Output.ErrorCodes.EnvsNoClose, "a ')' goes here", "close it with ')'"), TokenKind.Open => (Output.ErrorCodes.EnvsNoOpen, "something goes here", "open it with '(': s(`date`)"), _ => (Output.ErrorCodes.EnvsUnexpected, "something goes here", "an entry is: name: value;"), }; private CustomCommandsException Fail(string code, Token token, string message, string label, string help, string joke) => new(Diagnostic.AtToken(code, text, file, token, message, label, help, joke)); } } /// /// The values of one module's tazel_envs.txt, for . A program or shell line runs /// when its value is first asked for, once per command, with Tazel's environment in the root of the module; its output, /// less the line endings at its end, is the value. A program that fails stops the project file that asked. /// internal sealed class ModuleEnvironment : IEnvironmentValues { private readonly Dictionary _entries; private readonly ConcurrentDictionary> _values = new(StringComparer.Ordinal); private readonly string _root; private readonly string _shown; private ModuleEnvironment(string root, string shown, IReadOnlyList entries) { _root = root; _shown = shown; _entries = entries.ToDictionary(entry => entry.Name, StringComparer.Ordinal); } /// /// The values of the module at , or null when it has no tazel_envs.txt; its /// conditions see target as the platform built for (this machine's when null). /// public static ModuleEnvironment? Load(string root, string shown) => Load(root, shown, null, false); /// , its conditions read for , else this machine. public static ModuleEnvironment? Load(string root, string shown, Common.Platform? target) => Load(root, shown, target, false); /// /// ; with , for the main module, the /// layers of tazel_envs.txt around the root too. /// public static ModuleEnvironment? Load(string root, string shown, Common.Platform? target, bool inherit) { var context = Configuration.ConfigurationContext.ForThisMachine(); context = context with { Target = Configuration.SystemFacts.Of(target ?? Driver.Host.Platform) }; // The main module takes the layers of tazel_envs.txt around its root, a value of a higher layer replacing the // one of the same name; a module it depends on takes its own file alone, so it sees nothing of the user's or // the main module's. Every value runs in the root of its module. IEnumerable files; if (inherit) { try { files = Configuration.ConfigurationLayers.For(TazelEnvsFile.FileName, root).Select(layer => layer.File); } catch (Configuration.ConfigurationHomeException error) { throw new CustomCommandsException(error.Message, error); } } else { var own = Path.Join(root, TazelEnvsFile.FileName); files = File.Exists(own) ? [own] : []; } var entries = new Dictionary(StringComparer.Ordinal); var any = false; foreach (var file in files) { any = true; var name = file == Path.Join(root, TazelEnvsFile.FileName) ? shown : file; foreach (var entry in TazelEnvsFile.Parse(File.ReadAllText(file), name, file, context)) { entries[entry.Name] = entry; } } return any ? new ModuleEnvironment(root, shown, entries.Values.ToList()) : null; } public string? Get(string name) { if (!_entries.TryGetValue(name, out var entry)) { return null; } return _values.GetOrAdd(name, _ => new Lazy(() => Evaluate(entry), LazyThreadSafetyMode.ExecutionAndPublication)) .Value; } private string Evaluate(EnvEntry entry) { IReadOnlyList command; switch (entry.Value) { case EnvValue.Text text: return text.Value; case EnvValue.Shell shell: command = OperatingSystem.IsWindows() ? [System.Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe", "/d", "/s", "/c", $"\"{shell.Line}\""] : ["/bin/sh", "-c", shell.Line]; break; default: var program = ((EnvValue.Program)entry.Value).Arguments; var found = Host.Which(program[0]) ?? throw new DslException( $"{_shown}:{entry.Line}: the program '{program[0]}' of {entry.Name} is not found"); command = [found, .. program.Skip(1)]; break; } var result = Processes.CaptureAsync(command, _root, Processes.Inherited(), CancellationToken.None).GetAwaiter().GetResult(); if (result.ExitCode != 0) { throw new DslException( $"{_shown}:{entry.Line}: {entry.Name} ran {command[0]}, which failed with {result.ExitCode}: {result.Errors.Trim()}"); } return result.Output.TrimEnd('\r', '\n'); } }