From 6acd8be05b0c2139300412a741e9609649a136de Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 30 Apr 2023 13:05:17 -0400 Subject: [PATCH] Added Version type to base tomo package and stuff --- nasin/plugin.go | 46 ++++++++-------------------------------------- nasin/unix.go | 22 ++++++++++++++++++++++ version.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 38 deletions(-) create mode 100644 nasin/unix.go create mode 100644 version.go diff --git a/nasin/plugin.go b/nasin/plugin.go index d75f2ad..3c820b6 100644 --- a/nasin/plugin.go +++ b/nasin/plugin.go @@ -1,16 +1,14 @@ package nasin import "os" -import "fmt" // TODO: possibly fork the official plugin module and add support for other // operating systems? perhaps enhance the Lookup function with // the generic extract function we have here for extra type safety goodness. import "plugin" -import "strings" import "path/filepath" import "git.tebibyte.media/sashakoshka/tomo" -type expectsFunc func () (int, int, int) +type expectsFunc func () tomo.Version type nameFunc func () string type descriptionFunc func () string type backendFactory func () (tomo.Backend, error) @@ -19,24 +17,10 @@ type themeFactory func () tomo.Theme var factories []backendFactory var theme tomo.Theme -func loadPlugins () { - // TODO: do not hardcode all these paths here, have separate files that - // build on different platforms that set these paths. - - pathVariable := os.Getenv("NASIN_PLUGIN_PATH") - paths := strings.Split(pathVariable, ":") - paths = append ( - paths, - "/usr/lib/nasin/plugins", - "/usr/local/lib/nasin/plugins") - homeDir, err := os.UserHomeDir() - if err != nil { - paths = append ( - paths, - filepath.Join(homeDir, ".local/lib/nasin/plugins")) - } +var pluginPaths []string - for _, dir := range paths { +func loadPlugins () { + for _, dir := range pluginPaths { loadPluginsFrom(dir) } } @@ -69,23 +53,19 @@ func loadPlugin (path string) { // check for and obtain basic plugin functions expects, ok := extract[expectsFunc](plugin, "Expects") - if !ok { die("does not implement Expects() (int, int, int)"); return } + if !ok { die("does not implement Expects() tomo.Version"); return } name, ok := extract[nameFunc](plugin, "Name") if !ok { die("does not implement Name() string"); return } _, ok = extract[descriptionFunc](plugin, "Description") if !ok { die("does not implement Description() string"); return } // check for version compatibility - // TODO: have exported version type in tomo base package, and have a - // function within that that gives the current tomo/nasin version. call - // that here. - major, minor, patch := expects() - currentVersion := version { 0, 0, 0 } - pluginVersion := version { major, minor, patch } + pluginVersion := expects() + currentVersion := tomo.CurrentVersion() if !pluginVersion.CompatibleABI(currentVersion) { die ( "plugin (" + pluginVersion.String() + - ") incompatible with nasin/tomo version (" + + ") incompatible with tomo/nasin version (" + currentVersion.String() + ")") return } @@ -107,13 +87,3 @@ func extract[T any] (plugin *plugin.Plugin, name string) (value T, ok bool) { value, ok = symbol.(T) return } - -type version [3]int - -func (version version) CompatibleABI (other version) bool { - return version[0] == other[0] && version[1] == other[1] -} - -func (version version) String () string { - return fmt.Sprint(version[0], ".", version[1], ".", version[2]) -} diff --git a/nasin/unix.go b/nasin/unix.go new file mode 100644 index 0000000..bb0283d --- /dev/null +++ b/nasin/unix.go @@ -0,0 +1,22 @@ +//go:build linux || darwin || freebsd + +package nasin + +import "os" +import "strings" +import "path/filepath" + +func init () { + pathVariable := os.Getenv("NASIN_PLUGIN_PATH") + pluginPaths = strings.Split(pathVariable, ":") + pluginPaths = append ( + pluginPaths, + "/usr/lib/nasin/plugins", + "/usr/local/lib/nasin/plugins") + homeDir, err := os.UserHomeDir() + if err != nil { + pluginPaths = append ( + pluginPaths, + filepath.Join(homeDir, ".local/lib/nasin/plugins")) + } +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..f9ed8fe --- /dev/null +++ b/version.go @@ -0,0 +1,32 @@ +package tomo + +import "fmt" + +// Version represents a semantic version number. +type Version [3]int + +// TODO: when 1.0 is released, remove the notices. remember to update +// CurrentVersion too! + +// CurrentVersion returns the current Tomo/Nasin version. Note that until 1.0 is +// released, this does not mean much. +func CurrentVersion () Version { + return Version { 0, 0, 0 } +} + +// CompatibleABI returns whether or not two versions are compatible on a binary +// level. Note that until 1.0 is released, this does not mean much. +func (version Version) CompatibleABI (other Version) bool { + return version[0] == other[0] && version[1] == other[1] +} + +// CompatibleAPI returns whether or not two versions are compatible on a source +// code level. Note that until 1.0 is released, this does not mean much. +func (version Version) CompatibleAPI (other Version) bool { + return version[0] == other[0] +} + +// String returns a string representation of the version. +func (version Version) String () string { + return fmt.Sprint(version[0], ".", version[1], ".", version[2]) +}