mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat(upgrade): update correct registry entry
This commit is contained in:
parent
58fd8e5f82
commit
62e782a4eb
|
@ -111,6 +111,7 @@ func (m *model) View() string {
|
||||||
m.spinner.Spinner = spinner.Moon
|
m.spinner.Spinner = spinner.Moon
|
||||||
message = "Verifying download"
|
message = "Verifying download"
|
||||||
case installing:
|
case installing:
|
||||||
|
m.spinner.Spinner = spinner.Jump
|
||||||
message = "Installing"
|
message = "Installing"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ func install(tag string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setState(installing)
|
||||||
|
|
||||||
targetDir := filepath.Dir(executable)
|
targetDir := filepath.Dir(executable)
|
||||||
fileName := filepath.Base(executable)
|
fileName := filepath.Base(executable)
|
||||||
|
|
||||||
|
@ -68,7 +70,7 @@ func install(tag string) error {
|
||||||
_ = hideFile(oldPath)
|
_ = hideFile(oldPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRegistry(tag)
|
updateRegistry(tag, executable)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,4 @@ func hideFile(_ string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateRegistry(_ string) {}
|
func updateRegistry(_, _ string) {}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package upgrade
|
package upgrade
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -27,9 +29,18 @@ func hideFile(path string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateRegistry(version string) {
|
func updateRegistry(version, executable string) {
|
||||||
key, err := getRegistryKey()
|
// needs to be the parent directory of the executable's bin directory
|
||||||
|
// with a trailing backslash to match the registry key
|
||||||
|
// in case this wasn't installed with the installer, nothing will match
|
||||||
|
// and we don't set the registry keys
|
||||||
|
installLocation := filepath.Dir(executable)
|
||||||
|
installLocation = filepath.Dir(installLocation)
|
||||||
|
installLocation += `\`
|
||||||
|
|
||||||
|
key, err := getRegistryKey(installLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
key.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +51,7 @@ func updateRegistry(version string) {
|
||||||
|
|
||||||
splitted := strings.Split(version, ".")
|
splitted := strings.Split(version, ".")
|
||||||
if len(splitted) < 3 {
|
if len(splitted) < 3 {
|
||||||
|
key.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,15 +66,51 @@ func updateRegistry(version string) {
|
||||||
_ = key.SetDWordValue("MinorVersion", minor)
|
_ = key.SetDWordValue("MinorVersion", minor)
|
||||||
_ = key.SetDWordValue("VersionMinor", minor)
|
_ = key.SetDWordValue("VersionMinor", minor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
key.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRegistryKey() (registry.Key, error) {
|
// getRegistryKey tries all known registry paths to find the one we need to adjust (if any)
|
||||||
path := `Software\Microsoft\Windows\CurrentVersion\Uninstall\Oh My Posh_is1`
|
func getRegistryKey(installLocation string) (registry.Key, error) {
|
||||||
|
knownRegistryPaths := []struct {
|
||||||
|
Key registry.Key
|
||||||
|
Path string
|
||||||
|
}{
|
||||||
|
{registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Uninstall`},
|
||||||
|
{registry.CURRENT_USER, `Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`},
|
||||||
|
{registry.LOCAL_MACHINE, `Software\Microsoft\Windows\CurrentVersion\Uninstall`},
|
||||||
|
{registry.LOCAL_MACHINE, `Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`},
|
||||||
|
}
|
||||||
|
|
||||||
key, err := registry.OpenKey(registry.CURRENT_USER, path, registry.WRITE)
|
for _, path := range knownRegistryPaths {
|
||||||
if err == nil {
|
key, ok := tryRegistryKey(path.Key, path.Path, installLocation)
|
||||||
|
if ok {
|
||||||
return key, nil
|
return key, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return registry.OpenKey(registry.LOCAL_MACHINE, path, registry.WRITE)
|
|
||||||
|
return registry.CURRENT_USER, errors.New("could not find registry key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// tryRegistryKey tries to open the registry key for the given path
|
||||||
|
// and checks if the install location matches with the current executable's location
|
||||||
|
func tryRegistryKey(key registry.Key, path, installLocation string) (registry.Key, bool) {
|
||||||
|
path += `\Oh My Posh_is1`
|
||||||
|
|
||||||
|
readKey, err := registry.OpenKey(key, path, registry.READ)
|
||||||
|
if err != nil {
|
||||||
|
return key, false
|
||||||
|
}
|
||||||
|
|
||||||
|
location, _, err := readKey.GetStringValue("InstallLocation")
|
||||||
|
if err != nil {
|
||||||
|
return key, false
|
||||||
|
}
|
||||||
|
|
||||||
|
if location != installLocation {
|
||||||
|
return key, false
|
||||||
|
}
|
||||||
|
|
||||||
|
key, err = registry.OpenKey(key, path, registry.WRITE)
|
||||||
|
return key, err == nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue