Skip to content

Commit

Permalink
Load plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Dec 25, 2019
1 parent 15dff72 commit 5ab6c97
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 80 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions internal/config/plugin.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 29 additions & 13 deletions internal/config/rtfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
)

const (
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
67 changes: 0 additions & 67 deletions internal/lua/plugin.go

This file was deleted.

0 comments on commit 5ab6c97

Please sign in to comment.