// What the newest release offers (old Setup.cs Release): Forgejo's release list at triforceproject.com, newest first,
// pre-releases included, with GitHub's field names. TRIFORCE_RELEASE_API replaces the address (testing against a local copy).
using System.Net;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace Triforce.Logic;
public sealed record ReleaseAsset(string Name, string? Url, long Size);
/// One "New in <version>" item on Home: a title and one line (DESIGN 6 Home, 7).
public sealed record NewInItem(string Title, string Text);
public sealed class Release
{
public string Tag { get; set; } = "";
public string Name { get; set; } = "";
/// The release notes (Markdown).
public string Body { get; set; } = "";
public bool Prerelease { get; set; }
public DateTimeOffset? Published { get; set; }
/// The release's own page ("All changes").
public string? PageUrl { get; set; }
public IReadOnlyList Assets { get; set; } = Array.Empty();
public string? SetupUrl { get; set; }
public long SetupSize { get; set; }
public string? ToolsUrl { get; set; }
public long ToolsSize { get; set; }
/// ((Triforce) the tools in parts, when the release has them: ToolsInstaller.)
public string? ToolsCodeUrl { get; set; }
public long ToolsCodeSize { get; set; }
public string? ToolsBaseUrl { get; set; }
public long ToolsBaseSize { get; set; }
/// Linux: triforce-linux.txt when the release carries one (none does today: LinuxUpdate).
public string? LinuxListUrl { get; set; }
/// Linux: the AppImage (Triforce-<tag>[-linux-test]-x86_64.AppImage).
public string? LinuxAppImageUrl { get; set; }
public long LinuxAppImageSize { get; set; }
/// The first three "New in" items of the notes (ReleaseNotes.NewIn).
public IReadOnlyList NewIn => ReleaseNotes.NewIn(Body, 3);
/// The release list's answer, read with a real JSON parser: a list of releases (the newest first) or one release.
/// Each asset's object holds a nested "uploader" object, so nothing simpler than a parser reads it right (v0.1.0 found no
/// assets). Asset names are matched without case.
public static Release Parse(string json)
{
using var doc = JsonDocument.Parse(json);
var root = doc.RootElement;
var r = new Release();
JsonElement release;
if (root.ValueKind == JsonValueKind.Array)
{
if (root.GetArrayLength() == 0) return r;
release = root[0];
}
else release = root;
if (release.ValueKind != JsonValueKind.Object) return r;
static string? Str(JsonElement o, string name) => o.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() : null;
static long Num(JsonElement o, string name) => o.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Number && v.TryGetInt64(out var n) ? n : 0;
r.Tag = Str(release, "tag_name") ?? "";
r.Name = Str(release, "name") ?? "";
r.Body = Str(release, "body") ?? "";
r.PageUrl = Str(release, "html_url");
r.Prerelease = release.TryGetProperty("prerelease", out var pre) && pre.ValueKind == JsonValueKind.True;
if (Str(release, "published_at") is { } when && DateTimeOffset.TryParse(when, out var t)) r.Published = t;
var assets = new List();
if (release.TryGetProperty("assets", out var list) && list.ValueKind == JsonValueKind.Array)
foreach (var f in list.EnumerateArray())
{
if (f.ValueKind != JsonValueKind.Object) continue;
var name = Str(f, "name") ?? "";
var link = Str(f, "browser_download_url");
var size = Num(f, "size");
assets.Add(new ReleaseAsset(name, link, size));
bool Is(string n) => string.Equals(name, n, StringComparison.OrdinalIgnoreCase);
if (Is(BuildInfo.SetupAsset)) { r.SetupUrl = link; r.SetupSize = size; }
if (Is(BuildInfo.ToolsAsset)) { r.ToolsUrl = link; r.ToolsSize = size; }
if (Is(BuildInfo.ToolsCodeAsset)) { r.ToolsCodeUrl = link; r.ToolsCodeSize = size; }
if (Is(BuildInfo.ToolsBaseAsset)) { r.ToolsBaseUrl = link; r.ToolsBaseSize = size; }
if (Is(BuildInfo.LinuxListAsset)) r.LinuxListUrl = link;
if (name.EndsWith("-x86_64.AppImage", StringComparison.OrdinalIgnoreCase) && name.StartsWith("Triforce", StringComparison.OrdinalIgnoreCase))
{
r.LinuxAppImageUrl = link;
r.LinuxAppImageSize = size;
}
}
r.Assets = assets;
return r;
}
/// The address asked: TRIFORCE_RELEASE_API, else the release list.
public static string ApiUrl(TriforceContext ctx) => ctx.Env("TRIFORCE_RELEASE_API") is { Length: > 0 } o ? o : BuildInfo.ReleaseApi;
/// The latest release (20 s for the answer). 404, an empty list or an empty tag: NoReleaseException; another answer
/// that is no success throws HttpRequestException.
public static async Task LatestAsync(TriforceContext ctx, HttpClient http, CancellationToken token)
{
using var ask = new CancellationTokenSource(TimeSpan.FromSeconds(20));
using var both = CancellationTokenSource.CreateLinkedTokenSource(token, ask.Token);
using var response = await http.GetAsync(ApiUrl(ctx), both.Token).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.NotFound) throw new NoReleaseException();
response.EnsureSuccessStatusCode();
var r = Parse(await response.Content.ReadAsStringAsync(both.Token).ConfigureAwait(false));
if (r.Tag.Length == 0) throw new NoReleaseException(); // (an empty list: nothing published)
return r;
}
}
/// The release notes' "New in" items.
public static class ReleaseNotes
{
static readonly Regex BoldItem = new(@"^\s*(?:[-*+]\s+)?\*\*(?.+?)\*\*\s*(?.*)$", RegexOptions.Compiled);
static readonly Regex Bullet = new(@"^\s*[-*+]\s+(?.+)$", RegexOptions.Compiled);
/// The first items of the notes: lines that are "**Title.** One line" (bulleted or not), the release rule DESIGN 7
/// asks for; when the notes have fewer than that many, their first bullet lines instead (a bullet's own bold start is its
/// title). Markdown is taken out of the words.
public static IReadOnlyList NewIn(string? notes, int count = 3)
{
if (string.IsNullOrWhiteSpace(notes)) return Array.Empty();
var lines = notes.Replace("\r", "").Split('\n');
var bold = new List();
var bullets = new List();
foreach (var line in lines)
{
if (line.TrimStart().StartsWith('#') || line.TrimStart().StartsWith('>')) continue;
if (BoldItem.Match(line) is { Success: true } m && bold.Count < count)
bold.Add(new NewInItem(Title(m.Groups["t"].Value), Plain(m.Groups["r"].Value)));
if (Bullet.Match(line) is { Success: true } b && bullets.Count < count)
{
var text = b.Groups["b"].Value;
bullets.Add(BoldItem.Match(text) is { Success: true } bm ? new NewInItem(Title(bm.Groups["t"].Value), Plain(bm.Groups["r"].Value)) : new NewInItem(Plain(text), ""));
}
}
if (bold.Count >= count) return bold;
return bullets.Count > 0 ? bullets : bold;
}
static string Title(string t) => Plain(t).TrimEnd('.', ':', ' ');
/// Markdown out: [text](link) -> text, `code` -> code, ** and __ gone, & and the like read.
public static string Plain(string s)
{
s = Regex.Replace(s, @"\[([^\]]*)\]\([^)]*\)", "$1");
s = s.Replace("**", "").Replace("__", "").Replace("`", "");
s = WebUtility.HtmlDecode(s);
return Regex.Replace(s, @"\s+", " ").Trim();
}
}