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 := ""