autostart: a magpie opened at login comes back from "update and restart" on the Mac (悠悠哥 on Discord: 更新后开机自启动没了) — open at login was never lost: the Mac's launch agent runs magpie as a launchd job, and launchd ends whatever a job started once the job exits, so the shell update.Relaunch leaves to open the new version was killed as magpie quit and magpie never came back. The launch agent now carries AbandonProcessGroup, and the desktop app rewrites one an older magpie wrote in the current form at start, keeping the program path it names; with none it stays off, and a launch agent magpie didn't write is left alone. Two throwaway launchd jobs showed children, detached ones too, killed on the job's exit without the key and living with it. TestLaunchAgentLetsTheRelaunchLive and TestRefreshOlderLaunchAgent fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
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(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
@@ -36,10 +47,32 @@ func enable(exe string) error {
|
||||
<key>RunAtLoad</key><true/>
|
||||
<key>LimitLoadToSessionType</key><string>Aqua</string>
|
||||
<key>ProcessType</key><string>Interactive</string>
|
||||
<key>AbandonProcessGroup</key><true/>
|
||||
</dict>
|
||||
</plist>
|
||||
`, label, html.EscapeString(exe), Arg)
|
||||
return os.WriteFile(p, []byte(body), 0o644)
|
||||
`, label, html.EscapeString(exe), Arg))
|
||||
}
|
||||
|
||||
var program = regexp.MustCompile(`<key>ProgramArguments</key>\s*<array><string>([^<]*)</string>`)
|
||||
|
||||
// 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 {
|
||||
|
||||
@@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key><string>com.yetone.magpie</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array><string>/Applications/Tom & Jerry/magpie.app/Contents/MacOS/magpie</string><string>tray</string></array>
|
||||
<key>RunAtLoad</key><true/>
|
||||
<key>LimitLoadToSessionType</key><string>Aqua</string>
|
||||
<key>ProcessType</key><string>Interactive</string>
|
||||
</dict>
|
||||
</plist>
|
||||
`
|
||||
|
||||
// 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), "<key>AbandonProcessGroup</key><true/>") {
|
||||
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 := "<plist><dict><key>Label</key><string>x</string></dict></plist>"
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -41,3 +41,6 @@ func disable() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// refresh: nothing an older magpie wrote here needs writing again
|
||||
func refresh() error { return nil }
|
||||
|
||||
@@ -70,3 +70,6 @@ func disable() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// refresh: nothing an older magpie wrote here needs writing again
|
||||
func refresh() error { return nil }
|
||||
|
||||
@@ -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 := ""
|
||||
|
||||
Reference in New Issue
Block a user