Skip to content

Commit

Permalink
feat: use command fuzzy match version number (#212)
Browse files Browse the repository at this point in the history
Fuzzy match version if the plugin doesn't implement `PreUse` hook.

---------

Co-authored-by: lihan <[email protected]>
  • Loading branch information
Chance-fyi and aooohan authored Apr 18, 2024
1 parent 46e716e commit f75b6e2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
9 changes: 5 additions & 4 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func (l *LuaPlugin) HasFunction(name string) bool {
}

func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseScope, cwd string, installedSdks []*Package) (Version, error) {
if !l.HasFunction("PreUse") {
logger.Debug("plugin does not have PreUse function")
return "", nil
}

L := l.vm.Instance

ctx := PreUseHookCtx{
Expand All @@ -300,10 +305,6 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
return "", err
}

if !l.HasFunction("PreUse") {
return "", nil
}

if err = l.CallFunction("PreUse", ctxTable); err != nil {
return "", err
}
Expand Down
25 changes: 17 additions & 8 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,28 @@ func (b *Sdk) EnvKeys(version Version) (*env.Envs, error) {
}

func (b *Sdk) PreUse(version Version, scope UseScope) (Version, error) {
if !b.Plugin.HasFunction("PreUse") {
logger.Debug("plugin does not have PreUse function")
return version, nil
}

newVersion, err := b.Plugin.PreUse(version, b.Current(), scope, b.sdkManager.PathMeta.WorkingDirectory, b.getLocalSdkPackages())
installedSdks := 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)
}

// If the plugin does not return a version, it means that the plugin does not want to change the version.
// If the plugin does not return a version, it means that the plugin does
// not want to change the version or not implement the PreUse function.
// We can simply fuzzy match the version based on the input version.
if newVersion == "" {
return version, nil
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) {
newVersion = Version(v)
break
}
}
}

return newVersion, nil
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 f75b6e2

Please sign in to comment.