GeezerVison is showing github.com — feat(cli): publish and upgrade official WinGet packages (#54808) · carllewis/opencode@d46588f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/workflows/publish.yml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -652,6 +652,7 @@ jobs: | 652 | 652 | TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} | 653 | 653 | TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }} | 654 | 654 | OPENCODE_DESKTOP_DIST: /tmp/desktop | | 655 | + WINGET_GITHUB_TOKEN: ${{ secrets.WINGET_GITHUB_TOKEN }} | 655 | 656 | CLOUDFLARE_ACCOUNT_ID: 15d29c8639fd3733b1b5486a2acfd968 | 656 | 657 | CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | 657 | 658 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/services/retained-image.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,14 +52,15 @@ export const relocate = Effect.fnUntraced(function* (directory: string, destinat | 52 | 52 |
| 53 | 53 | /** | 54 | 54 | * Whether the running binary was installed by a package manager or the curl installer, the installs | 55 | | - * bun can hang on. The updater decides the same question from the package manifest it already reads. | | 55 | + * whose upgrades replace it in place. The updater decides the same question from the install location. | 56 | 56 | */ | 57 | 57 | export function installed(home: string) { | 58 | 58 | const runtime = path.basename(process.execPath, path.extname(process.execPath)).toLowerCase() | 59 | 59 | if (runtime === "bun" || runtime === "node" || runtime === "nodejs") return false | 60 | 60 | const executable = path.resolve(process.execPath) | 61 | 61 | return ( | 62 | 62 | executable.split(path.sep).includes("node_modules") || | | 63 | + /[\\/]winget[\\/]packages[\\/]/i.test(executable) || | 63 | 64 | executable === path.resolve(home, ".opencode", "bin", "opencode.exe") | 64 | 65 | ) | 65 | 66 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/src/services/updater.ts +83-8Lines changed: 83 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -11,7 +11,7 @@ import { RetainedImage } from "./retained-image" | 11 | 11 | import { action, parseReleaseVersion, type Policy } from "./updater-action" | 12 | 12 | import { errorMessage } from "../util/error" | 13 | 13 |
| 14 | | -export const methods = ["curl", "npm", "pnpm", "bun", "yarn", "vp", "brew"] as const | | 14 | +export const methods = ["curl", "npm", "pnpm", "bun", "yarn", "vp", "brew", "winget"] as const | 15 | 15 |
| 16 | 16 | export type Method = (typeof methods)[number] | 17 | 17 | export type RunResult = { readonly type: "available" | "installed"; readonly version: string } | @@ -53,8 +53,20 @@ const installNames: Record<Method, string> = { | 53 | 53 | yarn: "Yarn", | 54 | 54 | vp: "Vite+", | 55 | 55 | brew: "Homebrew", | | 56 | + winget: "WinGet", | 56 | 57 | } | 57 | 58 |
| | 59 | +const wingetPackage = "Anomaly.OpenCode" | | 60 | +const wingetArgs = [ | | 61 | + "--id", | | 62 | + wingetPackage, | | 63 | + "--exact", | | 64 | + "--source", | | 65 | + "winget", | | 66 | + "--accept-source-agreements", | | 67 | + "--disable-interactivity", | | 68 | +] | | 69 | + | 58 | 70 | function conciseDetail(input: string) { | 59 | 71 | const lines = stripVTControlCharacters(input) | 60 | 72 | .trim() | @@ -146,6 +158,8 @@ const make = Effect.gen(function* () { | 146 | 158 | const installedFormula = ["opencode", "opencode-beta", "opencode-v2"].find((name) => | 147 | 159 | executable.includes(`${path.sep}Cellar${path.sep}${name}${path.sep}`), | 148 | 160 | ) | | 161 | + // WinGet extracts portable packages into a directory named after the package identifier. | | 162 | + const installedWinget = /[\\/]winget[\\/]packages[\\/]anomaly\.opencode_/i.test(executable) | 149 | 163 |
| 150 | 164 | const readPolicy = Effect.fnUntraced(function* () { | 151 | 165 | const values = yield* Effect.forEach(["config.json", "opencode.json", "opencode.jsonc"], (name) => | @@ -200,6 +214,7 @@ const make = Effect.gen(function* () { | 200 | 214 | const method = Effect.fnUntraced(function* () { | 201 | 215 | if (executable === curlBinary) return "curl" | 202 | 216 | if (installedFormula) return "brew" | | 217 | + if (installedWinget) return "winget" | 203 | 218 | if (!installedPackage) return | 204 | 219 |
| 205 | 220 | const parts = executable.toLowerCase().split(path.sep).map((part) => part.replace(/^\./, "")) | @@ -229,7 +244,8 @@ const make = Effect.gen(function* () { | 229 | 244 | })?.check.method | 230 | 245 | }) | 231 | 246 |
| 232 | | - const removal = (method: Method) => { | | 247 | + const removalCommand = (method: Method) => { | | 248 | + if (method === "winget") return ["winget", "uninstall", ...wingetArgs] | 233 | 249 | if (method === "curl" || method === "brew" || !installedPackage) return undefined | 234 | 250 | const commands = { | 235 | 251 | npm: ["npm", "uninstall", "--global", installedPackage], | @@ -238,7 +254,12 @@ const make = Effect.gen(function* () { | 238 | 254 | yarn: ["yarn", "global", "remove", installedPackage], | 239 | 255 | vp: ["vp", "uninstall", "-g", installedPackage], | 240 | 256 | } | 241 | | - const command = commands[method] | | 257 | + return commands[method] | | 258 | + } | | 259 | + | | 260 | + const removal = (method: Method) => { | | 261 | + const command = removalCommand(method) | | 262 | + if (!command) return undefined | 242 | 263 | return { | 243 | 264 | command, | 244 | 265 | run: retaining( | @@ -309,10 +330,39 @@ const make = Effect.gen(function* () { | 309 | 330 | return { package: packageName, version } | 310 | 331 | }) | 311 | 332 |
| | 333 | + // WinGet moderates every submission, so only its own index knows which release it can install. | | 334 | + const wingetVersion = Effect.fnUntraced(function* () { | | 335 | + const failure = (detail: string, cause?: unknown) => | | 336 | + new UpgradeError( | | 337 | + { | | 338 | + title: "Could not check for OpenCode updates", | | 339 | + detail, | | 340 | + command: ["winget", "search", ...wingetArgs].join(" "), | | 341 | + retry: "Fix the issue above, then run opencode upgrade again.", | | 342 | + }, | | 343 | + cause === undefined ? undefined : { cause }, | | 344 | + ) | | 345 | + const result = yield* exec(["winget", "search", ...wingetArgs], "1 minute").pipe( | | 346 | + Effect.mapError((cause) => failure(errorDetail(cause), cause)), | | 347 | + ) | | 348 | + // Column headers are localized, but the result row always lists the identifier followed by the version. | | 349 | + const version = result.stdout | | 350 | + .split(/\r?\n|\r/) | | 351 | + .map((line) => line.trim().split(/\s+/)) | | 352 | + .flatMap((tokens) => { | | 353 | + const index = tokens.findIndex((token) => token.toLowerCase() === wingetPackage.toLowerCase()) | | 354 | + return index === -1 ? [] : tokens.slice(index + 1, index + 2) | | 355 | + }) | | 356 | + .find((token) => parseReleaseVersion(token)) | | 357 | + if (result.code !== 0 || !version) return yield* Effect.fail(failure(resultDetail(result))) | | 358 | + return version | | 359 | + }) | | 360 | + | 312 | 361 | const latest = (selected?: Method) => | 313 | 362 | (selected ? Effect.succeed(selected) : method()).pipe( | 314 | | - Effect.flatMap(release), | 315 | | - Effect.map((data) => data.version), | | 363 | + Effect.flatMap((method) => | | 364 | + method === "winget" ? wingetVersion() : release(method).pipe(Effect.map((data) => data.version)), | | 365 | + ), | 316 | 366 | ) | 317 | 367 |
| 318 | 368 | const temporaryDirectory = (prefix: string) => | @@ -326,7 +376,12 @@ const make = Effect.gen(function* () { | 326 | 376 | const retaining = <A, E, R>(method: Method, effect: Effect.Effect<A, E, R>, directory = global.cache) => { | 327 | 377 | if (process.platform !== "win32" || method === "brew") return effect | 328 | 378 | // Only the installed binary is at stake; source checkouts run inside bun or node. | 329 | | - const owned = method === "curl" ? executable === curlBinary : installedPackage !== undefined | | 379 | + const owned = | | 380 | + method === "curl" | | 381 | + ? executable === curlBinary | | 382 | + : method === "winget" | | 383 | + ? installedWinget | | 384 | + : installedPackage !== undefined | 330 | 385 | if (!owned) return effect | 331 | 386 | return Effect.scoped(RetainedImage.retain(directory, "upgrade").pipe(Effect.andThen(effect))).pipe( | 332 | 387 | Effect.provideService(FileSystem.FileSystem, fs), | @@ -371,12 +426,16 @@ const make = Effect.gen(function* () { | 371 | 426 | if (!parseReleaseVersion(input)) return yield* Effect.fail(new Error(`Invalid version: ${input}`)) | 372 | 427 | const version = input.trim().replace(/^v/, "") | 373 | 428 | const packageName = | 374 | | - method === "brew" && installedFormula === "opencode" ? "opencode" : (yield* release(method)).package | | 429 | + method === "winget" | | 430 | + ? wingetPackage | | 431 | + : method === "brew" && installedFormula === "opencode" | | 432 | + ? "opencode" | | 433 | + : (yield* release(method)).package | 375 | 434 | const target = `${packageName}@${version}` | 376 | 435 | if (installedPackage && packageName !== installedPackage && (method === "pnpm" || method === "yarn")) { | 377 | 436 | return yield* Effect.fail(new Error(`Reinstall ${target} with ${method} to migrate from ${installedPackage}.`)) | 378 | 437 | } | 379 | | - const commands: Record<Exclude<Method, "bun" | "curl" | "brew">, string[]> = { | | 438 | + const commands: Record<Exclude<Method, "bun" | "curl" | "brew" | "winget">, string[]> = { | 380 | 439 | // Keep the old package: uninstalling it can unlink the replacement command. | 381 | 440 | npm: [ | 382 | 441 | "npm", | @@ -436,6 +495,22 @@ const make = Effect.gen(function* () { | 436 | 495 | ) | 437 | 496 | } | 438 | 497 | if (method === "brew") return yield* runUpgrade({ method, command: ["brew", "upgrade", packageName] }) | | 498 | + if (method === "winget") | | 499 | + return yield* retaining( | | 500 | + method, | | 501 | + runUpgrade({ | | 502 | + method, | | 503 | + command: [ | | 504 | + "winget", | | 505 | + "upgrade", | | 506 | + ...wingetArgs, | | 507 | + "--version", | | 508 | + version, | | 509 | + "--silent", | | 510 | + "--accept-package-agreements", | | 511 | + ], | | 512 | + }), | | 513 | + ) | 439 | 514 | return yield* retaining(method, runUpgrade({ method, command: commands[method] })) | 440 | 515 | }), | 441 | 516 | ).pipe( | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/test/retained-image.test.ts +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,6 +68,19 @@ windows("installed accepts package-manager and curl binaries only", () => | 68 | 68 | const cases = [ | 69 | 69 | [path.join(home, ".bun", "install", "global", "node_modules", "@opencode", "cli", "bin", "opencode.exe"), true], | 70 | 70 | [path.join(home, ".opencode", "bin", "opencode.exe"), true], | | 71 | + [ | | 72 | + path.join( | | 73 | + home, | | 74 | + "AppData", | | 75 | + "Local", | | 76 | + "Microsoft", | | 77 | + "WinGet", | | 78 | + "Packages", | | 79 | + "Anomaly.OpenCode_Microsoft.Winget.Source_8wekyb3d8bbwe", | | 80 | + "opencode.exe", | | 81 | + ), | | 82 | + true, | | 83 | + ], | 71 | 84 | [path.join(home, "AppData", "Local", "Programs", "OpenCode", "resources", "opencode-cli.exe"), false], | 72 | 85 | [path.join(home, "node_modules", "bun", "bin", "bun.exe"), false], | 73 | 86 | ] as const | |
Collapse file Copy file name to clipboardExpand all lines: packages/cli/test/updater-install.test.ts +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -340,6 +340,71 @@ it.live("Homebrew Core installs check and upgrade the Core formula", () => | 340 | 340 | ) | 341 | 341 | }) | 342 | 342 |
| | 343 | +const winget = (search: Partial<AppProcess.RunResult>) => | | 344 | + fixture( | | 345 | + (command) => (command.args[0] === "search" ? search : {}), | | 346 | + "@opencode/cli", | | 347 | + false, | | 348 | + "@opencode/cli", | | 349 | + undefined, | | 350 | + (root) => | | 351 | + Effect.succeed( | | 352 | + path.join( | | 353 | + root, | | 354 | + "AppData", | | 355 | + "Local", | | 356 | + "Microsoft", | | 357 | + "WinGet", | | 358 | + "Packages", | | 359 | + "Anomaly.OpenCode_Microsoft.Winget.Source_8wekyb3d8bbwe", | | 360 | + "opencode.exe", | | 361 | + ), | | 362 | + ), | | 363 | + ) | | 364 | +const wingetArgs = [ | | 365 | + "--id", | | 366 | + "Anomaly.OpenCode", | | 367 | + "--exact", | | 368 | + "--source", | | 369 | + "winget", | | 370 | + "--accept-source-agreements", | | 371 | + "--disable-interactivity", | | 372 | +] | | 373 | + | | 374 | +it.live("WinGet installs check, upgrade, and remove the version in the WinGet index", () => | | 375 | + Effect.gen(function* () { | | 376 | + const test = yield* winget({ | | 377 | + stdout: Buffer.from( | | 378 | + "Name Id Version\r\n------------------------------\r\nOpenCode Anomaly.OpenCode 2.0.28\r\n", | | 379 | + ), | | 380 | + }) | | 381 | + expect(yield* test.updater.method()).toBe("winget") | | 382 | + expect(yield* test.updater.latest()).toBe("2.0.28") | | 383 | + yield* test.updater.upgrade("winget", "v2.0.28") | | 384 | + const removal = test.updater.removal("winget") | | 385 | + if (!removal) return yield* Effect.die("Expected winget removal command") | | 386 | + yield* removal.run | | 387 | + expect(test.commands).toEqual([ | | 388 | + ["winget", "search", ...wingetArgs], | | 389 | + ["winget", "upgrade", ...wingetArgs, "--version", "2.0.28", "--silent", "--accept-package-agreements"], | | 390 | + ["winget", "uninstall", ...wingetArgs], | | 391 | + ]) | | 392 | + // WinGet moderates submissions, so the OpenCode release service may list a version WinGet cannot install yet. | | 393 | + expect(test.requests).toEqual([]) | | 394 | + }), | | 395 | +) | | 396 | + | | 397 | +it.live("WinGet update checks report a missing package instead of a version", () => | | 398 | + Effect.gen(function* () { | | 399 | + const test = yield* winget({ | | 400 | + exitCode: -1978335212, | | 401 | + stdout: Buffer.from("No package found matching input criteria.\r\n"), | | 402 | + }) | | 403 | + const error = yield* test.updater.latest().pipe(Effect.flip) | | 404 | + expect(error.message).toBe("No package found matching input criteria.") | | 405 | + }), | | 406 | +) | | 407 | + | 343 | 408 | // Links are named opencode-upgrade-<pid>-<random>.exe; read them from inside the installer run. | 344 | 409 | const links = (directory: string) => | 345 | 410 | existsSync(directory) ? readdirSync(directory).filter((name) => name.startsWith("opencode-")) : [] | |
|