Skip to content

Commit

Permalink
feat: ctx use marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Mar 6, 2024
1 parent 882bb9a commit 0edeb1d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 45 deletions.
13 changes: 13 additions & 0 deletions internal/luai/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ type PreUseHookCtx struct {
PreviousVersion string `luai:"previousVersion"`
InstalledSdks map[string]*LuaSDKInfo `luai:"installedSdks"`
}

type PostInstallHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
RootPath string `luai:"rootPath"`
SdkInfo map[string]*LuaSDKInfo `luai:"sdkInfo"`
}

type EnvKeysHookCtx struct {
RuntimeVersion string `luai:"runtimeVersion"`
// TODO Will be deprecated in future versions
Path string `luai:"path"`
SdkInfo map[string]*LuaSDKInfo `luai:"sdkInfo"`
}
15 changes: 14 additions & 1 deletion internal/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package internal

import "path/filepath"
import (
"path/filepath"

"github.com/version-fox/vfox/internal/luai"
)

type Package struct {
Main *Info
Expand All @@ -41,3 +45,12 @@ func (i *Info) storagePath(parentDir string) string {
}
return filepath.Join(parentDir, i.Name+"-"+string(i.Version))
}

func NewLuaSDKInfo(info *Info) *luai.LuaSDKInfo {
return &luai.LuaSDKInfo{
Name: info.Name,
Version: string(info.Version),
Path: info.Path,
Note: info.Note,
}
}
72 changes: 28 additions & 44 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,21 @@ func (l *LuaPlugin) parseInfo(table *lua.LTable) (*Info, error) {

func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
L := l.vm.Instance
sdkArr := L.NewTable()

ctx := &luai.PostInstallHookCtx{
RuntimeVersion: RuntimeVersion,
RootPath: rootPath,
SdkInfo: make(map[string]*luai.LuaSDKInfo),
}

for _, v := range sdks {
sdkTable := l.createSdkInfoTable(v)
L.SetField(sdkArr, v.Name, sdkTable)
ctx.SdkInfo[v.Name] = NewLuaSDKInfo(v)
}

ctxTable := L.NewTable()
L.SetField(ctxTable, "sdkInfo", sdkArr)
L.SetField(ctxTable, "runtimeVersion", lua.LString(RuntimeVersion))
L.SetField(ctxTable, "rootPath", lua.LString(rootPath))
ctxTable, err := luai.Marshal(L, ctx)
if err != nil {
return err
}

function := l.pluginObj.RawGetString(PostInstallHook)
if function.Type() == lua.LTNil {
Expand All @@ -296,18 +301,24 @@ func (l *LuaPlugin) PostInstall(rootPath string, sdks []*Info) error {
func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
L := l.vm.Instance
mainInfo := sdkPackage.Main
sdkArr := L.NewTable()

ctx := &luai.EnvKeysHookCtx{
// TODO Will be deprecated in future versions
Path: mainInfo.Path,
RuntimeVersion: RuntimeVersion,
SdkInfo: make(map[string]*luai.LuaSDKInfo),
}

for _, v := range sdkPackage.Additions {
sdkTable := l.createSdkInfoTable(v)
L.SetField(sdkArr, v.Name, sdkTable)
ctx.SdkInfo[v.Name] = NewLuaSDKInfo(v)
}

ctxTable, err := luai.Marshal(L, ctx)
if err != nil {
return nil, err
}
ctxTable := L.NewTable()
L.SetField(ctxTable, "sdkInfo", sdkArr)
L.SetField(ctxTable, "runtimeVersion", lua.LString(RuntimeVersion))
// TODO Will be deprecated in future versions
L.SetField(ctxTable, "path", lua.LString(mainInfo.Path))

if err := l.vm.CallFunction(l.pluginObj.RawGetString(EnvKeysHook), l.pluginObj, ctxTable); err != nil {
if err = l.vm.CallFunction(l.pluginObj.RawGetString(EnvKeysHook), l.pluginObj, ctxTable); err != nil {
return nil, err
}

Expand All @@ -316,7 +327,6 @@ 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")
}
var err error
envKeys := make(env.Envs)
table.ForEach(func(key lua.LValue, value lua.LValue) {
kvTable, ok := value.(*lua.LTable)
Expand All @@ -336,28 +346,10 @@ func (l *LuaPlugin) EnvKeys(sdkPackage *Package) (env.Envs, error) {
return envKeys, nil
}

func (l *LuaPlugin) getTableField(table *lua.LTable, fieldName string) (lua.LValue, error) {
value := table.RawGetString(fieldName)
if value.Type() == lua.LTNil {
return nil, fmt.Errorf("field '%s' not found", fieldName)
}
return value, nil
}

func (l *LuaPlugin) Label(version string) string {
return fmt.Sprintf("%s@%s", l.Name, version)
}

func (l *LuaPlugin) createSdkInfoTable(info *Info) *lua.LTable {
L := l.vm.Instance
sdkTable := L.NewTable()
L.SetField(sdkTable, "name", lua.LString(info.Name))
L.SetField(sdkTable, "version", lua.LString(info.Version))
L.SetField(sdkTable, "path", lua.LString(info.Path))
L.SetField(sdkTable, "note", lua.LString(info.Note))
return sdkTable
}

func (l *LuaPlugin) HasFunction(name string) bool {
return l.pluginObj.RawGetString(name) != lua.LNil
}
Expand All @@ -375,15 +367,7 @@ func (l *LuaPlugin) PreUse(version Version, previousVersion Version, scope UseSc
}

for _, v := range installedSdks {
sdk := v.Main

lSdk := &luai.LuaSDKInfo{
Name: sdk.Name,
Version: string(sdk.Version),
Path: sdk.Path,
Note: sdk.Note,
}

lSdk := NewLuaSDKInfo(v.Main)
ctx.InstalledSdks[lSdk.Version] = lSdk
}

Expand Down

0 comments on commit 0edeb1d

Please sign in to comment.