cosmos72 / gomacro

Interactive Go interpreter and debugger with REPL, Eval, generics and Lisp-like macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Idea for making adding third party packages to Gomacro easier when plugins aren't available

Keithcat1 opened this issue · comments

The cleanest way I've thought of is for you to create a local Go package with only a main() function that just calls gomacro.main(), you use go get to add the third-party packages you want to import, and then you have Gomacro generate the import files as part of this package. This lets you control where the packages are coming from and should let you easily set up a template for building Gomacro with a bunch of popular Science packages and then other people can download and compile it with just a go get.

Currently what makes this hard is that gomacro --genimport doesn't let you dump the output in the current directory.

Background: current status

Gomacro uses the plugin package from standard library to import third-party packages at runtime.
As stated in https://pkg.go.dev/plugin "Plugins are currently supported only on Linux, FreeBSD, and macOS"

When running on other systems - for example Windows - the currently recommended workaround is to create an x_package.go file somewhere that collects all declarations from the package you want to import, add such file to gomacro sources, and recompile gomacro.

For that to work, you need a local writable copy of gomacro sources: go install github.com/cosmos72/gomacro@latest is not enough because it downloads gomacro sources into a somewhat hidden and usually read-only directory inside $GOPATH/go/pkg/mod/github.com/cosmos72/gomacro@...

Instead, you should download a copy of gomacro sources in $GOPATH/go/src/github.com/cosmos72/gomacro
then follow the instructions described at https://github.com/cosmos72/gomacro#other-systems

Basically you start gomacro and import "some/package/path" for each package you want to add.
For each such import, you will get a message like

// warning: created file "/home/user/go/src/github.com/cosmos72/gomacro/imports/thirdparty/some_package_path.go", recompile gomacro to use it

which means your local copy of gomacro sources have been modified, and should be recompiled.

Of course such solution is not go-gettable.

Proposed enhancement

If I understand correctly, what you are asking for is a simple method to generate an x_package.go file that collects all declarations for an arbitrary package X, and write it into inside an arbitrary package Y - or at least in the current directory.
In this way, you can manually add to package Y a Go function main() that registers the declarations for package X (or for multiple such packages) into gomacro, then starts gomacro itself.
And finally, you publish package Y somewhere, in order to allow users to go get it.

This is feasible, and it should be quite straightforward to implement, as the file x_package.go to create is similar enough to the one generated by import "some/package/path"

That's about it. Maybe add another command to the Gomacro CLI that generates a package adding the specified third-party packages to Gomacro, then you just go build it. This could be the default method to add third-party packages, it generally seems simpler and less hacky.