Skip to content

Commit

Permalink
improve: find weasel new version in windows;
Browse files Browse the repository at this point in the history
  • Loading branch information
MapoMagpie committed Feb 10, 2024
1 parent e2ba8a9 commit 794bc19
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func FlushAndSync(opts *Options, dc *dict.Dictionary, ok bool) {
}
dc.Flush()
if opts.RestartRimeCmd != "" {
// TODO: check RestartRimeCmd, if weasel updated, the program path may be changed
cmd := util.ExecCommand(opts.RestartRimeCmd, false)
err := cmd.Run()
if err != nil {
Expand Down
37 changes: 35 additions & 2 deletions core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/goccy/go-yaml"
Expand Down Expand Up @@ -142,16 +144,22 @@ func osRimeDefaultValue() (dictPath, restartRimeCmd string) {
case "windows":
// find rime install path
dirEntries, err := os.ReadDir("C:\\PROGRA~2\\Rime")
var maxVersion string
if err == nil && len(dirEntries) > 0 {
for _, dir := range dirEntries {
if dir.IsDir() && strings.HasPrefix(dir.Name(), "weasel") {
restartRimeCmd = filepath.Join("C:\\PROGRA~2\\Rime", dir.Name(), "WeaselDeployer.exe") + " /deploy"
break
dirName := dir.Name()
if compareVersion(dirName, maxVersion) {
maxVersion = dirName
}
} else {
continue
}
}
}
if maxVersion != "" {
restartRimeCmd = filepath.Join("C:\\PROGRA~2\\Rime", maxVersion, "WeaselDeployer.exe") + " /deploy"
}
defaultSchema := findRimeDefaultSchema(filepath.Join(configDir, "rime", "default.custom.yaml"))
dictPath = filepath.Join(configDir, "Rime", defaultSchema+".dict.yaml")
case "dwain":
Expand Down Expand Up @@ -228,3 +236,28 @@ func fixPath(path string) string {
}
return os.ExpandEnv(path)
}

func parseVersion(version string) []int {
ret := make([]int, 0)
reg := regexp.MustCompile(`\d+`)
res := reg.FindAllString(version, -1)
for _, v := range res {
num, err := strconv.Atoi(v)
if err != nil {
fmt.Println("convert error", v)
}
ret = append(ret, num)
}
return ret
}

func compareVersion(v1, v2 string) bool {
vi := parseVersion(v1)
vj := parseVersion(v2)
for k := 0; k < len(vi) && k < len(vj); k++ {
if vi[k] != vj[k] {
return vi[k] > vj[k]
}
}
return v1 > v2
}
29 changes: 29 additions & 0 deletions core/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core

import (
"sort"
"testing"
)

func Test_compareMaxVersion(t *testing.T) {
tests := []struct {
name string
want string
versions []string
}{
{"case1", "weasel-0.15.15", []string{"weasel-0.14.3", "weasel-0.15.15", "weasel-0.13.5"}},
{"case2", "weasel-0.15.15", []string{"weasel-0.15.5", "weasel-0.15.15", "weasel-0.15.14"}},
{"case3", "weasel-0.115.5", []string{"weasel-0.115.5", "weasel-0.15.15", "weasel-0.15.14"}},
{"case4", "weasel-0.115.5", []string{"weasel-0.115.5", "", "weasel-0.15.14"}},
}
for _, tt := range tests {
t.Run(tt.name, func(_ *testing.T) {
sort.Slice(tt.versions, func(i, j int) bool {
return compareVersion(tt.versions[i], tt.versions[j])
})
if tt.versions[0] != tt.want {
t.Errorf("compareMaxVersion() = %v, want %v", tt.versions[0], tt.want)
}
})
}
}

0 comments on commit 794bc19

Please sign in to comment.