Skip to content

Commit

Permalink
chore(install): do not update registry
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 7, 2024
1 parent 06a3724 commit 2ddb3b1
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 94 deletions.
2 changes: 0 additions & 2 deletions src/upgrade/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,5 @@ func install(cfg *Config) error {
_ = hideFile(oldPath)
}

updateRegistry(cfg.Version, executable)

return nil
}
92 changes: 0 additions & 92 deletions src/upgrade/install_windows.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package upgrade

import (
"errors"
"path/filepath"
"strconv"
"strings"
"syscall"
"unsafe"

"golang.org/x/sys/windows/registry"
)

func hideFile(path string) error {
Expand All @@ -28,89 +22,3 @@ func hideFile(path string) error {

return nil
}

func updateRegistry(version, executable string) {
// 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 {
key.Close()
return
}

version = strings.TrimLeft(version, "v")

_ = key.SetStringValue("DisplayVersion", version)
_ = key.SetStringValue("DisplayName", "Oh My Posh")

splitted := strings.Split(version, ".")
if len(splitted) < 3 {
key.Close()
return
}

if u64, err := strconv.ParseUint(splitted[0], 10, 32); err == nil {
major := uint32(u64)
_ = key.SetDWordValue("MajorVersion", major)
_ = key.SetDWordValue("VersionMajor", major)
}

if u64, err := strconv.ParseUint(splitted[1], 10, 32); err == nil {
minor := uint32(u64)
_ = key.SetDWordValue("MinorVersion", minor)
_ = key.SetDWordValue("VersionMinor", minor)
}

key.Close()
}

// getRegistryKey tries all known registry paths to find the one we need to adjust (if any)
func getRegistryKey(installLocation string) (registry.Key, error) {
knownRegistryPaths := []struct {
Path string
Key registry.Key
}{
{`Software\Microsoft\Windows\CurrentVersion\Uninstall`, registry.CURRENT_USER},
{`Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`, registry.CURRENT_USER},
{`Software\Microsoft\Windows\CurrentVersion\Uninstall`, registry.LOCAL_MACHINE},
{`Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall`, registry.LOCAL_MACHINE},
}

for _, path := range knownRegistryPaths {
key, ok := tryRegistryKey(path.Key, path.Path, installLocation)
if ok {
return key, nil
}
}

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
}

0 comments on commit 2ddb3b1

Please sign in to comment.