-
Trying to import a module and my parser / eval loop is embedded in my Go code. Any ideas on how to import an external module? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OK, I see, I need to create a new local importer with the source directory I'm looking for and then call // Instantiate script importer with imported libraries
if scriptImporter == nil {
globalNames := []string{}
sourceDir := "assets/entities/libraries"
// builtins.Builtins() is built-ins that are normally automatically made available in an explicitly run Eval() function call (len(), keys(), etc.).
for k := range builtins.Builtins() {
globalNames = append(globalNames, k)
}
// Create the script importer; the source directory is the location of the source directory where modules can be found
scriptImporter = importer.NewLocalImporter(importer.LocalImporterOptions{SourceDir: sourceDir, GlobalNames: globalNames})
// Walk the source directory to import the modules by their module names (e.g. "Vec3"), not their filenames ("Vec3.rsr") and not their full paths ("assets/entities/libraries/Vec3.rsr")
fs.WalkDir(assetFileSystem, sourceDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil // Skip
}
_, file := filepath.Split(path)
moduleName := strings.TrimSuffix(file, filepath.Ext(file))
scriptImporter.Import(context.Background(), moduleName)
return nil
})
} Maybe the importer should take a file system and be able to import a directory, rather than needing to take a string for the source directory and manually import each file one at a time? Also might be good to have a non-specified In any case, things seem to be working well now. Thank you for Risor! Backup question: I can't seem to use |
Beta Was this translation helpful? Give feedback.
OK, I see, I need to create a new local importer with the source directory I'm looking for and then call
Importer.Import()
with a context and the name of the modules I'm interested in importing. The following works for me: