mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 15:28:48 +03:00
fix(desktop): migrate mac beta to stable installer (#48724)
Co-authored-by: Hona <10430890+Hona@users.noreply.github.com> Co-authored-by: Brendonovich <14191578+Brendonovich@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
4fc5ba1358
commit
c31ac3c919
@@ -283,7 +283,6 @@ export const DESKTOP_NATIVE_ENGLISH = {
|
||||
"desktop.updater.dialog.ready.title": "Update Ready",
|
||||
"desktop.updater.dialog.restart": "Restart",
|
||||
"desktop.updater.dialog.later": "Later",
|
||||
|
||||
"desktop.cli.installed.title": "CLI Installed",
|
||||
"desktop.cli.installed.message": "CLI installed to {{path}}\n\nRestart your terminal to use the 'opencode' command.",
|
||||
"desktop.cli.failed.title": "Installation Failed",
|
||||
|
||||
@@ -1226,6 +1226,10 @@ export const dict = {
|
||||
"settings.updates.action.checkNow": "Check now",
|
||||
"settings.updates.action.checking": "Checking…",
|
||||
"settings.updates.action.downloading": "Downloading…",
|
||||
"settings.updates.action.download": "Download OpenCode",
|
||||
"settings.updates.migration.title": "Move from Beta to Stable",
|
||||
"settings.updates.migration.description":
|
||||
"For now, OpenCode Beta is moving to OpenCode Stable. Download version {{version}}, then install OpenCode from the disk image to continue receiving updates.",
|
||||
"settings.updates.action.installing": "Installing…",
|
||||
"settings.updates.toast.latest.title": "You're up to date",
|
||||
"settings.updates.toast.latest.description": "You're running the latest version of OpenCode.",
|
||||
|
||||
@@ -3,15 +3,24 @@ import { usePlatform } from "@/runtime/platform/platform"
|
||||
import { useCommand, type CommandOption } from "./command"
|
||||
import { useDialog } from "@opencode/ui/context/dialog"
|
||||
import { DialogSsh } from "@/servers/ssh/dialog"
|
||||
import { useUpdaterAction } from "@/shell/updates/action"
|
||||
|
||||
export function DesktopCommands() {
|
||||
const command = useCommand()
|
||||
const language = useLanguage()
|
||||
const platform = usePlatform()
|
||||
const dialog = useDialog()
|
||||
const updater = useUpdaterAction()
|
||||
|
||||
command.register("desktop", () => {
|
||||
const commands: CommandOption[] = []
|
||||
if (platform.platform === "desktop")
|
||||
commands.push({
|
||||
id: "app.checkForUpdates",
|
||||
title: language.t("desktop.menu.checkForUpdates"),
|
||||
hidden: true,
|
||||
onSelect: () => void updater.check(),
|
||||
})
|
||||
if (platform.sshServers)
|
||||
commands.push({
|
||||
id: "server.ssh.add",
|
||||
|
||||
@@ -267,8 +267,7 @@ export const ErrorPage: Component<ErrorPageProps> = (props) => {
|
||||
|
||||
const updateVersion = () => {
|
||||
const state = platform.updater?.state()
|
||||
if (state?.status !== "ready") return
|
||||
return state.version
|
||||
return state?.status === "ready" || state?.status === "download-required" ? state.version : undefined
|
||||
}
|
||||
|
||||
async function exportDebugLogs() {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { TitlebarRightProvider } from "@/shell/titlebar/right-slot"
|
||||
import { useSettingsSurface } from "@/settings/surface"
|
||||
import { useSettings } from "@/settings/model"
|
||||
import { SshAuthentication } from "@/servers/ssh/authentication"
|
||||
import { useUpdaterInstall } from "@/shell/updates/download"
|
||||
|
||||
const DebugBar = lazy(() => import("@/shell/debug/debug-bar").then((module) => ({ default: module.DebugBar })))
|
||||
|
||||
@@ -16,6 +17,7 @@ export default function Layout(props: ParentProps) {
|
||||
const platform = usePlatform()
|
||||
const settings = useSettingsSurface()
|
||||
const preferences = useSettings()
|
||||
const installUpdate = useUpdaterInstall()
|
||||
const mobile = createMediaQuery("(max-width: 767px)")
|
||||
const [state, setState] = createStore({
|
||||
debugTools: false,
|
||||
@@ -26,15 +28,10 @@ export default function Layout(props: ParentProps) {
|
||||
const bottomTitlebar = () => mobile() && preferences.general.mobileTitlebarPosition() === "bottom"
|
||||
|
||||
const update: TitlebarUpdate = {
|
||||
get version() {
|
||||
const state = platform.updater?.state()
|
||||
if (state?.status !== "ready") return undefined
|
||||
return state.version
|
||||
get state() {
|
||||
return platform.updater?.state()
|
||||
},
|
||||
get installing() {
|
||||
return platform.updater?.state().status === "installing"
|
||||
},
|
||||
install: () => void platform.updater?.install(),
|
||||
install: installUpdate,
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -31,6 +31,8 @@ import { SessionTabAvatar } from "@/shell/layout/session-tab-avatar"
|
||||
import { SessionProgressIndicatorV2 } from "@opencode/session-ui/v2/session-progress-indicator-v2"
|
||||
import { projectForSession } from "@/shell/layout/helpers"
|
||||
import { useSettingsDialog } from "@/settings/command"
|
||||
import { updaterAction } from "@/shell/updates/action"
|
||||
import type { UpdaterState } from "@/shell/updates/types"
|
||||
import devIcon from "../../../../desktop/icons/dev/64x64.png"
|
||||
import betaIcon from "../../../../desktop/icons/beta/64x64.png"
|
||||
|
||||
@@ -43,8 +45,7 @@ const macTrafficLightsBaseWidth = 68
|
||||
const macTrafficLightsTopClearance = 28
|
||||
|
||||
export type TitlebarUpdate = {
|
||||
version: string | undefined
|
||||
installing: boolean
|
||||
state: UpdaterState | undefined
|
||||
install: () => void
|
||||
}
|
||||
|
||||
@@ -96,13 +97,14 @@ export function Titlebar(props: {
|
||||
})
|
||||
|
||||
const updateState = createMemo<TitlebarUpdatePillState>(() => {
|
||||
const installing = props.update?.installing ?? false
|
||||
const version = props.update?.version
|
||||
const state = props.update?.state
|
||||
const installing = state?.status === "installing"
|
||||
const version = state?.status === "ready" || state?.status === "download-required" ? state.version : undefined
|
||||
return {
|
||||
visible: version !== undefined || installing,
|
||||
installing,
|
||||
label: language.t("titlebar.update"),
|
||||
ariaLabel: language.t("toast.update.action.installRestart"),
|
||||
ariaLabel: language.t(updaterAction(state).label),
|
||||
title: version ? language.t("titlebar.updateVersion", { version }) : undefined,
|
||||
onInstall: () => props.update?.install(),
|
||||
}
|
||||
|
||||
@@ -19,6 +19,10 @@ describe("updaterAction", () => {
|
||||
label: "toast.update.action.installRestart",
|
||||
run: "install",
|
||||
})
|
||||
expect(updaterAction({ status: "download-required", version: "2.0.0" })).toEqual({
|
||||
label: "settings.updates.action.download",
|
||||
run: "install",
|
||||
})
|
||||
expect(updaterAction({ status: "installing", version: "2.0.0" })).toEqual({
|
||||
label: "settings.updates.action.installing",
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import type { UpdaterState } from "./types"
|
||||
import { usePlatform } from "@/runtime/platform/platform"
|
||||
import { useLanguage } from "@/runtime/i18n/language"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
import { formatServerError } from "@/runtime/server/errors"
|
||||
import { useUpdaterInstall } from "@/shell/updates/download"
|
||||
|
||||
export function updaterAction(state: UpdaterState | undefined) {
|
||||
if (!state) return { label: "settings.updates.action.checkNow" as const }
|
||||
@@ -14,6 +14,8 @@ export function updaterAction(state: UpdaterState | undefined) {
|
||||
return { label: "settings.updates.action.downloading" as const }
|
||||
case "ready":
|
||||
return { label: "toast.update.action.installRestart" as const, run: "install" as const }
|
||||
case "download-required":
|
||||
return { label: "settings.updates.action.download" as const, run: "install" as const }
|
||||
case "installing":
|
||||
return { label: "settings.updates.action.installing" as const }
|
||||
case "disabled":
|
||||
@@ -26,34 +28,38 @@ export function updaterAction(state: UpdaterState | undefined) {
|
||||
export function useUpdaterAction() {
|
||||
const platform = usePlatform()
|
||||
const language = useLanguage()
|
||||
const install = useUpdaterInstall()
|
||||
const action = createMemo(() => updaterAction(platform.updater?.state()))
|
||||
const check = async () => {
|
||||
const state = await platform.updater?.check()
|
||||
if (state?.status === "download-required") {
|
||||
install()
|
||||
return
|
||||
}
|
||||
if (state?.status === "up-to-date") {
|
||||
showToast({
|
||||
variant: "success",
|
||||
icon: "circle-check",
|
||||
title: language.t("settings.updates.toast.latest.title"),
|
||||
description: language.t("settings.updates.toast.latest.description", { version: platform.version ?? "" }),
|
||||
})
|
||||
}
|
||||
if (state?.status === "error") {
|
||||
showToast({ title: language.t("common.requestFailed"), description: state.message })
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
action,
|
||||
check,
|
||||
async run() {
|
||||
const run = action().run
|
||||
if (run === "install") {
|
||||
return platform.updater?.install().catch((error) => {
|
||||
showToast({
|
||||
title: language.t("common.requestFailed"),
|
||||
description: formatServerError(error, language.t, language.t("common.requestFailed")),
|
||||
})
|
||||
})
|
||||
install()
|
||||
return
|
||||
}
|
||||
if (run !== "check") return
|
||||
|
||||
const state = await platform.updater?.check()
|
||||
if (state?.status === "up-to-date") {
|
||||
showToast({
|
||||
variant: "success",
|
||||
icon: "circle-check",
|
||||
title: language.t("settings.updates.toast.latest.title"),
|
||||
description: language.t("settings.updates.toast.latest.description", { version: platform.version ?? "" }),
|
||||
})
|
||||
}
|
||||
if (state?.status === "error") {
|
||||
showToast({ title: language.t("common.requestFailed"), description: state.message })
|
||||
}
|
||||
await check()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { useDialog } from "@opencode/ui/context/dialog"
|
||||
import { Dialog, DialogFooter, DialogHeader, DialogTitleGroup } from "@opencode/ui/dialog"
|
||||
import { useLanguage } from "@/runtime/i18n/language"
|
||||
import { usePlatform } from "@/runtime/platform/platform"
|
||||
import { formatServerError } from "@/runtime/server/errors"
|
||||
import { showToast } from "@/shell/notifications/toast"
|
||||
|
||||
export function useUpdaterInstall() {
|
||||
const dialog = useDialog()
|
||||
const language = useLanguage()
|
||||
const platform = usePlatform()
|
||||
const download = () =>
|
||||
platform.updater?.install().catch((error) => {
|
||||
showToast({
|
||||
title: language.t("common.requestFailed"),
|
||||
description: formatServerError(error, language.t, language.t("common.requestFailed")),
|
||||
})
|
||||
})
|
||||
|
||||
return () => {
|
||||
const state = platform.updater?.state()
|
||||
if (state?.status !== "download-required") {
|
||||
void download()
|
||||
return
|
||||
}
|
||||
void dialog.show(() => <DialogStableDownload version={state.version} download={download} />)
|
||||
}
|
||||
}
|
||||
|
||||
export function DialogStableDownload(props: { version: string; download: () => Promise<void> | undefined }) {
|
||||
const dialog = useDialog()
|
||||
const language = useLanguage()
|
||||
const download = () => {
|
||||
dialog.close()
|
||||
void props.download()
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog fit>
|
||||
<DialogHeader>
|
||||
<DialogTitleGroup
|
||||
title={language.t("settings.updates.migration.title")}
|
||||
description={language.t("settings.updates.migration.description", { version: props.version })}
|
||||
/>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button type="button" variant="neutral" onClick={() => dialog.close()}>
|
||||
{language.t("common.cancel")}
|
||||
</Button>
|
||||
<Button type="button" variant="contrast" autofocus onClick={download}>
|
||||
{language.t("settings.updates.action.download")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ export type UpdaterState =
|
||||
| { status: "checking" }
|
||||
| { status: "downloading"; version: string }
|
||||
| { status: "ready"; version: string }
|
||||
| { status: "download-required"; version: string }
|
||||
| { status: "up-to-date" }
|
||||
| { status: "installing"; version: string }
|
||||
| { status: "error"; message: string }
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { MenuCommandTriggered } from "../../shared/ipc-rpc/events"
|
||||
import { emitIpcEvent } from "../ipc-events"
|
||||
|
||||
import { UPDATER_ENABLED } from "../constants"
|
||||
import { CHANNEL, UPDATER_ENABLED } from "../constants"
|
||||
import { runDesktopMenuAction } from "./menu-actions"
|
||||
import { nativeT } from "./translations"
|
||||
|
||||
@@ -58,6 +58,10 @@ function nativeItem(entry: DesktopMenuEntry, deps: Deps): MenuItemConstructorOpt
|
||||
}
|
||||
if (entry.action) {
|
||||
const action = entry.action
|
||||
if (action === "app.checkForUpdates" && CHANNEL === "beta") {
|
||||
item.click = () => deps.trigger(action)
|
||||
return item
|
||||
}
|
||||
item.click = () =>
|
||||
runDesktopMenuAction(BrowserWindow.getFocusedWindow(), action, {
|
||||
checkForUpdates: deps.checkForUpdates,
|
||||
|
||||
@@ -17,6 +17,8 @@ function setup(input?: {
|
||||
latest?: () => string
|
||||
stage?: () => Promise<void>
|
||||
install?: () => Promise<never>
|
||||
external?: boolean
|
||||
open?: () => Promise<void>
|
||||
}) {
|
||||
const calls: string[] = []
|
||||
let ready = input?.ready
|
||||
@@ -26,7 +28,10 @@ function setup(input?: {
|
||||
checkForUpdate: Effect.try({
|
||||
try: () => {
|
||||
calls.push("check")
|
||||
return input?.latest?.() ?? "2.0.0"
|
||||
const version = input?.latest?.() ?? "2.0.0"
|
||||
return input?.external
|
||||
? { mode: "external" as const, version, url: `https://files.test/${version}.dmg` }
|
||||
: { mode: "restart" as const, version }
|
||||
},
|
||||
catch: (error) => error,
|
||||
}),
|
||||
@@ -42,6 +47,13 @@ function setup(input?: {
|
||||
catch: (error) => error,
|
||||
})
|
||||
}),
|
||||
externalInstall: input?.external
|
||||
? (url) =>
|
||||
Effect.tryPromise(async () => {
|
||||
calls.push(`external:${url}`)
|
||||
await input.open?.()
|
||||
})
|
||||
: undefined,
|
||||
dispose: () => {},
|
||||
},
|
||||
prepareToRestart: Effect.sync(() => {
|
||||
@@ -83,6 +95,62 @@ describe("updater", () => {
|
||||
expect(app.getReady()).toEqual({ version: "2.0.0" })
|
||||
})
|
||||
|
||||
test("offers an external installer without staging or preparing to restart", async () => {
|
||||
const app = setup({ external: true })
|
||||
|
||||
await app.updater.start()
|
||||
expect(app.calls).toEqual(["check"])
|
||||
expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" })
|
||||
expect(app.getReady()).toBeUndefined()
|
||||
|
||||
await app.updater.install()
|
||||
expect(app.calls).toEqual(["check", "check", "external:https://files.test/2.0.0.dmg"])
|
||||
expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" })
|
||||
})
|
||||
|
||||
test("offers an external installer when its version equals the running beta", async () => {
|
||||
const app = setup({ currentVersion: "2.0.0", external: true })
|
||||
|
||||
await app.updater.start()
|
||||
|
||||
expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" })
|
||||
})
|
||||
|
||||
test("keeps an external installer available when a refresh fails", async () => {
|
||||
let offline = false
|
||||
const app = setup({
|
||||
external: true,
|
||||
latest: () => {
|
||||
if (offline) throw new Error("offline")
|
||||
return "2.0.0"
|
||||
},
|
||||
})
|
||||
await app.updater.start()
|
||||
|
||||
offline = true
|
||||
await app.updater.check()
|
||||
|
||||
expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" })
|
||||
})
|
||||
|
||||
test("opens an external installer once for concurrent clicks", async () => {
|
||||
let release = () => {}
|
||||
const app = setup({
|
||||
external: true,
|
||||
open: () => new Promise<void>((resolve) => (release = resolve)),
|
||||
})
|
||||
await app.updater.start()
|
||||
|
||||
const first = app.updater.install()
|
||||
const second = app.updater.install()
|
||||
await new Promise((resolve) => setTimeout(resolve, 0))
|
||||
|
||||
expect(app.calls).toEqual(["check", "check", "external:https://files.test/2.0.0.dmg"])
|
||||
release()
|
||||
await Promise.all([first, second])
|
||||
expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" })
|
||||
})
|
||||
|
||||
test("reports up to date and clears the record once the update is installed", async () => {
|
||||
const app = setup({ currentVersion: "2.0.0", ready: { version: "2.0.0" } })
|
||||
|
||||
|
||||
@@ -6,10 +6,15 @@ import type { UpdaterState } from "@opencode/app/updater"
|
||||
import { UpdaterStateChanged } from "../../shared/ipc-rpc/events"
|
||||
import { emitIpcEvent } from "../ipc-events"
|
||||
|
||||
export type UpdateTarget =
|
||||
| { readonly mode: "restart"; readonly version: string }
|
||||
| { readonly mode: "external"; readonly version: string; readonly url: string }
|
||||
|
||||
export type Platform = {
|
||||
readonly checkForUpdate: Effect.Effect<string | undefined, unknown>
|
||||
readonly checkForUpdate: Effect.Effect<UpdateTarget | undefined, unknown>
|
||||
readonly stageUpdate: (options: { readonly differential: boolean }) => Effect.Effect<unknown, unknown>
|
||||
readonly installAndRestart: Effect.Effect<never, unknown>
|
||||
readonly externalInstall?: (url: string) => Effect.Effect<void, unknown>
|
||||
readonly dispose: () => void
|
||||
}
|
||||
|
||||
@@ -59,6 +64,7 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc
|
||||
// the last download. Once a release is staged without installing, the two no longer match and every later delta fails
|
||||
// its checksum before falling back to a full download, so remember which release the cache holds and skip the attempt.
|
||||
let downloaded: string | undefined
|
||||
let target: UpdateTarget | undefined
|
||||
const stage = (platform: Platform, version: string) =>
|
||||
Effect.gen(function* () {
|
||||
if (downloaded)
|
||||
@@ -74,14 +80,19 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc
|
||||
const findAndStage = (platform: Platform) =>
|
||||
Effect.gen(function* () {
|
||||
yield* Effect.sync(() => transition({ status: "checking" }))
|
||||
const version = yield* platform.checkForUpdate
|
||||
if (!version || version === dependencies.currentVersion) {
|
||||
const next = yield* platform.checkForUpdate
|
||||
if (!next || (next.mode === "restart" && next.version === dependencies.currentVersion)) {
|
||||
yield* dependencies.persistence.clear
|
||||
return transition({ status: "up-to-date" })
|
||||
}
|
||||
transition({ status: "downloading", version })
|
||||
yield* stage(platform, version)
|
||||
return transition({ status: "ready", version })
|
||||
target = next
|
||||
if (next.mode === "external") {
|
||||
yield* dependencies.persistence.clear
|
||||
return transition({ status: "download-required", version: next.version })
|
||||
}
|
||||
transition({ status: "downloading", version: next.version })
|
||||
yield* stage(platform, next.version)
|
||||
return transition({ status: "ready", version: next.version })
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() =>
|
||||
@@ -91,10 +102,16 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc
|
||||
)
|
||||
const refreshStaged = (platform: Platform, staged: string) =>
|
||||
Effect.gen(function* () {
|
||||
const version = yield* platform.checkForUpdate
|
||||
if (!version || version === staged || version === dependencies.currentVersion) return state
|
||||
yield* stage(platform, version)
|
||||
return transition({ status: installing ? "installing" : "ready", version })
|
||||
const next = yield* platform.checkForUpdate
|
||||
if (!next) return state
|
||||
target = next
|
||||
if (next.mode === "external") {
|
||||
yield* dependencies.persistence.clear
|
||||
return transition({ status: "download-required", version: next.version })
|
||||
}
|
||||
if (next.version === staged || next.version === dependencies.currentVersion) return state
|
||||
yield* stage(platform, next.version)
|
||||
return transition({ status: installing ? "installing" : "ready", version: next.version })
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
@@ -108,13 +125,38 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc
|
||||
}),
|
||||
),
|
||||
)
|
||||
const refreshExternal = (platform: Platform) =>
|
||||
Effect.gen(function* () {
|
||||
const next = yield* platform.checkForUpdate
|
||||
if (!next || next.mode !== "external") return state
|
||||
target = next
|
||||
if (state.status === "download-required" && state.version === next.version) return state
|
||||
return transition({ status: "download-required", version: next.version })
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.sync(() => {
|
||||
runFork(
|
||||
Effect.logWarning("external updater refresh failed, keeping download available", {
|
||||
message: error instanceof Error ? error.message : String(error),
|
||||
}),
|
||||
)
|
||||
return state
|
||||
}),
|
||||
),
|
||||
)
|
||||
const check = Effect.suspend(() => {
|
||||
const platform = dependencies.platform
|
||||
if (!platform || state.status === "installing") return Effect.succeed(state)
|
||||
if (pending) return Deferred.await(pending)
|
||||
const deferred = Deferred.makeUnsafe<UpdaterState>()
|
||||
pending = deferred
|
||||
return (state.status === "ready" ? refreshStaged(platform, state.version) : findAndStage(platform)).pipe(
|
||||
const update =
|
||||
state.status === "ready"
|
||||
? refreshStaged(platform, state.version)
|
||||
: state.status === "download-required"
|
||||
? refreshExternal(platform)
|
||||
: findAndStage(platform)
|
||||
return update.pipe(
|
||||
Effect.tap((result) => Deferred.succeed(deferred, result)),
|
||||
Effect.ensuring(Effect.sync(() => (pending = undefined))),
|
||||
)
|
||||
@@ -122,7 +164,34 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc
|
||||
const install = Effect.suspend(() => {
|
||||
if (installing) return Deferred.await(installing)
|
||||
const platform = dependencies.platform
|
||||
if (!platform || state.status !== "ready") return Effect.fail(new Error("Update is not ready to install"))
|
||||
if (!platform) return Effect.fail(new Error("Update is not ready to install"))
|
||||
if (state.status === "download-required") {
|
||||
const staged = state.version
|
||||
const deferred = Deferred.makeUnsafe<void, unknown>()
|
||||
installing = deferred
|
||||
return Effect.gen(function* () {
|
||||
yield* pending ? Deferred.await(pending) : refreshExternal(platform)
|
||||
if (!target || target.mode !== "external" || !platform.externalInstall)
|
||||
return yield* Effect.fail(new Error("External installer is unavailable"))
|
||||
transition({ status: "installing", version: target.version })
|
||||
return yield* platform.externalInstall(target.url)
|
||||
}).pipe(
|
||||
Effect.exit,
|
||||
Effect.flatMap((exit) =>
|
||||
Deferred.done(deferred, exit).pipe(
|
||||
Effect.andThen(
|
||||
Effect.sync(() => {
|
||||
installing = undefined
|
||||
if (state.status === "installing")
|
||||
transition({ status: "download-required", version: target?.version ?? staged })
|
||||
}),
|
||||
),
|
||||
Effect.andThen(Deferred.await(deferred)),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
if (state.status !== "ready") return Effect.fail(new Error("Update is not ready to install"))
|
||||
const staged = state.version
|
||||
transition({ status: "installing", version: staged })
|
||||
const deferred = Deferred.makeUnsafe<void, unknown>()
|
||||
|
||||
@@ -65,6 +65,7 @@ const show = Effect.fn("Updater.show")(function* (
|
||||
)
|
||||
return
|
||||
}
|
||||
if (state.status === "download-required") return
|
||||
if (state.status !== "ready") return
|
||||
|
||||
const response = yield* promise(() =>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { requiresStableMacInstaller, stableMacDownload } from "./migration"
|
||||
|
||||
test("uses the external stable installer only for macOS beta", () => {
|
||||
expect(requiresStableMacInstaller("darwin", "beta")).toBe(true)
|
||||
expect(requiresStableMacInstaller("darwin", "prod")).toBe(false)
|
||||
expect(requiresStableMacInstaller("win32", "beta")).toBe(false)
|
||||
expect(requiresStableMacInstaller("linux", "beta")).toBe(false)
|
||||
})
|
||||
|
||||
test("selects the signed stable installer for the current Mac architecture", () => {
|
||||
const artifact = {
|
||||
version: "2.0.2",
|
||||
metadata: {
|
||||
files: {
|
||||
"opencode-desktop-mac-arm64.dmg": { url: "https://files.test/OpenCode-arm64.dmg" },
|
||||
"opencode-desktop-mac-x64.dmg": { url: "https://files.test/OpenCode-x64.dmg" },
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expect(stableMacDownload(artifact, "arm64")).toEqual({
|
||||
version: "2.0.2",
|
||||
url: "https://files.test/OpenCode-arm64.dmg",
|
||||
})
|
||||
expect(stableMacDownload(artifact, "x64")).toEqual({
|
||||
version: "2.0.2",
|
||||
url: "https://files.test/OpenCode-x64.dmg",
|
||||
})
|
||||
expect(stableMacDownload(artifact, "ia32")).toBeUndefined()
|
||||
expect(stableMacDownload({ version: "2.0.2", metadata: { files: {} } }, "arm64")).toBeUndefined()
|
||||
})
|
||||
@@ -0,0 +1,20 @@
|
||||
import { Option, Schema } from "effect"
|
||||
|
||||
const Artifact = Schema.Struct({
|
||||
version: Schema.String,
|
||||
metadata: Schema.Struct({
|
||||
files: Schema.Record(Schema.String, Schema.Struct({ url: Schema.String })),
|
||||
}),
|
||||
})
|
||||
const decode = Schema.decodeUnknownOption(Artifact)
|
||||
|
||||
export function requiresStableMacInstaller(platform: string, channel: string) {
|
||||
return platform === "darwin" && channel === "beta"
|
||||
}
|
||||
|
||||
export function stableMacDownload(input: unknown, arch: string) {
|
||||
if (arch !== "arm64" && arch !== "x64") return undefined
|
||||
const artifact = Option.getOrUndefined(decode(input))
|
||||
const file = artifact?.metadata.files[`opencode-desktop-mac-${arch}.dmg`]
|
||||
return file && { version: artifact.version, url: file.url }
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
import { app, autoUpdater } from "electron"
|
||||
import pkg from "electron-updater"
|
||||
import { Effect } from "effect"
|
||||
import { CHANNEL } from "../constants"
|
||||
import { openExternalURL } from "../files"
|
||||
import { setAppQuitting } from "../windows"
|
||||
import type { Platform } from "./index"
|
||||
import { requiresStableMacInstaller, stableMacDownload } from "./migration"
|
||||
|
||||
const updateClient = pkg.autoUpdater
|
||||
const restartTimeout = 10_000
|
||||
const stableArtifact = "https://opencode.ai/update/api/latest/desktop/opencode"
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const external = requiresStableMacInstaller(process.platform, CHANNEL)
|
||||
const runFork = Effect.runForkWith(yield* Effect.context())
|
||||
updateClient.logger = {
|
||||
info: (...args) => runFork(Effect.logInfo(...args)),
|
||||
@@ -32,13 +37,22 @@ export const make = Effect.gen(function* () {
|
||||
return {
|
||||
checkForUpdate: Effect.tryPromise({
|
||||
try: async () => {
|
||||
if (external) {
|
||||
const response = await fetch(stableArtifact)
|
||||
if (!response.ok) throw new Error(`Stable OpenCode update check failed: ${response.status}`)
|
||||
const download = stableMacDownload(await response.json(), process.arch)
|
||||
if (!download) throw new Error("Stable OpenCode download is unavailable")
|
||||
return { mode: "external", ...download } as const
|
||||
}
|
||||
const result = await updateClient.checkForUpdates()
|
||||
return result?.isUpdateAvailable ? result.updateInfo.version : undefined
|
||||
if (!result?.isUpdateAvailable) return undefined
|
||||
return { mode: "restart", version: result.updateInfo.version } as const
|
||||
},
|
||||
catch: (error) => error,
|
||||
}),
|
||||
stageUpdate,
|
||||
installAndRestart,
|
||||
externalInstall: external ? (url) => openExternalURL(url) : undefined,
|
||||
dispose: () => autoUpdater.off("before-quit-for-update", beforeQuit),
|
||||
} satisfies Platform
|
||||
})
|
||||
|
||||
@@ -7,6 +7,7 @@ export const UpdaterStateSchema = Schema.Union([
|
||||
Schema.Struct({ status: Schema.Literal("checking") }),
|
||||
Schema.Struct({ status: Schema.Literal("downloading"), version: Schema.String }),
|
||||
Schema.Struct({ status: Schema.Literal("ready"), version: Schema.String }),
|
||||
Schema.Struct({ status: Schema.Literal("download-required"), version: Schema.String }),
|
||||
Schema.Struct({ status: Schema.Literal("up-to-date") }),
|
||||
Schema.Struct({ status: Schema.Literal("installing"), version: Schema.String }),
|
||||
Schema.Struct({ status: Schema.Literal("error"), message: Schema.String }),
|
||||
|
||||
Reference in New Issue
Block a user