using System.Globalization; namespace Febe.Schermate; /// /// Retakes the screenshots of docs/schermate from a running demo (spec §10.7): /// /// dotnet run --project src/Febe.Server -- --demo --porta 8490 (in another terminal) /// dotnet run --project tools/Febe.Schermate -- [porta] [cartella] (all of them) /// dotnet run --project tools/Febe.Schermate -- una <indirizzo> <file.png> [larghezza] [altezza] [attesa-ms] [passo.js] /// /// One screenshot at a time, each in its own Chromium: the browser and the demo share the computer. /// internal static class Program { internal const string Usage = "Uso: febe-schermate [porta] [cartella]\n" + " febe-schermate una [larghezza] [altezza] [attesa-ms] [passo.js]"; private static readonly TimeSpan PerShot = TimeSpan.FromSeconds(90); public static async Task Main(string[] args) { Request request; try { request = Request.Parse(args, Directory.GetCurrentDirectory()); } catch (ArgumentException ex) { await Console.Error.WriteLineAsync(ex.Message); await Console.Error.WriteLineAsync(Usage); return 2; } if (Screenshotter.FindChromium() is not { } chromium) { await Console.Error.WriteLineAsync("Chromium non trovato: indica il percorso in FEBE_CHROMIUM."); return 2; } Directory.CreateDirectory(request.Folder); var failed = 0; foreach (var shot in request.Shots) { Console.WriteLine($"== {shot.File}"); try { await TakeAsync(chromium, request, shot); } catch (Exception ex) when (ex is OperationCanceledException or InvalidOperationException or TimeoutException or IOException or System.Net.WebSockets.WebSocketException or ArgumentException) { failed++; Console.WriteLine($"non riuscita: {ex.Message}"); } } return failed == 0 ? 0 : 1; } private static async Task TakeAsync(string chromium, Request request, Shot shot) { using var timeout = new CancellationTokenSource(PerShot); var step = shot.Step is null ? null : await File.ReadAllTextAsync(request.StepPath(shot.Step), timeout.Token); await using var browser = await Screenshotter.StartAsync(chromium, shot.Width, shot.Height, Console.Out, timeout.Token); var png = await browser.TakeAsync(request.AddressOf(shot), shot.Width, shot.Height, TimeSpan.FromMilliseconds(shot.WaitMs), step, timeout.Token); var file = Path.Combine(request.Folder, shot.File); await File.WriteAllBytesAsync(file, png, timeout.Token); Console.WriteLine($"salvata {file}"); } } /// What the command line asks: the shots, where they go, and against which address. /// The demo («http://127.0.0.1:8480»); for a single shot, its whole address. /// Where the PNG files go. /// The shots, in order. /// The folder of the steps named by the list; a single shot names its step by path. internal sealed record Request(Uri Base, string Folder, IReadOnlyList Shots, string Steps) { public const int DefaultPort = 8480; public Uri AddressOf(Shot shot) => shot.Page.Length == 0 ? Base : new Uri(Base, shot.Page); public string StepPath(string step) => Path.IsPathFullyQualified(step) ? step : Path.Combine(Steps, step); public static Request Parse(IReadOnlyList args, string currentDirectory) { if (args.Count > 0 && args[0] == "una") { return One(args, currentDirectory); } if (args.Count > 2) { throw new ArgumentException("Troppi argomenti."); } var port = args.Count > 0 ? Port(args[0]) : DefaultPort; var folder = args.Count > 1 ? Path.GetFullPath(args[1], currentDirectory) : Path.Combine(RepositoryRoot(currentDirectory) ?? throw new ArgumentException("Fuori dal repository di Febe: indica la cartella delle schermate."), "docs", "schermate"); return new Request(new Uri($"http://127.0.0.1:{port.ToString(CultureInfo.InvariantCulture)}"), folder, Schermate.Shots.All, StepsFolder()); } private static Request One(IReadOnlyList args, string currentDirectory) { if (args.Count is < 3 or > 7) { throw new ArgumentException("«una» vuole l'indirizzo e il file, poi a scelta larghezza, altezza, attesa e passo."); } if (!Uri.TryCreate(args[1], UriKind.Absolute, out var address) || !Schermate.Shots.IsOnThisComputer(address)) { throw new ArgumentException("Solo l'interfaccia di Febe su questo computer (127.0.0.1, localhost o [::1])."); } var file = Path.GetFullPath(args[2], currentDirectory); if (!file.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("Il file della schermata è un .png."); } var shot = new Shot( string.Empty, Path.GetFileName(file), args.Count > 3 ? Size(args[3], "larghezza") : 1440, args.Count > 4 ? Size(args[4], "altezza") : 900, args.Count > 5 ? Wait(args[5]) : 3000, args.Count > 6 ? Path.GetFullPath(args[6], currentDirectory) : null); return new Request(address, Path.GetDirectoryName(file)!, [shot], StepsFolder()); } private static int Port(string text) => int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out var port) && port is >= 1 and <= 65535 ? port : throw new ArgumentException($"Porta non valida: {text}."); private static int Size(string text, string what) => int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out var size) && size is >= 200 and <= 8000 ? size : throw new ArgumentException($"{what} non valida: {text} (da 200 a 8000 punti)."); private static int Wait(string text) => int.TryParse(text, NumberStyles.None, CultureInfo.InvariantCulture, out var wait) && wait <= 60_000 ? wait : throw new ArgumentException($"Attesa non valida: {text} (millisecondi, al massimo 60000)."); /// The steps next to the program (copied at build), else in the sources. private static string StepsFolder() { var beside = Path.Combine(AppContext.BaseDirectory, "passi"); return Directory.Exists(beside) ? beside : Path.Combine(RepositoryRoot(AppContext.BaseDirectory) ?? AppContext.BaseDirectory, "tools", "Febe.Schermate", "passi"); } /// The nearest folder, from up, that holds Febe.slnx. internal static string? RepositoryRoot(string start) { for (var folder = new DirectoryInfo(start); folder is not null; folder = folder.Parent) { if (File.Exists(Path.Combine(folder.FullName, "Febe.slnx"))) { return folder.FullName; } } return null; } }