|
|
@@ -1,7 +1,8 @@
|
|
|
#!/usr/bin/env bun
|
|
|
|
|
|
-import { Buffer } from "node:buffer"
|
|
|
import { $ } from "bun"
|
|
|
+import path from "node:path"
|
|
|
+import { parseArgs } from "node:util"
|
|
|
|
|
|
const { values } = parseArgs({
|
|
|
args: Bun.argv.slice(2),
|
|
|
@@ -12,145 +13,167 @@ const { values } = parseArgs({
|
|
|
|
|
|
const dryRun = values["dry-run"]
|
|
|
|
|
|
-import { parseArgs } from "node:util"
|
|
|
-
|
|
|
const repo = process.env.GH_REPO
|
|
|
if (!repo) throw new Error("GH_REPO is required")
|
|
|
|
|
|
-const releaseId = process.env.OPENCODE_RELEASE
|
|
|
-if (!releaseId) throw new Error("OPENCODE_RELEASE is required")
|
|
|
-
|
|
|
const version = process.env.OPENCODE_VERSION
|
|
|
-if (!releaseId) throw new Error("OPENCODE_VERSION is required")
|
|
|
+if (!version) throw new Error("OPENCODE_VERSION is required")
|
|
|
+
|
|
|
+const dir = process.env.LATEST_YML_DIR
|
|
|
+if (!dir) throw new Error("LATEST_YML_DIR is required")
|
|
|
+const root = dir
|
|
|
|
|
|
const token = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN
|
|
|
if (!token) throw new Error("GH_TOKEN or GITHUB_TOKEN is required")
|
|
|
|
|
|
-const apiHeaders = {
|
|
|
- Authorization: `token ${token}`,
|
|
|
- Accept: "application/vnd.github+json",
|
|
|
-}
|
|
|
-
|
|
|
-const releaseRes = await fetch(`https://api.github.com/repos/${repo}/releases/${releaseId}`, {
|
|
|
- headers: apiHeaders,
|
|
|
-})
|
|
|
-
|
|
|
-if (!releaseRes.ok) {
|
|
|
- throw new Error(`Failed to fetch release: ${releaseRes.status} ${releaseRes.statusText}`)
|
|
|
-}
|
|
|
-
|
|
|
-type Asset = {
|
|
|
- name: string
|
|
|
+type Item = {
|
|
|
url: string
|
|
|
}
|
|
|
|
|
|
-type Release = {
|
|
|
- tag_name?: string
|
|
|
- assets?: Asset[]
|
|
|
+type Yml = {
|
|
|
+ version: string
|
|
|
+ files: Item[]
|
|
|
}
|
|
|
|
|
|
-const release = (await releaseRes.json()) as Release
|
|
|
-const assets = release.assets ?? []
|
|
|
-const assetByName = new Map(assets.map((asset) => [asset.name, asset]))
|
|
|
+function parse(text: string): Yml {
|
|
|
+ const lines = text.split("\n")
|
|
|
+ let version = ""
|
|
|
+ const files: Item[] = []
|
|
|
+ let url = ""
|
|
|
|
|
|
-const latestAsset = assetByName.get("latest.json")
|
|
|
-if (!latestAsset) throw new Error("latest.json asset not found")
|
|
|
+ const flush = () => {
|
|
|
+ if (!url) return
|
|
|
+ files.push({ url })
|
|
|
+ url = ""
|
|
|
+ }
|
|
|
|
|
|
-const latestRes = await fetch(latestAsset.url, {
|
|
|
- headers: {
|
|
|
- Authorization: `token ${token}`,
|
|
|
- Accept: "application/octet-stream",
|
|
|
- },
|
|
|
-})
|
|
|
+ for (const line of lines) {
|
|
|
+ const trim = line.trim()
|
|
|
+ if (line.startsWith("version:")) {
|
|
|
+ version = line.slice("version:".length).trim()
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if (trim.startsWith("- url:")) {
|
|
|
+ flush()
|
|
|
+ url = trim.slice("- url:".length).trim()
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ const indented = line.startsWith(" ") || line.startsWith("\t")
|
|
|
+ if (!indented) flush()
|
|
|
+ }
|
|
|
+ flush()
|
|
|
|
|
|
-if (!latestRes.ok) {
|
|
|
- throw new Error(`Failed to fetch latest.json: ${latestRes.status} ${latestRes.statusText}`)
|
|
|
+ return { version, files }
|
|
|
}
|
|
|
|
|
|
-const latestText = new TextDecoder().decode(await latestRes.arrayBuffer())
|
|
|
-const latest = JSON.parse(latestText)
|
|
|
-const base = { ...latest }
|
|
|
-delete base.platforms
|
|
|
-
|
|
|
-const fetchSignature = async (asset: Asset) => {
|
|
|
- const res = await fetch(asset.url, {
|
|
|
- headers: {
|
|
|
- Authorization: `token ${token}`,
|
|
|
- Accept: "application/octet-stream",
|
|
|
- },
|
|
|
- })
|
|
|
-
|
|
|
- if (!res.ok) {
|
|
|
- throw new Error(`Failed to fetch signature: ${res.status} ${res.statusText}`)
|
|
|
- }
|
|
|
-
|
|
|
- return Buffer.from(await res.arrayBuffer()).toString()
|
|
|
+async function read(sub: string, file: string) {
|
|
|
+ const item = Bun.file(path.join(root, sub, file))
|
|
|
+ if (!(await item.exists())) return undefined
|
|
|
+ return parse(await item.text())
|
|
|
}
|
|
|
|
|
|
-const entries: Record<string, { url: string; signature: string }> = {}
|
|
|
-const add = (key: string, asset: Asset, signature: string) => {
|
|
|
- if (entries[key]) return
|
|
|
- entries[key] = {
|
|
|
- url: `https://github.com/${repo}/releases/download/v${version}/${asset.name}`,
|
|
|
- signature,
|
|
|
+function pick(list: Item[], exts: string[]) {
|
|
|
+ for (const ext of exts) {
|
|
|
+ const found = list.find((item) => item.url.split("?")[0]?.toLowerCase().endsWith(ext))
|
|
|
+ if (found) return found.url
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-const targets = [
|
|
|
- { key: "linux-x86_64-deb", asset: "opencode-desktop-linux-amd64.deb" },
|
|
|
- { key: "linux-x86_64-rpm", asset: "opencode-desktop-linux-x86_64.rpm" },
|
|
|
- { key: "linux-aarch64-deb", asset: "opencode-desktop-linux-arm64.deb" },
|
|
|
- { key: "linux-aarch64-rpm", asset: "opencode-desktop-linux-aarch64.rpm" },
|
|
|
- { key: "windows-aarch64-nsis", asset: "opencode-desktop-windows-arm64.exe" },
|
|
|
- { key: "windows-x86_64-nsis", asset: "opencode-desktop-windows-x64.exe" },
|
|
|
- { key: "darwin-x86_64-app", asset: "opencode-desktop-darwin-x64.app.tar.gz" },
|
|
|
- {
|
|
|
- key: "darwin-aarch64-app",
|
|
|
- asset: "opencode-desktop-darwin-aarch64.app.tar.gz",
|
|
|
- },
|
|
|
-]
|
|
|
+function link(raw: string) {
|
|
|
+ if (raw.startsWith("https://") || raw.startsWith("http://")) return raw
|
|
|
+ return `https://github.com/${repo}/releases/download/v${version}/${raw}`
|
|
|
+}
|
|
|
|
|
|
-for (const target of targets) {
|
|
|
- const asset = assetByName.get(target.asset)
|
|
|
- if (!asset) continue
|
|
|
+function lkey(arch: string, raw: string | undefined) {
|
|
|
+ if (!raw) return
|
|
|
+ const low = raw.split("?")[0]?.toLowerCase() ?? ""
|
|
|
+ if (low.endsWith(".deb")) return `linux-${arch}-deb`
|
|
|
+ if (low.endsWith(".rpm")) return `linux-${arch}-rpm`
|
|
|
+ if (low.endsWith(".appimage")) return `linux-${arch}-appimage`
|
|
|
+}
|
|
|
|
|
|
- const sig = assetByName.get(`${target.asset}.sig`)
|
|
|
- if (!sig) continue
|
|
|
+async function sign(url: string, key: string) {
|
|
|
+ const res = await fetch(url, {
|
|
|
+ headers: { Authorization: `token ${token}` },
|
|
|
+ })
|
|
|
+ if (!res.ok) throw new Error(`Failed to fetch file: ${res.status} ${res.statusText}`)
|
|
|
+
|
|
|
+ const name = decodeURIComponent(new URL(url).pathname.split("/").pop() ?? key)
|
|
|
+ const tmp = process.env.RUNNER_TEMP ?? "/tmp"
|
|
|
+ const file = path.join(tmp, name)
|
|
|
+ await Bun.write(file, await res.arrayBuffer())
|
|
|
+ const out = await $`bunx @tauri-apps/cli signer sign ${file}`.text()
|
|
|
+ return out.trim()
|
|
|
+}
|
|
|
|
|
|
- const signature = await fetchSignature(sig)
|
|
|
- add(target.key, asset, signature)
|
|
|
+const add = async (data: Record<string, { url: string; signature: string }>, key: string, raw: string | undefined) => {
|
|
|
+ if (!raw) return
|
|
|
+ if (data[key]) return
|
|
|
+ const url = link(raw)
|
|
|
+ data[key] = { url, signature: await sign(url, key) }
|
|
|
}
|
|
|
|
|
|
-const alias = (key: string, source: string) => {
|
|
|
- if (entries[key]) return
|
|
|
- const entry = entries[source]
|
|
|
- if (!entry) return
|
|
|
- entries[key] = entry
|
|
|
+const alias = (data: Record<string, { url: string; signature: string }>, key: string, src: string) => {
|
|
|
+ if (data[key]) return
|
|
|
+ if (!data[src]) return
|
|
|
+ data[key] = data[src]
|
|
|
}
|
|
|
|
|
|
-alias("linux-x86_64", "linux-x86_64-deb")
|
|
|
-alias("linux-aarch64", "linux-aarch64-deb")
|
|
|
-alias("windows-aarch64", "windows-aarch64-nsis")
|
|
|
-alias("windows-x86_64", "windows-x86_64-nsis")
|
|
|
-alias("darwin-x86_64", "darwin-x86_64-app")
|
|
|
-alias("darwin-aarch64", "darwin-aarch64-app")
|
|
|
+const winx = await read("latest-yml-x86_64-pc-windows-msvc", "latest.yml")
|
|
|
+const wina = await read("latest-yml-aarch64-pc-windows-msvc", "latest.yml")
|
|
|
+const macx = await read("latest-yml-x86_64-apple-darwin", "latest-mac.yml")
|
|
|
+const maca = await read("latest-yml-aarch64-apple-darwin", "latest-mac.yml")
|
|
|
+const linx = await read("latest-yml-x86_64-unknown-linux-gnu", "latest-linux.yml")
|
|
|
+const lina = await read("latest-yml-aarch64-unknown-linux-gnu", "latest-linux-arm64.yml")
|
|
|
+
|
|
|
+const yver = winx?.version ?? wina?.version ?? macx?.version ?? maca?.version ?? linx?.version ?? lina?.version
|
|
|
+if (yver && yver !== version) throw new Error(`latest.yml version mismatch: expected ${version}, got ${yver}`)
|
|
|
+
|
|
|
+const out: Record<string, { url: string; signature: string }> = {}
|
|
|
+
|
|
|
+const winxexe = pick(winx?.files ?? [], [".exe"])
|
|
|
+const winaexe = pick(wina?.files ?? [], [".exe"])
|
|
|
+const macxzip = pick(macx?.files ?? [], [".zip", ".dmg"])
|
|
|
+const macazip = pick(maca?.files ?? [], [".zip", ".dmg"])
|
|
|
+const linxapp = pick(linx?.files ?? [], [".appimage", ".deb", ".rpm"])
|
|
|
+const linaapp = pick(lina?.files ?? [], [".appimage", ".deb", ".rpm"])
|
|
|
+const linxkey = lkey("x86_64", linxapp)
|
|
|
+const linakey = lkey("aarch64", linaapp)
|
|
|
+
|
|
|
+await add(out, "windows-x86_64-nsis", winxexe)
|
|
|
+await add(out, "windows-aarch64-nsis", winaexe)
|
|
|
+await add(out, "darwin-x86_64-app", macxzip)
|
|
|
+await add(out, "darwin-aarch64-app", macazip)
|
|
|
+if (linxkey) await add(out, linxkey, linxapp)
|
|
|
+if (linakey) await add(out, linakey, linaapp)
|
|
|
+
|
|
|
+alias(out, "windows-x86_64", "windows-x86_64-nsis")
|
|
|
+alias(out, "windows-aarch64", "windows-aarch64-nsis")
|
|
|
+alias(out, "darwin-x86_64", "darwin-x86_64-app")
|
|
|
+alias(out, "darwin-aarch64", "darwin-aarch64-app")
|
|
|
+if (linxkey) alias(out, "linux-x86_64", linxkey)
|
|
|
+if (linakey) alias(out, "linux-aarch64", linakey)
|
|
|
|
|
|
const platforms = Object.fromEntries(
|
|
|
- Object.keys(entries)
|
|
|
+ Object.keys(out)
|
|
|
.sort()
|
|
|
- .map((key) => [key, entries[key]]),
|
|
|
+ .map((key) => [key, out[key]]),
|
|
|
)
|
|
|
-const output = {
|
|
|
- ...base,
|
|
|
+
|
|
|
+if (!Object.keys(platforms).length) throw new Error("No updater files found in latest.yml artifacts")
|
|
|
+
|
|
|
+const data = {
|
|
|
+ version,
|
|
|
+ notes: "",
|
|
|
+ pub_date: new Date().toISOString(),
|
|
|
platforms,
|
|
|
}
|
|
|
|
|
|
-const dir = process.env.RUNNER_TEMP ?? "/tmp"
|
|
|
-const file = `${dir}/latest.json`
|
|
|
-await Bun.write(file, JSON.stringify(output, null, 2))
|
|
|
+const tmp = process.env.RUNNER_TEMP ?? "/tmp"
|
|
|
+const file = path.join(tmp, "latest.json")
|
|
|
+await Bun.write(file, JSON.stringify(data, null, 2))
|
|
|
|
|
|
-const tag = release.tag_name
|
|
|
-if (!tag) throw new Error("Release tag not found")
|
|
|
+const tag = `v${version}`
|
|
|
|
|
|
if (dryRun) {
|
|
|
console.log(`dry-run: wrote latest.json for ${tag} to ${file}`)
|