]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/rtfiles.go
Use messenger error instead of termerror
[micro.git] / cmd / micro / rtfiles.go
index 5b0f51da4857ea9567d7d8fe8c0af11a1c6a1246..a72b6ee35b1299c697a3867e9693cd5f7fac2193 100644 (file)
@@ -8,9 +8,10 @@ import (
 )
 
 const (
-       FILE_ColorScheme = "colorscheme"
-       FILE_Syntax      = "syntax"
-       FILE_Help        = "help"
+       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))]
@@ -78,7 +92,7 @@ func AddRuntimeFilesFromDirectory(fileType, directory, pattern string) {
        }
 }
 
-// AddRuntimeFilesFromDirectory registers each file from the given asset-directory for
+// AddRuntimeFilesFromAssets registers each file from the given asset-directory for
 // the filetype which matches the file-pattern
 func AddRuntimeFilesFromAssets(fileType, directory, pattern string) {
        files, err := AssetDir(directory)
@@ -103,28 +117,49 @@ func FindRuntimeFile(fileType, name string) RuntimeFile {
        return nil
 }
 
-// Lists all known runtime files for the given filetype
+// ListRuntimeFiles lists all known runtime files for the given filetype
 func ListRuntimeFiles(fileType string) []RuntimeFile {
        if files, ok := allFiles[fileType]; ok {
                return files
-       } else {
-               return []RuntimeFile{}
        }
+       return []RuntimeFile{}
 }
 
-// Initializes all assets file and the config directory
+// InitRuntimeFiles initializes all assets file and the config directory
 func InitRuntimeFiles() {
        add := func(fileType, dir, pattern string) {
                AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern)
                AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
        }
 
-       add(FILE_ColorScheme, "colorschemes", "*.micro")
-       add(FILE_Syntax, "syntax", "*.micro")
-       add(FILE_Help, "help", "*.md")
+       add(RTColorscheme, "colorschemes", "*.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))
+                       }
+               }
+       }
 }
 
-// Allows plugin scripts to read the content of a runtime file
+// PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file
 func PluginReadRuntimeFile(fileType, name string) string {
        if file := FindRuntimeFile(fileType, name); file != nil {
                if data, err := file.Data(); err == nil {
@@ -134,7 +169,7 @@ func PluginReadRuntimeFile(fileType, name string) string {
        return ""
 }
 
-// Allows plugins to lists all runtime files of the given type
+// PluginListRuntimeFiles allows plugins to lists all runtime files of the given type
 func PluginListRuntimeFiles(fileType string) []string {
        files := ListRuntimeFiles(fileType)
        result := make([]string, len(files))
@@ -144,12 +179,29 @@ func PluginListRuntimeFiles(fileType string) []string {
        return result
 }
 
-func PluginAddRuntimeFile(plugin, filetype, path string) {
-       fullpath := configDir + "/plugins/" + plugin + "/" + path
+// PluginAddRuntimeFile adds a file to the runtime files for a plugin
+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))
        }
 }
+
+// PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
+func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern string) {
+       fullpath := filepath.Join(configDir, "plugins", plugin, directory)
+       if _, err := os.Stat(fullpath); err == nil {
+               AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
+       } else {
+               fullpath = path.Join("runtime", "plugins", plugin, directory)
+               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)})
+}