Skip to content

Commit

Permalink
mod: Use the plugin name as the installed sdk name
Browse files Browse the repository at this point in the history
Previously the plugin's filename was used as the sdk name.
  • Loading branch information
aooohan committed Jan 18, 2024
1 parent 3fa4d9e commit b221f57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
22 changes: 10 additions & 12 deletions internal/sdk/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ func (m *Manager) Update(pluginName string) error {
}

func (m *Manager) Add(pluginName, url, alias string) error {
pname := pluginName
// official plugin
if len(url) == 0 {
args := strings.Split(pluginName, "/")
Expand All @@ -205,7 +204,6 @@ func (m *Manager) Add(pluginName, url, alias string) error {
}
category := args[0]
name := args[1]
pname = category
availablePlugins, err := m.Available()
if err != nil {
return err
Expand All @@ -222,16 +220,7 @@ func (m *Manager) Add(pluginName, url, alias string) error {
}
}

if len(alias) > 0 {
pname = alias
}

destPath := filepath.Join(m.PathMeta.PluginPath, pname+".lua")
if util.FileExists(destPath) {
return fmt.Errorf("plugin %s already exists", pname)
}

pterm.Printf("Adding plugin from %s...\n", url)
pterm.Printf("Loading plugin from %s...\n", url)
content, err := m.loadLuaFromFileOrUrl(url)
if err != nil {
return fmt.Errorf("failed to load plugin: %w", err)
Expand All @@ -242,6 +231,15 @@ func (m *Manager) Add(pluginName, url, alias string) error {
return fmt.Errorf("check plugin error: %w", err)
}
defer source.Close()

pname := source.Name
if len(alias) > 0 {
pname = alias
}
destPath := filepath.Join(m.PathMeta.PluginPath, pname+".lua")
if util.FileExists(destPath) {
return fmt.Errorf("plugin %s already exists", pname)
}
err = os.WriteFile(destPath, []byte(content), 0644)
if err != nil {
return fmt.Errorf("add plugin error: %w", err)
Expand Down
19 changes: 14 additions & 5 deletions internal/sdk/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
"github.com/version-fox/vfox/internal/module"
"github.com/version-fox/vfox/internal/util"
lua "github.com/yuin/gopher-lua"
"path/filepath"
"strings"
"regexp"
)

const (
Expand All @@ -38,7 +37,6 @@ type LuaPlugin struct {
pluginObj *lua.LTable
// plugin source path
SourcePath string
SourceName string
Name string
Author string
Version string
Expand Down Expand Up @@ -342,15 +340,19 @@ func NewLuaPlugin(content, path string, osType util.OSType, archType util.ArchTy
state: luaVMInstance,
pluginObj: PLUGIN,
SourcePath: path,
SourceName: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
}

if err := source.checkValid(); err != nil {
return nil, err
}

if name := PLUGIN.RawGetString("name"); name.Type() != lua.LTNil {
if name := PLUGIN.RawGetString("name"); name.Type() == lua.LTNil {
return nil, fmt.Errorf("no plugin name provided")
} else {
source.Name = name.String()
if !isValidName(source.Name) {
return nil, fmt.Errorf("invalid plugin name")
}
}
if version := PLUGIN.RawGetString("version"); version.Type() != lua.LTNil {
source.Version = version.String()
Expand All @@ -366,3 +368,10 @@ func NewLuaPlugin(content, path string, osType util.OSType, archType util.ArchTy
}
return source, nil
}

func isValidName(name string) bool {
// The regular expression means: start with a letter,
// followed by any number of letters, digits, or underscores.
re := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]*$`)
return re.MatchString(name)
}
6 changes: 3 additions & 3 deletions internal/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (b *Sdk) Use(version Version, scope UseScope) error {
return err
}
}
b.sdkManager.Record.Add(b.Plugin.SourceName, string(version))
b.sdkManager.Record.Add(b.Plugin.Name, string(version))
defer b.sdkManager.Record.Save()
pterm.Printf("Now using %s.\n", pterm.LightGreen(label))
if !env.IsHookEnv() {
Expand Down Expand Up @@ -238,7 +238,7 @@ func (b *Sdk) List() []Version {
}

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

Expand Down Expand Up @@ -394,7 +394,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.SourceName)),
InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(source.Name)),
Plugin: source,
}, nil
}

0 comments on commit b221f57

Please sign in to comment.