// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.Linq; namespace Progmasoft.Tazel.Dsl.Custom; /// What is wrong with a tazel_custom.txt or tazel_options.txt, with where it is. public sealed class CustomCommandsException : Exception { /// A problem of the file. public CustomCommandsException(string message) : base(message) { } /// A problem of the file, with all it takes to show it well. public CustomCommandsException(Diagnostic diagnostic) : base((diagnostic ?? throw new ArgumentNullException(nameof(diagnostic))).Plain) { Diagnostic = diagnostic; } /// The place, the line, the help and the joke, when the problem has a place. public Diagnostic? Diagnostic { get; } /// Nothing to say. public CustomCommandsException() { } /// A problem and its cause. public CustomCommandsException(string message, Exception inner) : base(message, inner) { } } /// The constants of tazel custom. public static class CustomCommandFiles { /// The file that holds the commands of tazel custom, in the directory they run in. public static string FileName { get; } = "tazel_custom.txt"; /// The most words tazel custom takes, those of the name of the command included. public static int MaxArguments { get; } = 191; /// Ends the name of a custom command on the command line; what follows it is arguments. public static string NameEnd { get; } = "--"; } /// One argument of a program in a custom command. public abstract record CustomArgument { /// A string of the file. /// The string. public sealed record Text(string Value) : CustomArgument; /// <N>: the Nth argument of the command line, from 1. /// N. public sealed record Position(int Number) : CustomArgument; /// <*>: all the arguments of the command line. public sealed record All : CustomArgument; } /// One step of a custom command. public abstract record CustomStep { /// /// A program started directly, with these arguments and no shell between; the first is the program, a string. /// /// The arguments. public sealed record Program(IReadOnlyList Arguments) : CustomStep { /// What to start, given the arguments of the command line. public IReadOnlyList Expanded(IReadOnlyList given) { ArgumentNullException.ThrowIfNull(given); return Arguments.SelectMany(argument => argument switch { CustomArgument.Text text => [text.Value], CustomArgument.Position position => [given[position.Number - 1]], _ => given, }).ToList(); } } /// A whole command line for the shell of the host: CMD on Windows, sh elsewhere. /// The line. public sealed record Shell(string Line) : CustomStep { /// What to start on this machine; the line reaches the shell unchanged. public IReadOnlyList Started(bool windows) => windows ? ["cmd.exe", "/d", "/s", "/c", $"\"{Line}\""] : ["sh", "-c", Line]; } } /// A named command of tazel_custom.txt: steps that run in order until one fails. /// The words of its name, joined by one space. /// Its steps. public sealed record CustomCommand(string Name, IReadOnlyList Steps) { /// The words of . public IReadOnlyList Words => Name.Split(' '); /// The highest <N>: the arguments the command cannot do without. public int Required => Placeholders.OfType().Select(p => p.Number).DefaultIfEmpty(0).Max(); /// Whether <*> takes any number of arguments. public bool TakesAll => Placeholders.OfType().Any(); private IEnumerable Placeholders => Steps.OfType().SelectMany(program => program.Arguments); } /// Finds the command a command line names. public static class CustomCommandMatching { /// /// The command that name, with the arguments after its name. Names may start with other /// names, so the longest one that fits wins. The first -- stops the name; everything after it is an argument. /// public static (CustomCommand Command, IReadOnlyList Arguments)? Matching( this IReadOnlyList commands, IReadOnlyList words) { ArgumentNullException.ThrowIfNull(commands); ArgumentNullException.ThrowIfNull(words); var end = words.ToList().IndexOf(CustomCommandFiles.NameEnd); var named = end < 0 ? words.ToList() : words.Take(end).ToList(); var after = end < 0 ? [] : words.Skip(end + 1).ToList(); var best = commands .Where(command => command.Words.Count <= named.Count && named.Take(command.Words.Count).SequenceEqual(command.Words)) .MaxBy(command => command.Words.Count); return best is null ? null : (best, named.Skip(best.Words.Count).Concat(after).ToList()); } }