Skip to content

Commit

Permalink
vfox use fuzzy match version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance-fyi committed Apr 17, 2024
1 parent e48be93 commit b53bd8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,25 @@ func (b *Sdk) EnvKeys(version Version) (*env.Envs, error) {
}

func (b *Sdk) PreUse(version Version, scope UseScope) (Version, error) {
installedSdks := b.getLocalSdkPackages()
if !b.Plugin.HasFunction("PreUse") {
logger.Debug("plugin does not have PreUse function")
installedVersions := make(util.VersionSort, 0, len(installedSdks))
for _, sdk := range installedSdks {
installedVersions = append(installedVersions, string(sdk.Main.Version))
}
sort.Sort(installedVersions)
prefix := string(version) + "."
for _, v := range installedVersions {
if strings.HasPrefix(v, prefix) {
version = Version(v)
break
}
}
return version, nil
}

newVersion, err := b.Plugin.PreUse(version, b.Current(), scope, b.sdkManager.PathMeta.WorkingDirectory, b.getLocalSdkPackages())
newVersion, err := b.Plugin.PreUse(version, b.Current(), scope, b.sdkManager.PathMeta.WorkingDirectory, installedSdks)
if err != nil {
return "", fmt.Errorf("plugin [PreUse] error: err:%w", err)
}
Expand Down
14 changes: 14 additions & 0 deletions internal/util/verison.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ import (
"strings"
)

type VersionSort []string

func (s VersionSort) Len() int {
return len(s)
}

func (s VersionSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func (s VersionSort) Less(i, j int) bool {
return CompareVersion(s[i], s[j]) > 0
}

func CompareVersion(v1, v2 string) int {
parts1 := strings.Split(v1, ".")
parts2 := strings.Split(v2, ".")
Expand Down

0 comments on commit b53bd8f

Please sign in to comment.