mirror of
https://github.com/zyedidia/micro.git
synced 2025-06-18 14:55:38 -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:
|
||||
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
|
||||
build-all: runtime build
|
||||
|
||||
|
@ -173,6 +173,8 @@ func main() {
|
||||
screen.TermMessage(err)
|
||||
}
|
||||
|
||||
config.LoadAllPlugins()
|
||||
|
||||
screen.Init()
|
||||
|
||||
// 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"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -134,23 +135,38 @@ func InitRuntimeFiles() {
|
||||
add(RTHelp, "help", "*.md")
|
||||
|
||||
// Search ConfigDir for plugin-scripts
|
||||
files, _ := ioutil.ReadDir(filepath.Join(ConfigDir, "plugins"))
|
||||
for _, f := range files {
|
||||
realpath, _ := filepath.EvalSymlinks(filepath.Join(ConfigDir, "plugins", f.Name()))
|
||||
realpathStat, _ := os.Stat(realpath)
|
||||
if realpathStat.IsDir() {
|
||||
scriptPath := filepath.Join(ConfigDir, "plugins", f.Name(), f.Name()+".lua")
|
||||
if _, err := os.Stat(scriptPath); err == nil {
|
||||
AddRuntimeFile(RTPlugin, realFile(scriptPath))
|
||||
plugdir := filepath.Join(ConfigDir, "plugins")
|
||||
files, _ := ioutil.ReadDir(plugdir)
|
||||
for _, d := range files {
|
||||
if d.IsDir() {
|
||||
srcs, _ := ioutil.ReadDir(filepath.Join(plugdir, d.Name()))
|
||||
p := new(Plugin)
|
||||
p.Name = d.Name()
|
||||
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 {
|
||||
for _, f := range files {
|
||||
scriptPath := path.Join("runtime/plugins", f, f+".lua")
|
||||
if _, err := AssetInfo(scriptPath); err == nil {
|
||||
AddRuntimeFile(RTPlugin, assetFile(scriptPath))
|
||||
plugdir = filepath.Join("runtime", "plugins")
|
||||
if files, err := AssetDir(plugdir); err == nil {
|
||||
for _, d := range files {
|
||||
if srcs, err := AssetDir(filepath.Join(plugdir, d)); err == nil {
|
||||
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