From 0edeb1df81d675238768a0df8e6e5ebc279eaef0 Mon Sep 17 00:00:00 2001 From: "lijiacheng.ljc" Date: Wed, 6 Mar 2024 18:05:16 +0800 Subject: [PATCH] feat: ctx use marshal --- internal/luai/interfaces.go | 13 +++++++ internal/package.go | 15 +++++++- internal/plugin.go | 72 +++++++++++++++---------------------- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/internal/luai/interfaces.go b/internal/luai/interfaces.go index 31e22f37..259a3100 100644 --- a/internal/luai/interfaces.go +++ b/internal/luai/interfaces.go @@ -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"` +} diff --git a/internal/package.go b/internal/package.go index a8519604..ae3fe1cd 100644 --- a/internal/package.go +++ b/internal/package.go @@ -16,7 +16,11 @@ package internal -import "path/filepath" +import ( + "path/filepath" + + "github.com/version-fox/vfox/internal/luai" +) type Package struct { Main *Info @@ -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, + } +} diff --git a/internal/plugin.go b/internal/plugin.go index d4c1e7fe..4e17c833 100644 --- a/internal/plugin.go +++ b/internal/plugin.go @@ -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 { @@ -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 } @@ -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) @@ -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 } @@ -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 }