mirror of
https://github.com/zyedidia/micro.git
synced 2025-06-19 07:15:34 -04:00
Load plugins
This commit is contained in:
parent
15dff722b0
commit
5ab6c9795f
3
Makefile
3
Makefile
@ -15,6 +15,9 @@ GOVARS := -X github.com/zyedidia/micro/internal/util.Version=$(VERSION) -X githu
|
|||||||
build:
|
build:
|
||||||
go build -ldflags "-s -w $(GOVARS) $(ADDITIONAL_GO_LINKER_FLAGS)" ./cmd/micro
|
go build -ldflags "-s -w $(GOVARS) $(ADDITIONAL_GO_LINKER_FLAGS)" ./cmd/micro
|
||||||
|
|
||||||
|
build-dbg:
|
||||||
|
go build -ldflags "-s -w $(ADDITIONAL_GO_LINKER_FLAGS)" ./cmd/micro
|
||||||
|
|
||||||
# Builds micro after building the runtime and checking dependencies
|
# Builds micro after building the runtime and checking dependencies
|
||||||
build-all: runtime build
|
build-all: runtime build
|
||||||
|
|
||||||
|
@ -173,6 +173,8 @@ func main() {
|
|||||||
screen.TermMessage(err)
|
screen.TermMessage(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.LoadAllPlugins()
|
||||||
|
|
||||||
screen.Init()
|
screen.Init()
|
||||||
|
|
||||||
// If we have an error, we can exit cleanly and not completely
|
// If we have an error, we can exit cleanly and not completely
|
||||||
|
57
internal/config/plugin.go
Normal file
57
internal/config/plugin.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
|
ulua "github.com/zyedidia/micro/internal/lua"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrNoSuchFunction = errors.New("No such function exists")
|
||||||
|
|
||||||
|
func LoadAllPlugins() {
|
||||||
|
for _, p := range Plugins {
|
||||||
|
p.Load()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Plugin struct {
|
||||||
|
Name string // name of plugin
|
||||||
|
Info RuntimeFile // json file containing info
|
||||||
|
Srcs []RuntimeFile // lua files
|
||||||
|
}
|
||||||
|
|
||||||
|
var Plugins []*Plugin
|
||||||
|
|
||||||
|
func (p *Plugin) Load() error {
|
||||||
|
for _, f := range p.Srcs {
|
||||||
|
dat, err := f.Data()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = ulua.LoadFile(p.Name, f.Name(), dat)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Plugin) Call(fn string, args ...lua.LValue) (lua.LValue, error) {
|
||||||
|
plug := ulua.L.GetGlobal(p.Name)
|
||||||
|
luafn := ulua.L.GetField(plug, fn)
|
||||||
|
if luafn == lua.LNil {
|
||||||
|
return nil, ErrNoSuchFunction
|
||||||
|
}
|
||||||
|
err := ulua.L.CallByParam(lua.P{
|
||||||
|
Fn: luafn,
|
||||||
|
NRet: 1,
|
||||||
|
Protect: true,
|
||||||
|
}, args...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ret := ulua.L.Get(-1)
|
||||||
|
ulua.L.Pop(1)
|
||||||
|
return ret, nil
|
||||||
|
}
|
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -134,23 +135,38 @@ func InitRuntimeFiles() {
|
|||||||
add(RTHelp, "help", "*.md")
|
add(RTHelp, "help", "*.md")
|
||||||
|
|
||||||
// Search ConfigDir for plugin-scripts
|
// Search ConfigDir for plugin-scripts
|
||||||
files, _ := ioutil.ReadDir(filepath.Join(ConfigDir, "plugins"))
|
plugdir := filepath.Join(ConfigDir, "plugins")
|
||||||
for _, f := range files {
|
files, _ := ioutil.ReadDir(plugdir)
|
||||||
realpath, _ := filepath.EvalSymlinks(filepath.Join(ConfigDir, "plugins", f.Name()))
|
for _, d := range files {
|
||||||
realpathStat, _ := os.Stat(realpath)
|
if d.IsDir() {
|
||||||
if realpathStat.IsDir() {
|
srcs, _ := ioutil.ReadDir(filepath.Join(plugdir, d.Name()))
|
||||||
scriptPath := filepath.Join(ConfigDir, "plugins", f.Name(), f.Name()+".lua")
|
p := new(Plugin)
|
||||||
if _, err := os.Stat(scriptPath); err == nil {
|
p.Name = d.Name()
|
||||||
AddRuntimeFile(RTPlugin, realFile(scriptPath))
|
for _, f := range srcs {
|
||||||
|
if strings.HasSuffix(f.Name(), ".lua") {
|
||||||
|
p.Srcs = append(p.Srcs, realFile(filepath.Join(plugdir, d.Name(), f.Name())))
|
||||||
|
} else if f.Name() == "info.json" {
|
||||||
|
p.Info = realFile(filepath.Join(plugdir, d.Name(), "info.json"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Plugins = append(Plugins, p)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if files, err := AssetDir("runtime/plugins"); err == nil {
|
plugdir = filepath.Join("runtime", "plugins")
|
||||||
for _, f := range files {
|
if files, err := AssetDir(plugdir); err == nil {
|
||||||
scriptPath := path.Join("runtime/plugins", f, f+".lua")
|
for _, d := range files {
|
||||||
if _, err := AssetInfo(scriptPath); err == nil {
|
if srcs, err := AssetDir(filepath.Join(plugdir, d)); err == nil {
|
||||||
AddRuntimeFile(RTPlugin, assetFile(scriptPath))
|
p := new(Plugin)
|
||||||
|
p.Name = d
|
||||||
|
for _, f := range srcs {
|
||||||
|
if strings.HasSuffix(f, ".lua") {
|
||||||
|
p.Srcs = append(p.Srcs, assetFile(filepath.Join(plugdir, d, f)))
|
||||||
|
} else if f == "info.json" {
|
||||||
|
p.Info = assetFile(filepath.Join(plugdir, d, "info.json"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Plugins = append(Plugins, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
package lua
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
lua "github.com/yuin/gopher-lua"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrNoSuchFunction = errors.New("No such function exists")
|
|
||||||
|
|
||||||
type Plugin struct {
|
|
||||||
name string
|
|
||||||
files []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPluginFromDir(name string, dir string) (*Plugin, error) {
|
|
||||||
files, err := ioutil.ReadDir(dir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p := new(Plugin)
|
|
||||||
p.name = name
|
|
||||||
|
|
||||||
for _, f := range files {
|
|
||||||
if strings.HasSuffix(f.Name(), ".lua") {
|
|
||||||
p.files = append(p.files, dir+f.Name())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin) Load() error {
|
|
||||||
for _, f := range p.files {
|
|
||||||
dat, err := ioutil.ReadFile(f)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = LoadFile(p.name, f, dat)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Plugin) Call(fn string, args ...lua.LValue) (lua.LValue, error) {
|
|
||||||
plug := L.GetGlobal(p.name)
|
|
||||||
luafn := L.GetField(plug, fn)
|
|
||||||
if luafn == lua.LNil {
|
|
||||||
return nil, ErrNoSuchFunction
|
|
||||||
}
|
|
||||||
err := L.CallByParam(lua.P{
|
|
||||||
Fn: luafn,
|
|
||||||
NRet: 1,
|
|
||||||
Protect: true,
|
|
||||||
}, args...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret := L.Get(-1)
|
|
||||||
L.Pop(1)
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user