11 RTColorscheme = "colorscheme"
17 // RuntimeFile allows the program to read runtime data like colorschemes or syntax files
18 type RuntimeFile interface {
19 // Name returns a name of the file without paths or extensions
21 // Data returns the content of the file.
22 Data() ([]byte, error)
25 // allFiles contains all available files, mapped by filetype
26 var allFiles map[string][]RuntimeFile
28 // some file on filesystem
34 // some file on filesystem but with a different name
35 type namedFile struct {
40 // a file with the data stored in memory
41 type memoryFile struct {
46 func (mf memoryFile) Name() string {
49 func (mf memoryFile) Data() ([]byte, error) {
53 func (rf realFile) Name() string {
54 fn := filepath.Base(string(rf))
55 return fn[:len(fn)-len(filepath.Ext(fn))]
58 func (rf realFile) Data() ([]byte, error) {
59 return ioutil.ReadFile(string(rf))
62 func (af assetFile) Name() string {
63 fn := path.Base(string(af))
64 return fn[:len(fn)-len(path.Ext(fn))]
67 func (af assetFile) Data() ([]byte, error) {
68 return Asset(string(af))
71 func (nf namedFile) Name() string {
75 // AddRuntimeFile registers a file for the given filetype
76 func AddRuntimeFile(fileType string, file RuntimeFile) {
78 allFiles = make(map[string][]RuntimeFile)
80 allFiles[fileType] = append(allFiles[fileType], file)
83 // AddRuntimeFilesFromDirectory registers each file from the given directory for
84 // the filetype which matches the file-pattern
85 func AddRuntimeFilesFromDirectory(fileType, directory, pattern string) {
86 files, _ := ioutil.ReadDir(directory)
87 for _, f := range files {
88 if ok, _ := filepath.Match(pattern, f.Name()); !f.IsDir() && ok {
89 fullPath := filepath.Join(directory, f.Name())
90 AddRuntimeFile(fileType, realFile(fullPath))
95 // AddRuntimeFilesFromAssets registers each file from the given asset-directory for
96 // the filetype which matches the file-pattern
97 func AddRuntimeFilesFromAssets(fileType, directory, pattern string) {
98 files, err := AssetDir(directory)
102 for _, f := range files {
103 if ok, _ := path.Match(pattern, f); ok {
104 AddRuntimeFile(fileType, assetFile(path.Join(directory, f)))
109 // FindRuntimeFile finds a runtime file of the given filetype and name
110 // will return nil if no file was found
111 func FindRuntimeFile(fileType, name string) RuntimeFile {
112 for _, f := range ListRuntimeFiles(fileType) {
113 if f.Name() == name {
120 // ListRuntimeFiles lists all known runtime files for the given filetype
121 func ListRuntimeFiles(fileType string) []RuntimeFile {
122 if files, ok := allFiles[fileType]; ok {
125 return []RuntimeFile{}
128 // InitRuntimeFiles initializes all assets file and the config directory
129 func InitRuntimeFiles() {
130 add := func(fileType, dir, pattern string) {
131 AddRuntimeFilesFromDirectory(fileType, filepath.Join(configDir, dir), pattern)
132 AddRuntimeFilesFromAssets(fileType, path.Join("runtime", dir), pattern)
135 add(RTColorscheme, "colorschemes", "*.micro")
136 add(RTSyntax, "syntax", "*.yaml")
137 add(RTHelp, "help", "*.md")
139 // Search configDir for plugin-scripts
140 files, _ := ioutil.ReadDir(filepath.Join(configDir, "plugins"))
141 for _, f := range files {
142 realpath, _ := filepath.EvalSymlinks(filepath.Join(configDir, "plugins", f.Name()))
143 realpathStat, _ := os.Stat(realpath)
144 if realpathStat.IsDir() {
145 scriptPath := filepath.Join(configDir, "plugins", f.Name(), f.Name()+".lua")
146 if _, err := os.Stat(scriptPath); err == nil {
147 AddRuntimeFile(RTPlugin, realFile(scriptPath))
152 if files, err := AssetDir("runtime/plugins"); err == nil {
153 for _, f := range files {
154 scriptPath := path.Join("runtime/plugins", f, f+".lua")
155 if _, err := AssetInfo(scriptPath); err == nil {
156 AddRuntimeFile(RTPlugin, assetFile(scriptPath))
162 // PluginReadRuntimeFile allows plugin scripts to read the content of a runtime file
163 func PluginReadRuntimeFile(fileType, name string) string {
164 if file := FindRuntimeFile(fileType, name); file != nil {
165 if data, err := file.Data(); err == nil {
172 // PluginListRuntimeFiles allows plugins to lists all runtime files of the given type
173 func PluginListRuntimeFiles(fileType string) []string {
174 files := ListRuntimeFiles(fileType)
175 result := make([]string, len(files))
176 for i, f := range files {
182 // PluginAddRuntimeFile adds a file to the runtime files for a plugin
183 func PluginAddRuntimeFile(plugin, filetype, filePath string) {
184 fullpath := filepath.Join(configDir, "plugins", plugin, filePath)
185 if _, err := os.Stat(fullpath); err == nil {
186 AddRuntimeFile(filetype, realFile(fullpath))
188 fullpath = path.Join("runtime", "plugins", plugin, filePath)
189 AddRuntimeFile(filetype, assetFile(fullpath))
193 // PluginAddRuntimeFilesFromDirectory adds files from a directory to the runtime files for a plugin
194 func PluginAddRuntimeFilesFromDirectory(plugin, filetype, directory, pattern string) {
195 fullpath := filepath.Join(configDir, "plugins", plugin, directory)
196 if _, err := os.Stat(fullpath); err == nil {
197 AddRuntimeFilesFromDirectory(filetype, fullpath, pattern)
199 fullpath = path.Join("runtime", "plugins", plugin, directory)
200 AddRuntimeFilesFromAssets(filetype, fullpath, pattern)
204 // PluginAddRuntimeFileFromMemory adds a file to the runtime files for a plugin from a given string
205 func PluginAddRuntimeFileFromMemory(plugin, filetype, filename, data string) {
206 AddRuntimeFile(filetype, memoryFile{filename, []byte(data)})