Skip to content

Commit 5abcf2d

Browse files
committed
update
1 parent 2edba20 commit 5abcf2d

File tree

4 files changed

+395
-6
lines changed

4 files changed

+395
-6
lines changed

internal/luapi/plugin/fromlua.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
type LuaConfItem string
99

1010
const (
11-
SDKName LuaConfItem = "sdk_name"
12-
PluginName LuaConfItem = "plugin_name"
13-
PluginVersion LuaConfItem = "plugin_version"
14-
Prequisite LuaConfItem = "prequisite"
15-
Homepage LuaConfItem = "homepage"
16-
Crawler LuaConfItem = "crawl"
11+
SDKName LuaConfItem = "sdk_name"
12+
PluginName LuaConfItem = "plugin_name"
13+
PluginVersion LuaConfItem = "plugin_version"
14+
Prequisite LuaConfItem = "prequisite"
15+
Homepage LuaConfItem = "homepage"
16+
Crawler LuaConfItem = "crawl"
17+
PostInstall LuaConfItem = "postInstall" // optional
18+
CustomedInstall LuaConfItem = "install" // optional
1719
)
1820

1921
var InstallerConfig LuaConfItem = LuaConfItem(lua_global.InstallerConfigName)

internal/luapi/plugin/fromlua_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,35 @@ func TestGetLuaConfItem(t *testing.T) {
5050
assert.Equal(t, noneShouldBe, s, fmt.Sprintf("none should be: %s", noneShouldBe))
5151
}
5252
}
53+
54+
var postInstallHandlerScript = `
55+
function postInstall(installed_path)
56+
print("Post-install handler executed!")
57+
print(installed_path)
58+
-- print(additional)
59+
return true
60+
end
61+
`
62+
63+
func TestPostInstallHandler(t *testing.T) {
64+
if l, err := ExecuteLuaScriptL(postInstallHandlerScript); err != nil {
65+
t.Error(err)
66+
} else {
67+
postInstall := l.GetGlobal(string(PostInstall))
68+
if postInstall == nil || postInstall.Type() != lua.LTFunction {
69+
return
70+
}
71+
72+
if err := l.CallByParam(lua.P{
73+
Fn: postInstall,
74+
NRet: 1,
75+
Protect: true,
76+
}, lua.LString("/a/b/c/d/e"), lua.LString("xxx")); err != nil {
77+
t.Error(err)
78+
return
79+
}
80+
81+
result := l.Get(-1)
82+
assert.Equal(t, "true", result.String(), "should be 'true'")
83+
}
84+
}

internal/luapi/plugin/plugin.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plugin
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"os"
78
"path/filepath"
@@ -125,6 +126,10 @@ func (p *Plugin) cacheFilePathForVersionList() string {
125126
}
126127

127128
func (p *Plugin) loadVersionListFromCache() {
129+
if cnf.GetCacheDisabled() {
130+
// cache is disabled.
131+
return
132+
}
128133
cachePath := p.cacheFilePathForVersionList()
129134
if ok, _ := gutils.PathIsExist(cachePath); !ok {
130135
return
@@ -260,3 +265,44 @@ func (p *Plugin) GetSDKName() (sdkName string, err error) {
260265
sdkName = p.SDKName
261266
return
262267
}
268+
269+
type CustomedFuncFromLua func() error
270+
271+
func (p *Plugin) getFuncFromLua(luaItem LuaConfItem, args ...string) CustomedFuncFromLua {
272+
luaFunc := p.result.Lua.L.GetGlobal(string(luaItem))
273+
if luaFunc == nil || luaFunc.Type() != lua.LTFunction {
274+
return nil
275+
}
276+
277+
f := func() error {
278+
luaFuncArgs := make([]lua.LValue, len(args))
279+
for i, arg := range args {
280+
luaFuncArgs[i] = lua.LString(arg)
281+
}
282+
283+
// if luaFuncArgs have more args than the lua function expects, the extra args will be ignored.
284+
if err := p.result.Lua.L.CallByParam(lua.P{
285+
Fn: luaFunc,
286+
NRet: 1,
287+
Protect: true,
288+
}, luaFuncArgs...); err != nil {
289+
return err
290+
}
291+
292+
result := p.result.Lua.L.Get(-1)
293+
if result.String() != "true" {
294+
return errors.New("post-install handler failed")
295+
}
296+
return nil
297+
}
298+
return f
299+
}
300+
301+
// user can custom his/her own install method in lua plugins.
302+
func (p *Plugin) GetCustomedInstallHandler(args ...string) CustomedFuncFromLua {
303+
return p.getFuncFromLua(CustomedInstall, args...)
304+
}
305+
306+
func (p *Plugin) GetPostInstallHandler(args ...string) CustomedFuncFromLua {
307+
return p.getFuncFromLua(PostInstall, args...)
308+
}

0 commit comments

Comments
 (0)