diff --git a/internal/update/executable_windows_test.go b/internal/update/executable_windows_test.go new file mode 100644 index 000000000..c0587081b --- /dev/null +++ b/internal/update/executable_windows_test.go @@ -0,0 +1,29 @@ +package update + +import ( + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/proc" +) + +// Scoop runs an app from apps\\current, a junction to the version +// folder, and Go's EvalSymlinks fails on it. The binary there is still +// judged as Scoop's, so `magpie update` names `scoop update` rather than +// failing with "The system cannot find the path specified". +func TestScoopJunctionStillPackaged(t *testing.T) { + root := filepath.Join(t.TempDir(), "scoop") + ver := filepath.Join(root, "apps", "magpie", "0.1.4478") + writeFile(t, filepath.Join(ver, "magpie.exe"), "MZ") + writeFile(t, filepath.Join(ver, "scoop-install.json"), `{"bucket":"magpie"}`) + writeFile(t, filepath.Join(ver, "scoop-manifest.json"), `{"version":"0.1.4478"}`) + cur := filepath.Join(root, "apps", "magpie", "current") + if out, err := proc.Command("cmd", "/c", "mklink", "/J", cur, ver).CombinedOutput(); err != nil { + t.Fatalf("mklink /J: %v %s", err, out) + } + exe := resolved(filepath.Join(cur, "magpie.exe")) + p := PackagedBy(exe) + if p == nil || p.Manager != "scoop" || p.Package != "magpie" { + t.Fatalf("PackagedBy(%q) = %+v", exe, p) + } +} diff --git a/internal/update/packaged.go b/internal/update/packaged.go index b7beefb37..5edacf82c 100644 --- a/internal/update/packaged.go +++ b/internal/update/packaged.go @@ -18,7 +18,7 @@ import ( // file that no longer matches it, and the manager's next upgrade, which // can be a version older than the one magpie put in, writes over it again. type Packaged struct { - Manager string `json:"manager"` // pacman, dpkg, rpm, homebrew or nix + Manager string `json:"manager"` // pacman, dpkg, rpm, homebrew, nix, scoop or winget Package string `json:"package,omitempty"` // the package's name, when known } @@ -31,6 +31,8 @@ func (p *Packaged) Name() string { return "Homebrew" case "nix": return "Nix" + case "scoop": + return "Scoop" } return p.Manager } @@ -53,6 +55,13 @@ func (p *Packaged) Command() string { return "sudo dnf upgrade " + pkg case "homebrew": return "brew upgrade " + pkg + case "scoop": + return "scoop update " + pkg + case "winget": + if p.Package == "" { + return "winget upgrade magpie" + } + return "winget upgrade --id " + p.Package } return "" } @@ -100,6 +109,9 @@ func packagedIn(root, goos, exe string) *Packaged { name, _, _ := strings.Cut(rest, "/") return &Packaged{Manager: "homebrew", Package: name} } + if goos == "windows" { + return packagedOnWindows(exe) + } if goos != "linux" { return nil } @@ -123,6 +135,50 @@ func packagedIn(root, goos, exe string) *Packaged { return nil } +// packagedOnWindows says whether Scoop or winget installed exe. Scoop keeps +// an app in \apps\\ (or its current junction, which +// Go doesn't resolve), with the scoop-install.json and scoop-manifest.json +// it writes beside it (install.json and manifest.json before Scoop 0.6); +// its root is $SCOOP, so that folder's name isn't relied on. +// winget keeps a portable package in WinGet\Packages\_ under +// %LOCALAPPDATA%\Microsoft (or Program Files, machine-wide), and the +// command on PATH is a link to it in WinGet\Links, which Executable +// resolves. Either one's next update would otherwise put back the version +// it holds, or fail on the file magpie changed. +func packagedOnWindows(exe string) *Packaged { + dir := filepath.Dir(exe) + app := filepath.Dir(dir) + if strings.EqualFold(filepath.Base(filepath.Dir(app)), "apps") && (scoopFiles(dir, "scoop-") || scoopFiles(dir, "")) { + return &Packaged{Manager: "scoop", Package: filepath.Base(app)} + } + p := strings.ReplaceAll(exe, `\`, "/") + if i := strings.Index(strings.ToLower(p), "/winget/packages/"); i >= 0 { + folder, _, _ := strings.Cut(p[i+len("/winget/packages/"):], "/") + // _: _Microsoft.Winget.Source_8wekyb3d8bbwe from the + // community repository, __DefaultSource from a local manifest + id := "" + for _, src := range []string{"_microsoft.winget.source", "__defaultsource"} { + if j := strings.Index(strings.ToLower(folder), src); j > 0 { + id = folder[:j] + break + } + } + return &Packaged{Manager: "winget", Package: id} + } + return nil +} + +// scoopFiles is whether dir has the two files Scoop writes into an app's +// version folder, their names starting with prefix. +func scoopFiles(dir, prefix string) bool { + return isFile(filepath.Join(dir, prefix+"install.json")) && isFile(filepath.Join(dir, prefix+"manifest.json")) +} + +func isFile(path string) bool { + st, err := os.Stat(path) + return err == nil && st.Mode().IsRegular() +} + // systemPath is whether a package may own p: /usr (but not /usr/local, // which packages leave to the machine's own), /bin, /sbin, /opt. func systemPath(p string) bool { diff --git a/internal/update/packaged_test.go b/internal/update/packaged_test.go index 4075069fa..4e83e6beb 100644 --- a/internal/update/packaged_test.go +++ b/internal/update/packaged_test.go @@ -133,3 +133,87 @@ func TestPackagedByHomebrewAndNix(t *testing.T) { t.Fatalf("the app: %+v", p) } } + +// Scoop's app folder as `scoop install magpie-app` from the magpie bucket +// leaves it: the version folder holds the binary with the +// scoop-install.json and scoop-manifest.json Scoop writes (seen on +// xiaomi-gpu, Scoop 0.6.0), install.json and manifest.json before 0.6, +// and current is a junction to it. +func TestPackagedByScoop(t *testing.T) { + root := filepath.Join(t.TempDir(), "scoop") + for i, v := range []string{"0.1.4478", "current"} { + dir := filepath.Join(root, "apps", "magpie-app", v) + pre := []string{"scoop-", ""}[i] + writeFile(t, filepath.Join(dir, "magpie.exe"), "MZ") + writeFile(t, filepath.Join(dir, pre+"install.json"), `{"bucket":"magpie","architecture":"64bit"}`) + writeFile(t, filepath.Join(dir, pre+"manifest.json"), `{"version":"0.1.4478"}`) + p := packagedIn("/", "windows", filepath.Join(dir, "magpie.exe")) + if p == nil || p.Manager != "scoop" || p.Package != "magpie-app" { + t.Fatalf("%s: got %+v, want scoop's magpie-app", v, p) + } + if c := p.Command(); c != "scoop update magpie-app" { + t.Fatalf("command %q", c) + } + if s := p.Say(); !strings.Contains(s, "Scoop") || !strings.HasSuffix(s, "scoop update magpie-app") { + t.Fatalf("say %q", s) + } + } + // a magpie.exe kept by hand in a folder named apps, without Scoop's + // files, is magpie's to update + own := filepath.Join(t.TempDir(), "apps", "magpie", "bin") + writeFile(t, filepath.Join(own, "magpie.exe"), "MZ") + writeFile(t, filepath.Join(own, "manifest.json"), `{}`) + writeFile(t, filepath.Join(own, "scoop-install.json"), `{}`) + if p := packagedIn("/", "windows", filepath.Join(own, "magpie.exe")); p != nil { + t.Fatalf("own folder: got %+v", p) + } + // only on Windows + dir := filepath.Join(root, "apps", "magpie-app", "current") + if p := packagedIn("/", "linux", filepath.Join(dir, "magpie.exe")); p != nil { + t.Fatalf("linux: got %+v", p) + } +} + +// winget's portable package folder, as `winget install yetone.Magpie` +// leaves it per user and machine-wide. +func TestPackagedByWinget(t *testing.T) { + for _, exe := range []string{ + `C:\Users\yeton\AppData\Local\Microsoft\WinGet\Packages\yetone.Magpie_Microsoft.Winget.Source_8wekyb3d8bbwe\magpie.exe`, + `C:\Program Files\WinGet\Packages\yetone.Magpie_Microsoft.Winget.Source_8wekyb3d8bbwe\magpie.exe`, + // winget install --manifest, as seen on xiaomi-gpu (winget 1.29) + `C:\Users\yeton\AppData\Local\Microsoft\WinGet\Packages\yetone.Magpie__DefaultSource\magpie.exe`, + } { + p := packagedIn("/", "windows", exe) + if p == nil || p.Manager != "winget" || p.Package != "yetone.Magpie" { + t.Fatalf("%s: got %+v", exe, p) + } + if c := p.Command(); c != "winget upgrade --id yetone.Magpie" { + t.Fatalf("command %q", c) + } + } + if p := packagedIn("/", "windows", `C:\Users\yeton\Downloads\magpie-windows-amd64.exe`); p != nil { + t.Fatalf("downloaded: got %+v", p) + } + p := packagedIn("/", "windows", `C:\Users\yeton\AppData\Local\Microsoft\WinGet\Packages\odd\magpie.exe`) + if p == nil || p.Manager != "winget" || p.Command() != "winget upgrade magpie" { + t.Fatalf("unknown id: got %+v", p) + } +} + +// A path whose links can't be resolved is kept as given, so a Scoop +// install's current junction (which EvalSymlinks fails on) is still +// judged by PackagedBy, instead of Executable failing and the check being +// skipped. +func TestResolvedKeepsUnresolvablePath(t *testing.T) { + dir := t.TempDir() + missing := filepath.Join(dir, "gone", "magpie.exe") + if got := resolved(missing); got != missing { + t.Fatalf("resolved(%q) = %q", missing, got) + } + real := filepath.Join(dir, "magpie") + writeFile(t, real, "x") + want, _ := filepath.EvalSymlinks(real) + if got := resolved(real); got != want { + t.Fatalf("resolved(%q) = %q, want %q", real, got, want) + } +} diff --git a/internal/update/update.go b/internal/update/update.go index cc9d08d0b..456dba70d 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -372,13 +372,24 @@ func Homebrew(exe string) bool { return strings.Contains(filepath.ToSlash(exe), "/Cellar/magpie/") } -// Executable is the running binary, symlinks resolved. +// Executable is the running binary, symlinks resolved. When they can't be +// (Go's EvalSymlinks fails on the current junction Scoop runs an app from, +// "The system cannot find the path specified"), it is the path the binary +// was started from, which still names it. func Executable() (string, error) { exe, err := os.Executable() if err != nil { return "", err } - return filepath.EvalSymlinks(exe) + return resolved(exe), nil +} + +// resolved is path with its symlinks resolved, or path when that fails. +func resolved(path string) string { + if p, err := filepath.EvalSymlinks(path); err == nil { + return p + } + return path } // Writable reports whether magpie may replace what lives in dir.