Skip to content

Commit

Permalink
bugfix: Invalid setting of alias #39
Browse files Browse the repository at this point in the history
In the local plug-in system, the name of the sdk depends on the file name of the plug-in.
When setting an alias, it is equivalent to setting the file name of the plug-in to the name of the alias.

close #39
  • Loading branch information
aooohan committed Feb 2, 2024
1 parent b41c2e6 commit 1987a2a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
8 changes: 4 additions & 4 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,23 @@ func (m *Manager) Update(pluginName string) error {
return fmt.Errorf("check %s plugin failed, err: %w", updateUrl, err)
}
success := false
backupPath := sdk.Plugin.SourcePath + ".bak"
err = util.CopyFile(sdk.Plugin.SourcePath, backupPath)
backupPath := sdk.Plugin.Filepath + ".bak"
err = util.CopyFile(sdk.Plugin.Filepath, backupPath)
if err != nil {
return fmt.Errorf("backup %s plugin failed, err: %w", updateUrl, err)
}
defer func() {
if success {
_ = os.Remove(backupPath)
} else {
_ = os.Rename(backupPath, sdk.Plugin.SourcePath)
_ = os.Rename(backupPath, sdk.Plugin.Filepath)
}
}()
pterm.Println("Checking plugin version...")
if util.CompareVersion(source.Version, sdk.Plugin.Version) <= 0 {
return fmt.Errorf("the plugin is already the latest version")
}
err = os.WriteFile(sdk.Plugin.SourcePath, []byte(content), 0644)
err = os.WriteFile(sdk.Plugin.Filepath, []byte(content), 0644)
if err != nil {
return fmt.Errorf("update %s plugin failed: %w", updateUrl, err)
}
Expand Down
14 changes: 10 additions & 4 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/module"
lua "github.com/yuin/gopher-lua"
"path/filepath"
"regexp"
"strings"
)

const (
Expand All @@ -35,7 +37,10 @@ type LuaPlugin struct {
state *lua.LState
pluginObj *lua.LTable
// plugin source path
SourcePath string
Filepath string
// plugin filename, this is also alias name, sdk-name
Filename string
// The name defined inside the plugin
Name string
Author string
Version string
Expand Down Expand Up @@ -365,9 +370,10 @@ func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
PLUGIN := pluginObj.(*lua.LTable)

source := &LuaPlugin{
state: luaVMInstance,
pluginObj: PLUGIN,
SourcePath: path,
state: luaVMInstance,
pluginObj: PLUGIN,
Filepath: path,
Filename: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
}

if err := source.checkValid(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ func (b *Sdk) preInstallSdk(info *Info, sdkDestPath string) (string, error) {
return path, nil
}
if strings.HasPrefix(info.Path, "https://") || strings.HasPrefix(info.Path, "http://") {
if err := b.moveRemoteFile(info, sdkDestPath); err != nil {
if err := b.moveRemoteFile(info, path); err != nil {
return "", err
}
return path, nil
} else {
if err := b.moveLocalFile(info, sdkDestPath); err != nil {
if err := b.moveLocalFile(info, path); err != nil {
return "", err
}
return path, nil
Expand Down Expand Up @@ -241,7 +241,7 @@ func (b *Sdk) Use(version Version, scope UseScope) error {
return err
}
}
b.sdkManager.Record.Add(b.Plugin.Name, string(version))
b.sdkManager.Record.Add(b.Plugin.Filename, string(version))
defer b.sdkManager.Record.Save()
pterm.Printf("Now using %s.\n", pterm.LightGreen(label))
if !env.IsHookEnv() {
Expand Down Expand Up @@ -271,7 +271,7 @@ func (b *Sdk) List() []Version {
}

func (b *Sdk) Current() Version {
version := b.sdkManager.Record.Export()[b.Plugin.Name]
version := b.sdkManager.Record.Export()[b.Plugin.Filename]
return Version(version)
}

Expand Down Expand Up @@ -427,7 +427,7 @@ func (b *Sdk) label(version Version) string {
func NewSdk(manager *Manager, source *LuaPlugin) (*Sdk, error) {
return &Sdk{
sdkManager: manager,
InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(source.Name)),
InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(source.Filename)),
Plugin: source,
}, nil
}

0 comments on commit 1987a2a

Please sign in to comment.