X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Frtfiles.go;h=a72b6ee35b1299c697a3867e9693cd5f7fac2193;hb=97e2fb12883b10002e7b08feef3110495d22bb4a;hp=3d0ea574325236a35da7810263aadee11b7c66ed;hpb=243f99aeb136ab15f5ed485510dbe7aa6a062c11;p=micro.git diff --git a/cmd/micro/rtfiles.go b/cmd/micro/rtfiles.go index 3d0ea574..a72b6ee3 100644 --- a/cmd/micro/rtfiles.go +++ b/cmd/micro/rtfiles.go @@ -11,6 +11,7 @@ const ( RTColorscheme = "colorscheme" RTSyntax = "syntax" RTHelp = "help" + RTPlugin = "plugin" ) // RuntimeFile allows the program to read runtime data like colorschemes or syntax files @@ -36,6 +37,19 @@ type namedFile struct { name string } +// a file with the data stored in memory +type memoryFile struct { + name string + data []byte +} + +func (mf memoryFile) Name() string { + return mf.name +} +func (mf memoryFile) Data() ([]byte, error) { + return mf.data, nil +} + func (rf realFile) Name() string { fn := filepath.Base(string(rf)) return fn[:len(fn)-len(filepath.Ext(fn))] @@ -119,8 +133,30 @@ func InitRuntimeFiles() { } add(RTColorscheme, "colorschemes", "*.micro") - add(RTSyntax, "syntax", "*.micro") + add(RTSyntax, "syntax", "*.yaml") 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)) + } + } + } + + 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)) + } + } + } } // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file @@ -144,12 +180,12 @@ func PluginListRuntimeFiles(fileType string) []string { } // PluginAddRuntimeFile adds a file to the runtime files for a plugin -func PluginAddRuntimeFile(plugin, filetype, path string) { - fullpath := configDir + "/plugins/" + plugin + "/" + path +func PluginAddRuntimeFile(plugin, filetype, filePath string) { + fullpath := filepath.Join(configDir, "plugins", plugin, filePath) if _, err := os.Stat(fullpath); err == nil { AddRuntimeFile(filetype, realFile(fullpath)) } else { - fullpath = "runtime/plugins/" + plugin + "/" + path + fullpath = path.Join("runtime", "plugins", plugin, filePath) AddRuntimeFile(filetype, assetFile(fullpath)) } } @@ -164,3 +200,8 @@ func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern str AddRuntimeFilesFromAssets(filetype, fullpath, pattern) } } + +// PluginAddRuntimeFileFromMemory adds a file to the runtime files for a plugin from a given string +func PluginAddRuntimeFileFromMemory(plugin, filetype, filename, data string) { + AddRuntimeFile(filetype, memoryFile{filename, []byte(data)}) +}