Skip to content

Commit 5ab808e

Browse files
committed
chore: update code
1 parent 3dc41b3 commit 5ab808e

4 files changed

Lines changed: 38 additions & 52 deletions

File tree

internal/base/interfaces.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"path/filepath"
2222

23-
"github.com/version-fox/vfox/internal/logger"
2423
"github.com/version-fox/vfox/internal/util"
2524
)
2625

@@ -101,43 +100,8 @@ type AvailableHookResultItem struct {
101100
Addition []*Info `json:"addition"`
102101
}
103102

104-
type AvailableHookResult = []*AvailableHookResultItem
105-
106-
func CreatePackages(sdkName string, hookResult []*AvailableHookResultItem) []*Package {
107-
var result []*Package
108-
for _, item := range hookResult {
109-
mainSdk := &Info{
110-
Name: sdkName,
111-
Version: item.Version,
112-
Note: item.Note,
113-
}
114-
115-
var additionalArr []*Info
116-
117-
for i, addition := range item.Addition {
118-
if addition.Name == "" {
119-
logger.Errorf("[Available] additional file %d no name provided", i+1)
120-
}
121-
122-
additionalArr = append(additionalArr, &Info{
123-
Name: addition.Name,
124-
Version: addition.Version,
125-
Path: addition.Path,
126-
Note: addition.Note,
127-
})
128-
}
129-
130-
result = append(result, &Package{
131-
Main: mainSdk,
132-
Additions: additionalArr,
133-
})
134-
}
135-
136-
return result
137-
}
138-
139103
type PreInstallHookCtx struct {
140-
Version Version `json:"version"`
104+
Version string `json:"version"`
141105
}
142106

143107
type PreInstallHookResultAdditionItem struct {
@@ -288,7 +252,7 @@ func (p *Package) Clone() *Package {
288252
}
289253

290254
type Plugin interface {
291-
Available(ctx *AvailableHookCtx) (*AvailableHookResult, error)
255+
Available(ctx *AvailableHookCtx) ([]*AvailableHookResultItem, error)
292256

293257
PreInstall(ctx *PreInstallHookCtx) (*PreInstallHookResult, error)
294258
PostInstall(ctx *PostInstallHookCtx) error

internal/plugin/luai/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (l *LuaPlugin) Close() {
2727
l.vm.Close()
2828
}
2929

30-
func (l *LuaPlugin) Available(ctx *base.AvailableHookCtx) (*base.AvailableHookResult, error) {
30+
func (l *LuaPlugin) Available(ctx *base.AvailableHookCtx) ([]*base.AvailableHookResultItem, error) {
3131
L := l.vm.Instance
3232
ctxTable, err := Marshal(L, ctx)
3333
if err != nil {
@@ -41,13 +41,13 @@ func (l *LuaPlugin) Available(ctx *base.AvailableHookCtx) (*base.AvailableHookRe
4141
return nil, errors.New("no result provided")
4242
}
4343

44-
hookResult := base.AvailableHookResult{}
44+
hookResult := []*base.AvailableHookResultItem{}
4545
err = Unmarshal(table, &hookResult)
4646
if err != nil {
4747
return nil, errors.New("failed to unmarshal the return value: " + err.Error())
4848
}
4949

50-
return &hookResult, nil
50+
return hookResult, nil
5151
}
5252
func (l *LuaPlugin) PreInstall(ctx *base.PreInstallHookCtx) (*base.PreInstallHookResult, error) {
5353
L := l.vm.Instance

internal/plugin/luai/vm.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,6 @@ func (vm *LuaVM) CallFunction(pluginObj *lua.LTable, funcName string, _args ...l
8888
return vm.ReturnedValue(), nil
8989
}
9090

91-
func (vm *LuaVM) GetTableString(table *lua.LTable, key string) string {
92-
if value := table.RawGetString(key); value.Type() != lua.LTNil {
93-
return value.String()
94-
}
95-
return ""
96-
}
97-
9891
func (vm *LuaVM) Close() {
9992
vm.Instance.Close()
10093
}

internal/plugin/plugin.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,44 @@ func (l *PluginWrapper) invokeAvailable(args []string) ([]*base.Package, error)
100100
ctx := base.AvailableHookCtx{
101101
Args: args,
102102
}
103-
result, err := l.impl.Available(&ctx)
103+
hookResult, err := l.impl.Available(&ctx)
104104
if err != nil {
105105
return nil, err
106106
}
107-
if result == nil {
107+
if hookResult == nil {
108108
return []*base.Package{}, nil
109109
}
110110

111-
return base.CreatePackages(l.Name, *result), nil
111+
var result []*base.Package
112+
for _, item := range hookResult {
113+
mainSdk := &base.Info{
114+
Name: l.Name,
115+
Version: item.Version,
116+
Note: item.Note,
117+
}
118+
119+
var additionalArr []*base.Info
120+
121+
for i, addition := range item.Addition {
122+
if addition.Name == "" {
123+
logger.Errorf("[Available] additional file %d no name provided", i+1)
124+
}
125+
126+
additionalArr = append(additionalArr, &base.Info{
127+
Name: addition.Name,
128+
Version: addition.Version,
129+
Path: addition.Path,
130+
Note: addition.Note,
131+
})
132+
}
133+
134+
result = append(result, &base.Package{
135+
Main: mainSdk,
136+
Additions: additionalArr,
137+
})
138+
}
139+
140+
return result, nil
112141
}
113142

114143
func (l *PluginWrapper) Available(args []string) ([]*base.Package, error) {
@@ -158,7 +187,7 @@ func (l *PluginWrapper) Available(args []string) ([]*base.Package, error) {
158187

159188
func (l *PluginWrapper) PreInstall(version base.Version) (*base.Package, error) {
160189
ctx := base.PreInstallHookCtx{
161-
Version: version,
190+
Version: string(version),
162191
}
163192

164193
result, err := l.impl.PreInstall(&ctx)

0 commit comments

Comments
 (0)