Files
magpie/internal/shortcut/shortcut.go
T
yetone 92e8edd7fc windows: magpie is listed in Installed apps, and its Uninstall puts every agent back
Jevik_AI on X uninstalled magpie and rebooted, and Codex's config.toml
still had openai_base_url = "http://127.0.0.1:3425/backend-api/codex";
Rk1ngggg asked whether an update cleans it. magpie's own uninstall
(Settings › About › Uninstall magpie, `magpie uninstall`) already
disconnects every agent and takes Codex's leftovers (#3866), but on
Windows magpie wasn't in Settings › Apps › Installed apps, so it was
removed the way Windows offers for a program it doesn't list: by
deleting the exe and folders, which runs no code.

shortcut.Ensure now writes
HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\magpie
(DisplayName, DisplayIcon, Publisher, InstallLocation, UninstallString,
URLInfoAbout, NoModify, NoRepair) at every installed start, beside App
Paths, and Remove (uninstall's System step) takes it out the same way:
only while it names this exe, or an exe that is gone. Its Uninstall
runs `magpie.exe uninstall --window` (shortcut.UninstallArgs). magpie.exe
has no console, where the terminal's `magpie uninstall` would refuse
for want of one to ask on, so those exact args open the app's window
(or forward to the running app, OnSecondInstanceLaunch) on Settings ›
About with Uninstall magpie's question asked (uninstallView,
?uninstall=1). It asks once: a redraw of Settings doesn't ask again,
and Cancel removes nothing. magpie web opened at that link asks
nothing. Deleting the app by hand still runs no code (macOS
drag-to-Trash included); agent-wiring.md says so.

Run on mote at the pushed head (rebased onto be9dbce02):
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. go build, GOOS=linux go build -tags
  nogui, GOOS=windows go build: ok.
- go test -tags nogui ./internal/gui ./internal/cli ./internal/shortcut
  ./internal/uninstall: ok (TestCoreScriptBudget included).
- uninstall-settings.test.cjs and i18n-keys.test.cjs: 40 of 40
  (chromium and webkit; the new Installed-apps cases in en and zh at
  1100 and 420).
At d3f40bedd, the same change on c7ebb6f8d, under suite.lock:
- go test -tags nogui -timeout 45m ./...: ok, every package.
- The whole GUI suite (--test-concurrency=4): 22518 of 22800; the 282
  reds are in 53 files the change doesn't reach (model-chip-steady 40,
  usage-agents-hide 22, zero-priced-free 20, provider-fetch-again 14,
  ...), the full-suite reds of #5475; the numbers are on that issue.
  Their run at c7ebb6f8d alone is queued behind other sessions' suites.
- The first full run found the core scripts 147 bytes over
  TestCoreScriptBudget; the page's part was cut to one line (the
  budget is not raised).
On xiaomi-gpu (win-run): go test ./internal/shortcut, incl.
TestInstalledAppsEntry on the real HKCU registry under a test key: ok.

Broken by hand, each red:
- registerAppPath not calling registerUninstall: TestInstalledAppsEntry
  (UninstallString = "") on Windows.
- main.go not taking UninstallArgs to the window:
  TestInstalledAppsUninstallOpensTheWindow (unknown "--window").
- app.js not asking on ?uninstall=1: 4 of 4 Installed-apps GUI cases.
- app.js asking without its once guard (params.delete): the same 4
  ("asked again").

Not tried: a real Settings › Apps › Uninstall click on Windows with an
installed magpie (the test box's install belongs to other sessions); the
second-instance forward (OnSecondInstanceLaunch) has no test of its own.
2026-11-09 22:33:31 +08:00

164 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package shortcut puts magpie in the Start menu on Windows. There it is
// one exe with no installer, so nothing else would: the first start of the
// desktop app makes a Magpie shortcut in the user's Start menu opening the
// exe it runs as, and Windows search finds it by that. It is made once:
// magpie remembers it did, and a shortcut the user deleted stays deleted
// (#508); one still there is pointed at the exe again when that was moved.
// A portable magpie (a data folder beside it) makes none and registers
// nothing, leaving no trace on the machine. Elsewhere it does nothing —
// the Mac has the app in Applications, Linux its own ways.
package shortcut
import (
"errors"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/yetone/magpie/internal/appdir"
)
// Name is what the shortcut is called, and what Windows search finds.
const Name = "Magpie"
// StateName is the file in magpie's folder recording that the shortcut was
// made, and for which exe.
const StateName = "start-menu-shortcut"
// what Ensure asks of the system; the tests put fakes in
var (
// startMenu is where the shortcut goes (…\Start Menu\Programs\Magpie.lnk)
startMenu = startMenuLink
// writeLink has the shortcut at lnk open exe, and reports whether it had to
writeLink = writeShortcut
// register has Win+R and `start magpie` find exe
register = registerAppPath
// registered reports whether a magpie registered itself there before
registered = registeredAppPath
)
// Ensure makes the shortcut on the first start, keeps it pointed at this
// magpie after that while it is there, and never makes it again once the
// user deleted it. A portable magpie makes none. It fails quietly — magpie
// runs the same without one — and is skipped with MAGPIE_NO_SHORTCUT=1
// (tests, sandboxes).
func Ensure() {
if os.Getenv("MAGPIE_NO_SHORTCUT") == "1" {
return
}
exe, err := os.Executable()
if err != nil {
return
}
if p, err := filepath.EvalSymlinks(exe); err == nil {
exe = p
}
if err := ensure(Target(exe), appdir.Portable(), filepath.Join(appdir.Config(), StateName)); err != nil {
log.Println("start menu shortcut:", err)
}
}
// errNoStartMenu is startMenu's answer where there is none (off Windows).
var errNoStartMenu = errors.New("no Start menu here")
// ensure is Ensure for exe, portable being the data folder of a portable
// magpie ("" installed) and state the file remembering the shortcut was
// made.
func ensure(exe, portable, state string) error {
if portable != "" {
return nil
}
lnk, err := startMenu()
if errors.Is(err, errNoStartMenu) {
return nil
}
// a magpie from before the state file registered itself at every start
before := registered()
register(exe)
if err != nil {
return err
}
_, gone := os.Stat(lnk)
if gone != nil {
if _, err := os.Stat(state); err == nil {
// made once already: the user deleted it, and it stays deleted
return nil
}
if before {
// an older magpie made it at its every start, and it is gone:
// the user deleted it, so it isn't made again now either
return remember(state, exe)
}
}
if _, err := writeLink(lnk, exe); err != nil {
return err
}
return remember(state, exe)
}
// remember records in state that the shortcut was made, for exe.
func remember(state, exe string) error {
if err := os.MkdirAll(filepath.Dir(state), 0o755); err != nil {
return err
}
return os.WriteFile(state, []byte(exe+"\n"), 0o644)
}
// aside is what an update moves the exe it replaces to, or stages the new
// one as: magpie.exe.old, magpie.exe.old-2…, magpie.exe.new
var aside = regexp.MustCompile(`(?i)(\.exe)\.(old(-\d+)?|new)$`)
// Target is what the shortcut opens for exe: exe itself, but for a magpie
// left running from where an update moved it aside, the exe in its place,
// which is what the next start runs. An update replaces the exe where it
// is, so the shortcut stays good through it.
func Target(exe string) string {
return aside.ReplaceAllString(exe, "$1")
}
// Stale reports whether a shortcut opening target in dir is not one for
// exe: there is none yet, or the exe was moved. Windows paths are the same
// in any case.
func Stale(target, dir, exe string) bool {
same := func(a, b string) bool {
return a != "" && strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
}
return !same(target, exe) || !same(dir, filepath.Dir(exe))
}
// UninstallArgs are what Windows' Installed apps runs magpie with for its
// Uninstall: the app's window on Settings › Uninstall magpie, which asks
// first and puts every agent back before magpie's files go. It is started
// with no console, so the terminal's `magpie uninstall` couldn't ask.
var UninstallArgs = []string{"uninstall", "--window"}
// UninstallCommand is the command line Installed apps runs for exe.
func UninstallCommand(exe string) string {
return `"` + exe + `" ` + strings.Join(UninstallArgs, " ")
}
// what Remove asks of the system besides startMenu; the tests put fakes in
var unregister = unregisterAppPath
// Remove takes out what Ensure made, for magpie uninstall: the Start menu
// shortcut, the App Paths and Installed apps entries while they name exe
// (or an exe gone), and
// the record of having made them. A portable magpie made none, and an
// entry another copy of magpie registered stays that copy's.
func Remove(exe string) error {
var errs []error
if lnk, err := startMenu(); err == nil {
if err := os.Remove(lnk); err != nil && !os.IsNotExist(err) {
errs = append(errs, err)
}
} else if !errors.Is(err, errNoStartMenu) {
errs = append(errs, err)
}
if err := unregister(Target(exe)); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}