GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(cli): publish and upgrade official WinGet packages (#54808) · carllewis/opencode@d46588f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .github/workflows
    • publish.yml
  • packages/cli
    • src/services
      • retained-image.ts
      • updater.ts
    • test
      • retained-image.test.ts
      • updater-install.test.ts
  • script
    • publish-winget.ts
    • publish.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.github/workflows/publish.yml‎

Copy file name to clipboardExpand all lines: .github/workflows/publish.yml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ jobs:
652652
TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
653653
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
654654
OPENCODE_DESKTOP_DIST: /tmp/desktop
655+
WINGET_GITHUB_TOKEN: ${{ secrets.WINGET_GITHUB_TOKEN }}
655656
CLOUDFLARE_ACCOUNT_ID: 15d29c8639fd3733b1b5486a2acfd968
656657
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
657658

Collapse file

‎packages/cli/src/services/retained-image.ts‎

Copy file name to clipboardExpand all lines: packages/cli/src/services/retained-image.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ export const relocate = Effect.fnUntraced(function* (directory: string, destinat
5252

5353
/**
5454
* 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.
5656
*/
5757
export function installed(home: string) {
5858
const runtime = path.basename(process.execPath, path.extname(process.execPath)).toLowerCase()
5959
if (runtime === "bun" || runtime === "node" || runtime === "nodejs") return false
6060
const executable = path.resolve(process.execPath)
6161
return (
6262
executable.split(path.sep).includes("node_modules") ||
63+
/[\\/]winget[\\/]packages[\\/]/i.test(executable) ||
6364
executable === path.resolve(home, ".opencode", "bin", "opencode.exe")
6465
)
6566
}
Collapse file

‎packages/cli/src/services/updater.ts‎

Copy file name to clipboardExpand all lines: packages/cli/src/services/updater.ts
+83-8Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { RetainedImage } from "./retained-image"
1111
import { action, parseReleaseVersion, type Policy } from "./updater-action"
1212
import { errorMessage } from "../util/error"
1313

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
1515

1616
export type Method = (typeof methods)[number]
1717
export type RunResult = { readonly type: "available" | "installed"; readonly version: string }
@@ -53,8 +53,20 @@ const installNames: Record<Method, string> = {
5353
yarn: "Yarn",
5454
vp: "Vite+",
5555
brew: "Homebrew",
56+
winget: "WinGet",
5657
}
5758

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+
5870
function conciseDetail(input: string) {
5971
const lines = stripVTControlCharacters(input)
6072
.trim()
@@ -146,6 +158,8 @@ const make = Effect.gen(function* () {
146158
const installedFormula = ["opencode", "opencode-beta", "opencode-v2"].find((name) =>
147159
executable.includes(`${path.sep}Cellar${path.sep}${name}${path.sep}`),
148160
)
161+
// WinGet extracts portable packages into a directory named after the package identifier.
162+
const installedWinget = /[\\/]winget[\\/]packages[\\/]anomaly\.opencode_/i.test(executable)
149163

150164
const readPolicy = Effect.fnUntraced(function* () {
151165
const values = yield* Effect.forEach(["config.json", "opencode.json", "opencode.jsonc"], (name) =>
@@ -200,6 +214,7 @@ const make = Effect.gen(function* () {
200214
const method = Effect.fnUntraced(function* () {
201215
if (executable === curlBinary) return "curl"
202216
if (installedFormula) return "brew"
217+
if (installedWinget) return "winget"
203218
if (!installedPackage) return
204219

205220
const parts = executable.toLowerCase().split(path.sep).map((part) => part.replace(/^\./, ""))
@@ -229,7 +244,8 @@ const make = Effect.gen(function* () {
229244
})?.check.method
230245
})
231246

232-
const removal = (method: Method) => {
247+
const removalCommand = (method: Method) => {
248+
if (method === "winget") return ["winget", "uninstall", ...wingetArgs]
233249
if (method === "curl" || method === "brew" || !installedPackage) return undefined
234250
const commands = {
235251
npm: ["npm", "uninstall", "--global", installedPackage],
@@ -238,7 +254,12 @@ const make = Effect.gen(function* () {
238254
yarn: ["yarn", "global", "remove", installedPackage],
239255
vp: ["vp", "uninstall", "-g", installedPackage],
240256
}
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
242263
return {
243264
command,
244265
run: retaining(
@@ -309,10 +330,39 @@ const make = Effect.gen(function* () {
309330
return { package: packageName, version }
310331
})
311332

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+
312361
const latest = (selected?: Method) =>
313362
(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+
),
316366
)
317367

318368
const temporaryDirectory = (prefix: string) =>
@@ -326,7 +376,12 @@ const make = Effect.gen(function* () {
326376
const retaining = <A, E, R>(method: Method, effect: Effect.Effect<A, E, R>, directory = global.cache) => {
327377
if (process.platform !== "win32" || method === "brew") return effect
328378
// 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
330385
if (!owned) return effect
331386
return Effect.scoped(RetainedImage.retain(directory, "upgrade").pipe(Effect.andThen(effect))).pipe(
332387
Effect.provideService(FileSystem.FileSystem, fs),
@@ -371,12 +426,16 @@ const make = Effect.gen(function* () {
371426
if (!parseReleaseVersion(input)) return yield* Effect.fail(new Error(`Invalid version: ${input}`))
372427
const version = input.trim().replace(/^v/, "")
373428
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
375434
const target = `${packageName}@${version}`
376435
if (installedPackage && packageName !== installedPackage && (method === "pnpm" || method === "yarn")) {
377436
return yield* Effect.fail(new Error(`Reinstall ${target} with ${method} to migrate from ${installedPackage}.`))
378437
}
379-
const commands: Record<Exclude<Method, "bun" | "curl" | "brew">, string[]> = {
438+
const commands: Record<Exclude<Method, "bun" | "curl" | "brew" | "winget">, string[]> = {
380439
// Keep the old package: uninstalling it can unlink the replacement command.
381440
npm: [
382441
"npm",
@@ -436,6 +495,22 @@ const make = Effect.gen(function* () {
436495
)
437496
}
438497
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+
)
439514
return yield* retaining(method, runUpgrade({ method, command: commands[method] }))
440515
}),
441516
).pipe(
Collapse file

‎packages/cli/test/retained-image.test.ts‎

Copy file name to clipboardExpand all lines: packages/cli/test/retained-image.test.ts
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ windows("installed accepts package-manager and curl binaries only", () =>
6868
const cases = [
6969
[path.join(home, ".bun", "install", "global", "node_modules", "@opencode", "cli", "bin", "opencode.exe"), true],
7070
[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+
],
7184
[path.join(home, "AppData", "Local", "Programs", "OpenCode", "resources", "opencode-cli.exe"), false],
7285
[path.join(home, "node_modules", "bun", "bin", "bun.exe"), false],
7386
] as const
Collapse file

‎packages/cli/test/updater-install.test.ts‎

Copy file name to clipboardExpand all lines: packages/cli/test/updater-install.test.ts
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,71 @@ it.live("Homebrew Core installs check and upgrade the Core formula", () =>
340340
)
341341
})
342342

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+
343408
// Links are named opencode-upgrade-<pid>-<random>.exe; read them from inside the installer run.
344409
const links = (directory: string) =>
345410
existsSync(directory) ? readdirSync(directory).filter((name) => name.startsWith("opencode-")) : []

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.