Skip to content

Build your personal knowledge base with Trilium Notes

feat(release): derive the Flathub metainfo releases from the release tags

Elian Doranda3561485db7
diff --git a/scripts/update-version.spec.ts b/scripts/update-version.spec.ts
new file mode 100644
index 0000000000..dffe11937f
--- /dev/null
+++ b/scripts/update-version.spec.ts
@@ -0,0 +1,106 @@
+import { describe, expect, it } from "vitest";
+
+import { parseReleaseTags, renderMetainfoReleases, selectReleases } from "./update-version";
+
+/** Trimmed to the shape the script anchors on, indentation included. */
+const METAINFO = `<?xml version="1.0" encoding="UTF-8"?>
+<component type="desktop-application">
+ <id>org.triliumnotes.Trilium</id>
+ <releases>
+ <release version="0.105.0" date="2026-08-19">
+ <url type="details">https://github.com/TriliumNext/Trilium/releases/tag/v0.105.0</url>
+ </release>
+ </releases>
+ <provides>
+ <id>com.github.zadam.trilium</id>
+ </provides>
+</component>
+`;
+
+describe("parseReleaseTags", () => {
+ it("keeps the three-part release tags in the order git gave them", () => {
+ const tags = parseReleaseTags([
+ "v0.105.0\t2026-08-19",
+ "v0.104.1\t2026-07-25",
+ "v0.104.0\t2026-07-18"
+ ].join("\n"));
+
+ expect(tags).toEqual([
+ { version: "0.105.0", date: "2026-08-19" },
+ { version: "0.104.1", date: "2026-07-25" },
+ { version: "0.104.0", date: "2026-07-18" }
+ ]);
+ });
+
+ it("drops the tags Flathub never sees and tolerates a trailing newline", () => {
+ const tags = parseReleaseTags([
+ "v0.105.0\t2026-08-19",
+ "v0.92.3-beta\t2025-01-02",
+ "v0.91.4-rc1\t2025-01-01",
+ "v0.48\t2021-05-01",
+ "nightly\t2026-09-01",
+ ""
+ ].join("\n"));
+
+ expect(tags).toEqual([ { version: "0.105.0", date: "2026-08-19" } ]);
+ });
+});
+
+describe("selectReleases", () => {
+ const TAGS: { version: string; date: string }[] = [
+ { version: "0.105.0", date: "2026-08-19" },
+ { version: "0.104.1", date: "2026-07-25" },
+ { version: "0.104.0", date: "2026-07-18" },
+ { version: "0.103.0", date: "2026-05-13" },
+ { version: "0.102.2", date: "2026-04-05" },
+ { version: "0.102.1", date: "2026-03-08" }
+ ];
+
+ it("dates the version being prepared today and keeps the five newest", () => {
+ expect(selectReleases(TAGS, "0.106.0", "2026-09-22")).toEqual([
+ { version: "0.106.0", date: "2026-09-22" },
+ { version: "0.105.0", date: "2026-08-19" },
+ { version: "0.104.1", date: "2026-07-25" },
+ { version: "0.104.0", date: "2026-07-18" },
+ { version: "0.103.0", date: "2026-05-13" }
+ ]);
+ });
+
+ it("prefers the tag's date once the version has been tagged", () => {
+ // A re-run after tagging must not re-date the release to the day of the re-run.
+ expect(selectReleases(TAGS, "0.105.0", "2026-09-22")).toEqual(TAGS.slice(0, 5));
+ });
+});
+
+describe("renderMetainfoReleases", () => {
+ it("rewrites the block, keeping the surrounding document and indentation", () => {
+ const updated = renderMetainfoReleases(METAINFO, [
+ { version: "0.106.0", date: "2026-09-22" },
+ { version: "0.105.0", date: "2026-08-19" }
+ ]);
+
+ expect(updated).toContain(` <releases>
+ <release version="0.106.0" date="2026-09-22">
+ <url type="details">https://github.com/TriliumNext/Trilium/releases/tag/v0.106.0</url>
+ </release>
+ <release version="0.105.0" date="2026-08-19">
+ <url type="details">https://github.com/TriliumNext/Trilium/releases/tag/v0.105.0</url>
+ </release>
+ </releases>`);
+ // The rest of the component is untouched.
+ expect(updated).toContain("<id>org.triliumnotes.Trilium</id>");
+ expect(updated).toContain("<id>com.github.zadam.trilium</id>");
+ expect(updated.endsWith("</component>\n")).toBe(true);
+ });
+
+ it("is stable when the list has not changed", () => {
+ const releases = [ { version: "0.105.0", date: "2026-08-19" } ];
+
+ expect(renderMetainfoReleases(METAINFO, releases)).toBe(METAINFO);
+ });
+
+ it("rejects a metainfo it cannot anchor on", () => {
+ expect(() => renderMetainfoReleases("<component></component>", []))
+ .toThrow(/releases/);
+ });
+});
diff --git a/scripts/update-version.ts b/scripts/update-version.ts
index 9cea6e6e76..a440b57e81 100644
--- a/scripts/update-version.ts
+++ b/scripts/update-version.ts
@@ -2,11 +2,90 @@
* @module
*
* This script synchronizes the `package.json` version of the monorepo (root `package.json`)
- * into the apps, so that it is properly displayed.
+ * into the apps, so that it is properly displayed. It also regenerates the Flathub
+ * metainfo's `<releases>`, which decides the version Flathub displays: the manifest
+ * installs that file from the tagged checkout, so the entry has to be in the commit that
+ * gets tagged.
*/
-import { join } from "path";
+import { execFileSync } from "child_process";
import { readFileSync, writeFileSync } from "fs";
+import { join } from "path";
+import { fileURLToPath } from "url";
+
+const SCRIPT_PATH = fileURLToPath(import.meta.url);
+const ROOT = join(SCRIPT_PATH, "..", "..");
+const METAINFO_PATH = join(ROOT, "apps", "desktop", "flatpak", "org.triliumnotes.Trilium.metainfo.xml");
+const RELEASE_URL = "https://github.com/TriliumNext/Trilium/releases/tag/v";
+const MAX_RELEASE_ENTRIES = 5;
+
+export interface Release {
+ version: string;
+ date: string;
+}
+
+export function main() {
+ const version = getVersion(join(ROOT, "package.json"));
+
+ for (const appName of ["server", "client", "standalone", "desktop", "edit-docs"]) {
+ patchPackageJson(join(ROOT, "apps", appName, "package.json"), version);
+ }
+
+ for (const packageName of ["commons", "pdfjs-viewer", "trilium-core"]) {
+ patchPackageJson(join(ROOT, "packages", packageName, "package.json"), version);
+ }
+
+ const tags = parseReleaseTags(git("for-each-ref", "--sort=-creatordate",
+ "--format=%(refname:short)\t%(creatordate:short)", "refs/tags/v*"));
+ if (tags.length === 0) {
+ throw new Error("Found no release tags to date the metainfo entries with.");
+ }
+ const releases = selectReleases(tags, version, new Date().toISOString().slice(0, 10));
+ writeFileSync(METAINFO_PATH,
+ renderMetainfoReleases(readFileSync(METAINFO_PATH, "utf-8"), releases));
+}
+
+/**
+ * Reads `git for-each-ref` output, keeping the `vX.Y.Z` tags in the order given. The rc
+ * and beta tags are releases Flathub never sees, and `v0.48`-style tags predate the
+ * three-part scheme.
+ */
+export function parseReleaseTags(output: string): Release[] {
+ const releases: Release[] = [];
+ for (const line of output.split("\n")) {
+ const tag = /^v(\d+\.\d+\.\d+)\t(\d{4}-\d{2}-\d{2})$/.exec(line.trim());
+ if (tag) {
+ releases.push({ version: tag[1], date: tag[2] });
+ }
+ }
+ return releases;
+}
+
+/**
+ * The newest MAX_RELEASE_ENTRIES releases, newest first. A version being prepared has no
+ * tag yet, so it is dated `today` until the tag it is about to get supplies the date.
+ */
+export function selectReleases(tags: Release[], version: string, today: string): Release[] {
+ const releases = tags.some((it) => it.version === version)
+ ? tags : [ { version, date: today }, ...tags ];
+ return releases.slice(0, MAX_RELEASE_ENTRIES);
+}
+
+/** Replaces the metainfo's `<releases>`, keeping the indentation the document uses. */
+export function renderMetainfoReleases(metainfo: string, releases: Release[]): string {
+ const block = /([ \t]*)<releases>\n[\s\S]*?[ \t]*<\/releases>/;
+ if (!block.test(metainfo)) {
+ throw new Error("Found no <releases> block in the metainfo.");
+ }
+
+ return metainfo.replace(block, (_, indent: string) => {
+ const entries = releases.map(({ version, date }) =>
+ `${indent} <release version="${version}" date="${date}">\n`
+ + `${indent} <url type="details">${RELEASE_URL}${version}</url>\n`
+ + `${indent} </release>\n`);
+ return `${indent}<releases>\n${entries.join("")}${indent}</releases>`;
+ });
+}
function patchPackageJson(packageJsonPath: string, version: string) {
// Read the version from package.json and process it.
@@ -23,16 +102,11 @@ function getVersion(packageJsonPath: string) {
return packageJson.version;
}
-function main() {
- const version = getVersion(join(__dirname, "..", "package.json"));
-
- for (const appName of ["server", "client", "standalone", "desktop", "edit-docs"]) {
- patchPackageJson(join(__dirname, "..", "apps", appName, "package.json"), version);
- }
-
- for (const packageName of ["commons", "pdfjs-viewer", "trilium-core"]) {
- patchPackageJson(join(__dirname, "..", "packages", packageName, "package.json"), version);
- }
+function git(...args: string[]): string {
+ return execFileSync("git", args, { cwd: ROOT }).toString();
}
-main();
+// Only when run as a script — the pure helpers above are imported by the spec.
+if (process.argv[1] === SCRIPT_PATH) {
+ main();
+}