From ee16c6576d0400ac03f94a2a124f173be629aee4 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 18:44:31 +0800 Subject: [PATCH] =?UTF-8?q?autostart:=20a=20magpie=20opened=20at=20login?= =?UTF-8?q?=20comes=20back=20from=20"update=20and=20restart"=20on=20the=20?= =?UTF-8?q?Mac=20(=E6=82=A0=E6=82=A0=E5=93=A5=20on=20Discord:=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=90=8E=E5=BC=80=E6=9C=BA=E8=87=AA=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=B2=A1=E4=BA=86)=20=E2=80=94=20open=20at=20login=20was=20nev?= =?UTF-8?q?er=20lost:=20the=20Mac's=20launch=20agent=20runs=20magpie=20as?= =?UTF-8?q?=20a=20launchd=20job,=20and=20launchd=20ends=20whatever=20a=20j?= =?UTF-8?q?ob=20started=20once=20the=20job=20exits,=20so=20the=20shell=20u?= =?UTF-8?q?pdate.Relaunch=20leaves=20to=20open=20the=20new=20version=20was?= =?UTF-8?q?=20killed=20as=20magpie=20quit=20and=20magpie=20never=20came=20?= =?UTF-8?q?back.=20The=20launch=20agent=20now=20carries=20AbandonProcessGr?= =?UTF-8?q?oup,=20and=20the=20desktop=20app=20rewrites=20one=20an=20older?= =?UTF-8?q?=20magpie=20wrote=20in=20the=20current=20form=20at=20start,=20k?= =?UTF-8?q?eeping=20the=20program=20path=20it=20names;=20with=20none=20it?= =?UTF-8?q?=20stays=20off,=20and=20a=20launch=20agent=20magpie=20didn't=20?= =?UTF-8?q?write=20is=20left=20alone.=20Two=20throwaway=20launchd=20jobs?= =?UTF-8?q?=20showed=20children,=20detached=20ones=20too,=20killed=20on=20?= =?UTF-8?q?the=20job's=20exit=20without=20the=20key=20and=20living=20with?= =?UTF-8?q?=20it.=20TestLaunchAgentLetsTheRelaunchLive=20and=20TestRefresh?= =?UTF-8?q?OlderLaunchAgent=20fail=20without=20the=20change.=20go=20vet,?= =?UTF-8?q?=20go=20build=20-tags=20nogui=20(darwin,=20GOOS=3Dlinux),=20GOO?= =?UTF-8?q?S=3Dwindows=20go=20build,=20go=20test=20-tags=20nogui=20./...?= =?UTF-8?q?=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/autostart/autostart.go | 4 + internal/autostart/autostart_darwin.go | 39 +++++++++- internal/autostart/autostart_darwin_test.go | 84 +++++++++++++++++++++ internal/autostart/autostart_other.go | 3 + internal/autostart/autostart_windows.go | 3 + internal/gui/app.go | 6 ++ 6 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 internal/autostart/autostart_darwin_test.go diff --git a/internal/autostart/autostart.go b/internal/autostart/autostart.go index 2d3347be4..5cbb147ab 100644 --- a/internal/autostart/autostart.go +++ b/internal/autostart/autostart.go @@ -31,6 +31,10 @@ func Set(on bool) error { return enable(exe) } +// Refresh brings the system's record up to date when magpie opens at +// login, for a record an older version wrote; it never turns it on. +func Refresh() error { return refresh() } + // self is the program to start: this one, where it will be at login. func self() (string, error) { // an AppImage runs from a folder mounted anew each time; the file diff --git a/internal/autostart/autostart_darwin.go b/internal/autostart/autostart_darwin.go index af8b84c24..2727e32af 100644 --- a/internal/autostart/autostart_darwin.go +++ b/internal/autostart/autostart_darwin.go @@ -1,10 +1,12 @@ package autostart import ( + "bytes" "fmt" "html" "os" "path/filepath" + "regexp" ) const label = "com.yetone.magpie" @@ -26,7 +28,16 @@ func enable(exe string) error { if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { return err } - body := fmt.Sprintf(` + return os.WriteFile(p, plist(exe), 0o644) +} + +// plist is the launch agent for exe. AbandonProcessGroup: launchd kills +// whatever a job started once the job exits, and a restart to update is +// the job quitting with the shell that opens the new version still +// waiting for it to go — without the key, a magpie opened at login never +// came back from an update. +func plist(exe string) []byte { + return []byte(fmt.Sprintf(` @@ -36,10 +47,32 @@ func enable(exe string) error { RunAtLoad LimitLoadToSessionTypeAqua ProcessTypeInteractive + AbandonProcessGroup -`, label, html.EscapeString(exe), Arg) - return os.WriteFile(p, []byte(body), 0o644) +`, label, html.EscapeString(exe), Arg)) +} + +var program = regexp.MustCompile(`ProgramArguments\s*([^<]*)`) + +// refresh writes a launch agent an older magpie wrote over as this one +// would, for the program it names: the path stays the one the user turned +// it on for, whichever copy of magpie is running now. +func refresh() error { + p := record() + b, err := os.ReadFile(p) + if err != nil { + return nil // off: stays off + } + m := program.FindSubmatch(b) + if m == nil { + return nil // not one magpie wrote + } + want := plist(html.UnescapeString(string(m[1]))) + if bytes.Equal(b, want) { + return nil + } + return os.WriteFile(p, want, 0o644) } func disable() error { diff --git a/internal/autostart/autostart_darwin_test.go b/internal/autostart/autostart_darwin_test.go new file mode 100644 index 000000000..26de40b84 --- /dev/null +++ b/internal/autostart/autostart_darwin_test.go @@ -0,0 +1,84 @@ +package autostart + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// what magpie up to now wrote for Open at login +const olderPlist = ` + + + + Labelcom.yetone.magpie + ProgramArguments + /Applications/Tom & Jerry/magpie.app/Contents/MacOS/magpietray + RunAtLoad + LimitLoadToSessionTypeAqua + ProcessTypeInteractive + + +` + +// launchd kills what a job started once the job exits, so the shell a +// restart to update leaves to open the new version died with a magpie +// opened at login: the launch agent lets them go. +func TestLaunchAgentLetsTheRelaunchLive(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + if err := Set(true); err != nil { + t.Fatal(err) + } + b, err := os.ReadFile(record()) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(b), "AbandonProcessGroup") { + t.Fatalf("the launch agent doesn't abandon its process group:\n%s", b) + } + if out, err := exec.Command("plutil", "-lint", record()).CombinedOutput(); err != nil { + t.Fatalf("not a valid plist: %s", out) + } +} + +// An older magpie's launch agent is written again as this one writes it, +// for the program it named; one that's off stays off. +func TestRefreshOlderLaunchAgent(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + if err := Refresh(); err != nil || Enabled() { + t.Fatalf("Refresh turned Open at login on: %v", err) + } + if _, err := os.Stat(filepath.Join(home, "Library")); !os.IsNotExist(err) { + t.Fatal("Refresh made the LaunchAgents folder for one that's off") + } + + p := record() + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(olderPlist), 0o644); err != nil { + t.Fatal(err) + } + if err := Refresh(); err != nil { + t.Fatal(err) + } + b, _ := os.ReadFile(p) + want := plist("/Applications/Tom & Jerry/magpie.app/Contents/MacOS/magpie") + if string(b) != string(want) { + t.Fatalf("not rewritten as this version writes it:\n%s\nwant:\n%s", b, want) + } + + // one magpie didn't write is left as it is + other := "Labelx" + os.WriteFile(p, []byte(other), 0o644) + if err := Refresh(); err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(p); string(b) != other { + t.Fatalf("a launch agent magpie didn't write was changed:\n%s", b) + } +} diff --git a/internal/autostart/autostart_other.go b/internal/autostart/autostart_other.go index 2378d0552..d2e09dd89 100644 --- a/internal/autostart/autostart_other.go +++ b/internal/autostart/autostart_other.go @@ -41,3 +41,6 @@ func disable() error { } return nil } + +// refresh: nothing an older magpie wrote here needs writing again +func refresh() error { return nil } diff --git a/internal/autostart/autostart_windows.go b/internal/autostart/autostart_windows.go index 04d8f65e6..f7e30363d 100644 --- a/internal/autostart/autostart_windows.go +++ b/internal/autostart/autostart_windows.go @@ -70,3 +70,6 @@ func disable() error { } return nil } + +// refresh: nothing an older magpie wrote here needs writing again +func refresh() error { return nil } diff --git a/internal/gui/app.go b/internal/gui/app.go index e1dee40bd..6ba5d9f44 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -20,6 +20,7 @@ import ( "github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/events" + "github.com/yetone/magpie/internal/autostart" "github.com/yetone/magpie/internal/library" "github.com/yetone/magpie/internal/omarchy" "github.com/yetone/magpie/internal/settings" @@ -244,6 +245,11 @@ func Run(version string, showMain bool, link string) error { } // Windows has no installer to put magpie in the Start menu shortcut.Ensure() + // Open at login as this version writes it (the Mac's, so a restart + // to update from a magpie opened at login comes back) + if err := autostart.Refresh(); err != nil { + log.Println("open at login:", err) + } }() // MAGPIE_THEME=light|dark forces the palette; handy for screenshots. theme := ""