Added Version type to base tomo package and stuff

This commit is contained in:
Sasha Koshka 2023-04-30 13:05:17 -04:00
parent 39b8b96513
commit 6acd8be05b
3 changed files with 62 additions and 38 deletions

View File

@ -1,16 +1,14 @@
package nasin package nasin
import "os" import "os"
import "fmt"
// TODO: possibly fork the official plugin module and add support for other // TODO: possibly fork the official plugin module and add support for other
// operating systems? perhaps enhance the Lookup function with // operating systems? perhaps enhance the Lookup function with
// the generic extract function we have here for extra type safety goodness. // the generic extract function we have here for extra type safety goodness.
import "plugin" import "plugin"
import "strings"
import "path/filepath" import "path/filepath"
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo"
type expectsFunc func () (int, int, int) type expectsFunc func () tomo.Version
type nameFunc func () string type nameFunc func () string
type descriptionFunc func () string type descriptionFunc func () string
type backendFactory func () (tomo.Backend, error) type backendFactory func () (tomo.Backend, error)
@ -19,24 +17,10 @@ type themeFactory func () tomo.Theme
var factories []backendFactory var factories []backendFactory
var theme tomo.Theme var theme tomo.Theme
func loadPlugins () { var pluginPaths []string
// 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"))
}
for _, dir := range paths { func loadPlugins () {
for _, dir := range pluginPaths {
loadPluginsFrom(dir) loadPluginsFrom(dir)
} }
} }
@ -69,23 +53,19 @@ func loadPlugin (path string) {
// check for and obtain basic plugin functions // check for and obtain basic plugin functions
expects, ok := extract[expectsFunc](plugin, "Expects") 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") name, ok := extract[nameFunc](plugin, "Name")
if !ok { die("does not implement Name() string"); return } if !ok { die("does not implement Name() string"); return }
_, ok = extract[descriptionFunc](plugin, "Description") _, ok = extract[descriptionFunc](plugin, "Description")
if !ok { die("does not implement Description() string"); return } if !ok { die("does not implement Description() string"); return }
// check for version compatibility // check for version compatibility
// TODO: have exported version type in tomo base package, and have a pluginVersion := expects()
// function within that that gives the current tomo/nasin version. call currentVersion := tomo.CurrentVersion()
// that here.
major, minor, patch := expects()
currentVersion := version { 0, 0, 0 }
pluginVersion := version { major, minor, patch }
if !pluginVersion.CompatibleABI(currentVersion) { if !pluginVersion.CompatibleABI(currentVersion) {
die ( die (
"plugin (" + pluginVersion.String() + "plugin (" + pluginVersion.String() +
") incompatible with nasin/tomo version (" + ") incompatible with tomo/nasin version (" +
currentVersion.String() + ")") currentVersion.String() + ")")
return return
} }
@ -107,13 +87,3 @@ func extract[T any] (plugin *plugin.Plugin, name string) (value T, ok bool) {
value, ok = symbol.(T) value, ok = symbol.(T)
return 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])
}

22
nasin/unix.go Normal file
View File

@ -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"))
}
}

32
version.go Normal file
View File

@ -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])
}