Files
yetone 90b1ac7956 update, gui: Add to PATH on Windows writes a magpie.cmd beside an app not named magpie.exe, so the site's magpie-windows-amd64.exe runs as magpie in a new terminal, and says so (#942, mintonight)
Add to PATH put the portable download's folder first in the user's PATH, but a terminal looks for magpie by that name only, so the command was never found. cliPlace now writes magpie.cmd beside a program not named magpie.exe: it runs %~dp0<the program's name> %*, which an update keeps, since it replaces the program where it is under its name. ReadCLI reads that magpie.cmd as this app (shimRuns), and the GUI says, in Add's tooltip, the row and the status, that magpie.cmd runs magpie-windows-amd64.exe. Going through cmd also makes PowerShell wait for the GUI-linked program and gives it the console. TestCLIShim and cli-path.test.cjs's new case fail without the change.
2026-10-06 02:18:07 +08:00

67 lines
2.1 KiB
Go

package update
import (
"fmt"
"os"
"path/filepath"
ptr "unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
// installedShells are PowerShell and cmd, which both take the registry's
// PATH (proc.ShellPath).
func installedShells() []cliShell {
return []cliShell{{Name: "PowerShell", Default: true}, {Name: "cmd"}}
}
// cliDir is the app's own folder, put first in the user's PATH: a link
// needs Developer Mode or an administrator, and a copy wouldn't follow
// the app's updates.
func cliDir(exe string, _ []string, _ string) string { return filepath.Dir(exe) }
// cliPlace puts dir first in the user's PATH in the registry, where a
// terminal opened from now on takes it up, and tells Explorer; an app not
// named magpie.exe gets its magpie.cmd there first (writeShim).
func cliPlace(exe, dir string) error {
if _, err := writeShim(exe); err != nil {
return err
}
k, err := registry.OpenKey(registry.CURRENT_USER, `Environment`, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
return err
}
defer k.Close()
v, typ, err := k.GetStringValue("Path")
if err != nil && err != registry.ErrNotExist {
return err
}
nv := pathFirst(v, dir)
if nv == v {
return nil
}
// kept REG_EXPAND_SZ, as Windows has it, so %USERPROFILE% in it still works
if err == registry.ErrNotExist || typ == registry.EXPAND_SZ {
err = k.SetExpandStringValue("Path", nv)
} else {
err = k.SetStringValue("Path", nv)
}
if err != nil {
return fmt.Errorf("adding %s to your PATH: %w", dir, err)
}
os.Setenv("PATH", dir+";"+os.Getenv("PATH"))
settingChanged()
return nil
}
// settingChanged tells Explorer the environment changed, so what it starts
// next — a terminal from the Start menu — has the new PATH.
func settingChanged() {
const hwndBroadcast, wmSettingChange, smtoAbortIfHung = 0xffff, 0x001A, 0x0002
env, _ := windows.UTF16PtrFromString("Environment")
var res uintptr
windows.NewLazySystemDLL("user32.dll").NewProc("SendMessageTimeoutW").Call(
hwndBroadcast, wmSettingChange, 0, uintptr(ptr.Pointer(env)), smtoAbortIfHung, 5000, uintptr(ptr.Pointer(&res)))
}