Files
magpie/internal/proc/appmain.go
T
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00

74 lines
2.6 KiB
Go

package proc
import (
"errors"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/lazyre"
)
// errAppMain is what a probe of an app's own program (a Mac bundle's, a
// Windows GUI program) ends with, unrun.
var errAppMain = errors.New("not run: it is an app's own program, which opens the app's window")
// appMain reports whether the program at path is the main executable of
// an app a user opens (…/X.app/Contents/MacOS/<CFBundleExecutable>, X.app
// in no other bundle), after links. Run, that opens the app: its Dock icon
// and window, whatever the arguments, and a GUI app given --version never
// answers it. An `opencode` on PATH that resolves to OpenCode.app's own
// program (a folder of the bundle on PATH, where a Mac's case-insensitive
// disk finds OpenCode as opencode, or a link to it) put OpenCode's window
// up each time magpie asked its version, at every start and after each
// update (saint carol on Discord). The bundle's other programs (OpenCode's
// opencode-cli, Kiro CLI.app's kiro-cli beside its kiro_cli_desktop) are
// CLIs and are run, and so is a bundle inside an app's own: ChatGPT.app's
// codex is CodexCLI.app's main executable. When Info.plist can't be read
// (a binary one, as Kiro CLI.app's is), the program named as its bundle
// is taken for the main one.
func appMain(path string) bool {
if path == "" {
return false
}
if real, err := filepath.EvalSymlinks(path); err == nil {
path = real
}
if abs, err := filepath.Abs(path); err == nil {
path = abs
}
macos := filepath.Dir(path)
contents := filepath.Dir(macos)
bundle := filepath.Dir(contents)
if !strings.EqualFold(filepath.Base(macos), "MacOS") || !strings.EqualFold(filepath.Base(contents), "Contents") ||
!strings.HasSuffix(strings.ToLower(filepath.Base(bundle)), ".app") {
return false
}
for up := filepath.Dir(bundle); up != filepath.Dir(up); up = filepath.Dir(up) {
if strings.HasSuffix(strings.ToLower(filepath.Base(up)), ".app") {
return false // a helper or a CLI another app ships
}
}
exe := bundleExecutable(filepath.Join(contents, "Info.plist"))
if exe == "" {
exe = filepath.Base(bundle)
exe = exe[:len(exe)-len(".app")]
}
return strings.EqualFold(exe, filepath.Base(path))
}
var bundleExeRe = lazyre.New(`<key>CFBundleExecutable</key>\s*<string>([^<]+)</string>`)
// bundleExecutable is the CFBundleExecutable an XML Info.plist names, ""
// when it can't be read (a binary plist too) or names none.
func bundleExecutable(plist string) string {
b, err := os.ReadFile(plist)
if err != nil {
return ""
}
if m := bundleExeRe.FindSubmatch(b); m != nil {
return strings.TrimSpace(string(m[1]))
}
return ""
}