Skip to content

Commit

Permalink
feat: unmarshal hook result
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 6, 2024
1 parent 0edeb1d commit a695f47
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 38 deletions.
9 changes: 9 additions & 0 deletions internal/luai/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type PreUseHookCtx struct {
InstalledSdks map[string]*LuaSDKInfo `luai:"installedSdks"`
}

type PreUseHookResult struct {
Version string `luai:"version"`
}

type PostInstallHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
RootPath string `luai:"rootPath"`
Expand All @@ -44,3 +48,8 @@ type EnvKeysHookCtx struct {
Path string `luai:"path"`
SdkInfo map[string]*LuaSDKInfo `luai:"sdkInfo"`
}

type EnvKeysHookResultItem struct {
Key string `luai:"key"`
Value string `luai:"value"`
}
66 changes: 28 additions & 38 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,33 +237,22 @@ func (l *LuaPlugin) PreInstall(version Version) (*Package, error) {
}

func (l *LuaPlugin) parseInfo(table *lua.LTable) (*Info, error) {
versionLua := table.RawGetString("version")
if versionLua == lua.LNil {
return nil, fmt.Errorf("no version number provided")
info := &luai.LuaSDKInfo{}
err := luai.Unmarshal(table, info)
if err != nil {
return nil, err
}
var (
path string
note string
name string
version string
)
version = versionLua.String()

if urlLua := table.RawGetString("url"); urlLua != lua.LNil {
path = urlLua.String()
}
if noteLua := table.RawGetString("note"); noteLua != lua.LNil {
note = noteLua.String()
}
if nameLua := table.RawGetString("name"); nameLua != lua.LNil {
name = nameLua.String()
if info.Version == "" {
return nil, fmt.Errorf("no version number provided")
}

checksum := l.Checksum(table)
return &Info{
Name: name,
Version: Version(version),
Path: path,
Note: note,
Name: info.Name,
Version: Version(info.Version),
Path: info.Path,
Note: info.Name,
Checksum: checksum,
}, nil
}
Expand Down Expand Up @@ -327,18 +316,19 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
if table == nil || table.Type() == lua.LTNil || table.Len() == 0 {
return nil, fmt.Errorf("no environment variables provided")
}

envKeys := make(env.Envs)
table.ForEach(func(key lua.LValue, value lua.LValue) {
kvTable, ok := value.(*lua.LTable)
if !ok {
err = fmt.Errorf("the return value is not a table")
return
}
key = kvTable.RawGetString("key")
value = kvTable.RawGetString("value")
s := value.String()
envKeys[key.String()] = &s
})

items := []*luai.EnvKeysHookResultItem{}
err = luai.Unmarshal(table, &items)
if err != nil {
return nil, err
}

for _, item := range items {
envKeys[item.Key] = &item.Value
}

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -392,13 +382,13 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
return "", nil
}

luaVer := l.vm.GetTableString(table, "version")
if luaVer == "" {
// ignore version field not found
return "", nil
result := &luai.PreUseHookResult{}

if err := luai.Unmarshal(table, result); err != nil {
return "", err
}

return Version(luaVer), nil
return Version(result.Version), nil
}

func NewLuaPlugin(content, path string, manager *Manager) (*LuaPlugin, error) {
Expand Down
43 changes: 43 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"strings"
"testing"

_ "embed"
Expand Down Expand Up @@ -28,6 +29,48 @@ func TestPlugin(t *testing.T) {
}
})

t.Run("EnvKeys", func(t *testing.T) {
manager := NewSdkManager()

plugin, err := NewLuaPlugin(pluginContent, pluginPath, manager)
if err != nil {
t.Fatal(err)
}

keys, err := plugin.EnvKeys(&Package{
Main: &Info{
Name: "java",
Version: "1.0.0",
Path: "/path/to/java",
Note: "xxxx",
},
Additions: []*Info{
{
Name: "sdk-name",
Version: "9.0.0",
Path: "/path/to/sdk",
Note: "xxxx",
},
},
})
if err != nil {
t.Fatal(err)
}

javaHome := keys["JAVA_HOME"]
if *javaHome == "" {
t.Errorf("expected JAVA_HOME to be set, got '%s'", *javaHome)
}
path := keys["PATH"]
if *path == "" {
t.Errorf("expected PATH to be set, got '%s'", *path)
}

if !strings.HasSuffix(*path, "/bin") {
t.Errorf("expected PATH to end with '/bin', got '%s'", *path)
}
})

t.Run("PreUse", func(t *testing.T) {
manager := NewSdkManager()

Expand Down

0 comments on commit a695f47

Please sign in to comment.