Files
Febe/tools/Febe.Schermate/Program.cs
T
Alby96andClaude Opus 5.5 be662f2d28 Correzioni della revisione di C6: installatore, CI senza jq, caratteri e schermate senza Node
Le 14 osservazioni della revisione di funzione/senza-terzi, ciascuna con la prova che l'avrebbe trovata.

Installatore: un aggiornamento senza /D= va nella cartella già installata (InstallLocation)
invece che in quella predefinita, che lasciava la vecchia copia. Una cartella senza permessi
non sembra più «Febe aperto». La dimensione in «App installate» conta solo i due file
dell'installatore. I file in sola lettura si sostituiscono e si tolgono. «Febe» in Esegui
all'avvio si toglie solo se avvia quella copia, non un Febe portatile. Febe parte alla fine
solo con «Fine» e la casella spuntata. /PROVA= rifiuta la radice di un disco.

CI: jq non c'era ed era un componente esterno. JSON letto e scritto con .gitea/scripts/json.sh
(json-campi.awk e json-stringa.awk sul lettore json.awk) per gitleaks, Trivy e la release.
Le credenziali del registro stanno solo in un DOCKER_CONFIG temporaneo della pubblicazione,
con plugin, contesti e contesto corrente del runner, tolto alla fine. Le prove del rilascio
girano in debian:trixie con Chromium, come la verifica, senza prove saltate. Ogni flusso usa
shell: bash.

Caratteri: tolti fonts-liberation e fonts-noto-color-emoji dall'immagine e dalle prove della
CI, perché Chromium non li chiede. Resta fonts-dejavu-core.

Icone: la prova della griglia legge davvero i percorsi SVG (tutti i comandi, archi e curve) e
i punti. La lettura dei nomi delle icone vede ogni forma con cui l'interfaccia li chiede.

Schermate: tools/schermate (Node) diventa tools/Febe.Schermate, in C# con ClientWebSocket e
System.Text.Json, nessun pacchetto, solo verso indirizzi di questo computer. Provato contro
una dimostrazione con Edge su Windows.

Documenti: CLAUDE.md, processo, guida, LEGGIMI e docker.md aggiornati. Righe nuove in
decisioni.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 16:04:39 +01:00

171 lines
7.3 KiB
C#

using System.Globalization;
namespace Febe.Schermate;
/// <summary>
/// Retakes the screenshots of docs/schermate from a running demo (spec §10.7):
/// <code>
/// 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 &lt;indirizzo&gt; &lt;file.png&gt; [larghezza] [altezza] [attesa-ms] [passo.js]
/// </code>
/// One screenshot at a time, each in its own Chromium: the browser and the demo share the computer.
/// </summary>
internal static class Program
{
internal const string Usage =
"Uso: febe-schermate [porta] [cartella]\n" +
" febe-schermate una <indirizzo> <file.png> [larghezza] [altezza] [attesa-ms] [passo.js]";
private static readonly TimeSpan PerShot = TimeSpan.FromSeconds(90);
public static async Task<int> 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}");
}
}
/// <summary>What the command line asks: the shots, where they go, and against which address.</summary>
/// <param name="Base">The demo («http://127.0.0.1:8480»); for a single shot, its whole address.</param>
/// <param name="Folder">Where the PNG files go.</param>
/// <param name="Shots">The shots, in order.</param>
/// <param name="Steps">The folder of the steps named by the list; a single shot names its step by path.</param>
internal sealed record Request(Uri Base, string Folder, IReadOnlyList<Shot> 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<string> 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<string> 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).");
/// <summary>The steps next to the program (copied at build), else in the sources.</summary>
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");
}
/// <summary>The nearest folder, from <paramref name="start"/> up, that holds Febe.slnx.</summary>
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;
}
}