From 3d9189077a329fd5a1b3cdd96677aceae28d335c Mon Sep 17 00:00:00 2001 From: CRT Date: Wed, 30 Sep 2026 23:28:44 +0700 Subject: [PATCH] waybetter manifests: winget, Chocolatey, Homebrew cask and AUR files from one JSON description Generic (publisher, id, name, version, installer URLs and sha256 or local files, silent switches, licence, homepage). winget: version, installer and locale manifests (validated with the installed winget), Chocolatey: nuspec + install script with checksum (choco pack), Homebrew: a cask for our own tap, AUR: PKGBUILD and .SRCINFO from the .deb or AppImage. waybetter release windows|macos|linux writes them to /distribution/ once the public download URL is given (--download-base); nothing is submitted. --- packages/waybetter/bin/waybetter.mjs | 7 +- packages/waybetter/cli/manifests.mjs | 383 ++++++++++++++++++ packages/waybetter/cli/release.mjs | 41 ++ .../waybetter/tests/manifests-cli.test.mjs | 249 ++++++++++++ 4 files changed, 678 insertions(+), 2 deletions(-) create mode 100644 packages/waybetter/cli/manifests.mjs create mode 100644 packages/waybetter/tests/manifests-cli.test.mjs diff --git a/packages/waybetter/bin/waybetter.mjs b/packages/waybetter/bin/waybetter.mjs index aa6f353..5cb2745 100644 --- a/packages/waybetter/bin/waybetter.mjs +++ b/packages/waybetter/bin/waybetter.mjs @@ -1,12 +1,13 @@ #!/usr/bin/env node // The `waybetter` command: `waybetter store build|sign|verify` (cli/store.mjs), -// `waybetter release windows|macos|linux` (cli/release.mjs), `waybetter i18n check|extract` +// `waybetter release windows|macos|linux` (cli/release.mjs), `waybetter manifests` (cli/manifests.mjs), `waybetter i18n check|extract` // (cli/i18n.mjs), `waybetter privacy-scan` (cli/privacy.mjs) and `waybetter build-env` // (cli/buildenv.mjs), `waybetter dev` (cli/dev.mjs: watchers + tauri dev), `waybetter strings check` // (cli/strings.mjs). import { readFileSync } from 'node:fs'; import { BUILD_ENV_USAGE, buildEnvCommand } from '../cli/buildenv.mjs'; import { DEV_USAGE, devCommand } from '../cli/dev.mjs'; +import { MANIFESTS_USAGE, manifestsCommand } from '../cli/manifests.mjs'; import { I18N_USAGE, i18nCommand } from '../cli/i18n.mjs'; import { PRIVACY_USAGE, privacyCommand } from '../cli/privacy.mjs'; import { STORE_USAGE, storeCommand } from '../cli/store.mjs'; @@ -19,6 +20,8 @@ try { for (const line of await storeCommand(rest)) console.log(line); } else if (area === 'release') { await releaseCommand(rest); + } else if (area === 'manifests') { + process.exitCode = manifestsCommand(rest); } else if (area === 'i18n') { process.exitCode = await i18nCommand(rest); } else if (area === 'strings') { @@ -32,7 +35,7 @@ try { } else if (area === '--version' || area === '-v') { console.log(JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version); } else { - console.log(`${STORE_USAGE}\n\n${RELEASE_USAGE}\n\n${I18N_USAGE}\n\n${STRINGS_USAGE}\n\n${PRIVACY_USAGE}\n\n${BUILD_ENV_USAGE} + console.log(`${STORE_USAGE}\n\n${RELEASE_USAGE}\n\n${MANIFESTS_USAGE}\n\n${I18N_USAGE}\n\n${STRINGS_USAGE}\n\n${PRIVACY_USAGE}\n\n${BUILD_ENV_USAGE} ${DEV_USAGE}`); if (area && area !== '--help' && area !== '-h') process.exitCode = 1; diff --git a/packages/waybetter/cli/manifests.mjs b/packages/waybetter/cli/manifests.mjs new file mode 100644 index 0000000..7728697 --- /dev/null +++ b/packages/waybetter/cli/manifests.mjs @@ -0,0 +1,383 @@ +// `waybetter manifests `: distribution manifests for any app from one small JSON +// description: a winget manifest set, a Chocolatey package, a Homebrew cask for the publisher's +// OWN tap and an AUR package (PKGBUILD + .SRCINFO). Generic: nothing here knows wayBetter. Nothing +// is submitted anywhere and no account is needed; the files are what the owner submits or pushes +// (see the app's docs/DISTRIBUTION.md). +// +// The description (JSON): +// +// { +// "publisher": "CRT", // winget/choco publisher +// "id": "CRT.WayBetterForX", // winget PackageIdentifier: Publisher.Package[.Sub] +// "name": "wayBetter for X", // display name +// "slug": "waybetter-for-x", // optional lower-case name for choco/brew/aur (derived from name) +// "version": "1.0.0", +// "shortDescription": "...", "description": "...", // description optional (defaults to short) +// "homepage": "https://...", "license": "GPL-3.0-or-later", "licenseUrl": "https://...", // licenseUrl optional +// "releaseNotesUrl": "https://...", "tags": ["a", "b"], "copyright": "...", +// "windows": { "url": "https://.../setup.exe", "sha256": "<64 hex>", "silentSwitches": "/S", +// "scope": "user", "architecture": "x64" }, +// "macos": { "url": "https://.../app.dmg", "sha256": "...", "app": "wayBetter for X.app", "zap": ["~/Library/..."] }, +// "linux": { "deb": { "url": "...", "sha256": "..." }, "appImage": { "url": "...", "sha256": "..." }, +// "depends": ["webkit2gtk-4.1", "gtk3"] } +// } +// +// An installer may give `"file": ""` instead of `sha256` (hashed here) and, with +// `--download-base ` (or "downloadBase" in the description), instead of `url` too +// (/). Unknown URLs are inputs, never guessed. A platform section that is absent +// skips the channels that need it. +// +// waybetter manifests [--out ] [--download-base ] [--only winget,chocolatey,homebrew,aur] +import { createHash } from 'node:crypto'; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; +import { basename, dirname, join, resolve } from 'node:path'; + +export const MANIFESTS_USAGE = `Usage: + waybetter manifests [--out ] [--download-base ] [--only winget,chocolatey,homebrew,aur] + Writes distribution manifests from a small JSON description (publisher, id, name, version, + installer URLs + sha256 or local files, silent switches, licence, homepage): a winget manifest + set (schema 1.28.0), a Chocolatey package, a Homebrew cask for your own tap and an AUR + PKGBUILD + .SRCINFO. Submits nothing.`; + +export const WINGET_MANIFEST_VERSION = '1.28.0'; +export const CHANNELS = ['winget', 'chocolatey', 'homebrew', 'aur']; + +class ManifestError extends Error {} +const fail = message => { throw new ManifestError(message); }; + +const HTTPS = /^https:\/\/[^\s"'<>]+$/; +const SHA256 = /^[0-9a-fA-F]{64}$/; +const VERSION = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/; +const WINGET_ID = /^[^.\s\\/:*?"<>|\x01-\x1f]{1,32}(\.[^.\s\\/:*?"<>|\x01-\x1f]{1,32}){1,7}$/; +const SLUG = /^[a-z0-9][a-z0-9-]*$/; + +export const slugOf = name => name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, ''); +const sha256File = file => createHash('sha256').update(readFileSync(file)).digest('hex'); + +/** Resolves `file` installers (hash, URL from the download base) and validates the description. */ +export function resolveSpec(raw, { baseDir = process.cwd(), downloadBase } = {}) { + const spec = structuredClone(raw); + const text = (key, { optional = false } = {}) => { + const value = spec[key]; + if (value === undefined && optional) return undefined; + if (typeof value !== 'string' || !value.trim()) fail(`"${key}" must be a non-empty string`); + return value.trim(); + }; + spec.publisher = text('publisher'); + spec.id = text('id'); + if (!WINGET_ID.test(spec.id)) fail('"id" must look like Publisher.Package (no spaces, at most 32 characters a part)'); + spec.name = text('name'); + spec.slug = text('slug', { optional: true }) ?? slugOf(spec.name); + if (!SLUG.test(spec.slug)) fail('"slug" must be lower-case letters, digits and hyphens'); + spec.version = text('version'); + if (!VERSION.test(spec.version)) fail(`"version" must be like 1.2.3, not ${spec.version}`); + spec.shortDescription = text('shortDescription'); + spec.description = text('description', { optional: true }) ?? spec.shortDescription; + spec.homepage = text('homepage'); + if (!HTTPS.test(spec.homepage)) fail('"homepage" must be an https URL'); + spec.license = text('license'); + spec.licenseUrl = text('licenseUrl', { optional: true }) ?? (/^[A-Za-z0-9.+-]+$/.test(spec.license) ? `https://spdx.org/licenses/${spec.license}.html` : undefined); + for (const key of ['releaseNotesUrl']) if (spec[key] !== undefined && !HTTPS.test(spec[key])) fail(`"${key}" must be an https URL`); + spec.tags = (spec.tags ?? []).map(String); + + const base = (downloadBase ?? spec.downloadBase)?.replace(/\/+$/, ''); + const installer = (where, entry) => { + if (!entry || typeof entry !== 'object') fail(`"${where}" must be an object`); + const file = entry.file && resolve(baseDir, entry.file); + if (file && !existsSync(file)) fail(`${where}: file not found: ${entry.file}`); + entry.sha256 = (entry.sha256 ?? (file && sha256File(file)))?.toLowerCase(); + entry.url ??= file && base ? `${base}/${basename(file)}` : undefined; + if (!entry.url) fail(`${where}: needs "url" (or "file" with --download-base)`); + if (!HTTPS.test(entry.url)) fail(`${where}: url must be an https URL`); + if (!SHA256.test(entry.sha256 ?? '')) fail(`${where}: needs a 64-digit "sha256" (or "file" to hash)`); + return entry; + }; + if (spec.windows) { + installer('windows', spec.windows); + spec.windows.architecture ??= 'x64'; + if (!['x86', 'x64', 'arm', 'arm64', 'neutral'].includes(spec.windows.architecture)) fail('windows.architecture must be x86, x64, arm, arm64 or neutral'); + spec.windows.scope ??= 'user'; + if (!['user', 'machine'].includes(spec.windows.scope)) fail('windows.scope must be user or machine'); + spec.windows.silentSwitches ??= '/S'; + spec.windows.installerType ??= 'nullsoft'; + if (!['nullsoft', 'inno', 'exe', 'msi', 'wix', 'burn'].includes(spec.windows.installerType)) fail('windows.installerType is not a winget installer type'); + } + if (spec.macos) { + installer('macos', spec.macos); + spec.macos.app = spec.macos.app ? String(spec.macos.app) : `${spec.name}.app`; + if (!spec.macos.app.endsWith('.app') || /[/"\\]/.test(spec.macos.app)) fail('macos.app must be a bundle name like "Name.app"'); + spec.macos.zap = (spec.macos.zap ?? []).map(String); + } + if (spec.linux) { + if (!spec.linux.deb && !spec.linux.appImage) fail('linux needs "deb" and/or "appImage"'); + for (const kind of ['deb', 'appImage']) if (spec.linux[kind]) installer(`linux.${kind}`, spec.linux[kind]); + spec.linux.depends = (spec.linux.depends ?? ['webkit2gtk-4.1', 'gtk3']).map(String); + } + if (!spec.windows && !spec.macos && !spec.linux) fail('give at least one of "windows", "macos", "linux"'); + return spec; +} + +// ---- YAML (only the shapes winget needs: maps, lists, strings, numbers) ---- + +const plain = /^[A-Za-z0-9_][A-Za-z0-9_./ -]*$/; +function scalar(value) { + if (typeof value === 'number' || typeof value === 'boolean') return String(value); + const text = String(value); + // Quote anything YAML could read as something else (numbers, booleans, null, "a: b", "- x", "#"). + if (plain.test(text) && !/^(true|false|null|yes|no|on|off|~|[-+]?[0-9.]+(e[0-9]+)?)$/i.test(text) && !/ $|: | #/.test(text) && !text.includes('- ') ) return text; + return JSON.stringify(text); +} +export function toYaml(value, indent = 0) { + const pad = ' '.repeat(indent); + const lines = []; + for (const [key, item] of Object.entries(value)) { + if (item === undefined || item === null) continue; + if (Array.isArray(item)) { + if (!item.length) continue; + lines.push(`${pad}${key}:`); + for (const entry of item) { + if (entry && typeof entry === 'object') { + const inner = toYaml(entry, indent + 2).split('\n'); + lines.push(`${pad}- ${inner[0].trimStart()}`, ...inner.slice(1)); + } else lines.push(`${pad}- ${scalar(entry)}`); + } + } else if (typeof item === 'object') { + lines.push(`${pad}${key}:`, toYaml(item, indent + 2)); + } else if (typeof item === 'string' && item.includes('\n')) { + lines.push(`${pad}${key}: |-`, ...item.split('\n').map(line => `${pad} ${line}`)); + } else lines.push(`${pad}${key}: ${scalar(item)}`); + } + return lines.join('\n'); +} + +// ---- winget ---- + +const WINGET_LOCALE = 'en-US'; + +export function wingetManifests(spec) { + if (!spec.windows) return undefined; + const w = spec.windows; + const header = name => `# Created by waybetter manifests\n# yaml-language-server: $schema=https://aka.ms/winget-manifest.${name}.${WINGET_MANIFEST_VERSION}.schema.json\n\n`; + const common = { PackageIdentifier: spec.id, PackageVersion: spec.version }; + const installer = { + ...common, + InstallerLocale: WINGET_LOCALE, + InstallerType: w.installerType, + Scope: w.scope, + InstallModes: ['interactive', 'silent', 'silentWithProgress'], + InstallerSwitches: { Silent: w.silentSwitches, SilentWithProgress: w.silentWithProgress ?? w.silentSwitches }, + UpgradeBehavior: 'install', + Installers: [{ Architecture: w.architecture, InstallerUrl: w.url, InstallerSha256: w.sha256.toUpperCase() }], + ManifestType: 'installer', + ManifestVersion: WINGET_MANIFEST_VERSION, + }; + const locale = { + ...common, + PackageLocale: WINGET_LOCALE, + Publisher: spec.publisher, + PublisherUrl: spec.publisherUrl, + PackageName: spec.name, + PackageUrl: spec.homepage, + License: spec.license, + LicenseUrl: spec.licenseUrl, + Copyright: spec.copyright, + ShortDescription: spec.shortDescription, + Description: spec.description, + Tags: spec.tags.length ? spec.tags : undefined, + ReleaseNotesUrl: spec.releaseNotesUrl, + ManifestType: 'defaultLocale', + ManifestVersion: WINGET_MANIFEST_VERSION, + }; + const version = { ...common, DefaultLocale: WINGET_LOCALE, ManifestType: 'version', ManifestVersion: WINGET_MANIFEST_VERSION }; + // winget-pkgs layout: manifests/////[.installer|.locale.].yaml + const dir = join('winget', 'manifests', spec.id[0].toLowerCase(), ...spec.id.split('.'), spec.version); + return { + [join(dir, `${spec.id}.yaml`)]: `${header('version')}${toYaml(version)}\n`, + [join(dir, `${spec.id}.installer.yaml`)]: `${header('installer')}${toYaml(installer)}\n`, + [join(dir, `${spec.id}.locale.${WINGET_LOCALE}.yaml`)]: `${header('defaultLocale')}${toYaml(locale)}\n`, + }; +} + +// ---- Chocolatey ---- + +const xml = value => String(value).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"'); +const psq = value => `'${String(value).replaceAll("'", "''")}'`; + +export function chocolateyPackage(spec) { + if (!spec.windows) return undefined; + const w = spec.windows; + const dir = join('chocolatey', spec.slug); + const fileType = w.installerType === 'msi' || w.installerType === 'wix' ? 'msi' : 'exe'; + const nuspec = ` + + + ${xml(spec.slug)} + ${xml(spec.version)} + ${xml(spec.name)} + ${xml(spec.publisher)} + ${xml(spec.homepage)}${spec.licenseUrl ? `\n ${xml(spec.licenseUrl)}` : ''} + false + ${xml(spec.shortDescription)} + ${xml(spec.description)}${spec.tags.length ? `\n ${xml(spec.tags.join(' '))}` : ''}${spec.copyright ? `\n ${xml(spec.copyright)}` : ''}${spec.releaseNotesUrl ? `\n ${xml(spec.releaseNotesUrl)}` : ''} + + + + + +`; + const install = `$ErrorActionPreference = 'Stop' + +$packageArgs = @{ + packageName = $env:ChocolateyPackageName + fileType = ${psq(fileType)} + url64bit = ${psq(w.url)} + checksum64 = ${psq(w.sha256.toLowerCase())} + checksumType64 = 'sha256' + silentArgs = ${psq(w.silentSwitches)} + validExitCodes = @(0) +} + +Install-ChocolateyPackage @packageArgs +`; + return { + [join(dir, `${spec.slug}.nuspec`)]: nuspec, + [join(dir, 'tools', 'chocolateyInstall.ps1')]: install, + }; +} + +// ---- Homebrew cask (the publisher's own tap: /Casks/.rb) ---- + +const rb = value => `"${String(value).replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('#{', '\\#{')}"`; + +export function homebrewCask(spec) { + if (!spec.macos) return undefined; + const m = spec.macos; + // The version inside the URL becomes #{version}, so a bump edits one line. + const url = m.url.includes(spec.version) ? `"${m.url.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll(spec.version, '#{version}')}"` : rb(m.url); + const zap = m.zap.length ? `\n\n zap trash: [\n${m.zap.map(path => ` ${rb(path)},`).join('\n')}\n ]` : ''; + const cask = `cask ${rb(spec.slug)} do + version ${rb(spec.version)} + sha256 ${rb(m.sha256)} + + url ${url} + name ${rb(spec.name)} + desc ${rb(spec.shortDescription)} + homepage ${rb(spec.homepage)} + + depends_on macos: ">= :big_sur" + + app ${rb(m.app)}${zap} +end +`; + return { [join('homebrew', 'Casks', `${spec.slug}.rb`)]: cask }; +} + +// ---- AUR: -bin repackaging the .deb (or, without one, the AppImage) ---- + +const bash = value => `'${String(value).replaceAll("'", `'\\''`)}'`; +export function aurPackage(spec) { + const l = spec.linux; + if (!l) return undefined; + const name = `${spec.slug}-bin`; + const isDeb = Boolean(l.deb); + const source = l.deb ?? l.appImage; + const ext = isDeb ? 'deb' : 'AppImage'; + const pkgver = spec.version.replaceAll('-', '_'); + const file = `${name}-${pkgver}.${ext}`; + // The version in the URL becomes ${pkgver}; a release version with a hyphen keeps its URL literally. + const simple = !spec.version.includes('-'); + const dq = value => value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('$', '\\$'); + const url = simple ? dq(source.url).replaceAll(spec.version, '${pkgver}') : dq(source.url); + const sourceEntry = simple ? `"\${pkgname}-\${pkgver}.${ext}::${url}"` : `"${file}::${url}"`; + const depends = isDeb ? l.depends : []; + const body = isDeb + ? ' bsdtar -xf "$srcdir"/data.tar.* -C "$pkgdir"' + : ` install -Dm755 "$srcdir/${file}" "$pkgdir/opt/${spec.slug}/${spec.slug}.AppImage" + install -d "$pkgdir/usr/bin" + ln -s "/opt/${spec.slug}/${spec.slug}.AppImage" "$pkgdir/usr/bin/${spec.slug}"`; + const pkgbuild = `# Maintainer: ${spec.publisher} +pkgname=${name} +pkgver=${pkgver} +pkgrel=1 +pkgdesc=${bash(spec.shortDescription)} +arch=('x86_64') +url=${bash(spec.homepage)} +license=(${bash(spec.license)}) +${depends.length ? `depends=(${depends.map(bash).join(' ')})\n` : ''}provides=(${bash(spec.slug)}) +conflicts=(${bash(spec.slug)}) +options=(!strip !debug) +${isDeb ? '' : `noextract=("${file}")\n`}source=(${sourceEntry}) +sha256sums=(${bash(source.sha256.toLowerCase())}) + +package() { +${body} +} +`; + const srcinfo = `pkgbase = ${name} +\tpkgdesc = ${spec.shortDescription} +\tpkgver = ${pkgver} +\tpkgrel = 1 +\turl = ${spec.homepage} +\tarch = x86_64 +\tlicense = ${spec.license} +${depends.map(dep => `\tdepends = ${dep}\n`).join('')}\toptions = !strip +\toptions = !debug +\tprovides = ${spec.slug} +\tconflicts = ${spec.slug} +${isDeb ? '' : `\tnoextract = ${file}\n`}\tsource = ${file}::${source.url} +\tsha256sums = ${source.sha256.toLowerCase()} + +pkgname = ${name} +`; + return { [join('aur', name, 'PKGBUILD')]: pkgbuild, [join('aur', name, '.SRCINFO')]: srcinfo }; +} + +/** All manifests as `{ relative path: text }`; `only` limits the channels. */ +export function buildManifests(spec, { only = CHANNELS } = {}) { + const builders = { winget: wingetManifests, chocolatey: chocolateyPackage, homebrew: homebrewCask, aur: aurPackage }; + const files = {}; + const skipped = []; + for (const channel of only) { + if (!builders[channel]) fail(`unknown channel ${channel} (${CHANNELS.join(', ')})`); + const made = builders[channel](spec); + if (made) Object.assign(files, made); + else skipped.push(channel); + } + return { files, skipped }; +} + +/** Writes them under `outDir` (LF line endings) and returns the written relative paths. */ +export function writeManifests(spec, outDir, options) { + const { files, skipped } = buildManifests(spec, options); + for (const [path, text] of Object.entries(files)) { + const target = join(outDir, path); + mkdirSync(dirname(target), { recursive: true }); + writeFileSync(target, text.replaceAll('\r\n', '\n')); + } + return { written: Object.keys(files), skipped }; +} + +function option(args, name) { + const i = args.indexOf(name); + return i >= 0 ? args[i + 1] : undefined; +} + +/** `waybetter manifests ...`; returns the exit code. */ +export function manifestsCommand(argv, { cwd = process.cwd(), log = console.log } = {}) { + const file = argv.find((arg, i) => !arg.startsWith('--') && !['--out', '--download-base', '--only'].includes(argv[i - 1])); + if (!file || argv.includes('--help') || argv.includes('-h')) { + log(MANIFESTS_USAGE); + return file ? 0 : 1; + } + const path = resolve(cwd, file); + if (!existsSync(path)) fail(`not found: ${path}`); + let raw; + try { raw = JSON.parse(readFileSync(path, 'utf8')); } catch (error) { fail(`${path}: ${error.message}`); } + const spec = resolveSpec(raw, { baseDir: dirname(path), downloadBase: option(argv, '--download-base') }); + const only = option(argv, '--only')?.split(',').map(s => s.trim()).filter(Boolean); + const out = resolve(cwd, option(argv, '--out') ?? 'distribution'); + const { written, skipped } = writeManifests(spec, out, { only: only ?? CHANNELS }); + for (const name of written) log(`manifests: ${join(out, name)}`); + if (skipped.length) log(`manifests: skipped ${skipped.join(', ')} (no installer for that platform in the description)`); + return 0; +} diff --git a/packages/waybetter/cli/release.mjs b/packages/waybetter/cli/release.mjs index b70aff7..582859e 100644 --- a/packages/waybetter/cli/release.mjs +++ b/packages/waybetter/cli/release.mjs @@ -15,6 +15,8 @@ // "manifestValidator": { "module": "packages/contracts/dist/index.js", "export": "NativeRelease", // "hint": "build packages/contracts first" }, // optional, zod-like .parse // "prepareCommand": "bun install --frozen-lockfile", // remote/WSL builds: runs first in the unpacked sources +// "distribution": { "publisher": "CRT", "id": "CRT.Name", "homepage": "https://...", "license": "...", "shortDescription": "...", "downloadBase": "https://..." }, +// // manifests written after each release step (see cli/manifests.mjs) // "exportExclude": ["verification"], // remote/WSL builds: app-repo paths left out of the upload // "forbiddenTerms": "~/.better-twttr-keys/forbidden-names.txt", // optional, outside the repo // "privacyAllowlist": "privacy-allowlist.txt" // optional, reviewed false positives @@ -36,6 +38,7 @@ import { dirname, isAbsolute, join, relative, resolve } from 'node:path'; import { pathToFileURL } from 'node:url'; import { pathFreeBuildEnv } from './buildenv.mjs'; import { assertClean } from './privacy.mjs'; +import { CHANNELS, resolveSpec, writeManifests } from './manifests.mjs'; import { buildLinuxWsl, buildMacRemote, scanBuilt } from './remote.mjs'; export const CONFIG_FILE = 'waybetter.release.json'; @@ -121,6 +124,7 @@ export function normalizeReleaseConfig(raw, root) { outDir: at(text('outDir', 'releases')), buildCommand: text('buildCommand', 'bun run tauri build'), prepareCommand: text('prepareCommand', 'bun install --frozen-lockfile'), + distribution: raw.distribution, exportExclude: Array.isArray(raw.exportExclude) ? raw.exportExclude.map(String) : [], manifestValidator: validator && { module: at(validator.module), export: validator.export, hint: validator.hint }, forbiddenTerms: raw.forbiddenTerms === undefined ? undefined : at(home(text('forbiddenTerms'))), @@ -187,6 +191,40 @@ function option(args, name) { const pubDate = () => new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'); +/** + * Distribution manifests (winget, Chocolatey, Homebrew, AUR) for the installers now in + * /upload, written to /distribution/. Needs "distribution" in the config + * and the public download URL (--download-base, WAYBETTER_DOWNLOAD_BASE or distribution.downloadBase): + * without it nothing is written, because that URL is not ours to guess. + */ +export function writeDistribution(config, version, args, { env = process.env, log = console.log } = {}) { + const d = config.distribution; + if (!d) return undefined; + const base = option(args, '--download-base') ?? env.WAYBETTER_DOWNLOAD_BASE ?? d.downloadBase; + if (!base) { + log('manifests: skipped: the public download URL is not known yet (pass --download-base , or set WAYBETTER_DOWNLOAD_BASE); run the release step again or `waybetter manifests` once it is'); + return undefined; + } + const upload = join(config.outDir, 'upload'); + const found = name => (existsSync(join(upload, name)) ? { file: join(upload, name) } : undefined); + const exe = found(`${config.artifact}_${version}_x64-setup.exe`); + const dmg = found(`${config.artifact}_${version}_universal.dmg`); + const deb = found(`${config.artifact}_${version}_amd64.deb`); + const appImage = found(`${config.artifact}_${version}_amd64.AppImage`); + const spec = resolveSpec({ + ...d, + name: d.name ?? config.product, + version, + windows: exe && { ...d.windows, ...exe }, + macos: dmg && { ...d.macos, ...dmg }, + linux: (deb || appImage) && { ...d.linux, ...(deb && { deb }), ...(appImage && { appImage }) }, + }, { baseDir: config.root, downloadBase: base }); + const out = join(config.outDir, 'distribution', version); + const { written, skipped } = writeManifests(spec, out, { only: CHANNELS }); + log(`manifests: ${written.length} file(s) in ${relative(config.root, out).replaceAll('\\', '/')}${skipped.length ? ` (${skipped.join(', ')}: no installer for that platform yet)` : ''}`); + return { out, written, skipped }; +} + /** `waybetter release windows`. */ export async function releaseWindows(config, args, { env = process.env, log = console.log } = {}) { const { version, productName, binary, dist } = readVersion(config); @@ -264,6 +302,7 @@ Nothing was uploaded. Run these steps yourself, in this order: 4. Verify the update endpoint answers an older client: curl -sS ${config.apiBase}/v1/updates/windows/x86_64/0.0.0 `); + writeDistribution(config, version, args, { env, log }); return { version, manifestPath, files: [fileName, `${fileName}.sig`] }; } @@ -333,6 +372,7 @@ then publish latest.json the same way (tmp file, then mv), and verify curl -sS ${config.apiBase}/v1/updates/darwin/aarch64/0.0.0 curl -sS ${config.apiBase}/v1/updates/darwin/x86_64/0.0.0 `); + writeDistribution(config, version, args, { env, log }); return { version, manifestPath, files: [dmgName, updaterName, `${updaterName}.sig`] }; } @@ -384,6 +424,7 @@ installer (same target and folder as \`waybetter release windows\` prints), chec then publish latest.json the same way (tmp file, then mv), and verify curl -sS ${config.apiBase}/v1/updates/linux/x86_64/0.0.0 `); + writeDistribution(config, version, args, { env, log }); return { version, manifestPath, files: [appimageName, `${appimageName}.sig`, debName] }; } diff --git a/packages/waybetter/tests/manifests-cli.test.mjs b/packages/waybetter/tests/manifests-cli.test.mjs new file mode 100644 index 0000000..7d0d012 --- /dev/null +++ b/packages/waybetter/tests/manifests-cli.test.mjs @@ -0,0 +1,249 @@ +// `waybetter manifests`: winget, Chocolatey, Homebrew and AUR files from a small description. +// Validated offline: the YAML is parsed back and checked against the winget schema's constraints +// (required keys, patterns, enums), the nuspec is checked for well-formedness, PowerShell and Ruby +// syntax-check their files when those tools exist, and the .SRCINFO must agree with the PKGBUILD. +import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { existsSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import test from 'node:test'; +import { fileURLToPath } from 'node:url'; +import { WINGET_MANIFEST_VERSION, buildManifests, resolveSpec, toYaml, writeManifests } from '../cli/manifests.mjs'; + +const BIN = fileURLToPath(new URL('../bin/waybetter.mjs', import.meta.url)); +const SHA = 'a'.repeat(63) + 'B'; +const SHA2 = '0123456789abcdef'.repeat(4); + +const RAW = { + publisher: 'Example', + id: 'Example.Some-App', + name: 'Some App', + version: '1.2.3', + shortDescription: 'Does: things, "quoted" and #hashed', + description: 'Line one\nLine two', + homepage: 'https://example.test/app', + license: 'GPL-3.0-or-later', + tags: ['a', 'b'], + windows: { url: 'https://dl.example.test/1.2.3/Some-App_1.2.3_x64-setup.exe', sha256: SHA, silentSwitches: '/S' }, + macos: { url: 'https://dl.example.test/1.2.3/Some-App_1.2.3.dmg', sha256: SHA2, app: 'Some App.app', zap: ['~/Library/Application Support/x.y'] }, + linux: { deb: { url: 'https://dl.example.test/1.2.3/some-app_1.2.3_amd64.deb', sha256: SHA2 }, appImage: { url: 'https://dl.example.test/1.2.3/Some-App.AppImage', sha256: SHA } }, +}; + +/** Parses the YAML subset the emitter writes (maps, lists of maps, lists of scalars, |- blocks). */ +function parseYaml(text) { + const lines = text.split('\n').filter(line => line.trim() && !line.trim().startsWith('#')); + let at = 0; + const value = raw => { + if (raw.startsWith('"')) return JSON.parse(raw); + return /^-?\d+(\.\d+)?$/.test(raw) ? Number(raw) : raw; + }; + const indentOf = line => line.length - line.trimStart().length; + function block(indent) { + if (lines[at].trimStart().startsWith('- ')) { + const list = []; + while (at < lines.length && indentOf(lines[at]) === indent && lines[at].trimStart().startsWith('- ')) { + const rest = lines[at].trimStart().slice(2); + if (/^[A-Za-z]\w*:( |$)/.test(rest)) { + lines[at] = `${' '.repeat(indent + 2)}${rest}`; + list.push(block(indent + 2)); + } else { list.push(value(rest)); at += 1; } + } + return list; + } + const map = {}; + while (at < lines.length && indentOf(lines[at]) === indent) { + const [, key, rest] = /^\s*([A-Za-z]\w*):(?: (.*))?$/.exec(lines[at]); + at += 1; + if (rest === '|-') { + const parts = []; + while (at < lines.length && indentOf(lines[at]) > indent) parts.push(lines[at++].slice(indent + 2)); + map[key] = parts.join('\n'); + } else if (rest === undefined) { + // a nested map is indented by 2; a list of maps by 0 or 2 (we write 0 extra) + map[key] = block(indentOf(lines[at])); + } else map[key] = value(rest); + } + return map; + } + return block(0); +} + +function wellFormed(xml) { + const stack = []; + for (const [, close, name, self] of xml.replace(/<\?[\s\S]*?\?>/g, '').matchAll(/<(\/?)([A-Za-z][\w:.-]*)(?:\s[^>]*?)?(\/?)>/g)) { + if (self) continue; + if (!close) stack.push(name); + else assert.equal(stack.pop(), name, `mismatched `); + } + assert.deepEqual(stack, []); + assert.doesNotMatch(xml.replace(/&(amp|lt|gt|quot|apos);/g, ''), /&/); +} + +test('resolveSpec validates ids, versions, URLs and hashes, and fills the defaults', () => { + const spec = resolveSpec(RAW); + assert.equal(spec.slug, 'some-app'); + assert.equal(spec.windows.scope, 'user'); + assert.equal(spec.licenseUrl, 'https://spdx.org/licenses/GPL-3.0-or-later.html'); + assert.throws(() => resolveSpec({ ...RAW, id: 'NoDot' }), /Publisher\.Package/); + assert.throws(() => resolveSpec({ ...RAW, version: 'v1' }), /version/); + assert.throws(() => resolveSpec({ ...RAW, homepage: 'http://x.test' }), /https/); + assert.throws(() => resolveSpec({ ...RAW, windows: { ...RAW.windows, url: 'http://x.test/a.exe' } }), /https/); + assert.throws(() => resolveSpec({ ...RAW, windows: { ...RAW.windows, sha256: 'abc' } }), /sha256/); + assert.throws(() => resolveSpec({ ...RAW, windows: { sha256: SHA } }), /url/); + assert.throws(() => resolveSpec({ publisher: 'E', id: 'E.A', name: 'A', version: '1.0.0', shortDescription: 's', homepage: 'https://a.test', license: 'MIT' }), /at least one/); +}); + +test('a file installer is hashed here and its URL comes from the download base, never invented', () => { + const dir = mkdtempSync(join(tmpdir(), 'wb-man-')); + writeFileSync(join(dir, 'Some-App_1.2.3_x64-setup.exe'), 'hello'); + const raw = { ...RAW, macos: undefined, linux: undefined, windows: { file: 'Some-App_1.2.3_x64-setup.exe' } }; + assert.throws(() => resolveSpec(raw, { baseDir: dir }), /download-base/); + const spec = resolveSpec(raw, { baseDir: dir, downloadBase: 'https://dl.example.test/1.2.3/' }); + assert.equal(spec.windows.url, 'https://dl.example.test/1.2.3/Some-App_1.2.3_x64-setup.exe'); + assert.equal(spec.windows.sha256, '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'); +}); + +test('winget: version, installer and locale manifests match the schema constraints', () => { + const { files } = buildManifests(resolveSpec(RAW), { only: ['winget'] }); + const byName = Object.fromEntries(Object.entries(files).map(([path, text]) => [path.replaceAll('\\', '/'), text])); + const base = 'winget/manifests/e/Example/Some-App/1.2.3/Example.Some-App'; + assert.deepEqual(Object.keys(byName).sort(), [`${base}.installer.yaml`, `${base}.locale.en-US.yaml`, `${base}.yaml`]); + const version = parseYaml(byName[`${base}.yaml`]); + const installer = parseYaml(byName[`${base}.installer.yaml`]); + const locale = parseYaml(byName[`${base}.locale.en-US.yaml`]); + const idPattern = /^[^.\s\\/:*?"<>|\x01-\x1f]{1,32}(\.[^.\s\\/:*?"<>|\x01-\x1f]{1,32}){1,7}$/; + for (const manifest of [version, installer, locale]) { + assert.match(manifest.PackageIdentifier, idPattern); + assert.equal(manifest.PackageVersion, '1.2.3'); + assert.equal(manifest.ManifestVersion, WINGET_MANIFEST_VERSION); + } + assert.deepEqual([version.ManifestType, installer.ManifestType, locale.ManifestType], ['version', 'installer', 'defaultLocale']); + assert.equal(version.DefaultLocale, 'en-US'); + assert.equal(locale.PackageLocale, 'en-US'); + for (const key of ['Publisher', 'PackageName', 'License', 'ShortDescription']) assert.ok(locale[key], key); + assert.equal(locale.ShortDescription, RAW.shortDescription); + assert.equal(locale.Description, 'Line one\nLine two'); + assert.deepEqual(locale.Tags, ['a', 'b']); + assert.ok(installer.Installers.length >= 1); + const [entry] = installer.Installers; + assert.match(entry.Architecture, /^(x86|x64|arm|arm64|neutral)$/); + assert.match(entry.InstallerUrl, /^https:\/\//); + assert.match(entry.InstallerSha256, /^[A-F0-9]{64}$/); + assert.equal(entry.InstallerSha256, SHA.toUpperCase()); + assert.equal(installer.InstallerType, 'nullsoft'); + assert.equal(installer.InstallerSwitches.Silent, '/S'); + assert.match(installer.Scope, /^(user|machine)$/); + assert.ok(installer.InstallModes.every(mode => ['interactive', 'silent', 'silentWithProgress'].includes(mode))); + assert.match(byName[`${base}.yaml`], /^# yaml-language-server: \$schema=https:\/\/aka\.ms\/winget-manifest\.version\.1\.28\.0\.schema\.json$/m); +}); + +test('toYaml quotes what YAML would misread', () => { + const text = toYaml({ A: 'true', B: '1.0', C: 'a: b', D: '- x', E: '#c', F: 'plain words', G: '' }); + const back = parseYaml(text); + assert.equal(back.A, 'true'); + assert.equal(back.B, '1.0'); + assert.equal(back.C, 'a: b'); + assert.equal(back.D, '- x'); + assert.equal(back.E, '#c'); + assert.equal(back.F, 'plain words'); +}); + +test('chocolatey: the nuspec is well-formed XML and the install script carries the checksum', () => { + const { files } = buildManifests(resolveSpec({ ...RAW, shortDescription: 'A & B "q"' }), { only: ['chocolatey'] }); + const nuspec = files[join('chocolatey', 'some-app', 'some-app.nuspec')]; + const script = files[join('chocolatey', 'some-app', 'tools', 'chocolateyInstall.ps1')]; + wellFormed(nuspec); + assert.match(nuspec, /some-app<\/id>/); + assert.match(nuspec, /1\.2\.3<\/version>/); + assert.match(nuspec, /A & B <tags> "q"/); + assert.match(script, new RegExp(`checksum64\\s+= '${SHA.toLowerCase()}'`)); + assert.match(script, /checksumType64 = 'sha256'/); + assert.match(script, /silentArgs\s+= '\/S'/); + assert.match(script, /url64bit\s+= 'https:\/\/dl\.example\.test\/1\.2\.3\/Some-App_1\.2\.3_x64-setup\.exe'/); + if (process.platform === 'win32') { + const dir = mkdtempSync(join(tmpdir(), 'wb-ps-')); + const file = join(dir, 'chocolateyInstall.ps1'); + writeFileSync(file, script); + const run = spawnSync('powershell', ['-NoProfile', '-Command', `$e=$null; [System.Management.Automation.Language.Parser]::ParseFile('${file}',[ref]$null,[ref]$e) | Out-Null; if ($e.Count) { $e | ForEach-Object { $_.Message }; exit 1 }`], { encoding: 'utf8' }); + if (!run.error) assert.equal(run.status, 0, run.stdout); + } +}); + +test('homebrew: a cask with the version interpolated in the URL', () => { + const { files } = buildManifests(resolveSpec(RAW), { only: ['homebrew'] }); + const cask = files[join('homebrew', 'Casks', 'some-app.rb')]; + assert.match(cask, /^cask "some-app" do$/m); + assert.match(cask, /version "1\.2\.3"/); + assert.match(cask, new RegExp(`sha256 "${SHA2}"`)); + assert.match(cask, /url "https:\/\/dl\.example\.test\/#\{version\}\/Some-App_#\{version\}\.dmg"/); + assert.match(cask, /app "Some App\.app"/); + assert.match(cask, /zap trash: \[\n "~\/Library\/Application Support\/x\.y",\n \]/); + assert.match(cask, /end\n$/); + const ruby = spawnSync('ruby', ['-c'], { input: cask, encoding: 'utf8' }); + if (!ruby.error) assert.equal(ruby.status, 0, ruby.stderr); +}); + +test('aur: PKGBUILD from the .deb, and a .SRCINFO that agrees with it', () => { + const { files } = buildManifests(resolveSpec(RAW), { only: ['aur'] }); + const pkgbuild = files[join('aur', 'some-app-bin', 'PKGBUILD')]; + const srcinfo = files[join('aur', 'some-app-bin', '.SRCINFO')]; + assert.match(pkgbuild, /^# Maintainer: Example$/m); + assert.match(pkgbuild, /^pkgname=some-app-bin$/m); + assert.match(pkgbuild, /^pkgver=1\.2\.3$/m); + assert.match(pkgbuild, /^source=\("\$\{pkgname\}-\$\{pkgver\}\.deb::https:\/\/dl\.example\.test\/\$\{pkgver\}\/some-app_\$\{pkgver\}_amd64\.deb"\)$/m); + assert.match(pkgbuild, new RegExp(`^sha256sums=\\('${SHA2}'\\)$`, 'm')); + assert.match(pkgbuild, /bsdtar -xf "\$srcdir"\/data\.tar\.\* -C "\$pkgdir"/); + assert.match(pkgbuild, /^depends=\('webkit2gtk-4\.1' 'gtk3'\)$/m); + const info = Object.fromEntries(srcinfo.split('\n').filter(line => line.includes(' = ')).map(line => line.trim().split(' = '))); + assert.equal(info.pkgbase, 'some-app-bin'); + assert.equal(info.pkgver, '1.2.3'); + assert.equal(info.source, 'some-app-bin-1.2.3.deb::https://dl.example.test/1.2.3/some-app_1.2.3_amd64.deb'); + assert.equal(info.sha256sums, SHA2); + assert.ok(srcinfo.endsWith('pkgname = some-app-bin\n')); + const syntax = spawnSync('bash', ['-n'], { input: pkgbuild, encoding: 'utf8' }); + if (!syntax.error) assert.equal(syntax.status, 0, syntax.stderr); +}); + +test('aur: an AppImage-only package installs under /opt with a launcher', () => { + const { files } = buildManifests(resolveSpec({ ...RAW, linux: { appImage: RAW.linux.appImage } }), { only: ['aur'] }); + const pkgbuild = files[join('aur', 'some-app-bin', 'PKGBUILD')]; + assert.match(pkgbuild, /^noextract=\("some-app-bin-1\.2\.3\.AppImage"\)$/m); + assert.match(pkgbuild, /install -Dm755 "\$srcdir\/some-app-bin-1\.2\.3\.AppImage" "\$pkgdir\/opt\/some-app\/some-app\.AppImage"/); + assert.doesNotMatch(pkgbuild, /^depends=/m); +}); + +test('a description without a platform skips its channels; unknown channels fail', () => { + const { files, skipped } = buildManifests(resolveSpec({ ...RAW, macos: undefined, linux: undefined })); + assert.deepEqual(skipped, ['homebrew', 'aur']); + assert.ok(Object.keys(files).some(path => path.includes('winget'))); + assert.throws(() => buildManifests(resolveSpec(RAW), { only: ['snap'] }), /unknown channel/); +}); + +test('the command: writes the tree from a file, LF endings, and prints what it wrote', () => { + const dir = mkdtempSync(join(tmpdir(), 'wb-man-cli-')); + writeFileSync(join(dir, 'app.json'), JSON.stringify(RAW)); + const out = execFileSync(process.execPath, [BIN, 'manifests', join(dir, 'app.json'), '--out', join(dir, 'out')], { encoding: 'utf8' }); + assert.match(out, /Some-App\.yaml|Example\.Some-App\.yaml/); + const pkgbuild = join(dir, 'out', 'aur', 'some-app-bin', 'PKGBUILD'); + assert.ok(existsSync(pkgbuild)); + assert.doesNotMatch(readFileSync(pkgbuild, 'utf8'), /\r/); + assert.throws(() => execFileSync(process.execPath, [BIN, 'manifests'], { encoding: 'utf8', stdio: 'pipe' })); + writeManifests(resolveSpec(RAW), join(dir, 'again'), { only: ['homebrew'] }); + assert.ok(existsSync(join(dir, 'again', 'homebrew', 'Casks', 'some-app.rb'))); +}); + +test('the real tools accept the files when they are installed: winget validate, choco pack', () => { + const dir = mkdtempSync(join(tmpdir(), 'wb-real-')); + writeManifests(resolveSpec(RAW), dir, { only: ['winget', 'chocolatey'] }); + const winget = spawnSync('winget', ['validate', '--manifest', join(dir, 'winget', 'manifests', 'e', 'Example', 'Some-App', '1.2.3')], { encoding: 'utf8' }); + if (!winget.error) { + assert.match(winget.stdout, /Manifest validation succeeded/, winget.stdout); + assert.doesNotMatch(winget.stdout, /Manifest Error/); + } + const choco = spawnSync('choco', ['pack', '--limit-output'], { cwd: join(dir, 'chocolatey', 'some-app'), encoding: 'utf8' }); + if (!choco.error && choco.status !== null) { + assert.match(choco.stdout, /Successfully created package|some-app/i, choco.stdout + choco.stderr); + assert.ok(existsSync(join(dir, 'chocolatey', 'some-app', 'some-app.1.2.3.nupkg'))); + } +});